{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- 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" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | 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 }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
    spec:
      {{- include "discourse.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- 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 "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "customLabels" $podLabels "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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | 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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: discourse-data
              mountPath: /bitnami/discourse
        {{- end }}
        {{- if .Values.discourse.plugins }}
        - name: install-plugins
          image: {{ include "discourse.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.discourse.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.discourse.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -c
            - |
              pushd "/opt/bitnami/discourse" >/dev/null || exit 1
              {{- range $plugin := .Values.discourse.plugins }}
              RAILS_ENV=production bundle exec rake plugin:install repo={{ $plugin }}
              {{- end }}
              {{- if .Values.discourse.persistPlugins }}
              RAILS_ENV=production bundle exec rake plugin:update_all
              {{- end }}
              {{- if .Values.discourse.compatiblePlugins }}
              RAILS_ENV=production LOAD_PLUGINS=0 bundle exec rake plugin:pull_compatible_all
              {{- end }}
              popd >/dev/null || exit 1
              cp -nr --preserve=mode /opt/bitnami/discourse/plugins/* /plugins
          {{- if .Values.discourse.extraEnvVars }}
          env:
            {{- include "common.tplvalues.render" (dict "value" .Values.discourse.extraEnvVars "context" $) | nindent 12 }}
          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 }}
          {{- end }}
          {{- if .Values.discourse.resources }}
          resources: {{- toYaml .Values.discourse.resources | nindent 12 }}
          {{- else if ne .Values.discourse.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.discourse.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /plugins
              subPath: app-plugins-dir
        {{- end }}
      containers:
        - name: discourse
          image: {{ include "discourse.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.discourse.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.discourse.containerSecurityContext "context" $) | 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 }}
          {{- else if and .Values.discourse.plugins .Values.discourse.persistPlugins }}
          command:
            - /bin/bash
          {{- 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 }}
          {{- else if and .Values.discourse.plugins .Values.discourse.persistPlugins }}
          args:
            - -ec
            - |
              cp -nr --preserve=mode /plugins/* /opt/bitnami/discourse/plugins
              /opt/bitnami/scripts/discourse/entrypoint.sh /opt/bitnami/scripts/discourse/run.sh
          {{- 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 not .Values.discourse.persistPlugins }}
            - name: DISCOURSE_DATA_TO_PERSIST
              value: "public/backups public/uploads"
            {{- 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 .Values.discourse.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.discourse.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- 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:
            tcpSocket:
              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 }}
          {{- else if ne .Values.discourse.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.discourse.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: discourse-data
              mountPath: /bitnami/discourse
              subPath: discourse
            {{- if .Values.discourse.plugins }}
            - name: empty-dir
              subPath: app-plugins-dir
              mountPath: {{ ternary "/plugins" "/opt/bitnami/discourse/plugins" .Values.discourse.persistPlugins }}
            {{- end }}
            {{- 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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.sidekiq.containerSecurityContext "context" $) | 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 not .Values.discourse.persistPlugins }}
            - name: DISCOURSE_DATA_TO_PERSIST
              value: "public/backups public/uploads"
            {{- 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 }}
          {{- else if ne .Values.sidekiq.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.sidekiq.resourcesPreset) | 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 and .Values.discourse.plugins (not .Values.discourse.persistPlugins) }}
            - name: empty-dir
              mountPath: /opt/bitnami/discourse/plugins
              subPath: app-plugins-dir
            {{- end }}
            {{- 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:
        {{- if .Values.discourse.plugins }}
        - name: empty-dir
          emptyDir: {}
        {{- end }}
        - 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 }}
