apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argocd.application-controller" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: controller
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.controller.replicaCount }}
  {{- if .Values.controller.updateStrategy }}
  strategy: {{- toYaml .Values.controller.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: controller
  template:
    metadata:
      {{- if .Values.controller.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.controller.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: controller
        {{- if .Values.controller.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "argocd.application-controller.serviceAccountName" . }}
      {{- include "argocd.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.controller.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.controller.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.controller.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.controller.podAffinityPreset "component" "controller" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.controller.podAntiAffinityPreset "component" "controller" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.controller.nodeAffinityPreset.type "key" .Values.controller.nodeAffinityPreset.key "values" .Values.controller.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.controller.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.controller.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.controller.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.schedulerName }}
      schedulerName: {{ .Values.controller.schedulerName }}
      {{- end }}
      {{- if .Values.controller.shareProcessNamespace }}
      shareProcessNamespace: {{ .Values.controller.shareProcessNamespace }}
      {{- end }}
      {{- if .Values.controller.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.controller.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.controller.priorityClassName }}
      priorityClassName: {{ .Values.controller.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.controller.runtimeClassName }}
      runtimeClassName: {{ .Values.controller.runtimeClassName }}
      {{- end }}
      {{- if .Values.controller.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.controller.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.redisWait.enabled }}
        - name: wait-for-redis
          image: {{ include "common.images.image" (dict "imageRoot" .Values.redis.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.redis.image.pullPolicy | quote }}
          {{- with .Values.redisWait.securityContext }}
          securityContext: {{ . | toYaml }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
                #!/bin/bash

                set -o errexit
                set -o nounset
                set -o pipefail

                . /opt/bitnami/scripts/libos.sh
                . /opt/bitnami/scripts/liblog.sh

                check_redis_connection() {
                  local result="$(redis-cli -h {{ include "argocd.redisHost" . }} -p {{ include "argocd.redisPort" . }} {{ .Values.redisWait.extraArgs }} PING)"
                  if [[ "$result" != "PONG" ]]; then
                    false
                  fi
                }

                info "Checking redis connection..."
                if ! retry_while "check_redis_connection"; then
                    error "Could not connect to the Redis server"
                    return 1
                else
                    info "Connected to the Redis instance"
                fi
          {{- if include "argocd.redis.auth.enabled" . }}
          env:
            - name: REDISCLI_AUTH
              valueFrom:
                secretKeyRef:
                  name: {{ include "argocd.redis.secretName" . }}
                  key: {{ include "argocd.redis.secretPasswordKey" . }}
          {{- end }}
        {{- end }}
        {{- if .Values.controller.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: controller
          image: {{ include "argocd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.controller.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.controller.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.controller.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.controller.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.controller.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.controller.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.controller.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.controller.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - argocd-application-controller
            - --status-processors
            - {{ .Values.controller.defaultArgs.statusProcessors | quote }}
            - --operation-processors
            - {{ .Values.controller.defaultArgs.operationProcessors | quote }}
            - --app-resync
            - {{ .Values.controller.defaultArgs.appResyncPeriod | quote }}
            - --self-heal-timeout-seconds
            - {{ .Values.controller.defaultArgs.selfHealTimeout | quote }}
            - --repo-server
            - {{ include "argocd.repo-server" . }}:{{ .Values.repoServer.service.port }}
            - --logformat
            - {{ .Values.controller.logFormat | quote }}
            - --loglevel
            - {{ .Values.controller.logLevel | quote }}
            # TODO(miguelaeh): Test the chart using redis sentinel enabled: https://github.com/argoproj/argo-cd/blob/2a410187565e15633b6f2a8c8d8da22cf02b257d/util/cache/cache.go#L40
            - --redis
            - {{ include "argocd.redisHost" . }}:{{ include "argocd.redisPort" . }}
          {{- if .Values.controller.extraArgs }}
          {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraArgs "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          ports:
            - name: controller
              containerPort: {{ .Values.controller.containerPorts.controller }}
              protocol: TCP
            - name: metrics
              containerPort: {{ .Values.controller.containerPorts.metrics }}
              protocol: TCP
          env:
            {{- if and .Values.redis.enabled (include "argocd.redis.auth.enabled" .) }}
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "argocd.redis.secretName" . }}
                  key: {{ include "argocd.redis.secretPasswordKey" . }}
            {{- end }}
            {{- if .Values.controller.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.controller.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.controller.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.controller.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.controller.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.controller.resources }}
          resources: {{- toYaml .Values.controller.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.controller.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controller.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.controller.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.controller.containerPorts.controller }}
            initialDelaySeconds: {{ .Values.controller.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.controller.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controller.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.controller.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.controller.containerPorts.controller }}
            initialDelaySeconds: {{ .Values.controller.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.controller.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.controller.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.controller.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: {{ .Values.controller.containerPorts.controller }}
            initialDelaySeconds: {{ .Values.controller.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.readinessProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            # Mounting into a path that will be read by Argo CD.
            # This secret will be autogenerated by Argo CD repo server unless it already exists. Users can create its own certificate to override it.
            # Ref: https://argoproj.github.io/argo-cd/operator-manual/tls/#inbound-tls-certificates-used-by-argocd-repo-sever
            - mountPath: /app/config/server/tls
              name: argocd-repo-server-tls
          {{- if .Values.controller.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.controller.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.controller.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: argocd-repo-server-tls
          secret:
            items:
              - key: tls.crt
                path: tls.crt
              - key: tls.key
                path: tls.key
              - key: ca.crt
                path: ca.crt
            optional: true
            secretName: argocd-repo-server-tls
        {{- if .Values.controller.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.controller.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
