{{- if .Values.server.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argo-workflows.server.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: argo-workflows
    app.kubernetes.io/component: server
    {{- 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.server.replicaCount }}
  {{- if .Values.server.updateStrategy }}
  strategy: {{- toYaml .Values.server.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: server
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- if .Values.server.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.server.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: server
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
        {{- end }}
        {{- if .Values.server.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.server.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.server.schedulerName }}
      schedulerName: {{ .Values.server.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ include "argo-workflows.server.serviceAccountName" . }}
      {{- include "argo-workflows.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.server.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.server.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.server.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.server.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.server.podAffinityPreset "component" "server" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.server.podAntiAffinityPreset "component" "server" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.server.nodeAffinityPreset.type "key" .Values.server.nodeAffinityPreset.key "values" .Values.server.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.server.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.server.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.server.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.server.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.server.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.server.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.server.priorityClassName }}
      priorityClassName: {{ .Values.server.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.server.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.server.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.server.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.server.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: argo-server
          image: {{ include "argo-workflows.server.image" . }}
          imagePullPolicy: {{ .Values.server.image.pullPolicy }}
          {{- if .Values.server.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.server.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.server.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.server.command "context" $) | nindent 12 }}
          {{- end }}
          args:
            {{- if .Values.server.args }}
            {{- include "common.tplvalues.render" (dict "value" .Values.server.args "context" $) | nindent 12 }}
            {{- else }}
            - server
            - --configmap={{ include "argo-workflows.controller.configMapName" . }}
            - --secure={{ .Values.server.secure }}
            {{- if .Values.rbac.singleNamespace }}
            - --namespaced
            {{- end }}
            {{- if .Values.server.auth.enabled }}
            - --auth-mode
            - {{ .Values.server.auth.mode }}
            {{- end }}
            - --port
            - {{ .Values.server.containerPorts.web | quote }}
            {{- if .Values.server.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.server.extraArgs "context" $) | nindent 12 }}
            {{- end }}
            {{- end }}
          ports:
            - name: web
              containerPort: {{ .Values.server.containerPorts.web }}
          env:
            - name: IN_CLUSTER
              value: "true"
            - name: ARGO_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: BASE_HREF
              value: {{ .Values.server.baseHref | quote }}
            {{- if .Values.server.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.server.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.server.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.server.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.server.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.server.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.server.resources }}
          resources: {{- toYaml .Values.server.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.server.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: {{ .Values.server.containerPorts.web }}
              {{- if .Values.server.secure }}
              scheme: HTTPS
              {{- end }}
            initialDelaySeconds: {{ .Values.server.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.server.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.server.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.server.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.server.livenessProbe.failureThreshold }}
          {{- else if .Values.server.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.server.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.server.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: {{ .Values.server.containerPorts.web }}
              {{- if .Values.server.secure }}
              scheme: HTTPS
              {{- end }}
            initialDelaySeconds: {{ .Values.server.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.server.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.server.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.server.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.server.readinessProbe.failureThreshold }}
          {{- else if .Values.server.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.server.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.server.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: {{ .Values.server.startupProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.server.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.server.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.server.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.server.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.server.startupProbe.failureThreshold }}
          {{- else if .Values.server.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.server.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.server.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.server.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
          {{- if .Values.server.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.server.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.server.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.server.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
        {{- if .Values.server.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.server.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
