apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "wordpress.fullname" . }}
  labels:
    {{- include "wordpress.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "wordpress.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "wordpress.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "wordpress.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: WORDPRESS_DB_HOST
              value: {{ .Values.env.WORDPRESS_DB_HOST | quote }}
            - name: WORDPRESS_DB_NAME
              value: {{ .Values.env.WORDPRESS_DB_NAME | quote }}
            - name: WORDPRESS_DB_USER
              value: {{ .Values.env.WORDPRESS_DB_USER | quote }}
           {{- with .Values.env.WORDPRESS_CONFIG_EXTRA }}
            - name: WORDPRESS_CONFIG_EXTRA
              value: {{ .Values.env.WORDPRESS_CONFIG_EXTRA }}
           {{- end }}
            - name: WORDPRESS_DB_PASSWORD_FILE
              value: "/run/secrets/mysql/password"
            - name: WORDPRESS_AUTH_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: AUTH_KEY
            - name: WORDPRESS_SECURE_AUTH_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: SECURE_AUTH_KEY
            - name: WORDPRESS_LOGGED_IN_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: LOGGED_IN_KEY
            - name: WORDPRESS_NONCE_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: NONCE_KEY
            - name: WORDPRESS_AUTH_SALT
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: AUTH_SALT
            - name: WORDPRESS_SECURE_AUTH_SALT
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: SECURE_AUTH_SALT
            - name: WORDPRESS_LOGGED_IN_SALT
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: LOGGED_IN_SALT
            - name: WORDPRESS_NONCE_SALT
              valueFrom:
                secretKeyRef:
                  name: {{ include "wordpress.fullname" . }}
                  key: NONCE_SALT
          volumeMounts:
           - name: secret
             mountPath: "/run/secrets/mysql"
             readOnly: true
           - name: data
             mountPath: "/var/www/html/wp-content"
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}

          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
       - name: data
         persistentVolumeClaim:
          claimName: {{ include "wordpress.fullname" . }}
       - name: secret
         secret:
           secretName: {{ include "wordpress.fullname" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
