From 5e18e56e8d66309d692884f54704c5f3cd9aab04 Mon Sep 17 00:00:00 2001
From: Guillermo Facundo Colunga <guillermo.facundo.colunga@cern.ch>
Date: Mon, 28 Oct 2024 16:16:13 +0100
Subject: [PATCH] push-gateway: add component and config

After requests from users this commits adds the push gateway component
to the cluster. For that adds a deployment that deploys a replica of the
prometheus gateway and then defines different resources like the
service and the ingress to access the push gateway and the
servicemonitor to scrape the metrics on it.

Testing:
- Installed locally via helm install.

JIRA Refs:
- MONIT-4040
---
 docs/metrics.md                           | 33 +++++++++++++++++++++++
 templates/pushgateway/deployment.yaml     | 30 +++++++++++++++++++++
 templates/pushgateway/ingress.yaml        | 29 ++++++++++++++++++++
 templates/pushgateway/service.yaml        | 16 +++++++++++
 templates/pushgateway/servicemonitor.yaml | 16 +++++++++++
 values.yaml                               | 28 +++++++++++++++++++
 6 files changed, 152 insertions(+)
 create mode 100644 templates/pushgateway/deployment.yaml
 create mode 100644 templates/pushgateway/ingress.yaml
 create mode 100644 templates/pushgateway/service.yaml
 create mode 100644 templates/pushgateway/servicemonitor.yaml

diff --git a/docs/metrics.md b/docs/metrics.md
index d62815a..ced61aa 100644
--- a/docs/metrics.md
+++ b/docs/metrics.md
@@ -48,6 +48,39 @@ metrics:
                 app: nginx
 ```
 
+### D. Push Gateway Metrics
+Sometimes you cannot scrape your metrics from an endpoint using a ServiceMonitor, PodMonitor or other solutions. For this scenario we have added the option to deploy a local Prometheus Push Gateway fully configured to be scraped from the local Prometheus and send the metrics to the IT Monitoring infrastructure. To configure it you have the following options:
+
+```yaml
+metrics:
+  pushgateway:
+    enabled: false
+    image:
+      repository: registry.cern.ch/monit/cern-it-monitoring-pushgateway
+      tag: v1.10.0
+      pullPolicy: IfNotPresent
+    resources:
+      requests:
+        cpu: 0.2
+        memory: 100Mi
+      limits:
+        cpu: 0.2
+        memory: 100Mi
+    # If set to true will install register a new ingress with the given
+    # configuration.
+    ingress:
+      enabled: false
+      className: "" # If no class set then default.
+      path: /
+      pathType: ImplementationSpecific
+      hosts: [] # A list of hosts.
+      tls: {} # Kubernetes plain ingress tls config.
+    # If given will override the defaultNodeSelector and install the component
+    # only on the nodes that match the given condition.
+    nodeSelector: {}
+```
+
+This configuration will create a service in your cluster named `it-monit-metrics-collector-pushgateway`. In this service there is a port named `http` (9091) where you can push your metrics. If you need to push metrics from outside the cluster you only need to enable the ingress and point the corresponding dns records to your cluster ingress nodes.
 
 ## 4 . Further Customizations
 
diff --git a/templates/pushgateway/deployment.yaml b/templates/pushgateway/deployment.yaml
new file mode 100644
index 0000000..f36f93f
--- /dev/null
+++ b/templates/pushgateway/deployment.yaml
@@ -0,0 +1,30 @@
+{{- if and .Values.metrics.enabled .Values.metrics.pushgateway.enabled -}}
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: it-monit-metrics-collector-pushgateway
+  namespace: {{ .Release.Namespace }}
+  labels:
+    app.kubernetes.io/name: it-monit-metrics-collector-pushgateway
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app.kubernetes.io/name: it-monit-metrics-collector-pushgateway
+  template:
+    metadata:
+      labels:
+        app.kubernetes.io/name: it-monit-metrics-collector-pushgateway
+    spec:
+      containers:
+      - image: "{{ .Values.metrics.pushgateway.image.repository }}:{{ .Values.metrics.pushgateway.image.tag }}"
+        name: it-monit-metrics-collector-pushgateway
+        ports:
+        - containerPort: 9091
+          name: http
+      nodeSelector:
+        kubernetes.io/os: linux
+      {{- if or (.Values.metrics.defaultNodeSelector) (.Values.metrics.pushgateway.nodeSelector) }}
+      {{- .Values.metrics.pushgateway.nodeSelector | default .Values.metrics.defaultNodeSelector | toYaml | nindent 8 }}
+      {{- end }}
+{{- end -}}
diff --git a/templates/pushgateway/ingress.yaml b/templates/pushgateway/ingress.yaml
new file mode 100644
index 0000000..f533d26
--- /dev/null
+++ b/templates/pushgateway/ingress.yaml
@@ -0,0 +1,29 @@
+{{- if and .Values.metrics.enabled .Values.metrics.pushgateway.enabled }}
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  name: it-monit-metrics-collector-pushgateway
+  namespace: {{ .Release.Namespace }}
+spec:
+  {{- if .Values.metrics.pushgateway.ingress.className }}
+  ingressClassName: {{ .Values.metrics.pushgateway.ingress.className }}
+  {{- end }}
+  {{- if .Values.metrics.pushgateway.ingress.tls }}
+  tls:
+    {{ .Values.metrics.pushgateway.ingress.tls | toYaml | nindent 4}}
+  {{- end }}
+  rules:
+  {{- $pathType := .Values.metrics.pushgateway.ingress.pathType}}
+  {{- range $host := .Values.metrics.pushgateway.ingress.hosts }}
+    - host: {{ $host }}
+      http:
+        paths:
+          - path: /
+            pathType: {{ $pathType }}
+            backend:
+              service:
+                name: it-monit-metrics-collector-pushgateway
+                port:
+                  number: 80
+    {{- end }}
+{{- end }}
diff --git a/templates/pushgateway/service.yaml b/templates/pushgateway/service.yaml
new file mode 100644
index 0000000..7768dcb
--- /dev/null
+++ b/templates/pushgateway/service.yaml
@@ -0,0 +1,16 @@
+{{- if and .Values.metrics.enabled .Values.metrics.pushgateway.enabled -}}
+apiVersion: v1
+kind: Service
+metadata:
+  name: it-monit-metrics-collector-pushgateway
+  namespace: {{ .Release.Namespace }}
+  labels:
+    app.kubernetes.io/name: it-monit-metrics-collector-pushgateway
+spec:
+  clusterIP: None
+  ports:
+  - name: http
+    port: 9091
+  selector:
+    app.kubernetes.io/name: it-monit-metrics-collector-pushgateway
+{{- end -}}
diff --git a/templates/pushgateway/servicemonitor.yaml b/templates/pushgateway/servicemonitor.yaml
new file mode 100644
index 0000000..44eec39
--- /dev/null
+++ b/templates/pushgateway/servicemonitor.yaml
@@ -0,0 +1,16 @@
+{{- if and .Values.metrics.enabled .Values.metrics.pushgateway.enabled -}}
+apiVersion: monitoring.coreos.com/v1
+kind: ServiceMonitor
+metadata:
+  name: it-monit-metrics-servicemonitor-pushgateway
+  namespace: {{ .Release.Namespace }}
+spec:
+  endpoints:
+  - port: http
+  namespaceSelector:
+    matchNames:
+    - {{ .Release.Namespace }}
+  selector:
+    matchLabels:
+      app.kubernetes.io/name: it-monit-metrics-collector-pushgateway
+{{- end -}}
\ No newline at end of file
diff --git a/values.yaml b/values.yaml
index a7d05c0..ecff84b 100644
--- a/values.yaml
+++ b/values.yaml
@@ -191,6 +191,34 @@ metrics:
         storage.total_limit_size: {{ .Values.metrics.fluentbit.diskMaxCache }}
         header: User-Agent {{ .Chart.Name }}/{{ .Chart.Version }}
 
+  # Pushgateway allows you to send metrics to the monitoring infrastructure
+  # by pushing them to the local cluster service it-monit-metrics-collector-pushgateway.
+  pushgateway:
+    enabled: false
+    image:
+      repository: registry.cern.ch/monit/cern-it-monitoring-pushgateway
+      tag: v1.10.0
+      pullPolicy: IfNotPresent
+    resources:
+      requests:
+        cpu: 0.2
+        memory: 100Mi
+      limits:
+        cpu: 0.2
+        memory: 100Mi
+    # If set to true will install register a new ingress with the given
+    # configuration.
+    ingress:
+      enabled: false
+      className: "" # If no class set then default.
+      path: /
+      pathType: ImplementationSpecific
+      hosts: []
+      tls: {}
+    # If given will override the defaultNodeSelector and install the component
+    # only on the nodes that match the given condition.
+    nodeSelector: {}
+
 logs:
   # -- indicates if logs metrics components should be enabled or not. If set to false no logs component will be installed nor configured
   enabled: false
-- 
GitLab