{{- if .Values.applicationSet.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argocd.applicationSet" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: applicationSet
    {{- 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.applicationSet.replicaCount }}
  {{- if .Values.applicationSet.updateStrategy }}
  strategy: {{- toYaml .Values.applicationSet.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | 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" . | nindent 8 }}
        app.kubernetes.io/component: applicationSet
        {{- if .Values.applicationSet.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "argocd.applicationSet.serviceAccountName" . }}
      {{- 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" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.applicationSet.podAntiAffinityPreset "component" "applicationSet" "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: {{- omit .Values.applicationSet.podSecurityContext "enabled" | toYaml | 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 }}"
            {{- if or (gt ( .Values.applicationSet.replicaCount | int64) 1) .Values.applicationSet.defaultArgs.enableLeaderElection }}
            - --enable-leader-election=true
            {{- end }}
            - --argocd-repo-server
            - {{ include "argocd.repo-server" . }}:{{ .Values.repoServer.service.port }}
            - --policy
            - {{ .Values.applicationSet.defaultArgs.policy | quote }}
            - --debug
            - {{ .Values.applicationSet.defaultArgs.debug | quote }}
            - --dry-run
            - {{ .Values.applicationSet.defaultArgs.dryRun | quote }}
          {{- 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:
            tcpSocket:
              port: probe
            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 }}
          {{- end }}
          {{- if .Values.applicationSet.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.applicationSet.containerSecurityContext "enabled" | toYaml | 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 }}
            - mountPath: /app/config/gpg/keys
              name: gpg-keyring
            - mountPath: /tmp
              name: tmp
          {{- 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 }}
        - name: gpg-keyring
          emptyDir: {}
        - name: tmp
          emptyDir: {}
        {{- if .Values.applicationSet.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.applicationSet.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
