apiVersion: apps/v1
{{- if .Values.autosharding.enabled }}
kind: StatefulSet
{{- else }}
kind: Deployment
{{- end }}
metadata:
  name: {{ template "kube-state-metrics.fullname" . }}
  namespace: {{ template "kube-state-metrics.namespace" . }}
  labels:
    app.kubernetes.io/name: {{ template "kube-state-metrics.name" . }}
    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    app.kubernetes.io/instance: "{{ .Release.Name }}"
    app.kubernetes.io/managed-by: "{{ .Release.Service }}"
{{- if .Values.customLabels }}
{{ toYaml .Values.customLabels | indent 4 }}
{{- end }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "kube-state-metrics.name" . }}
  replicas: {{ .Values.replicas }}
{{- if .Values.autosharding.enabled }}
  serviceName: {{ template "kube-state-metrics.fullname" . }}
  volumeClaimTemplates: []
{{- end }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "kube-state-metrics.name" . }}
        app.kubernetes.io/instance: "{{ .Release.Name }}"
{{- if .Values.customLabels }}
{{ toYaml .Values.customLabels | indent 8 }}
{{- end }}
{{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
    spec:
      hostNetwork: {{ .Values.hostNetwork }}
      serviceAccountName: {{ template "kube-state-metrics.serviceAccountName" . }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsGroup: {{ .Values.securityContext.runAsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
    {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
    {{- end }}
      containers:
      - name: {{ .Chart.Name }}
{{- if .Values.autosharding.enabled }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
{{- end }}
        args:
{{  if .Values.collectors.certificatesigningrequests  }}
        - --collectors=certificatesigningrequests
{{  end  }}
{{  if .Values.collectors.configmaps  }}
        - --collectors=configmaps
{{  end  }}
{{  if .Values.collectors.cronjobs  }}
        - --collectors=cronjobs
{{  end  }}
{{ if .Values.collectors.daemonsets  }}
        - --collectors=daemonsets
{{  end  }}
{{  if .Values.collectors.deployments  }}
        - --collectors=deployments
{{  end  }}
{{  if .Values.collectors.endpoints  }}
        - --collectors=endpoints
{{  end  }}
{{  if .Values.collectors.horizontalpodautoscalers  }}
        - --collectors=horizontalpodautoscalers
{{  end  }}
{{  if .Values.collectors.ingresses  }}
        - --collectors=ingresses
{{  end  }}
{{  if .Values.collectors.jobs  }}
        - --collectors=jobs
{{  end  }}
{{  if .Values.collectors.limitranges  }}
        - --collectors=limitranges
{{  end  }}
{{  if .Values.collectors.mutatingwebhookconfigurations  }}
        - --collectors=mutatingwebhookconfigurations
{{  end  }}
{{  if .Values.collectors.namespaces  }}
        - --collectors=namespaces
{{  end  }}
{{  if .Values.collectors.networkpolicies  }}
        - --collectors=networkpolicies
{{  end  }}
{{  if .Values.collectors.nodes  }}
        - --collectors=nodes
{{  end  }}
{{  if .Values.collectors.persistentvolumeclaims  }}
        - --collectors=persistentvolumeclaims
{{  end  }}
{{  if .Values.collectors.persistentvolumes  }}
        - --collectors=persistentvolumes
{{  end  }}
{{  if .Values.collectors.poddisruptionbudgets  }}
        - --collectors=poddisruptionbudgets
{{  end  }}
{{  if .Values.collectors.pods  }}
        - --collectors=pods
{{  end  }}
{{  if .Values.collectors.replicasets  }}
        - --collectors=replicasets
{{  end  }}
{{  if .Values.collectors.replicationcontrollers  }}
        - --collectors=replicationcontrollers
{{  end  }}
{{  if .Values.collectors.resourcequotas  }}
        - --collectors=resourcequotas
{{  end  }}
{{  if .Values.collectors.secrets  }}
        - --collectors=secrets
{{  end  }}
{{  if .Values.collectors.services  }}
        - --collectors=services
{{  end  }}
{{  if .Values.collectors.statefulsets  }}
        - --collectors=statefulsets
{{  end  }}
{{  if .Values.collectors.storageclasses  }}
        - --collectors=storageclasses
{{  end  }}
{{  if .Values.collectors.validatingwebhookconfigurations  }}
        - --collectors=validatingwebhookconfigurations
{{  end  }}
{{  if .Values.collectors.verticalpodautoscalers  }}
        - --collectors=verticalpodautoscalers
{{  end  }}
{{  if .Values.collectors.volumeattachments  }}
        - --collectors=volumeattachments
{{  end  }}
{{ if .Values.namespace }}
        - --namespace={{ .Values.namespace }}
{{ end }}
{{ if .Values.autosharding.enabled }}
        - --pod=$(POD_NAME)
        - --pod-namespace=$(POD_NAMESPACE)
{{ end }}
{{ if .Values.kubeconfig.enabled }}
        - --kubeconfig=/opt/k8s/.kube/config
{{ end }}
{{- if .Values.kubeconfig.enabled }}
        volumeMounts:
        - name: kubeconfig
          mountPath: /opt/k8s/.kube/
          readOnly: true
{{- end }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 5
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 5
          timeoutSeconds: 5
{{- if .Values.resources }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
{{- end }}
{{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
{{- end }}
{{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
{{- end }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
{{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
{{- end }}
{{- if .Values.kubeconfig.enabled}}
      volumes:
        - name: kubeconfig
          secret:
            secretName: {{ template "kube-state-metrics.fullname" . }}-kubeconfig
{{- end }}
