apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "wordpress.fullname" . }}
  labels:
    {{- include "wordpress.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ .Values.service.name }}
  selector:
    matchLabels:
      {{- include "wordpress.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      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: {{ .Values.nginx.name }}
          securityContext:
            {{- toYaml .Values.nginx.securityContext | nindent 12 }}
          image: "{{ .Values.nginx.image.repository }}:{{ .Values.nginx.image.tag }}"
          imagePullPolicy: {{ .Values.nginx.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: http
          readinessProbe:
            tcpSocket:
              port: http
          resources:
            {{- toYaml .Values.nginx.resources | nindent 12 }}
          volumeMounts:
          - mountPath: /var/www/html
            name: wordpress-pv
          - mountPath: /etc/nginx/nginx.conf
            name: nginx-conf
            subPath: nginx.conf
          - mountPath: /etc/nginx/conf.d/default.conf
            name: nginx-conf
            subPath: default.conf
        - name: {{ .Values.wordpress.name }}
          securityContext:
            {{- toYaml .Values.wordpress.securityContext | nindent 12 }}
          image: "{{ .Values.wordpress.image.repository }}:{{ .Values.wordpress.image.tag }}"
          imagePullPolicy: {{ .Values.wordpress.image.pullPolicy }}
          env:
          - name: WORDPRESS_DB_HOST
            valueFrom:
              secretKeyRef:
                name: wp-db
                key: db-host
          - name: WORDPRESS_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: wp-db
                key: wp-db-pass
          - name: WORDPRESS_DB_NAME
            valueFrom:
              secretKeyRef:
                name: wp-db
                key: wp-db
          - name: WORDPRESS_DB_USER
            valueFrom:
              secretKeyRef:
                name: wp-db
                key: wp-db-user
          ports:
            - name: php
              containerPort: 9000
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: 9000
          readinessProbe:
            tcpSocket:
              port: 9000
          resources:
            {{- toYaml .Values.wordpress.resources | nindent 12 }}
          volumeMounts:
          - mountPath: /var/www/html
            name: wordpress-pv
      volumes:
      - name: nginx-conf
        secret:
          defaultMode: 420
          secretName: nginx-conf
      - name: wordpress-pv
        persistentVolumeClaim:
          claimName: wp-data
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
