{{- if .Values.kubeStateMetrics.enabled -}}
apiVersion: {{ template "prometheus.deployment.apiVersion" . }}
kind: Deployment
metadata:
  labels:
    {{- include "prometheus.kubeStateMetrics.labels" . | nindent 4 }}
  name: {{ template "prometheus.kubeStateMetrics.fullname" . }}
{{ include "prometheus.namespace" . | indent 2 }}
spec:
  selector:
    matchLabels:
      {{- include "prometheus.kubeStateMetrics.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.kubeStateMetrics.replicaCount }}
  template:
    metadata:
    {{- if .Values.kubeStateMetrics.podAnnotations }}
      annotations:
{{ toYaml .Values.kubeStateMetrics.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        {{- include "prometheus.kubeStateMetrics.labels" . | nindent 8 }}
        {{- if .Values.kubeStateMetrics.podLabels}}
        {{ toYaml .Values.kubeStateMetrics.podLabels | nindent 8 }}
        {{- end}}
    spec:
      hostNetwork: {{ .Values.kubeStateMetrics.hostNetwork }}
      serviceAccountName: {{ template "prometheus.serviceAccountName.kubeStateMetrics" . }}
      {{- if .Values.kubeStateMetrics.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.kubeStateMetrics.securityContext.fsGroup }}
        runAsGroup: {{ .Values.kubeStateMetrics.securityContext.runAsGroup }}
        runAsUser: {{ .Values.kubeStateMetrics.securityContext.runAsUser }}
      {{- end }}
    {{- if .Values.kubeStateMetrics.priorityClassName }}
      priorityClassName: {{ .Values.kubeStateMetrics.priorityClassName }}
    {{- end }}
      containers:
        - name: {{ template "prometheus.name" . }}-{{ .Values.kubeStateMetrics.name }}
          image: "{{ .Values.kubeStateMetrics.image.repository }}:{{ .Values.kubeStateMetrics.image.tag }}"
          imagePullPolicy: "{{ .Values.kubeStateMetrics.image.pullPolicy }}"
        {{- if .Values.kubeStateMetrics.args }}
          args:
          {{- range $key, $value := .Values.kubeStateMetrics.args }}
            - --{{ $key }}={{ $value }}
          {{- end }}
        {{- end }}
          ports:
            - name: metrics
              containerPort: 8080
          resources:
{{ toYaml .Values.kubeStateMetrics.resources | indent 12 }}
    {{- if .Values.kubeStateMetrics.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.kubeStateMetrics.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.kubeStateMetrics.tolerations }}
      tolerations:
{{ toYaml .Values.kubeStateMetrics.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.kubeStateMetrics.affinity }}
      affinity:
{{ toYaml .Values.kubeStateMetrics.affinity | indent 8 }}
    {{- end }}
{{- end }}
