{{- if and .Values.controller.prometheus.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor

metadata:
  name: {{ template "jenkins.fullname" . }}
{{- if .Values.controller.prometheus.serviceMonitorNamespace }}
  namespace: {{ .Values.controller.prometheus.serviceMonitorNamespace }}
{{- else }}
  namespace: {{ template "jenkins.namespace" . }}
{{- end }}
  labels:
    {{- include "jenkins.labels" . | nindent 4 }}
    {{- range $key, $val := .Values.controller.prometheus.serviceMonitorAdditionalLabels }}
    {{ $key }}: {{ $val | quote }}
    {{- end}}

spec:
  endpoints:
  - interval: {{ .Values.controller.prometheus.scrapeInterval }}
    port: http
    path: {{ .Values.controller.jenkinsUriPrefix }}{{ .Values.controller.prometheus.scrapeEndpoint }}
    {{- with .Values.controller.prometheus.relabelings }}
    relabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    {{- with .Values.controller.prometheus.metricRelabelings }}
    metricRelabelings:
      {{- toYaml . | nindent 6 }}
    {{- end }}
  jobLabel: {{ template "jenkins.fullname" . }}
  namespaceSelector:
    matchNames:
    - "{{ template "jenkins.namespace" $ }}"
  selector:
    matchLabels:
      "app.kubernetes.io/instance": "{{ .Release.Name }}"
      "app.kubernetes.io/component": "{{ .Values.controller.componentName }}"
{{- end }}
