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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "certmanager.webhook.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.webhook.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: webhook
  {{- 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.webhook.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: webhook
  {{- if .Values.webhook.updateStrategy }}
  strategy: {{- toYaml .Values.webhook.updateStrategy | nindent 4 }}
  {{- end }}
  replicas: {{ .Values.webhook.replicaCount }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: webhook
      annotations:
        {{- if .Values.webhook.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.webhook.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      hostNetwork: {{ .Values.webhook.hostNetwork }}
      {{- include "certmanager.webhook.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.webhook.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.webhook.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.webhook.schedulerName }}
      schedulerName: {{ .Values.webhook.schedulerName }}
      {{- end }}
      {{- if .Values.webhook.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.webhook.priorityClassName }}
      priorityClassName: {{ .Values.webhook.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.webhook.runtimeClassName }}
      runtimeClassName: {{ .Values.webhook.runtimeClassName }}
      {{- end }}
      {{- if .Values.webhook.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.webhook.podAffinityPreset "component" "webhook" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.webhook.podAntiAffinityPreset "component" "webhook" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.webhook.nodeAffinityPreset.type "key" .Values.webhook.nodeAffinityPreset.key "values" .Values.webhook.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.webhook.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.webhook.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.tolerations "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.webhook.automountServiceAccountToken }}
      {{- if .Values.webhook.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.webhook.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.webhook.initContainers "context" $) | nindent 8 }}
        {{- end }}
      serviceAccountName: {{ template "certmanager.webhook.serviceAccountName" . }}
      containers:
        - name: cert-manager-webhook
          image: {{ template "certmanager.webhook.image" . }}
          imagePullPolicy: {{ .Values.webhook.image.pullPolicy | quote }}
          {{- if .Values.webhook.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.webhook.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --v={{ .Values.logLevel }}
            - --secure-port={{ coalesce .Values.webhook.containerPort .Values.webhook.containerPorts.https }}
            - --healthz-port={{ .Values.webhook.containerPorts.health }}
            - --dynamic-serving-ca-secret-namespace=$(POD_NAMESPACE)
            - --dynamic-serving-ca-secret-name={{ template "certmanager.webhook.fullname" . }}-ca
            - --dynamic-serving-dns-names={{ template "certmanager.webhook.fullname" . }},{{ template "certmanager.webhook.fullname" . }}.{{ .Release.Namespace }},{{ template "certmanager.webhook.fullname" . }}.{{ .Release.Namespace }}.svc
            {{- if .Values.webhook.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.webhook.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          {{- if .Values.webhook.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.webhook.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.webhook.image.debug | quote }}
            {{- if .Values.webhook.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.webhook.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          envFrom:
            {{- if .Values.webhook.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.webhook.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.webhook.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.webhook.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.webhook.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - name: https
              containerPort: {{ coalesce .Values.webhook.containerPort .Values.webhook.containerPorts.https }}
            - name: health
              containerPort: {{ .Values.webhook.containerPorts.health }}
          {{- if .Values.webhook.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.webhook.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.webhook.livenessProbe.enabled }}
          livenessProbe:
             httpGet:
               path: {{ .Values.webhook.livenessProbe.path }}
               port: {{ .Values.webhook.containerPorts.health }}
               scheme: HTTP
             initialDelaySeconds: {{ .Values.webhook.livenessProbe.initialDelaySeconds }}
             periodSeconds: {{ .Values.webhook.livenessProbe.periodSeconds }}
             timeoutSeconds: {{ .Values.webhook.livenessProbe.timeoutSeconds }}
             successThreshold: {{ .Values.webhook.livenessProbe.successThreshold }}
             failureThreshold: {{ .Values.webhook.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.webhook.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.webhook.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.webhook.readinessProbe.enabled }}
          readinessProbe:
             httpGet:
               path: {{ .Values.webhook.readinessProbe.path }}
               port: {{ .Values.webhook.containerPorts.health }}
               scheme: HTTP
             initialDelaySeconds: {{ .Values.webhook.readinessProbe.initialDelaySeconds }}
             periodSeconds: {{ .Values.webhook.readinessProbe.periodSeconds }}
             timeoutSeconds: {{ .Values.webhook.readinessProbe.timeoutSeconds }}
             successThreshold: {{ .Values.webhook.readinessProbe.successThreshold }}
             failureThreshold: {{ .Values.webhook.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.webhook.resources }}
          resources: {{- toYaml .Values.webhook.resources | nindent 12 }}
          {{- else if ne .Values.webhook.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.webhook.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.webhook.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.webhook.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.webhook.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.webhook.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.webhook.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.webhook.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
