{{- if .Values.kubeStateMetrics.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-kube-state-metrics
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.name" . }}-kube-state-metrics
{{ include "kube-prometheus-stack.labels" . | indent 4 }}
spec:
  jobLabel: app.kubernetes.io/name
  endpoints:
  - port: http
    {{- if .Values.kubeStateMetrics.serviceMonitor.interval }}
    interval: {{ .Values.kubeStateMetrics.serviceMonitor.interval }}
    {{- end }}
    honorLabels: true
{{- if .Values.kubeStateMetrics.serviceMonitor.metricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubeStateMetrics.serviceMonitor.metricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubeStateMetrics.serviceMonitor.relabelings }}
    relabelings:
{{ toYaml .Values.kubeStateMetrics.serviceMonitor.relabelings | indent 4 }}
{{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/name: kube-state-metrics
      app.kubernetes.io/instance: "{{ $.Release.Name }}"
{{- end }}
