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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "cilium.operator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
    app.kubernetes.io/component: operator
  {{- if or .Values.operator.deploymentAnnotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.operator.deploymentAnnotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.operator.autoscaling.hpa.enabled }}
  replicas: {{ .Values.operator.replicaCount }}
  {{- end }}
  {{- if .Values.operator.updateStrategy }}
  strategy: {{- toYaml .Values.operator.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.operator.podLabels .Values.commonLabels) "context" .) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: cilium
      app.kubernetes.io/component: operator
  template:
    metadata:
      annotations:
        {{- if not .Values.existingConfigmap }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.operator.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.operator.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: cilium
        app.kubernetes.io/component: operator
    spec:
      {{- include "cilium.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "cilium.operator.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.operator.automountServiceAccountToken }}
      {{- if .Values.operator.hostNetwork }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      {{- end }}
      restartPolicy: Always
      {{- if .Values.operator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.operator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.operator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        {{- if not (empty .Values.operator.podAffinityPreset) }}
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAffinityPreset "component" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        {{- end }}
        {{- if not (empty .Values.operator.podAntiAffinityPreset) }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.operator.podAntiAffinityPreset "component" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        {{- end }}
        {{- if not (empty .Values.operator.nodeAffinityPreset.type) }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.operator.nodeAffinityPreset.type "key" .Values.operator.nodeAffinityPreset.key "values" .Values.operator.nodeAffinityPreset.values) | nindent 10 }}
        {{- end }}
      {{- end }}
      {{- if .Values.operator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.operator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.operator.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.priorityClassName }}
      priorityClassName: {{ .Values.operator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.operator.schedulerName }}
      schedulerName: {{ .Values.operator.schedulerName | quote }}
      {{- end }}
      {{- if .Values.operator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.operator.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.operator.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.operator.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.operator.initContainers }}
      initContainers:
        {{- include "common.tplvalues.render" (dict "value" .Values.operator.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: cilium-operator
          image: {{ include "cilium.operator.image" . }}
          imagePullPolicy: {{ .Values.operator.image.pullPolicy }}
          {{- if .Values.operator.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.operator.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else }}
          command: {{- include "cilium.operator.command" . | nindent 12 }}
          {{- end }}
          args:
          {{- if .Values.diagnosticMode.enabled }}
            {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.operator.args }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.args "context" $) | nindent 12 }}
          {{- else }}
            - --config-dir=/opt/bitnami/cilium-operator/conf
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.operator.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: K8S_NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
            - name: CILIUM_K8S_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            {{- if .Values.azure.enabled }}
            {{- if .Values.azure.subscriptionID }}
            - name: AZURE_SUBSCRIPTION_ID
              value: {{ .Values.azure.subscriptionID }}
            {{- end }}
            {{- if .Values.azure.tenantID }}
            - name: AZURE_TENANT_ID
              value: {{ .Values.azure.tenantID }}
            {{- end }}
            {{- if .Values.azure.resourceGroup }}
            - name: AZURE_RESOURCE_GROUP
              value: {{ .Values.azure.resourceGroup }}
            {{- end }}
            - name: AZURE_CLIENT_ID
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-azure" (include "cilium.operator.fullname" .) | trunc 63 | trimSuffix "-" }}
                  key: client-id
            - name: AZURE_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-azure" (include "cilium.operator.fullname" .) | trunc 63 | trimSuffix "-" }}
                  key: client-secret
            {{- end }}
            {{- if .Values.aws.enabled }}
            {{- if .Values.aws.region }}
            - name: AWS_DEFAULT_REGION
              value: {{ .Values.aws.region }}
            {{- end }}
            - name: AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-aws" (include "cilium.operator.fullname" .) | trunc 63 | trimSuffix "-" }}
                  key: access-key-id
            - name: AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-aws" (include "cilium.operator.fullname" .) | trunc 63 | trimSuffix "-" }}
                  key: secret-access-key
            {{- end }}
            {{- if .Values.operator.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.operator.extraEnvVarsCM .Values.operator.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.operator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.operator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.operator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          {{- if .Values.operator.resources }}
          resources: {{- toYaml .Values.operator.resources | nindent 12 }}
          {{- else if ne .Values.operator.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.operator.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: api
              containerPort: {{ .Values.operator.containerPorts.api }}
            {{- if .Values.operator.enablePprof }}
            - name: pprof
              containerPort: {{ .Values.operator.containerPorts.pprof }}
            {{- end }}
            {{- if .Values.operator.metrics.enabled }}
            - name: metrics
              containerPort: {{ .Values.operator.containerPorts.metrics }}
            {{- end }}
            {{- if .Values.operator.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.operator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: api
          {{- end }}
          {{- if .Values.operator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: api
              scheme: HTTP
          {{- end }}
          {{- if .Values.operator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.operator.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.operator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.operator.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: api
          {{- end }}
          {{- end }}
          lifecycle:
          {{- if .Values.operator.lifecycleHooks }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /.config
              subPath: gops-config-dir
            - name: config
              mountPath: /opt/bitnami/cilium-operator/conf
              readOnly: true
            {{- if .Values.operator.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.operator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.operator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config
          configMap:
            name: {{ template "cilium.configmapName" . }}
        {{- if .Values.operator.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.operator.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
       