{{- if and .Values.operator.prometheus.serviceMonitor.enabled .Values.operator.enabled }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  clusterIP: None
  type: ClusterIP
  ports:
    - port: 8080
      targetPort: metrics
      protocol: TCP
      name: metrics
  selector: {{- include "common.labels.matchLabels" . | nindent 4 }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ default .Release.Namespace .Values.operator.prometheus.serviceMonitor.namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.operator.prometheus.serviceMonitor.jobLabel }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  endpoints:
    - port: metrics
      {{- if .Values.operator.prometheus.serviceMonitor.interval }}
      interval: {{ .Values.operator.prometheus.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.operator.prometheus.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{ toYaml .Values.operator.prometheus.serviceMonitor.metricRelabelings | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.prometheus.serviceMonitor.relabelings }}
      relabelings: {{ toYaml .Values.operator.prometheus.serviceMonitor.relabelings | nindent 8 }}
      {{- end }}
{{- end }}
