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

{{- if .Values.controlPlane.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: {{ ternary "DaemonSet" "Deployment" .Values.controlPlane.useDaemonSet }}
metadata:
  name: {{ template "apisix.control-plane.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: apisix
    app.kubernetes.io/component: control-plane
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if and (not .Values.controlPlane.useDaemonSet) (not .Values.controlPlane.autoscaling.hpa.enabled) }}
  replicas: {{ .Values.controlPlane.replicaCount }}
  {{- end }}
  {{- if .Values.controlPlane.updateStrategy }}
  {{ ternary "updateStrategy" "strategy" .Values.controlPlane.useDaemonSet }}: {{- toYaml .Values.controlPlane.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.controlPlane.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: apisix
      app.kubernetes.io/component: control-plane
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/control-plane/configmap.yaml") . | sha256sum }}
        checksum/config-extra: {{ include (print $.Template.BasePath "/control-plane/extra-configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/control-plane/api-token-secret.yaml") . | sha256sum }}
        {{- if .Values.controlPlane.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.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: control-plane
    spec:
      serviceAccountName: {{ template "apisix.control-plane.serviceAccountName" . }}
      {{- if .Values.controlPlane.hostNetwork }}
      hostNetwork: {{ .Values.controlPlane.hostNetwork }}
      {{- end }}
      {{- if .Values.controlPlane.dnsPolicy }}
      dnsPolicy: {{ .Values.controlPlane.dnsPolicy | quote }} 
      {{- end }}   
      {{- include "apisix.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.controlPlane.automountServiceAccountToken }}
      {{- if .Values.controlPlane.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controlPlane.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.controlPlane.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.controlPlane.podAffinityPreset "component" "control-plane" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.controlPlane.podAntiAffinityPreset "component" "control-plane" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.controlPlane.nodeAffinityPreset.type "key" .Values.controlPlane.nodeAffinityPreset.key "values" .Values.controlPlane.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.controlPlane.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.controlPlane.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controlPlane.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.controlPlane.priorityClassName }}
      priorityClassName: {{ .Values.controlPlane.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.controlPlane.schedulerName }}
      schedulerName: {{ .Values.controlPlane.schedulerName | quote }}
      {{- end }}
      {{- if .Values.controlPlane.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.controlPlane.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.controlPlane.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controlPlane.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.controlPlane.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- include "apisix.waitForETCDInitContainer" . | nindent 8 }}
        {{- include "apisix.renderConfInitContainer" (dict "component" "control-plane" "context" $) | nindent 8 }}
        {{- include "apisix.prepareApisixInitContainer" (dict "component" "control-plane" "context" $) | nindent 8 }}
        {{- if .Values.controlPlane.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: apisix
          image: {{ template "apisix.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.controlPlane.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.controlPlane.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.controlPlane.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - bash
            - -c
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.controlPlane.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - |
              #!/bin/bash
              # Clean up UDS file before starting APISIX
              echo "Checking for existing UDS file..."
              UDS_FILE="/usr/local/apisix/logs/worker_events.sock"
              if [ -e "$UDS_FILE" ]; then
                  echo "Found existing UDS file: $UDS_FILE - removing it"
                  rm -f "$UDS_FILE"
                  echo "UDS file removed successfully"
              else
                  echo "No existing UDS file found at: $UDS_FILE"
              fi
              
              # Start OpenResty
              exec openresty -p /usr/local/apisix -g "daemon off;"
          {{- end }}
          env:
            {{- if .Values.controlPlane.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.controlPlane.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.controlPlane.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.controlPlane.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.controlPlane.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.controlPlane.resources }}
          resources: {{- toYaml .Values.controlPlane.resources | nindent 12 }}
          {{- else if ne .Values.controlPlane.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.controlPlane.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: {{ ternary "https" "http" .Values.controlPlane.tls.enabled }}-admin-api
              containerPort: {{ .Values.controlPlane.containerPorts.adminAPI }}
            - name: {{ ternary "https" "http" .Values.controlPlane.tls.enabled }}-cfg-srv
              containerPort: {{ .Values.controlPlane.containerPorts.configServer }}
            - name: http-control
              containerPort: {{ .Values.controlPlane.containerPorts.control }}
            {{- if .Values.controlPlane.metrics.enabled }}
            - name: http-metrics
              containerPort: {{ .Values.controlPlane.containerPorts.metrics }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.controlPlane.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.controlPlane.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.controlPlane.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-control
          {{- end }}
          {{- if .Values.controlPlane.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.controlPlane.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.controlPlane.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /v1/healthcheck
              port: http-control
          {{- end }}
          {{- if .Values.controlPlane.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.controlPlane.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.controlPlane.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /v1/healthcheck
              port: http-control
          {{- end }}
          {{- end }}
          {{- if .Values.controlPlane.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /usr/local/apisix
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.controlPlane.tls.enabled }}
            - name: certs
              mountPath: /bitnami/certs
            {{- end }}
          {{- if .Values.controlPlane.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.controlPlane.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.controlPlane.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ include "apisix.control-plane.defaultConfigmapName" . }}
        {{- if .Values.usePasswordFiles }}
        - name: apisix-secrets
          projected:
            sources:
              - secret:
                  name: {{ include "apisix.control-plane.secretName" . }}
              {{- if (include "apisix.etcd.authEnabled" .) }}
              - secret:
                  name:  {{ include "apisix.etcd.secretName" . }}
              {{- end }}
        {{- end }}
        {{- if or .Values.controlPlane.extraConfig .Values.controlPlane.extraConfigExistingConfigMap }}
        - name: extra-config
          configMap:
            name: {{ include "apisix.control-plane.extraConfigmapName" . }}
        {{- end }}
        {{- if .Values.controlPlane.tls.enabled }}
        - name: certs
          secret:
            secretName: {{ template "apisix.control-plane.tlsSecretName" . }}
        {{- end }}
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.controlPlane.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controlPlane.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
