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

{{- if .Values.applicationSet.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argocd.applicationSet" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: applicationSet
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.applicationSet.replicaCount }}
  {{- if .Values.applicationSet.updateStrategy }}
  strategy: {{- toYaml .Values.applicationSet.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.applicationSet.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: applicationSet
  template:
    metadata:
      {{- if .Values.applicationSet.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: applicationSet
    spec:
      serviceAccountName: {{ include "argocd.applicationSet.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.applicationSet.serviceAccount.automountServiceAccountToken }}
      {{- include "argocd.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.applicationSet.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.applicationSet.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.applicationSet.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.applicationSet.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.applicationSet.podAffinityPreset "component" "applicationSet" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.applicationSet.podAntiAffinityPreset "component" "applicationSet" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.applicationSet.nodeAffinityPreset.type "key" .Values.applicationSet.nodeAffinityPreset.key "values" .Values.applicationSet.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.applicationSet.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.applicationSet.priorityClassName }}
      priorityClassName: {{ .Values.applicationSet.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.applicationSet.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.applicationSet.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: applicationset-controller
          image: {{ include "argocd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.applicationSet.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.applicationSet.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - argocd-applicationset-controller
            - --logformat
            - {{ .Values.applicationSet.logFormat | quote }}
            - --loglevel
            - {{ .Values.applicationSet.logLevel | quote }}
            - --metrics-addr
            - ":{{ .Values.applicationSet.containerPorts.metrics }}"
            - --probe-addr
            - ":{{ .Values.applicationSet.containerPorts.probe }}"
            - --argocd-repo-server
            - {{ include "argocd.repo-server" . }}:{{ coalesce .Values.repoServer.service.port .Values.repoServer.service.ports.repoServer }}
            - --policy
            - {{ .Values.applicationSet.defaultArgs.policy | quote }}
            {{- if or (gt ( .Values.applicationSet.replicaCount | int64) 1) .Values.applicationSet.defaultArgs.enableLeaderElection }}
            - --enable-leader-election=true
            {{- end }}
            {{- if .Values.applicationSet.defaultArgs.debug }}
            - --debug
            {{- end }}
            {{- if .Values.applicationSet.defaultArgs.dryRun }}
            - --dry-run
            {{- end }}
          {{- if .Values.applicationSet.extraArgs }}
          {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraArgs "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          env:
            {{- if .Values.applicationSet.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.applicationSet.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.applicationSet.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.applicationSet.containerPorts.metrics }}
              protocol: TCP
            - name: probe
              containerPort: {{ .Values.applicationSet.containerPorts.probe }}
              protocol: TCP
            - name: webhook
              containerPort: 7000
              protocol: TCP
          {{- if .Values.applicationSet.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.applicationSet.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - pgrep
                - -f
                - argocd-applicationset-controller
            initialDelaySeconds: {{ .Values.applicationSet.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.applicationSet.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.applicationSet.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.applicationSet.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.applicationSet.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.applicationSet.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.applicationSet.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: probe
            initialDelaySeconds: {{ .Values.applicationSet.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.applicationSet.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.applicationSet.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.applicationSet.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.applicationSet.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.applicationSet.resources }}
          resources: {{- toYaml .Values.applicationSet.resources | nindent 12 }}
          {{- else if ne .Values.applicationSet.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.applicationSet.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.applicationSet.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.applicationSet.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /app/config/ssh
              name: ssh-known-hosts
            {{- if .Values.config.tlsCerts }}
            - mountPath: /app/config/tls
              name: tls-certs
            {{- end }}
            {{- if .Values.config.gpgKeys }}
            - mountPath: /app/config/gpg/source
              name: gpg-keys
            {{- end }}
            {{- if .Values.config.rbac }}
            - mountPath: /app/config/rbac
              name: rbac
            {{- end }}
            - name: empty-dir
              mountPath: /app/config/gpg/keys
              subPath: app-keys-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.applicationSet.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
      volumes:
        - name: ssh-known-hosts
          configMap:
            name: argocd-ssh-known-hosts-cm
        {{- if .Values.config.tlsCerts }}
        - name: tls-certs
          configMap:
            name: argocd-tls-certs-cm
        {{- end }}
        {{- if .Values.config.gpgKeys }}
        - name: gpg-keys
          configMap:
            name: argocd-gpg-keys-cm
        {{- end }}
        {{- if .Values.config.rbac }}
        - name: rbac
          configMap:
            name: argocd-rbac-cm
        {{- end }}
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.applicationSet.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
