apiVersion: {{ template "wordpress.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "wordpress.fullname" . }}
  labels: {{- include "wordpress.labels" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "wordpress.tplValue" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "wordpress.tplValue" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  selector:
    matchLabels: {{- include "wordpress.matchLabels" . | nindent 6 }}
  {{- if .Values.updateStrategy }}
  strategy: {{ toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels: {{- include "wordpress.labels" . | nindent 8 }}
      {{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "wordpress.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.metrics.podAnnotations }}
        {{- include "wordpress.tplValue" (dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
{{- include "wordpress.imagePullSecrets" . | indent 6 }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      hostAliases:
        - ip: "127.0.0.1"
          hostnames:
            - "status.localhost"
      {{- if .Values.affinity }}
      affinity: {{- include "wordpress.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "wordpress.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "wordpress.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        runAsUser: {{ .Values.securityContext.runAsUser }}
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      {{- if .Values.initContainers }}
      initContainers: {{- include "wordpress.tplValue" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: wordpress
          image: {{ template "wordpress.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.command }}
          command: {{- include "wordpress.tplValue" ( dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.args }}
          args: {{- include "wordpress.tplValue" ( dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            {{- if .Values.image.debug }}
            - name: NAMI_DEBUG
              value: "--log-level trace"
            {{- end }}
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ ternary "yes" "no" .Values.allowEmptyPassword | quote }}
            - name: MARIADB_HOST
              value: {{ include "wordpress.databaseHost" . | quote }}
            - name: MARIADB_PORT_NUMBER
              value: {{ include "wordpress.databasePort" . | quote }}
            - name: WORDPRESS_DATABASE_NAME
              value: {{ include "wordpress.databaseName" . | quote }}
            - name: WORDPRESS_DATABASE_USER
              value: {{ include "wordpress.databaseUser" . | quote }}
            - name: WORDPRESS_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.databaseSecretName" . }}
                  key: mariadb-password
            - name: WORDPRESS_USERNAME
              value: {{ .Values.wordpressUsername | quote }}
            - name: WORDPRESS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.secretName" . }}
                  key: wordpress-password
            - name: WORDPRESS_EMAIL
              value: {{ .Values.wordpressEmail | quote }}
            - name: WORDPRESS_FIRST_NAME
              value: {{ .Values.wordpressFirstName | quote }}
            - name: WORDPRESS_LAST_NAME
              value: {{ .Values.wordpressLastName | quote }}
            - name: WORDPRESS_HTACCESS_OVERRIDE_NONE
              value: {{ ternary "yes" "no" .Values.allowOverrideNone | quote }}
            - name: WORDPRESS_HTACCESS_PERSISTENCE_ENABLED
              value: {{ ternary "yes" "no" .Values.htaccessPersistenceEnabled | quote }}
            - name: WORDPRESS_BLOG_NAME
              value: {{ .Values.wordpressBlogName | quote }}
            - name: WORDPRESS_SKIP_INSTALL
              value: {{ ternary "yes" "no" .Values.wordpressSkipInstall | quote }}
            - name: WORDPRESS_TABLE_PREFIX
              value: {{ .Values.wordpressTablePrefix | quote }}
            - name: WORDPRESS_SCHEME
              value: {{ .Values.wordpressScheme | quote }}
            - name: WORDPRESS_EXTRA_WP_CONFIG_CONTENT
              value: {{ .Values.wordpressExtraConfigContent | 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 (or .Values.smtpPassword .Values.smtpExistingSecret) }}
            - name: SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.smtpSecretName" . }}
                  key: smtp-password
            {{- end }}
            {{- if .Values.smtpUsername }}
            - name: SMTP_USERNAME
              value: {{ .Values.smtpUsername | quote }}
            {{- end }}
            {{- if .Values.smtpProtocol }}
            - name: SMTP_PROTOCOL
              value: {{ .Values.smtpProtocol | quote }}
            {{- end }}
            {{- if .Values.extraEnv }}
            {{- include "wordpress.tplValue" (dict "value" .Values.extraEnv "context" $) | nindent 12 }}
            {{- end }}
          ports:
            - name: http
              containerPort: 8080
            - name: https
              containerPort: 8443
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
            httpGet:
              path: /wp-admin/install.php
              port: {{ ternary "https" "http" .Values.healthcheckHttps }}
              {{- if .Values.healthcheckHttps }}
              scheme: HTTPS
              {{- end }}
              {{- if .Values.livenessProbeHeaders }}
              httpHeaders: {{- toYaml .Values.livenessProbeHeaders | nindent 16 }}
              {{- end }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "wordpress.tplValue" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
            httpGet:
              path: /wp-login.php
              port: {{ ternary "https" "http" .Values.healthcheckHttps }}
              {{- if .Values.healthcheckHttps }}
              scheme: HTTPS
              {{- end }}
              {{- if .Values.readinessProbeHeaders }}
              httpHeaders: {{- toYaml .Values.readinessProbeHeaders | nindent 16 }}
              {{- end }}
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "wordpress.tplValue" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /bitnami/wordpress
              name: wordpress-data
              subPath: wordpress
            {{- if and .Values.allowOverrideNone .Values.customHTAccessCM }}
            - mountPath: /htaccess
              name: custom-htaccess
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "wordpress.tplValue" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ template "wordpress.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          command:
            - /bin/apache_exporter
            - --scrape_uri
            - http://status.localhost:8080/server-status/?auto
          ports:
            - name: metrics
              containerPort: 9117
          livenessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: 15
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: 5
            timeoutSeconds: 1
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "wordpress.tplValue" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if and .Values.allowOverrideNone .Values.customHTAccessCM }}
        - name: custom-htaccess
          configMap:
            name: {{ template "wordpress.customHTAccessCM" . }}
            items:
              - key: wordpress-htaccess.conf
                path: wordpress-htaccess.conf
        {{- end }}
        - name: wordpress-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "wordpress.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{ end }}
        {{- if .Values.extraVolumes }}
        {{- include "wordpress.tplValue" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
