{{- if .Values.mailReceiver.enabled }}
apiVersion: {{ include "common.capabilities.cronjob.apiVersion" . }}
kind: CronJob
metadata:
  name: {{ template "redmine.mailReceiver.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: mail-receiver
    {{- 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:
  schedule: {{ .Values.mailReceiver.schedule | quote }}
  suspend: {{ .Values.mailReceiver.suspend }}
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
          labels: {{- include "common.labels.standard" . | nindent 12 }}
            app.kubernetes.io/component: mail-receiver
            {{- if .Values.mailReceiver.podLabels }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.mailReceiver.podLabels "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if .Values.mailReceiver.podAnnotations }}
          annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.mailReceiver.podAnnotations "context" $ ) | nindent 12 }}
          {{- end }}
        spec:
          {{- include "redmine.imagePullSecrets" . | nindent 10 }}
          restartPolicy: Never
          {{- if .Values.mailReceiver.priorityClassName }}
          priorityClassName: {{ .Values.mailReceiver.priorityClassName | quote }}
          {{- end }}
          {{- if .Values.mailReceiver.podSecurityContext.enabled }}
          securityContext: {{- omit .Values.mailReceiver.podSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          initContainers:
            {{- if .Values.mailReceiver.initContainers }}
            {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.initContainers "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.mailReceiver.affinity }}
          affinity: {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.affinity "context" $) | nindent 12 }}
          {{- else }}
          affinity:
            podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.mailReceiver.podAffinityPreset "component" "mail-receiver" "context" $) | nindent 14 }}
            podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.mailReceiver.podAntiAffinityPreset "component" "mail-receiver" "context" $) | nindent 14 }}
            nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.mailReceiver.nodeAffinityPreset.type "key" .Values.mailReceiver.nodeAffinityPreset.key "values" .Values.mailReceiver.nodeAffinityPreset.values) | nindent 14 }}
          {{- end }}
          {{- if .Values.mailReceiver.nodeSelector }}
          nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.nodeSelector "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.mailReceiver.tolerations }}
          tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.tolerations "context" $) | nindent 12 }}
          {{- end }}
          containers:
            - name: {{ template "redmine.mailReceiver.fullname" . }}
              image: {{ template "redmine.image" . }}
              imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
              {{- if .Values.mailReceiver.containerSecurityContext.enabled }}
              securityContext: {{- omit .Values.mailReceiver.containerSecurityContext "enabled" | toYaml | nindent 16 }}
              {{- end }}
              {{- if .Values.diagnosticMode.enabled }}
              command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 16 }}
              {{- else if .Values.mailReceiver.command }}
              command: {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.command "context" $) | nindent 16 }}
              {{- else }}
              command:
                - bash
              {{- end }}
              {{- if .Values.diagnosticMode.enabled }}
              args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 16 }}
              {{- else if .Values.mailReceiver.args }}
              args: {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.args "context" $) | nindent 16 }}
              {{- else }}
              args:
                - -c
                - /cj/receive.sh
              {{- end }}
              env:
                - name: REDMINE_DATABASE_TYPE
                  value: {{ .Values.databaseType | quote }}
                {{- if eq .Values.databaseType "mariadb" }}
                - name: REDMINE_MAIL_RECEIVER_DB_MYSQL
                {{- else }}
                - name: REDMINE_MAIL_RECEIVER_DB_POSTGRES
                {{- end }}
                  value: {{ template "redmine.database.host" . }}
                - name: REDMINE_MAIL_RECEIVER_DB_DATABASE
                  value: {{ template "redmine.database.name" . }}
                - name: REDMINE_MAIL_RECEIVER_DB_USERNAME
                  value: {{ template "redmine.database.username" . }}
                - name: REDMINE_MAIL_RECEIVER_DB_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: {{ include "redmine.database.secretName" . }}
                      key: {{ include "redmine.database.secretKey" . }}
                - name: REDMINE_MAIL_RECEIVER_DB_PORT
                  value: {{ include "redmine.database.port" . | quote }}
                - name: REDMINE_MAIL_RECEIVER_HOST
                  value: {{ .Values.mailReceiver.host | quote }}
                - name: REDMINE_MAIL_RECEIVER_PORT
                  value: {{ .Values.mailReceiver.port | quote }}
                - name: REDMINE_MAIL_RECEIVER_USERNAME
                  value: {{ .Values.mailReceiver.username | quote }}
                - name: REDMINE_MAIL_RECEIVER_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: {{ template "redmine.secretName" . }}
                      key: mail-receiver-password
                - name: REDMINE_MAIL_RECEIVER_USE_SSL
                  value: {{ .Values.mailReceiver.ssl | quote }}
                - name: REDMINE_MAIL_RECEIVER_STARTTLS
                  value: {{ .Values.mailReceiver.startTLS | quote }}
                - name: REDMINE_MAIL_RECEIVER_IMAP_FOLDER
                  value: {{ .Values.mailReceiver.imapFolder | quote }}
                - name: REDMINE_MAIL_RECEIVER_IMAP_MOVE_ON_SUCCESS
                  value: {{ .Values.mailReceiver.moveOnSuccess | quote }}
                - name: REDMINE_MAIL_RECEIVER_IMAP_MOVE_ON_FAILURE
                  value: {{ .Values.mailReceiver.moveOnFailure | quote }}
                - name: REDMINE_MAIL_RECEIVER_UNKNOWN_USER_ACTION
                  value: {{ .Values.mailReceiver.unknownUserAction | quote }}
                - name: REDMINE_MAIL_RECEIVER_NO_PERMISSION_CHECK
                  value: {{ .Values.mailReceiver.noPermissionCheck | quote }}
                - name: REDMINE_MAIL_RECEIVER_NO_ACCOUNT_NOTICE
                  value: {{ .Values.mailReceiver.noAccountNotice | quote }}
                - name: REDMINE_MAIL_RECEIVER_DEFAULT_GROUP
                  value: {{ .Values.mailReceiver.defaultGroup | quote }}
                - name: REDMINE_MAIL_RECEIVER_PROJECT
                  value: {{ .Values.mailReceiver.project | quote }}
                - name: REDMINE_MAIL_RECEIVER_PROJECT_FROM_SUBADDRESS
                  value: {{ .Values.mailReceiver.projectFromSubaddress | quote }}
                - name: REDMINE_MAIL_RECEIVER_STATUS
                  value: {{ .Values.mailReceiver.status | quote }}
                - name: REDMINE_MAIL_RECEIVER_TRACKER
                  value: {{ .Values.mailReceiver.tracker | quote }}
                - name: REDMINE_MAIL_RECEIVER_CATEGORY
                  value: {{ .Values.mailReceiver.category | quote }}
                - name: REDMINE_MAIL_RECEIVER_PRIORITY
                  value: {{ .Values.mailReceiver.priority | quote }}
                - name: REDMINE_MAIL_RECEIVER_ASSIGNED_TO
                  value: {{ .Values.mailReceiver.assignedTo | quote }}
                - name: REDMINE_MAIL_RECEIVER_ALLOW_OVERRIDE
                  value: {{ .Values.mailReceiver.allowOverride | quote }}
                - name: REDMINE_MAIL_RECEIVER_SECRET_KEY_BASE
                  value: {{ randAlphaNum 64 | quote }}
                - name: REDMINE_MAIL_RECEIVER_PROTOCOL
                  value: {{ .Values.mailReceiver.mailProtocol | quote }}
                - name: REDMINE_MAIL_RECEIVER_POP3_DELETE_UNPROCESSED
                  value: {{ .Values.mailReceiver.moveOnFailure | quote }}
                {{- if .Values.mailReceiver.extraEnvVars }}
                {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.extraEnvVars "context" $) | nindent 16 }}
                {{- end }}
              envFrom:
                {{- if .Values.mailReceiver.extraEnvVarsCM }}
                - configMapRef:
                    name: {{ include "common.tplvalues.render" (dict "value" .Values.mailReceiver.extraEnvVarsCM "context" $) }}
                {{- end }}
                {{- if .Values.mailReceiver.extraEnvVarsSecret }}
                - secretRef:
                    name: {{ include "common.tplvalues.render" (dict "value" .Values.mailReceiver.extraEnvVarsSecret "context" $) }}
                {{- end }}
              {{- if .Values.mailReceiver.resources }}
              resources: {{- toYaml .Values.mailReceiver.resources | nindent 16 }}
              {{- end }}
              volumeMounts:
                - name: mail-receiver
                  mountPath: /cj
                {{- if .Values.mailReceiver.extraVolumeMounts }}
                {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.extraVolumeMounts "context" $) | nindent 16 }}
                {{- end }}
            {{- if .Values.mailReceiver.sidecars }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.mailReceiver.sidecars "context" $) | nindent 12 }}
            {{- end }}
          volumes:
            - name: mail-receiver
              configMap:
                name: {{ include "common.names.fullname" . }}
                defaultMode: 0744
            {{- if .Values.mailReceiver.extraVolumes }}
            {{- include "common.tplvalues.render" (dict "value" .Values.mailReceiver.extraVolumes "context" $) | nindent 12 }}
            {{- end }}
{{- end }}
