{{- if include "ghost.host" . -}}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "ghost.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  replicas: 1
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
      {{- if .Values.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.podAnnotations "context" $ ) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "ghost.imagePullSecrets" . | indent 6 }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      initContainers:
      {{- if not .Values.securityContext.enabled }}
        - name: volume-permissions
          image: {{ include "ghost.volumePermissions.image" . }}
          imagePullPolicy: "{{ .Values.volumePermissions.image.pullPolicy }}"
          command: ['sh', '-c', 'chmod -R g+rwX {{ .Values.persistence.path }}']
          volumeMounts:
            - mountPath: {{ .Values.persistence.path }}
              name: ghost-data
      {{- end }}
      {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.initContainers "context" $ ) | indent 8 }}
      {{- end }}
      containers:
        - name: ghost
          image: {{ include "ghost.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ ternary "yes" "no" .Values.allowEmptyPassword | quote }}
            {{- $host := .Values.externalDatabase.host -}}
            {{- $port := .Values.externalDatabase.port -}}
            {{- $database := .Values.externalDatabase.database -}}
            {{- $user := .Values.externalDatabase.user -}}

            {{- if .Values.mariadb.enabled -}}
              {{- $host = (include "ghost.mariadb.fullname" .) -}}
              {{- $port = .Values.mariadb.service.port -}}
              {{- $database = .Values.mariadb.db.name -}}
              {{- $user = .Values.mariadb.db.user -}}
            {{- end }}
            - name: MARIADB_HOST
              value: {{ $host | quote }}
            - name: MARIADB_PORT_NUMBER
              value: {{ $port | quote }}
            - name: GHOST_DATABASE_NAME
              value: {{ $database | quote }}
            - name: GHOST_DATABASE_USER
              value: {{ $user | quote }}
            - name: GHOST_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ ternary ( include "ghost.mariadb.fullname" . ) (include "ghost.externalDb.secret.name" .) .Values.mariadb.enabled }}
                  key: mariadb-password
            - name: GHOST_HOST
              value: {{ include "ghost.endpoint" . | quote }}
            - name: GHOST_PROTOCOL
              value: {{ .Values.ghostProtocol | quote }}
            - name: GHOST_PORT_NUMBER
              value: {{ default .Values.service.port .Values.ghostPort | quote }}
            - name: GHOST_USERNAME
              value: {{ .Values.ghostUsername | quote }}
            - name: GHOST_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.secrets.name" (dict "existingSecret" .Values.existingSecret "context" $) }}
                  key: {{ include "common.secrets.key" (dict "existingSecret" .Values.existingSecret "key" "ghost-password") }}
            - name: GHOST_EMAIL
              value: {{ .Values.ghostEmail | quote }}
            - name: BLOG_TITLE
              value: {{ .Values.ghostBlogTitle | 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: {{ include "common.secrets.name" (dict "existingSecret" .Values.existingSecret "context" $) }}
                  key: {{ include "common.secrets.key" (dict "existingSecret" .Values.existingSecret "key" "smtp-password") }}
            {{- end }}
            {{- if .Values.smtpFromAddress }}
            - name: SMTP_FROM_ADDRESS
              value: {{ .Values.smtpFromAddress | quote }}
            {{- end }}
            {{- if .Values.smtpService }}
            - name: SMTP_SERVICE
              value: {{ .Values.smtpService | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if or .Values.extraEnvVarsConfigMap .Values.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.extraEnvVarsConfigMap }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.extraEnvVarsConfigMap "context" $ ) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: 2368
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ .Values.ghostPath }}
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "ghost.host" . | quote }}
                {{- if eq .Values.ghostProtocol "https" }}
                - name: X-Forwarded-Proto
                  value: https
                {{- end }}
            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: {{ .Values.ghostPath }}
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "ghost.host" . | quote }}
                {{- if eq .Values.ghostProtocol "https" }}
                - name: X-Forwarded-Proto
                  value: https
                {{- end }}
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: ghost-data
              mountPath: /bitnami/ghost
          {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumeMounts "context" $ ) | nindent 10 }}
          {{- end }}
      {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $ ) | nindent 8 }}
      {{- end }}
      volumes:
        - name: ghost-data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ default (include "ghost.fullname" .) .Values.persistence.existingClaim }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- if .Values.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
{{- end -}}
