{{- if .Values.worker.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "papercrate.fullname" . }}-worker
  labels:
    {{- include "papercrate.labels" . | nindent 4 }}
    app.kubernetes.io/component: worker
spec:
  replicas: {{ .Values.worker.replicaCount }}
  selector:
    matchLabels:
      {{- include "papercrate.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: worker
  template:
    metadata:
      labels:
        {{- include "papercrate.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: worker
    spec:
      {{- if or .Values.global.imagePullSecrets .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- range .Values.global.imagePullSecrets }}
        - name: {{ . }}
        {{- end }}
        {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
        {{- end }}
      {{- end }}
      {{- $workerSecrets := dict "primary" "" -}}
      {{- if .Values.worker.env.existingSecret -}}
        {{- $_ := set $workerSecrets "primary" .Values.worker.env.existingSecret -}}
      {{- else if .Values.backend.env.existingSecret -}}
        {{- $_ := set $workerSecrets "primary" .Values.backend.env.existingSecret -}}
      {{- else -}}
        {{- $_ := set $workerSecrets "primary" (printf "%s-backend-env" (include "papercrate.fullname" .)) -}}
      {{- end }}
      containers:
        - name: worker
          image: {{ include "papercrate.backend.image" . }}:{{ include "papercrate.image.tag" (dict "image" .Values.backend.image "Chart" .Chart) }}
          imagePullPolicy: {{ .Values.backend.image.pullPolicy }}
          command: ["/usr/local/bin/papercrate-worker"]
          env:
            {{- with .Values.global.s3.endpoint }}
            - name: AWS_ENDPOINT_URL
              value: {{ . | quote }}
            {{- end }}
            {{- with .Values.global.s3.region }}
            - name: AWS_REGION
              value: {{ . | quote }}
            {{- end }}
            {{- with .Values.global.s3.bucket }}
            - name: S3_BUCKET
              value: {{ . | quote }}
            {{- end }}
            {{- range .Values.worker.env.extra }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}
          envFrom:
            - secretRef:
                name: {{ index $workerSecrets "primary" }}
            {{- with .Values.global.s3.credentialsSecret }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- range .Values.worker.envFrom }}
            - {{ toYaml . | nindent 12 | trim }}
            {{- end }}
          {{- with .Values.worker.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.worker.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.worker.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.worker.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
