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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: kube-arangodb
    app.kubernetes.io/component: operator
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.autoscaling.hpa.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- 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/part-of: kube-arangodb
      app.kubernetes.io/component: operator
  template:
    metadata:
      {{- if or .Values.podAnnotations (not .Values.auth.existingSecret) }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if not .Values.auth.existingSecret }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- end }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: kube-arangodb
        app.kubernetes.io/component: operator
    spec:
      serviceAccountName: {{ template "kube-arangodb.serviceAccountName" . }}
      {{- include "kube-arangodb.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- 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" "operator" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "operator" "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.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- 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: operator
          image: {{ template "kube-arangodb.operator.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.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.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.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --scope={{ .Values.scope }}
            - --server.admin-secret-name={{ include "kube-arangodb.secretName" . }}
            - --server.port={{ .Values.containerPorts.server }}
            - --api.enabled={{ .Values.enableAPI }}
            {{- if .Values.deploymentFeatures.ephemeralVolumes }}
            - --deployment.feature.ephemeral-volumes
            {{- end }}
            {{- if .Values.deploymentFeatures.securedContainers }}
            - --deployment.feature.secured-containers
            {{- end }}
            {{- if .Values.enableAPI }}
            - --api.http-port={{ .Values.containerPorts.apiHttp }}
            - --api.grpc-port={{ .Values.containerPorts.apiGrpc }}
            {{- end }}
            {{- if .Values.features.deployment }}
            - --operator.deployment
            {{- end -}}
            {{ if .Values.features.deploymentReplications }}
            - --operator.deployment-replication
            {{- end -}}
            {{ if .Values.features.storage }}
            - --operator.storage
            {{- end }}
            {{ if .Values.features.backup }}
            - --operator.backup
            {{- end }}
            {{- if (eq (int .Values.replicaCount) 1) }}
            - --mode.single
            {{- end }}
            {{ if .Values.features.apps }}
            - --operator.apps
            {{- end }}
            {{ if .Values.features.ml }}
            - --operator.ml
            {{- end }}
            {{ if .Values.features.analytics }}
            - --operator.analytics
            {{- end }}
            {{ if .Values.features.networking }}
            - --operator.networking
            {{- end }}
            {{ if .Values.features.scheduler }}
            - --operator.scheduler
            {{- end }}
            {{ if .Values.features.platform }}
            - --operator.platform
            {{- end }}
            {{ if .Values.features.k8sToK8sClusterSync }}
            - --operator.k2k-cluster-sync
            {{- end }}
            - --chaos.allowed={{ .Values.allowChaos }}
            {{- if .Values.extraArgs }}
            {{- range .Values.extraArgs }}
            - {{ . | quote }}
            {{- end }}
            {{- end }}
          {{- end }}
          env:
            - name: MY_POD_NAMESPACE
              valueFrom:
                  fieldRef:
                      fieldPath: metadata.namespace
            - name: MY_POD_IP
              valueFrom:
                  fieldRef:
                      fieldPath: status.podIP
            - name: MY_POD_NAME
              valueFrom:
                  fieldRef:
                      fieldPath: metadata.name
            - name: MY_CONTAINER_NAME
              value: "operator"
            - name: RELATED_IMAGE_DATABASE
              value: {{ include "kube-arangodb.arangodb.image" . }}
            {{- if .Values.features.apps }}
            - name: ARANGOJOB_SA_NAME
              value: {{ include "kube-arangodb.job.serviceAccountName" . | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: https-server
              containerPort: {{ .Values.containerPorts.server }}
            {{- if .Values.enableAPI }}
            - name: http-api
              containerPort: {{ .Values.containerPorts.apiHttp }}
            - name: grpc-api
              containerPort: {{ .Values.containerPorts.apiGrpc }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /health
              port: https-server
              scheme: HTTPS
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: https-server
              scheme: HTTPS
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: https-server
              scheme: HTTPS
          {{- end }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.webhooks.enabled }}
        - name: webhooks
          image: {{ template "kube-arangodb.operator.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.webhooks.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.webhooks.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.webhooks.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.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.webhooks.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - webhook
            - --server.address=0.0.0.0:{{ .Values.webhooks.containerPorts.webhook }}
            {{- if .Values.webhooks.extraArgs }}
            {{- range .Values.webhooks.extraArgs }}
            - {{ . | quote }}
            {{- end }}
            {{- end }}
          {{- end }}
          env:
            - name: MY_POD_NAMESPACE
              valueFrom:
                  fieldRef:
                      fieldPath: metadata.namespace
            - name: MY_POD_NAME
              valueFrom:
                  fieldRef:
                      fieldPath: metadata.name
            - name: MY_CONTAINER_NAME
              value: "webhooks"
            - name: MY_POD_IP
              valueFrom:
                  fieldRef:
                      fieldPath: status.podIP
            {{- if .Values.webhooks.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.webhooks.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.webhooks.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.webhooks.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.webhooks.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.webhooks.resources }}
          resources: {{- toYaml .Values.webhooks.resources | nindent 12 }}
          {{- else if ne .Values.webhooks.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.webhooks.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http-webhook
              containerPort: {{ .Values.webhooks.containerPorts.webhook }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.webhooks.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.webhooks.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.webhooks.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /health
              port: http-webhook
              scheme: HTTPS
          {{- end }}
          {{- if .Values.webhooks.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.webhooks.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.webhooks.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: http-webhook
              scheme: HTTPS
          {{- end }}
          {{- if .Values.webhooks.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.webhooks.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.webhooks.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: http-webhook
              scheme: HTTPS
          {{- end }}
          {{- end }}
          {{- if .Values.webhooks.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.webhooks.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" (dict "value" .Values.webhooks.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.extraVolumes }}
      volumes: {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
      {{- end }}