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

{{- if .Values.ingressController.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "apisix.ingress-controller.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.ingressController.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/part-of: apisix
    app.kubernetes.io/component: ingress-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.ingressController.autoscaling.hpa.enabled }}
  replicas: {{ .Values.ingressController.replicaCount }}
  {{- end }}
  {{- if .Values.ingressController.updateStrategy }}
  strategy: {{- toYaml .Values.ingressController.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.ingressController.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: apisix
      app.kubernetes.io/component: ingress-controller
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/ingress-controller/configmap.yaml") . | sha256sum }}
        checksum/config-extra: {{ include (print $.Template.BasePath "/ingress-controller/extra-configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/control-plane/api-token-secret.yaml") . | sha256sum }}
        {{- if .Values.ingressController.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: apisix
        app.kubernetes.io/component: ingress-controller
    spec:
      serviceAccountName: {{ template "apisix.ingress-controller.serviceAccountName" . }}
      {{- include "apisix.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.ingressController.automountServiceAccountToken }}
      {{- if .Values.ingressController.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingressController.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.ingressController.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.ingressController.podAffinityPreset "component" "ingress-controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.ingressController.podAntiAffinityPreset "component" "ingress-controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.ingressController.nodeAffinityPreset.type "key" .Values.ingressController.nodeAffinityPreset.key "values" .Values.ingressController.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.ingressController.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.ingressController.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingressController.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingressController.priorityClassName }}
      priorityClassName: {{ .Values.ingressController.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.ingressController.schedulerName }}
      schedulerName: {{ .Values.ingressController.schedulerName | quote }}
      {{- end }}
      {{- if .Values.ingressController.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingressController.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.ingressController.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingressController.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.ingressController.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.controlPlane.enabled }}
        {{- include "apisix.waitForControlPlaneInitContainer" . | nindent 8 }}
        {{- end }}
        {{- include "apisix.renderConfInitContainer" (dict "component" "ingress-controller" "context" $) | nindent 8 }}
        {{- if .Values.ingressController.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: apisix-ingress-controller
          image: {{ template "apisix.ingress-controller.image" . }}
          imagePullPolicy: {{ .Values.ingressController.image.pullPolicy }}
          {{- if .Values.ingressController.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.ingressController.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.ingressController.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.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.ingressController.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - ingress
            - --config-path
            - /bitnami/apisix-ingress-controller/conf/config.yaml
          {{- end }}
          env:
            {{- if .Values.ingressController.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.ingressController.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.ingressController.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.ingressController.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.ingressController.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.ingressController.resources }}
          resources: {{- toYaml .Values.ingressController.resources | nindent 12 }}
          {{- else if ne .Values.ingressController.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.ingressController.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.ingressController.containerPorts.http }}
            - name: https
              containerPort: {{ .Values.ingressController.containerPorts.https }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.ingressController.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.ingressController.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ingressController.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.ingressController.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.ingressController.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ingressController.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http
          {{- end }}
          {{- if .Values.ingressController.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.ingressController.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ingressController.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.ingressController.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /bitnami/apisix-ingress-controller/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.ingressController.tls.enabled }}
            - name: certs
              mountPath: /bitnami/certs
            {{- end }}
            {{- if .Values.controlPlane.tls.enabled}}
            - name: control-plane-certs
              mountPath: /etc/ssl/certs/{{ .Values.controlPlane.tls.certCAFilename }}
              subPath: {{ .Values.controlPlane.tls.certCAFilename }}
            {{- end }}
          {{- if .Values.ingressController.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.ingressController.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.ingressController.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ include "apisix.ingress-controller.defaultConfigmapName" . }}
        {{- if and .Values.usePasswordFiles (or .Values.controlPlane.enabled (include "apisix.etcd.authEnabled" .)) }}
        - name: apisix-secrets
          projected:
            sources:
              {{- if (include "apisix.etcd.authEnabled" .) }}
              - secret:
                  name:  {{ include "apisix.etcd.secretName" . }}
              {{- end }}
              {{- if .Values.controlPlane.enabled }}
              - secret:
                  name: {{ include "apisix.control-plane.secretName" . }}
              {{- end }}
        {{- end }}
        {{- if or .Values.ingressController.extraConfig .Values.ingressController.extraConfigExistingConfigMap }}
        - name: extra-config
          configMap:
            name: {{ include "apisix.ingress-controller.extraConfigmapName" . }}
        {{- end }}
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.ingressController.tls.enabled }}
        - name: certs
          secret:
            secretName: {{ template "apisix.ingress-controller.tlsSecretName" . }}
        {{- end }}
        {{- if .Values.controlPlane.tls.enabled }}
        - name: control-plane-certs
          secret:
            secretName: {{ template "apisix.control-plane.tlsSecretName" . }}
        {{- end }}
       {{- if .Values.ingressController.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.extraVolumes "context" $) | nindent 8 }}
       {{- end }}
{{- end }}
