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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
{{- if .Values.useDaemonset }}
kind: DaemonSet
{{- else }}
kind: Deployment
{{- end }}
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: server
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.useDaemonset }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- $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 }}
      app.kubernetes.io/component: server
  {{- if .Values.updateStrategy }}
  {{- if .Values.useDaemonset }}
  updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- else }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: server
      annotations:
        {{- if (include "kong.createExternalDBSecret" .) }}
        checksum/secret: {{ include (print $.Template.BasePath "/external-database-secret.yaml") . | sha256sum }}
        {{- end }}
        checksum/configmap-kong: {{ include (print $.Template.BasePath "/kong-script-configmap.yaml") . | sha256sum }}
        {{- if .Values.metrics.enabled }}
        checksum/configmap-metrics-plugin: {{ include (print $.Template.BasePath "/metrics-script-configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "kong.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ingressController.enabled }}
      serviceAccountName: {{ include "kong.ingressController.serviceAccountName" . }}
      {{- 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 "component" "server" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "server" "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 }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- 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 }}
      {{- if .Values.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: kong
          image: {{ template "kong.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.kong.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.kong.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.kong.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.kong.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: KONG_PREFIX
              value: /opt/bitnami/kong/server
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            {{- if .Values.service.exposeAdmin }}
            - name: KONG_ADMIN_LISTEN_ADDRESS
              value: "0.0.0.0"
            {{- end }}
            - name: KONG_DATABASE
              value: {{ include "kong.database" . | quote }}
            {{- if (eq .Values.database "postgresql") }}
            {{- if .Values.usePasswordFiles }}
            - name: KONG_PG_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/kong/secrets/%s" (include "kong.postgresql.databaseSecretKey" .) }}
            {{- else }}
            - name: KONG_PG_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kong.postgresql.secretName" . }}
                  key: {{ include "kong.postgresql.databaseSecretKey" . }}
            {{- end }}
            - name: KONG_PG_HOST
              value: {{ include "kong.postgresql.host" . }}
            - name: KONG_PG_PORT
              value: {{ include "kong.postgresql.port" . }}
            - name: KONG_PG_USER
              value: {{ include "kong.postgresql.user" . }}
            {{- end }}
            {{- if (eq .Values.database "cassandra") }}
            {{- if .Values.usePasswordFiles }}
            - name: KONG_CASSANDRA_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/kong/secrets/%s" (include "kong.cassandra.databaseSecretKey" .) }}
            {{- else }}
            - name: KONG_CASSANDRA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kong.cassandra.secretName" . }}
                  key: {{ include "kong.cassandra.databaseSecretKey" . }}
            {{- end }}
            - name: KONG_CASSANDRA_CONTACT_POINTS
              value: {{ include "kong.cassandra.contactPoints" . }}
            - name: KONG_CASSANDRA_PORT
              value: {{ include "kong.cassandra.port" . }}
            - name: KONG_CASSANDRA_USER
              value: {{ include "kong.cassandra.user" . }}
            {{- end }}
            {{- if .Values.metrics.enabled }}
            - name: KONG_NGINX_HTTP_INCLUDE
              value: "/bitnami/kong/metrics-exporter/exporter.conf"
            {{- end }}
            {{- if (include "kong.declarativeConfigMap" .) }}
            - name: KONG_DECLARATIVE_CONFIG
              value: "/bitnami/kong/declarative-conf/kong.yml"
            {{- end }}
            {{- if .Values.kong.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.kong.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.kong.extraEnvVarsCM .Values.kong.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.kong.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.kong.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.kong.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.kong.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          ports:
            - name: http-proxy
              containerPort: {{ .Values.kong.containerPorts.proxyHttp }}
              protocol: TCP
            - name: https-proxy
              containerPort: {{ .Values.kong.containerPorts.proxyHttps }}
              protocol: TCP
            - name: http-admin
              containerPort: {{ .Values.kong.containerPorts.adminHttp }}
              protocol: TCP
            - name: https-admin
              containerPort: {{ .Values.kong.containerPorts.adminHttps }}
              protocol: TCP
            {{- if .Values.metrics.enabled }}
            - name: http-metrics
              containerPort: {{ .Values.metrics.containerPorts.http }}
              protocol: TCP
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.kong.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.kong.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.kong.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.kong.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-proxy
          {{- end }}
          {{- if .Values.kong.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.kong.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.kong.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.kong.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - -f
                - 'kong start'
          {{- end }}
          {{- if .Values.kong.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.kong.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.kong.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.kong.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - /health/kong-container-health.sh
          {{- end }}
          {{- if not .Values.kong.lifecycleHooks }}
          lifecycle:
            preStop:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - kong quit
          {{- else }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.kong.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.kong.resources }}
          resources: {{- toYaml .Values.kong.resources | nindent 12 }}
          {{- else if ne .Values.kong.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.kong.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/kong/server
              subPath: app-server-dir
            - name: health
              mountPath: /health
            {{- if and .Values.usePasswordFiles (or (eq .Values.database "cassandra") (eq .Values.database "postgresql")) }}
            - name: kong-secrets
              mountPath: /opt/bitnami/kong/secrets
            {{- end -}}
            {{- if .Values.metrics.enabled }}
            - name: metrics-init-scripts
              mountPath: /docker-entrypoint-initdb.d/metrics-init
            - name: metrics-server-block
              mountPath: "/bitnami/kong/metrics-exporter"
            {{ end }}
            {{- if .Values.kong.initScriptsCM }}
            - name: custom-init-scripts-cm
              mountPath: /docker-entrypoint-initdb.d/cm
            {{- end }}
            {{- if .Values.kong.initScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/secret
            {{- end }}
            {{- if (include "kong.declarativeConfigMap" .) }}
            - name: kong-declarative-conf
              mountPath: /bitnami/kong/declarative-conf/
            {{- end }}
            {{- if .Values.kong.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.kong.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.ingressController.enabled }}
        - name: kong-ingress-controller
          image: {{ template "kong.ingress-controller.image" . }}
          imagePullPolicy: {{ .Values.ingressController.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.ingressController.args }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - bash
            - -ec
            - /health/ingress-container-start.sh
          {{- 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 }}
          {{- end }}
          env:
            - name: CONTROLLER_KONG_ADMIN_URL
              value: http://127.0.0.1:{{ .Values.kong.containerPorts.adminHttp }}
            - name: CONTROLLER_PUBLISH_SERVICE
              value: {{ printf "%s/%s" .Release.Namespace (include "common.names.fullname" .) | quote }}
            - name: CONTROLLER_INGRESS_CLASS
              value: {{ .Values.ingressController.ingressClass }}
            - name: CONTROLLER_ELECTION_ID
              value: {{ printf "kong-ingress-controller-leader-%s" .Values.ingressController.ingressClass }}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- if .Values.ingressController.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.ingressController.extraEnvVarsCM .Values.ingressController.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.ingressController.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.ingressController.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.ingressController.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.ingressController.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          ports:
            - name: http-health
              containerPort: {{ .Values.ingressController.containerPorts.health }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- 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 }}
            tcpSocket:
              port: http-health
          {{- end }}
          {{- 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-health
          {{- 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-health
              scheme: HTTP
          {{- end }}
          {{- end }}
          {{- if .Values.ingressController.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.lifecycleHooks "context" $) | nindent 12 }}
          {{- 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 }}
          volumeMounts:
            - name: health
              mountPath: /health
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.ingressController.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ingressController.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: health
          configMap:
            name: {{ template "common.names.fullname" . }}-scripts
            defaultMode: 0755
        {{- if and .Values.usePasswordFiles (or (eq .Values.database "cassandra") (eq .Values.database "postgresql")) }}
        - name: kong-secrets
          secret:
            {{- if (eq .Values.database "cassandra") }}
            secretName: {{ include "kong.cassandra.secretName" . }}
            {{- else if (eq .Values.database "postgresql") }}
            secretName: {{ include "kong.postgresql.secretName" . }}
            {{- end }}
        {{- end -}}
        {{- if .Values.metrics.enabled }}
        - name: metrics-init-scripts
          configMap:
            name: {{ template "common.names.fullname" . }}-metrics-scripts
            defaultMode: 0755
        - name: metrics-server-block
          configMap:
            name: {{ template "common.names.fullname" . }}-metrics-exporter
        {{- end }}
        {{- if .Values.kong.initScriptsCM }}
        - name: custom-init-scripts-cm
          configMap:
            name: {{ include "common.tplvalues.render" (dict "value" .Values.kong.initScriptsCM "context" $) }}
            defaultMode: 0755
        {{- end }}
        {{- if .Values.kong.initScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            secretName: {{ include "common.tplvalues.render" (dict "value" .Values.kong.initScriptsSecret "context" $) }}
            defaultMode: 0755
        {{- end }}
        {{- if (include "kong.declarativeConfigMap" .) }}
        - name: kong-declarative-conf
          configMap:
            name: {{ include "kong.declarativeConfigMap" . | quote }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
