{{- /*
Copyright VMware, Inc.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  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:
  {{- $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 }}
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
      {{- if or .Values.podAnnotations .Values.metrics.enabled (include "wordpress.createConfigSecret" .) }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.metrics.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if (include "wordpress.createConfigSecret" .) }}
        checksum/config-secret: {{ include (print $.Template.BasePath "/config-secret.yaml") . | sha256sum }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "wordpress.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      # yamllint disable rule:indentation
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      # yamllint enable rule:indentation
      {{- 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.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "wordpress.serviceAccountName" .}}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if or (and .Values.podSecurityContext.enabled .Values.volumePermissions.enabled .Values.persistence.enabled) (.Values.initContainers) }}
      initContainers:
        {{- if and .Values.podSecurityContext.enabled .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: "{{ include "wordpress.volumePermissions.image" . }}"
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/wordpress
              {{- if eq ( toString ( .Values.volumePermissions.containerSecurityContext.runAsUser )) "auto" }}
              find /bitnami/wordpress -mindepth 0 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R $(id -u):$(id -G | cut -d " " -f2)
              {{- else }}
              find /bitnami/wordpress -mindepth 0 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
              {{- end }}
          {{- if eq ( toString ( .Values.volumePermissions.containerSecurityContext.runAsUser )) "auto " }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "runAsUser" | toYaml | nindent 12 }}
          {{- else }}
          securityContext: {{- .Values.volumePermissions.containerSecurityContext | toYaml | 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:
            - mountPath: /bitnami/wordpress
              name: wordpress-data
              subPath: wordpress
        {{- end }}
        {{- if .Values.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: wordpress
          image: {{ include "wordpress.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.command }}
          command: {{- include "common.tplvalues.render" ( dict "value" .Values.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.args }}
          args: {{- include "common.tplvalues.render" ( dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ ternary "yes" "no" .Values.allowEmptyPassword | quote }}
            - name: WORDPRESS_SKIP_BOOTSTRAP
              value: {{ ternary "yes" "no" .Values.wordpressSkipInstall | quote }}
            {{- if or .Values.wordpressConfiguration .Values.existingWordPressConfigurationSecret }}
            # Override the default data to persist omiting wp-config.php from the list since
            # it is mounted as a read-only file from a Secret
            - name: WORDPRESS_DATA_TO_PERSIST
              value: "wp-content"
            {{- else }}
            - 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_ENABLE_HTACCESS_PERSISTENCE
              value: {{ ternary "yes" "no" .Values.htaccessPersistenceEnabled | quote }}
            - name: WORDPRESS_BLOG_NAME
              value: {{ .Values.wordpressBlogName | 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 }}
            - name: WORDPRESS_PLUGINS
              value: {{ join "," .Values.wordpressPlugins | quote }}
            - name: WORDPRESS_OVERRIDE_DATABASE_SETTINGS
              value: {{ ternary "yes" "no" .Values.overrideDatabaseSettings | quote }}
            {{- end }}
            {{- if .Values.multisite.enable }}
            - name: WORDPRESS_ENABLE_MULTISITE
              value: "yes"
            - name: WORDPRESS_MULTISITE_HOST
              value: {{ .Values.multisite.host | quote }}
            - name: WORDPRESS_MULTISITE_EXTERNAL_HTTP_PORT_NUMBER
              value: {{ .Values.service.ports.http | quote }}
            - name: WORDPRESS_MULTISITE_EXTERNAL_HTTPS_PORT_NUMBER
              value: {{ .Values.service.ports.https | quote }}
            - name: WORDPRESS_MULTISITE_NETWORK_TYPE
              value: {{ .Values.multisite.networkType | quote }}
            - name: WORDPRESS_MULTISITE_ENABLE_NIP_IO_REDIRECTION
              value: {{ ternary "yes" "no" .Values.multisite.enableNipIoRedirect | quote }}
            {{- end }}
            {{- 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.smtpProtocol }}
            - name: SMTP_PROTOCOL
              value: {{ .Values.smtpProtocol | quote }}
            {{- end }}
            - name: APACHE_HTTP_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: APACHE_HTTPS_PORT_NUMBER
              value: {{ .Values.containerPorts.https | quote }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
            - name: https
              containerPort: {{ .Values.containerPorts.https }}
            {{- if .Values.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /bitnami/wordpress
              name: wordpress-data
              subPath: wordpress
            {{- if or .Values.wordpressConfiguration .Values.existingWordPressConfigurationSecret }}
            - name: wordpress-config
              mountPath: /opt/bitnami/wordpress/wp-config.php
              subPath: wp-config.php
            {{- end }}
            {{- if or .Values.apacheConfiguration .Values.existingApacheConfigurationConfigMap }}
            - name: apache-config
              mountPath: /opt/bitnami/apache/conf/httpd.conf
              subPath: httpd.conf
            {{- end }}
            {{- if and (not .Values.allowOverrideNone) .Values.customHTAccessCM }}
            - mountPath: /opt/bitnami/apache/conf/vhosts/htaccess
              name: custom-htaccess
            {{- end }}
            {{- if or .Values.customPostInitScripts .Values.wordpressConfigureCache }}
            - mountPath: /docker-entrypoint-init.d
              name: custom-postinit
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "wordpress.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /bin/apache_exporter
            - --scrape_uri
            - http://status.localhost:8080/server-status/?auto
          {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.containerPorts.metrics }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.metrics.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.metrics.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- end }}
          {{- if .Values.metrics.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.metrics.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- end }}
          {{- if .Values.metrics.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.metrics.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- end }}
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- else if ne .Values.metrics.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.metrics.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.metrics.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.metrics.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if or .Values.wordpressConfiguration .Values.existingWordPressConfigurationSecret }}
        - name: wordpress-config
          secret:
            secretName: {{ include "wordpress.configSecretName" . }}
            defaultMode: 0644
        {{- end }}
        {{- if or .Values.apacheConfiguration .Values.existingApacheConfigurationConfigMap }}
        - name: apache-config
          configMap:
            name: {{ include "wordpress.apache.configmapName" . }}
            defaultMode: 0644
        {{- end }}
        {{- if and (not .Values.allowOverrideNone) .Values.customHTAccessCM }}
        - name: custom-htaccess
          configMap:
            name: {{ include "wordpress.customHTAccessCM" . }}
            items:
              - key: wordpress-htaccess.conf
                path: wordpress-htaccess.conf
        {{- end }}
        {{- if or .Values.customPostInitScripts .Values.wordpressConfigureCache }}
        - name: custom-postinit
          configMap:
            name: {{ printf "%s-postinit" (include "common.names.fullname" .) }}
            defaultMode: 0755
        {{- end }}
        - name: wordpress-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "common.names.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
