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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "certmanager.controller.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.controller.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: controller
  {{- 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.controller.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: controller
  {{- if .Values.controller.updateStrategy }}
  strategy: {{- toYaml .Values.controller.updateStrategy | nindent 4 }}
  {{- end }}
  replicas: {{ .Values.controller.replicaCount }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: controller
      annotations:
        {{- if .Values.controller.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.metrics.enabled (not .Values.metrics.serviceMonitor.enabled) }}
        {{- include "common.tplvalues.render" (dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "certmanager.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.controller.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.controller.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.schedulerName }}
      schedulerName: {{ .Values.controller.schedulerName }}
      {{- end }}
      {{- if .Values.controller.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.controller.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.priorityClassName }}
      priorityClassName: {{ .Values.controller.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.controller.runtimeClassName }}
      runtimeClassName: {{ .Values.controller.runtimeClassName }}
      {{- end }}
      {{- if .Values.controller.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.controller.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.controller.podAffinityPreset "component" "controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.controller.podAntiAffinityPreset "component" "controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.controller.nodeAffinityPreset.type "key" .Values.controller.nodeAffinityPreset.key "values" .Values.controller.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.controller.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.controller.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.controller.tolerations "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.controller.automountServiceAccountToken }}
      {{- if .Values.controller.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.controller.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.controller.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.initContainers "context" $) | nindent 8 }}
        {{- end }}
      serviceAccountName: {{ template "certmanager.controller.serviceAccountName" . }}
      containers:
        - name: cert-manager
          image: {{ template "certmanager.image" . }}
          imagePullPolicy: {{ .Values.controller.image.pullPolicy | quote }}
          {{- if .Values.controller.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.controller.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.controller.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.controller.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --v={{ .Values.logLevel }}
            - --metrics-listen-address=0.0.0.0:{{ .Values.controller.containerPorts.http }}
            - --internal-healthz-listen-address=0.0.0.0:{{ .Values.controller.containerPorts.health }}
            {{- if .Values.controller.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.clusterResourceNamespace }}
            - --cluster-resource-namespace={{ .Values.clusterResourceNamespace }}
          {{- else }}
            - --cluster-resource-namespace=$(POD_NAMESPACE)
          {{- end }}
            - --leader-election-namespace={{ .Values.leaderElection.namespace }}
            - --acme-http01-solver-image={{ template "certmanager.acmesolver.image" . }}
          {{- end }}
          {{- if .Values.controller.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.controller.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.controller.image.debug | quote }}
            {{- if .Values.controller.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          envFrom:
            {{- if .Values.controller.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.controller.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.controller.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.controller.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.controller.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.controller.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              protocol: TCP
              containerPort: {{ coalesce .Values.controller.containerPort .Values.controller.containerPorts.http }}
            - name: health
              protocol: TCP
              containerPort: {{ .Values.controller.containerPorts.health }}
          {{- if .Values.controller.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controller.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.controller.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.controller.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.controller.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controller.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.controller.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              port: http
              path: /metrics
            initialDelaySeconds: {{ .Values.controller.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.controller.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controller.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.controller.startupProbe.enabled }}
          startupProbe:
            httpGet:
              port: http
              path: /metrics
            initialDelaySeconds: {{ .Values.controller.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.controller.resources }}
          resources: {{- toYaml .Values.controller.resources | nindent 12 }}
          {{- else if ne .Values.controller.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.controller.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.controller.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.controller.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.controller.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.controller.dnsPolicy }}
      dnsPolicy: {{ .Values.controller.dnsPolicy }}
      {{- end }}
      {{- if .Values.controller.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.controller.dnsConfig "context" $) | nindent 8 }}
      {{- end }}
