{{- if and (include "discourse.host" .) (or .Values.postgresql.enabled .Values.externalDatabase.host) -}}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- 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.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmaps.yaml") . | sha256sum }}
        checksum/secrets-discourse: {{ include (print $.Template.BasePath "/secrets-discourse.yaml") . | sha256sum }}
        checksum/secrets-database: {{ include (print $.Template.BasePath "/secrets-database.yaml") . | sha256sum }}
        checksum/secrets-redis: {{ include (print $.Template.BasePath "/secrets-redis.yaml") . | sha256sum }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "discourse.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "discourse.serviceAccountName" . }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "discourse.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - sh
            - -c
            - |
              mkdir -p /bitnami/discourse
              find /bitnami/discourse -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs chown -R {{ .Values.discourse.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
          {{- if .Values.volumePermissions.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: discourse-data
              mountPath: /bitnami/discourse
        {{- end }}
      containers:
        - name: discourse
          image: {{ include "discourse.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.discourse.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.discourse.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.discourse.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.discourse.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.discourse.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.discourse.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: DISCOURSE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.secretName" . }}
                  key: discourse-password
            - name: DISCOURSE_PORT_NUMBER
              value: {{ .Values.discourse.containerPorts.http | quote }}
            - name: DISCOURSE_EXTERNAL_HTTP_PORT_NUMBER
              value: {{ .Values.service.ports.http | quote }}
            - name: DISCOURSE_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.database.secretName" . }}
                  key: {{ include "discourse.database.secretPasswordKey" . }}
            - name: POSTGRESQL_CLIENT_CREATE_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.database.secretName" . }}
                  key: {{ include "discourse.database.secretPasswordKey" . }}
            {{- if or .Values.postgresql.enabled .Values.externalDatabase.create }}
            - name: POSTGRESQL_CLIENT_POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.database.secretName" . }}
                  key: {{ include "discourse.database.secretPostgresPasswordKey" . }}
            {{- end }}
            {{- if (include "discourse.redis.auth.enabled" .) }}
            - name: DISCOURSE_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.redis.secretName" . }}
                  key: {{ include "discourse.redis.secretPasswordKey" . }}
            {{- end }}
            {{- if (include "smtp.password.enabled" .) }}
            - name: DISCOURSE_SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "smtp.secretName" . }}
                  key: smtp-password
            {{- end }}
            {{- if .Values.discourse.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.discourse.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "common.names.fullname" . }}
            {{- if .Values.discourse.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.discourse.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.discourse.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.discourse.extraEnvVarsSecret }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.discourse.containerPorts.http }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.discourse.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.discourse.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.discourse.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /srv/status
              port: http
            initialDelaySeconds: {{ .Values.discourse.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.discourse.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.discourse.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.discourse.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.discourse.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.discourse.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.discourse.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.discourse.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /srv/status
              port: http
            initialDelaySeconds: {{ .Values.discourse.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.discourse.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.discourse.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.discourse.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.discourse.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.discourse.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.discourse.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.discourse.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.discourse.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.discourse.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.discourse.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.discourse.resources }}
          resources: {{- toYaml .Values.discourse.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: discourse-data
              mountPath: /bitnami/discourse
              subPath: discourse
            {{- if .Values.discourse.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.discourse.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        - name: sidekiq
          image: {{ include "discourse.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.sidekiq.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.sidekiq.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.sidekiq.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.sidekiq.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: DISCOURSE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.secretName" . }}
                  key: discourse-password
            - name: DISCOURSE_POSTGRESQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.database.secretName" . }}
                  key: {{ include "discourse.database.secretPasswordKey" . }}
            {{- if (include "discourse.redis.auth.enabled" .) }}
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "discourse.redis.secretName" . }}
                  key: {{ include "discourse.redis.secretPasswordKey" . }}
            {{- end }}
            {{- if (include "smtp.password.enabled" .) }}
            - name: DISCOURSE_SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "smtp.secretName" . }}
                  key: smtp-password
            {{- end }}
            {{- if .Values.sidekiq.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "common.names.fullname" . }}
            {{- if .Values.sidekiq.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.sidekiq.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.sidekiq.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.sidekiq.extraEnvVarsSecret }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.sidekiq.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.sidekiq.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command: ["/bin/sh", "-c", "pgrep -f ^sidekiq"]
            initialDelaySeconds: {{ .Values.sidekiq.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.sidekiq.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.sidekiq.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.sidekiq.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.sidekiq.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.sidekiq.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.sidekiq.readinessProbe.enabled }}
          readinessProbe:
            exec:
              command: ["/bin/sh", "-c", "pgrep -f ^sidekiq"]
            initialDelaySeconds: {{ .Values.sidekiq.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.sidekiq.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.sidekiq.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.sidekiq.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.sidekiq.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.sidekiq.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.sidekiq.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.sidekiq.startupProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command: ["/bin/sh", "-c", "pgrep -f ^sidekiq"]
          {{- end }}
          {{- end }}
          {{- if .Values.sidekiq.resources }}
          resources: {{- toYaml .Values.sidekiq.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.sidekiq.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: discourse-data
              mountPath: /bitnami/discourse
              subPath: discourse
            {{- if .Values.sidekiq.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.sidekiq.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: discourse-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ default (include "common.names.fullname" .) (tpl .Values.persistence.existingClaim $)}}
          {{- else }}
          emptyDir: {}
          {{ end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
