apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "wordpress.fullname" . }}
  labels:
    {{- include "wordpress.labels" . | nindent 4 }}
    {{- with .Values.customLabels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.customAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.revisionHistoryLimit }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "wordpress.selectorLabels" . | nindent 6 }}
  {{- with .Values.strategy }}
  strategy:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  template:
    metadata:
      annotations:
        checksum/secureconfig: {{ include (print $.Template.BasePath "/secureconfig.yaml") . | sha256sum }}
        checksum/extendedconfig: {{ include (print $.Template.BasePath "/extendedconfig.yaml") . | sha256sum }}
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "wordpress.selectorLabels" . | nindent 8 }}
      {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "wordpress.serviceAccountName" . }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.extraInitContainers }}
      initContainers:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
            - secretRef:
                name: {{ include "wordpress.fullname" . }}
          {{- range .Values.extraEnvSecrets }}
            - secretRef:
                name: {{ . }}
          {{- end }}
          {{- range .Values.extraEnvConfigs }}
            - configMapRef:
                name: {{ . }}
          {{- end }}
          {{- with .Values.env }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.args }}
          args:
            {{- range .Values.args }}
            - {{ . }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPort }}
              protocol: TCP
          {{- if .Values.customStartupProbe }}
          startupProbe:
            {{- toYaml .Values.customStartupProbe | nindent 12 }}
          {{- else }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /wp-login.php
              port: http
              httpHeaders:
                - name: Host
                  value: localhost:{{ .Values.containerPort  }}
                - name: x-forwarded-proto
                  value: https
          {{- with .Values.startupProbe }}
            initialDelaySeconds: {{ .initialDelaySeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            failureThreshold: {{ .failureThreshold }}
            successThreshold: {{ .successThreshold }}
            periodSeconds: {{ .periodSeconds }}
          {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe:
            {{- toYaml .Values.customLivenessProbe | nindent 12 }}
          {{- else }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /wp-admin/install.php
              port: http
              httpHeaders:
                - name: Host
                  value: localhost:{{ .Values.containerPort  }}
                - name: x-forwarded-proto
                  value: https
          {{- with .Values.livenessProbe }}
            initialDelaySeconds: {{ .initialDelaySeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            failureThreshold: {{ .failureThreshold }}
            successThreshold: {{ .successThreshold }}
            periodSeconds: {{ .periodSeconds }}
          {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe:
            {{- toYaml .Values.customReadinessProbe | nindent 12 }}
          {{- else }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /wp-login.php
              port: http
              httpHeaders:
                - name: Host
                  value: localhost:{{ .Values.containerPort  }}
                - name: x-forwarded-proto
                  value: https
          {{- with .Values.readinessProbe }}
            initialDelaySeconds: {{ .initialDelaySeconds }}
            timeoutSeconds: {{ .timeoutSeconds }}
            failureThreshold: {{ .failureThreshold }}
            successThreshold: {{ .successThreshold }}
            periodSeconds: {{ .periodSeconds }}
          {{- end }}
          {{- end }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /var/www/html
              name: wordpress-vol
            - mountPath: /tmp
              name: tmp
            - mountPath: /var/run
              name: run
            - mountPath: /etc/apache2/ports.conf
              subPath: ports.conf
              name: extended
            - mountPath: /etc/apache2/000-default.conf
              subPath: 000-default.conf
              name: extended
            - mountPath: /usr/local/etc/php/conf.d/custom.ini
              subPath: custom.ini
              name: extended
            {{- range $secret := .Values.extraSecrets }}
            - mountPath: {{ $secret.mountPath }}
              name: {{ $secret.name }}
            {{- end }}
      {{- with .Values.extraContainers }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
        - name: run
          emptyDir: {}
        - name: extended
          configMap:
            name: {{ include "wordpress.fullname" . }}-extended
        {{- range $secret := .Values.extraSecrets }}
        - name: {{ $secret.name }}
          secret:
            secretName: {{ $secret.name }}
            defaultMode: 0440
        {{- end }}
        - name: wordpress-vol
        {{- if .Values.storage.persistentVolumeClaimName }}
          persistentVolumeClaim:
            claimName: {{ .Values.storage.persistentVolumeClaimName }}
        {{- else }}
        {{- if .Values.storage.requestedSize }}
          persistentVolumeClaim:
            claimName: {{ include "wordpress.fullname" . }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- end }}