Repository: fission/fission (language: go)

Issue:
Add ServiceMonitor Labels customisation so they work out of the box with Kube-Prometheus-Stack
**Is your feature request related to a problem? Please describe.**
The ServiceMonitor templates in the fission-all chart do not currently have any way to customise labels. This would be a good thing to add so monitoring works out of the box for most kube-prometheus-stack configs with a values.yaml change.

**Describe the solution you'd like**
Allow labels to be passed the service monitor. This can be done by either setting the `release` label and populating it from values.yaml or by adding `additionalServiceMonitorLabels` and passing all the values under it.

**Describe alternatives you've considered**
Alternatives are to either remove the LabelSelector from the Prometheus CR, but thats not ideal as there might be multiple Prometheus instances and specific ServiceMonitors might want to be picked. Or to manually edit the generated templates to add in the kube-prometheus-stack release name. Neither are ideal solutions.


Discussion / hints:
Thank you for your first issue! ✨😊

Fix patch:
diff --git a/charts/fission-all/templates/buildermgr/podmonitor.yaml b/charts/fission-all/templates/buildermgr/podmonitor.yaml
new file mode 100644
index 0000000000..c0e5799687
--- /dev/null
+++ b/charts/fission-all/templates/buildermgr/podmonitor.yaml
@@ -0,0 +1,23 @@
+{{- if .Values.podMonitor.enabled }}
+apiVersion: monitoring.coreos.com/v1
+kind: PodMonitor
+metadata:
+  name: buildermgr-monitor
+  {{- if .Values.podMonitor.namespace }}
+  namespace: {{ .Values.podMonitor.namespace }}
+  {{- end }}
+  {{- with .Values.podMonitor.additionalPodMonitorLabels }}
+  labels:
+    {{- toYaml . | nindent 4 }}
+  {{- end }}
+spec:
+  namespaceSelector:
+    matchNames:
+      - {{ .Release.Namespace }}
+  selector:
+    matchLabels:
+      svc: buildermgr
+  podMetricsEndpoints:
+  - port: "metrics"
+    path: "/metrics"
+{{- end -}}
diff --git a/charts/fission-all/templates/controller/servicemonitor.yaml b/charts/fission-all/templates/controller/servicemonitor.yaml
index e4c68ccfc1..19cedd3a6b 100644
--- a/charts/fission-all/templates/controller/servicemonitor.yaml
+++ b/charts/fission-all/templates/controller/servicemonitor.yaml
@@ -6,6 +6,10 @@ metadata:
   {{- if .Values.serviceMonitor.namespace }}
   namespace: {{ .Values.serviceMonitor.namespace }}
   {{- end }}
+  {{- with .Values.serviceMonitor.additionalServiceMonitorLabels }}
+  labels:
+    {{- toYaml . | nindent 4 }}
+  {{- end }}
 spec:
   namespaceSelector:
     matchNames:
diff --git a/charts/fission-all/templates/executor/servicemonitor.yaml b/charts/fission-all/templates/executor/servicemonitor.yaml
index 0a9ba668a0..f816421a32 100644
--- a/charts/fission-all/templates/executor/servicemonitor.yaml
+++ b/charts/fission-all/templates/executor/servicemonitor.yaml
@@ -6,6 +6,10 @@ metadata:
   {{- if .Values.serviceMonitor.namespace }}
   namespace: {{ .Values.serviceMonitor.namespace }}
   {{- end }}
+  {{- with .Values.serviceMonitor.additionalServiceMonitorLabels }}
+  labels:
+    {{- toYaml . | nindent 4 }}
+  {{- end }}
 spec:
   namespaceSelector:
     matchNames:
diff --git a/charts/fission-all/templates/mqt-fission-kafka/deployment.yaml b/charts/fission-all/templates/mqt-fission-kafka/deployment.yaml
index 7ea1dbedff..eff9d3640b 100644
--- a/charts/fission-all/templates/mqt-fission-kafka/deployment.yaml
+++ b/charts/fission-all/templates/mqt-fission-kafka/deployment.yaml
@@ -33,6 +33,9 @@ spec:
         imagePullPolicy: {{ .Values.pullPolicy }}
         command: ["/fission-bundle"]
         args: ["--mqt", "--routerUrl", "http://router.{{ .Release.Namespace }}"]
+        ports:
+          - containerPort: 8080
+            name: metrics
         env:
         - name: MESSAGE_QUEUE_TYPE
           value: kafka
diff --git a/charts/fission-all/templates/mqt-fission-kafka/podmonitor.yaml b/charts/fission-all/templates/mqt-fission-kafka/podmonitor.yaml
new file mode 100644
index 0000000000..d15c3a5f9f
--- /dev/null
+++ b/charts/fission-all/templates/mqt-fission-kafka/podmonitor.yaml
@@ -0,0 +1,23 @@
+{{- if .Values.podMonitor.enabled }}
+apiVersion: monitoring.coreos.com/v1
+kind: PodMonitor
+metadata:
+  name: mqt-fission-kafka-monitor
+  {{- if .Values.podMonitor.namespace }}
+  namespace: {{ .Values.podMonitor.namespace }}
+  {{- end }}
+  {{- with .Values.podMonitor.additionalPodMonitorLabels }}
+  labels:
+    {{- toYaml . | nindent 4 }}
+  {{- end }}
+spec:
+  namespaceSelector:
+    matchNames:
+      - {{ .Release.Namespace }}
+  selector:
+    matchLabels:
+      svc: mqtrigger
+  podMetricsEndpoints:
+  - port: "metrics"
+    path: "/metrics"
+{{- end -}}
diff --git a/charts/fission-all/templates/router/servicemonitor.yaml b/charts/fission-all/templates/router/servicemonitor.yaml
index 7f1b4c61a4..56e11a3b7e 100644
--- a/charts/fission-all/templates/router/servicemonitor.yaml
+++ b/charts/fission-all/templates/router/servicemonitor.yaml
@@ -6,6 +6,10 @@ metadata:
   {{- if .Values.serviceMonitor.namespace }}
   namespace: {{ .Values.serviceMonitor.namespace }}
   {{- end }}
+  {{- with .Values.serviceMonitor.additionalServiceMonitorLabels }}
+  labels:
+    {{- toYaml . | nindent 4 }}
+  {{- end }}
 spec:
   namespaceSelector:
     matchNames:
diff --git a/charts/fission-all/templates/storagesvc/servicemonitor.yaml b/charts/fission-all/templates/storagesvc/servicemonitor.yaml
index 808f3855ab..1ef3495854 100644
--- a/charts/fission-all/templates/storagesvc/servicemonitor.yaml
+++ b/charts/fission-all/templates/storagesvc/servicemonitor.yaml
@@ -5,7 +5,11 @@ metadata:
   name: storagesvc-monitor
   {{- if .Values.serviceMonitor.namespace }}
   namespace: {{ .Values.serviceMonitor.namespace }}
-  {{- end }}  
+  {{- end }}
+  {{- with .Values.serviceMonitor.additionalServiceMonitorLabels }}
+  labels:
+    {{- toYaml . | nindent 4 }}
+  {{- end }}
 spec:
   namespaceSelector:
     matchNames:
diff --git a/charts/fission-all/values.yaml b/charts/fission-all/values.yaml
index e3294c388e..4de9748ab6 100644
--- a/charts/fission-all/values.yaml
+++ b/charts/fission-all/values.yaml
@@ -452,11 +452,33 @@ kafka:
   ##
   # version: "0.11.2.0"
 
+# The following components expose Prometheus metrics and have servicemonitors in this chart (disabled by default)
+# Controller, router, executor, storage svc
 serviceMonitor:
   enabled: false
   ##namespace in which you want to deploy servicemonitor
   ##
   namespace: ""
+  ## Map of additional lables to add to the ServiceMonitor resources
+  #  to allow selecting sepcific ServiceMonitors
+  #  in case of multiple prometheus deployments
+  additionalServiceMonitorLabels: {}
+  #  release: "monitoring"
+  #  key: "value"
+
+# The following components expose Prometheus metrics and have podmonitors in this chart (disabled by default)
+# 
+podMonitor:
+  enabled: false
+  ##namespace in which you want to deploy podmonitor
+  ##
+  namespace: ""
+  ## Map of additional lables to add to the PodMonitor resources
+  #  to allow selecting sepcific PodMonitor
+  #  in case of multiple prometheus deployments
+  additionalPodMonitorLabels: {}
+  #  release: "monitoring"
+  #  key: "value"
 
 ## Persist data to a persistent volume.
 ##


Test patch: