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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argocd.repo-server" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: repo-server
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.repoServer.replicaCount }}
  {{- if .Values.repoServer.updateStrategy }}
  strategy: {{- toYaml .Values.repoServer.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.repoServer.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: repo-server
  template:
    metadata:
      {{- if .Values.repoServer.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: repo-server
    spec:
      serviceAccountName: {{ include "argocd.repo-server.serviceAccountName" . }}
      {{- include "argocd.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.repoServer.automountServiceAccountToken }}
      {{- if .Values.repoServer.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.repoServer.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.repoServer.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.repoServer.podAffinityPreset "component" "repo-server" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.repoServer.podAntiAffinityPreset "component" "repo-server" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.repoServer.nodeAffinityPreset.type "key" .Values.repoServer.nodeAffinityPreset.key "values" .Values.repoServer.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.repoServer.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.repoServer.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.repoServer.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.repoServer.schedulerName }}
      schedulerName: {{ .Values.repoServer.schedulerName }}
      {{- end }}
      {{- if .Values.repoServer.shareProcessNamespace }}
      shareProcessNamespace: {{ .Values.repoServer.shareProcessNamespace }}
      {{- end }}
      {{- if .Values.repoServer.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.controller.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.repoServer.priorityClassName }}
      priorityClassName: {{ .Values.repoServer.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.repoServer.runtimeClassName }}
      runtimeClassName: {{ .Values.repoServer.runtimeClassName }}
      {{- end }}
      {{- if .Values.repoServer.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.repoServer.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.volumePermissions.enabled }}
        - name: volume-permissions
          image: {{ include "argocd.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.repoServer.containerSecurityContext.runAsUser }}:{{ .Values.repoServer.podSecurityContext.fsGroup }} /tmp
          securityContext:
            runAsUser: {{ .Values.volumePermissions.containerSecurityContext.runAsUser }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- end }}
        {{- if .Values.redisWait.enabled }}
        - name: wait-for-redis
          image: {{ include "argocd.redis.image" . }}
          imagePullPolicy: {{ .Values.redis.image.pullPolicy | quote }}
          {{- if .Values.redisWait.securityContext }}
          # Deprecated: use redisWait.containerSecurityContext
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.redisWait.securityContext "context" $) | nindent 12 }}
          {{- else if .Values.redisWait.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.redisWait.containerSecurityContext "context" $) | nindent 12 }}
          {{- 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.repoServer.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: argocd-repo-server
          image: {{ include "argocd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.repoServer.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.repoServer.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.repoServer.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.repoServer.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.repoServer.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - argocd-repo-server
            - --logformat
            - {{ .Values.repoServer.logFormat | quote }}
            - --loglevel
            - {{ .Values.repoServer.logLevel | quote }}
            - --redis
            - {{ include "argocd.redisHost" . }}:{{ include "argocd.redisPort" . }}
            - --port={{ .Values.repoServer.containerPorts.repoServer }}
            - --metrics-port={{ .Values.repoServer.containerPorts.metrics }}
            {{- if .Values.repoServer.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          ports:
            - name: repo-server
              containerPort: {{ .Values.repoServer.containerPorts.repoServer }}
              protocol: TCP
            - name: metrics
              containerPort: {{ .Values.repoServer.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" . }}
            {{- else if .Values.externalRedis.enabled }}
            - name: REDIS_PASSWORD
              {{- if not ( eq "" .Values.externalRedis.password ) }}
              value: {{ .Values.externalRedis.password }}
              {{- else }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.externalRedis.existingSecret }}
                  key: {{ .Values.externalRedis.existingSecretPasswordKey }}
              {{- end }}
            {{- end }}
            {{- if .Values.repoServer.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.repoServer.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.repoServer.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.repoServer.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.repoServer.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.repoServer.resources }}
          resources: {{- toYaml .Values.repoServer.resources | nindent 12 }}
          {{- else if ne .Values.repoServer.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.repoServer.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.repoServer.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.repoServer.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /healthz?full=true
              port: metrics
            initialDelaySeconds: {{ .Values.repoServer.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.repoServer.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.repoServer.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.repoServer.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.repoServer.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.repoServer.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.repoServer.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /healthz?full=true
              port: metrics
            initialDelaySeconds: {{ .Values.repoServer.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.repoServer.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.repoServer.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.repoServer.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.repoServer.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.repoServer.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.repoServer.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: metrics
            initialDelaySeconds: {{ .Values.repoServer.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.repoServer.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.repoServer.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.repoServer.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.repoServer.readinessProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            # Mounting into a path that will be read by Argo CD
            # Ref: https://argoproj.github.io/argo-cd/operator-manual/declarative-setup/#ssh-known-host-public-keys
            - name: ssh-known-hosts
              mountPath: /app/config/ssh
            {{- if .Values.config.tlsCerts }}
            # Mounting into a path that will be read by Argo CD
            # Ref: https://argoproj.github.io/argo-cd/operator-manual/declarative-setup/#repositories-using-self-signed-tls-certificates-or-are-signed-by-custom-ca
            - mountPath: /app/config/tls
              name: tls-certs
            {{- end }}
            # 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
            - name: empty-dir
              mountPath: /app/config/gpg/keys
              subPath: app-keys-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.repoServer.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.repoServer.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.repoServer.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: ssh-known-hosts
          configMap:
            name: argocd-ssh-known-hosts-cm
        {{- if .Values.config.tlsCerts }}
        - configMap:
            name: argocd-tls-certs-cm
          name: tls-certs
        {{- end }}
        - 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
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.repoServer.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.repoServer.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
