Skip to content

Commit

Permalink
Deployment config
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Nov 8, 2023
1 parent 79f885a commit 76600c9
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 27 deletions.
36 changes: 36 additions & 0 deletions config/default/exporter_auth_proxy_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This patch inject a sidecar container which is a HTTP proxy for the
# exporter, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
apiVersion: apps/v1
kind: Deployment
metadata:
name: alerts-exporter
spec:
template:
spec:
containers:
- name: kube-rbac-proxy
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- "ALL"
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.13.1
args:
- "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8080/"
- "--logtostderr=true"
- "--v=0"
ports:
- containerPort: 8443
protocol: TCP
name: https
resources:
limits:
cpu: 500m
memory: 128Mi
requests:
cpu: 5m
memory: 64Mi
- name: exporter
args:
- "--listen-addr=127.0.0.1:8080"
9 changes: 9 additions & 0 deletions config/default/exporter_config_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: alerts-exporter
spec:
template:
spec:
containers:
- name: exporter
11 changes: 11 additions & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Adds namespace to all resources.
namespace: alerts-exporter

bases:
- ../rbac
- ../exporter
- ../prometheus

patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
- exporter_auth_proxy_patch.yaml
25 changes: 0 additions & 25 deletions config/deployment/deployment.yaml

This file was deleted.

64 changes: 64 additions & 0 deletions config/exporter/exporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
apiVersion: v1
kind: Namespace
metadata:
labels:
control-plane: alerts-exporter
app.kubernetes.io/name: namespace
app.kubernetes.io/instance: exporter
app.kubernetes.io/component: exporter
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: alerts-exporter
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: alerts-exporter
labels:
control-plane: alerts-exporter
app.kubernetes.io/name: deployment
app.kubernetes.io/instance: exporter
app.kubernetes.io/component: exporter
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
spec:
selector:
matchLabels:
control-plane: alerts-exporter
replicas: 1
template:
metadata:
annotations:
kubectl.kubernetes.io/default-container: exporter
labels:
control-plane: alerts-exporter
spec:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
containers:
- name: exporter
image: ghcr.io/appuio/alerts-exporter:latest
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- "ALL"
readinessProbe:
httpGet:
path: /readyz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 10m
memory: 64Mi
serviceAccountName: alerts-exporter
terminationGracePeriodSeconds: 10
2 changes: 2 additions & 0 deletions config/exporter/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- exporter.yaml
27 changes: 27 additions & 0 deletions config/openshift4/exporter_openshift_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This patch inject a sidecar container which is a HTTP proxy for the
# exporter, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
apiVersion: apps/v1
kind: Deployment
metadata:
name: alerts-exporter
spec:
template:
spec:
containers:
- name: exporter
args:
- --listen-addr=127.0.0.1:8080
- --tls
- --host=alertmanager-operated.openshift-monitoring.svc.cluster.local:9095
- --tls-server-name=alertmanager-main.openshift-monitoring.svc.cluster.local
- --bearer-token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
- --tls-ca-cert=/etc/ssl/certs/serving-certs/service-ca.crt
volumeMounts:
- mountPath: /etc/ssl/certs/serving-certs/
name: ca-bundle
readOnly: true
volumes:
- configMap:
defaultMode: 288
name: openshift-service-ca.crt
name: ca-bundle
6 changes: 6 additions & 0 deletions config/openshift4/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bases:
- ../default

patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
- exporter_openshift_patch.yaml
2 changes: 2 additions & 0 deletions config/prometheus/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- monitor.yaml
30 changes: 30 additions & 0 deletions config/prometheus/monitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# Prometheus Monitor Service (Metrics)
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
control-plane: alerts-exporter
app.kubernetes.io/name: servicemonitor
app.kubernetes.io/instance: alerts-exporter-metrics-monitor
app.kubernetes.io/component: metrics
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: alerts-exporter-metrics-monitor
spec:
endpoints:
- path: /metrics
port: https
scheme: https
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
tlsConfig:
insecureSkipVerify: true
metricRelabelings:
- action: keep
regex: alerts_exporter_.+
sourceLabels:
- __name__
selector:
matchLabels:
control-plane: alerts-exporter
16 changes: 16 additions & 0 deletions config/rbac/auth_proxy_client_clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: clusterrole
app.kubernetes.io/instance: metrics-reader
app.kubernetes.io/component: kube-rbac-proxy
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: metrics-reader
rules:
- nonResourceURLs:
- "/metrics"
verbs:
- get
24 changes: 24 additions & 0 deletions config/rbac/auth_proxy_role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/name: clusterrole
app.kubernetes.io/instance: proxy-role
app.kubernetes.io/component: kube-rbac-proxy
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: proxy-role
rules:
- apiGroups:
- authentication.k8s.io
resources:
- tokenreviews
verbs:
- create
- apiGroups:
- authorization.k8s.io
resources:
- subjectaccessreviews
verbs:
- create
18 changes: 18 additions & 0 deletions config/rbac/auth_proxy_role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
app.kubernetes.io/name: clusterrolebinding
app.kubernetes.io/instance: proxy-rolebinding
app.kubernetes.io/component: kube-rbac-proxy
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: proxy-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: proxy-role
subjects:
- kind: ServiceAccount
name: alerts-exporter
20 changes: 20 additions & 0 deletions config/rbac/auth_proxy_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Service
metadata:
labels:
control-plane: alerts-exporter
app.kubernetes.io/name: service
app.kubernetes.io/instance: alerts-exporter-metrics-service
app.kubernetes.io/component: kube-rbac-proxy
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: alerts-exporter-metrics-service
spec:
ports:
- name: https
port: 8443
protocol: TCP
targetPort: https
selector:
control-plane: alerts-exporter
9 changes: 9 additions & 0 deletions config/rbac/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resources:
- service_account.yaml
- role.yaml
- role_binding.yaml

- auth_proxy_service.yaml
- auth_proxy_role.yaml
- auth_proxy_role_binding.yaml
- auth_proxy_client_clusterrole.yaml
10 changes: 10 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: exporter-role
rules:
- nonResourceURLs:
- /api/v2/alerts
verbs:
- get
18 changes: 18 additions & 0 deletions config/rbac/role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
app.kubernetes.io/name: clusterrolebinding
app.kubernetes.io/instance: exporter-rolebinding
app.kubernetes.io/component: rbac
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: exporter-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: exporter-role
subjects:
- kind: ServiceAccount
name: alerts-exporter
11 changes: 11 additions & 0 deletions config/rbac/service_account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: serviceaccount
app.kubernetes.io/instance: alerts-exporter
app.kubernetes.io/component: rbac
app.kubernetes.io/created-by: alerts-exporter
app.kubernetes.io/part-of: alerts-exporter
app.kubernetes.io/managed-by: kustomize
name: alerts-exporter
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"

Expand All @@ -13,6 +14,8 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var listenAddr string

var host string
var withInhibited, withSilenced, withUnprocessed, withActive bool
var filters stringSliceFlag
Expand All @@ -23,6 +26,8 @@ var useTLS bool
var bearerToken string

func main() {
flag.StringVar(&listenAddr, "listen-addr", ":8080", "The addr to listen on")

flag.StringVar(&host, "host", "localhost:9093", "The host of the Alertmanager")

flag.BoolVar(&useTLS, "tls", false, "Use TLS when connecting to Alertmanager")
Expand Down Expand Up @@ -85,8 +90,9 @@ func main() {
// Expose metrics and custom registry via an HTTP server
// using the HandleFor function. "/metrics" is the usual endpoint for that.
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
log.Println("Listening on `:8080/metrics`")
log.Fatal(http.ListenAndServe(":8080", nil))
http.Handle("/readyz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "ok") }))
log.Printf("Listening on `%s`", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, nil))
}

type stringSliceFlag []string
Expand Down

0 comments on commit 76600c9

Please sign in to comment.