apiVersion: {{ template "redmine.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "redmine.fullname" . }}
  labels: {{- include "redmine.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{- include "redmine.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.replicas }}
  template:
    metadata:
      labels: {{- include "redmine.labels" . | nindent 8 }}
      {{- if .Values.podAnnotations }}
      annotations: {{- include "redmine.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
{{- include "redmine.imagePullSecrets" . | indent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "redmine.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "redmine.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "redmine.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "redmine.serviceAccountName" . }}
      {{- with .Values.securityContext }}
      securityContext: {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ template "redmine.fullname" . }}
          image: {{ template "redmine.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          env:
            {{- if .Values.extraVars }}
            {{- toYaml .Values.extraVars | nindent 12 }}
            {{- end }}
            {{- if eq .Values.databaseType "mariadb" }}
            - name: REDMINE_DB_MYSQL
            {{- if .Values.mariadb.enabled }}
              value: {{ template "redmine.mariadb.fullname" . }}
            - name: REDMINE_DB_NAME
              value: {{ .Values.mariadb.db.name | quote }}
            - name: REDMINE_DB_USERNAME
              value: {{ .Values.mariadb.db.user | quote }}
            - name: REDMINE_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "redmine.mariadb.fullname" . }}
                  key: mariadb-password
            {{- else }}
              value: {{ .Values.externalDatabase.host | quote }}
            {{- end }}
            {{- else if eq .Values.databaseType "postgresql" }}
            - name: REDMINE_DB_POSTGRES
            {{- if .Values.postgresql.enabled }}
              value: {{ template "redmine.postgresql.fullname" . }}
            - name: REDMINE_DB_NAME
              value: {{ .Values.postgresql.postgresqlDatabase | quote }}
            - name: REDMINE_DB_USERNAME
              value: {{ .Values.postgresql.postgresqlUsername | quote }}
            - name: REDMINE_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "redmine.postgresql.fullname" . }}
                  key: postgresql-password
            {{- else }}
              value: {{ .Values.externalDatabase.host | quote }}
            {{- end }}
            {{- end }}
            {{- if or (and (eq .Values.databaseType "mariadb") (not .Values.mariadb.enabled)) (and (eq .Values.databaseType "postgresql") (not .Values.postgresql.enabled)) }}
            - name: REDMINE_DB_NAME
              value: {{ .Values.externalDatabase.name | quote }}
            - name: REDMINE_DB_USERNAME
              value: {{ .Values.externalDatabase.user | quote }}
            - name: REDMINE_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "redmine.secretName" . }}
                  key: external-db-password
            - name: REDMINE_DB_PORT_NUMBER
              value: {{ .Values.externalDatabase.port | quote }}
            {{- end }}
            - name: REDMINE_USERNAME
              value: {{ default "" .Values.redmineUsername | quote }}
            - name: REDMINE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "redmine.secretName" . }}
                  key: redmine-password
            - name: REDMINE_EMAIL
              value: {{ .Values.redmineEmail | quote }}
            - name: REDMINE_LANG
              value: {{ .Values.redmineLanguage | quote }}
            {{- if .Values.smtpHost }}
            - name: SMTP_HOST
              value: {{ .Values.smtpHost | quote }}
            {{- end }}
            {{- if .Values.smtpPort }}
            - name: SMTP_PORT
              value: {{ .Values.smtpPort | quote }}
            {{- end }}
            {{- if .Values.smtpUser }}
            - name: SMTP_USER
              value: {{ .Values.smtpUser | quote }}
            {{- end }}
            {{- if .Values.smtpPassword }}
            - name: SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "redmine.secretName" . }}
                  key: smtp-password
            {{- end }}
            {{- if .Values.smtpTls }}
            - name: SMTP_TLS
              value: {{ .Values.smtpTls | quote }}
            {{- end }}
          ports:
            - name: http-redmine
              containerPort: 3000
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: http-redmine
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: http-redmine
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            - name: redmine-data
              mountPath: /bitnami/redmine
      volumes:
        - name: redmine-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "redmine.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
