{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.imageReflectorController.metrics.enabled .Values.imageReflectorController.metrics.serviceMonitor.enabled  }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "flux.image-reflector-controller.fullname" . }}
  namespace: {{ default (include "common.names.namespace" .) .Values.imageReflectorController.metrics.serviceMonitor.namespace | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.imageReflectorController.image "chart" .Chart ) ) }}  
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.imageReflectorController.metrics.serviceMonitor.labels .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flux
    app.kubernetes.io/component: image-reflector-controller
  {{- if or .Values.imageReflectorController.metrics.serviceMonitor.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.imageReflectorController.metrics.serviceMonitor.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.imageReflectorController.metrics.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: image-reflector-controller
      {{- if .Values.imageReflectorController.metrics.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.imageReflectorController.metrics.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  endpoints:
    - port: http-metrics
      path: "/metrics"
      {{- if .Values.imageReflectorController.metrics.serviceMonitor.interval }}
      interval: {{ .Values.imageReflectorController.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.imageReflectorController.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.imageReflectorController.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.imageReflectorController.metrics.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.imageReflectorController.metrics.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.imageReflectorController.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.imageReflectorController.metrics.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.imageReflectorController.metrics.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.imageReflectorController.metrics.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
  namespaceSelector:
    matchNames:
    - {{ include "common.names.namespace" . | quote }}
{{- end }}
