{{- if .Values.opencost.metrics.serviceMonitor.enabled -}}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "opencost.fullname" . }}
  {{- if .Values.opencost.metrics.serviceMonitor.namespace }}
  namespace: {{ .Values.opencost.metrics.serviceMonitor.namespace | quote }}
  {{- end }}
  labels:
    {{- include "opencost.labels" . | nindent 4 }}
    {{- if .Values.opencost.metrics.serviceMonitor.additionalLabels }}
      {{- toYaml .Values.opencost.metrics.serviceMonitor.additionalLabels | nindent 4 }}
    {{- end }}
spec:
  endpoints:
    - port: http
      scheme: {{ .Values.opencost.metrics.serviceMonitor.scheme }}
      path: /metrics
      interval: {{ .Values.opencost.metrics.serviceMonitor.scrapeInterval }}
      scrapeTimeout: {{ .Values.opencost.metrics.serviceMonitor.scrapeTimeout }}
      {{- if .Values.opencost.metrics.serviceMonitor.honorLabels }}
      honorLabels: true
      {{- end }}
      {{- with .Values.opencost.metrics.serviceMonitor.relabelings }}
      relabelings: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.opencost.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.opencost.metrics.serviceMonitor.extraEndpoints }}
      {{- toYaml . | nindent 4 }}
      {{- end }}
      {{- with .Values.opencost.metrics.serviceMonitor.tlsConfig }}
      tlsConfig:
      {{- toYaml . | nindent 8}}
      {{- end }}
  selector:
    matchLabels: {{- include "opencost.selectorLabels" . | nindent 6 }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
{{- end }}
