{{- range $workerName, $workerValues := .Values.workers }}
{{- with $ -}}
---
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}-worker-{{ $workerName }}
  labels:
    {{- include "common.labels.standard" . | nindent 4 }}
    openproject/process: worker-{{ $workerName }}
spec:
  replicas: {{( kindIs "invalid" $workerValues.replicaCount) | ternary .Values.backgroundReplicaCount $workerValues.replicaCount }}
  strategy:
    {{ coalesce $workerValues.strategy .Values.strategy | toYaml | nindent 4 }}
  selector:
    matchLabels:
      {{- include "common.labels.matchLabels" . | nindent 6 }}
      openproject/process: worker-{{ $workerName }}
  template:
    metadata:
      annotations:
        {{- range $key, $val := .Values.podAnnotations }}
        {{ $key }}: {{ $val | quote }}
        {{- end }}
        {{- include "openproject.envChecksums" . | nindent 8 }}
      labels:
        {{- include "common.labels.standard" . | nindent 8 }}
        openproject/process: worker-{{ $workerName }}
    spec:
      {{- include "openproject.imagePullSecrets" . | indent 6 }}
      {{- with .Values.affinity }}
      affinity:
        {{ toYaml . | nindent 8 | trim }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{ toYaml . | nindent 8 | trim }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{ toYaml . | nindent 8 | trim }}
      {{- end }}
      {{- include "openproject.podSecurityContext" . | indent 6 }}
      serviceAccountName: {{ include "common.names.fullname" . }}
      volumes:
        {{- include "openproject.tmpVolumeSpec" . | indent 8 }}                     
        {{- if .Values.egress.tls.rootCA.fileName }}
        - name: ca-pemstore
          configMap:
            name: "{{- .Values.egress.tls.rootCA.configMap }}"
        {{- end }}
        {{- if .Values.persistence.enabled }}
        - name: "data"
          persistentVolumeClaim:
            claimName: {{ include "common.names.fullname" . }}
        {{- end }}
      initContainers:
        - name: wait-for-db
          {{- include "openproject.containerSecurityContext" . | indent 10 }}
          image: {{ include "openproject.image" . }}
          imagePullPolicy: {{ .Values.image.imagePullPolicy }}
          envFrom:
            {{- include "openproject.envFrom" . | nindent 12 }}
          env:
            {{- include "openproject.env" . | nindent 12 }}
          command:
            - bash
            - /app/docker/prod/wait-for-db
      containers:
        - name: "openproject"
          {{- include "openproject.containerSecurityContext" . | indent 10 }}
          image: {{ include "openproject.image" . }}
          imagePullPolicy: {{ .Values.image.imagePullPolicy }}
          envFrom:
            {{- include "openproject.envFrom" . | nindent 12 }}
          command:
            - bash
            - /app/docker/prod/worker
          env:
            {{- include "openproject.env" . | nindent 12 }}
            - name: "QUEUE"
              value: "{{ $workerValues.queues }}"
          volumeMounts:
            {{- include "openproject.tmpVolumeMounts" . | indent 12 }}
            {{- if .Values.persistence.enabled }}
            - name: "data"
              mountPath: "/var/openproject/assets"
            {{- end }}
            {{- if .Values.egress.tls.rootCA.fileName }}
            - name: ca-pemstore
              mountPath: /etc/ssl/certs/custom-ca.pem
              subPath: {{ .Values.egress.tls.rootCA.fileName }}
              readOnly: false
            {{- end }}
          resources:
            {{- coalesce $workerValues.resources .Values.resources | toYaml | nindent 12 }}
{{- end }}
{{ end }}