{{- if .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "sealed-secrets.fullname" . }}
  {{- if .Values.metrics.serviceMonitor.namespace }}
  namespace: {{ .Values.metrics.serviceMonitor.namespace }}
  {{- else }}
  namespace: {{ include "sealed-secrets.namespace" . }}
  {{- end }}
  labels: {{- include "sealed-secrets.labels" . | nindent 4 }}
    {{- if .Values.metrics.serviceMonitor.labels }}
    {{- include "sealed-secrets.render" ( dict "value" .Values.metrics.serviceMonitor.labels "context" $) | nindent 4 }}
    {{- end }}
  {{- if .Values.metrics.serviceMonitor.annotations }}
  annotations: {{- include "sealed-secrets.render" (dict "value" .Values.metrics.serviceMonitor.annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  endpoints:
    - honorLabels: true
      port: http
      {{- if .Values.metrics.serviceMonitor.interval }}
      interval: {{ .Values.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{ toYaml .Values.metrics.serviceMonitor.metricRelabelings | nindent 8 }}
      {{- end }}
      {{- if .Values.metrics.serviceMonitor.relabelings }}
      relabelings: {{ toYaml .Values.metrics.serviceMonitor.relabelings | nindent 8 }}
      {{- end }}
  namespaceSelector:
    matchNames:
      - {{ include "sealed-secrets.namespace" . }}
  selector:
    matchLabels: {{- include "sealed-secrets.matchLabels" . | nindent 6 }}
{{- end }}
