{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
  replicas: {{ .Values.replicaCount }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  minReadySeconds: {{ .Values.minReadySeconds }}
  template:
    metadata:
      {{- if or .Values.podAnnotations .Values.commonAnnotations }}
      {{- $podAnnotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.podAnnotations .Values.commonAnnotations ) "context" . ) }}
      annotations: {{- include "common.tplvalues.render" ( dict "value" $podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
    spec:
      {{- include "kube-state-metrics.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.hostNetwork }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      serviceAccountName: {{ template "kube-state-metrics.serviceAccountName" . }}
      {{- if .Values.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: kube-state-metrics
          image: {{ template "kube-state-metrics.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            {{- if .Values.kubeResources.certificatesigningrequests }}
            - --resources=certificatesigningrequests
            {{- end }}
            {{- if .Values.kubeResources.configmaps }}
            - --resources=configmaps
            {{- end }}
            {{- if .Values.kubeResources.cronjobs }}
            - --resources=cronjobs
            {{- end }}
            {{- if .Values.kubeResources.daemonsets }}
            - --resources=daemonsets
            {{- end }}
            {{- if .Values.kubeResources.deployments }}
            - --resources=deployments
            {{- end }}
            {{- if .Values.kubeResources.endpoints }}
            - --resources=endpoints
            {{- end }}
            {{- if .Values.kubeResources.horizontalpodautoscalers }}
            - --resources=horizontalpodautoscalers
            {{- end }}
            {{- if .Values.kubeResources.ingresses }}
            - --resources=ingresses
            {{- end }}
            {{- if .Values.kubeResources.jobs }}
            - --resources=jobs
            {{- end }}
            {{- if .Values.kubeResources.leases }}
            - --resources=leases
            {{- end }}
            {{- if .Values.kubeResources.limitranges }}
            - --resources=limitranges
            {{- end }}
            {{- if .Values.kubeResources.mutatingwebhookconfigurations }}
            - --resources=mutatingwebhookconfigurations
            {{- end }}
            {{- if .Values.kubeResources.namespaces }}
            - --resources=namespaces
            {{- end }}
            {{- if .Values.kubeResources.networkpolicies }}
            - --resources=networkpolicies
            {{- end }}
            {{- if .Values.kubeResources.nodes }}
            - --resources=nodes
            {{- end }}
            {{- if .Values.kubeResources.persistentvolumeclaims }}
            - --resources=persistentvolumeclaims
            {{- end }}
            {{- if .Values.kubeResources.persistentvolumes }}
            - --resources=persistentvolumes
            {{- end }}
            {{- if .Values.kubeResources.poddisruptionbudgets }}
            - --resources=poddisruptionbudgets
            {{- end }}
            {{- if .Values.kubeResources.pods }}
            - --resources=pods
            {{- end }}
            {{- if .Values.kubeResources.replicasets }}
            - --resources=replicasets
            {{- end }}
            {{- if .Values.kubeResources.replicationcontrollers }}
            - --resources=replicationcontrollers
            {{- end }}
            {{- if .Values.kubeResources.resourcequotas }}
            - --resources=resourcequotas
            {{- end }}
            {{- if .Values.kubeResources.secrets }}
            - --resources=secrets
            {{- end }}
            {{- if .Values.kubeResources.services }}
            - --resources=services
            {{- end }}
            {{- if .Values.kubeResources.statefulsets }}
            - --resources=statefulsets
            {{- end }}
            {{- if .Values.kubeResources.storageclasses }}
            - --resources=storageclasses
            {{- end }}
            {{- if .Values.kubeResources.validatingwebhookconfigurations }}
            - --resources=validatingwebhookconfigurations
            {{- end }}
            {{- if .Values.kubeResources.volumeattachments }}
            - --resources=volumeattachments
            {{- end }}
            {{- if .Values.customResourceState.enabled }}
            - --custom-resource-state-config-file=/etc/customresourcestate/configuration.yaml
            {{- end }}
            {{- if .Values.namespaces }}
            - --namespaces={{- include "common.tplvalues.render" ( dict "value" .Values.namespaces "context" $ ) }}
            {{- end }}
            - --port={{ .Values.containerPorts.http }}
            - --telemetry-port={{ coalesce .Values.selfMonitor.telemetryPort .Values.containerPorts.telemetry }}
            {{- range $key, $value := .Values.extraArgs }}
            {{- if $value }}
            - --{{ $key }}={{ $value }}
            {{- else }}
            - --{{ $key }}
            {{- end }}
            {{- end }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
              protocol: TCP
            - name: metrics
              containerPort: {{ coalesce .Values.selfMonitor.telemetryPort .Values.containerPorts.telemetry }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: http
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.customResourceState.enabled }}
            - name: customresourcestate-configuration
              mountPath: /etc/customresourcestate
              readOnly: true
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.customResourceState.enabled}}
        - name: customresourcestate-configuration
          configMap:
            name: {{ printf "%s-customresourcestate-configuration" (include "common.names.fullname" .) }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
