apiVersion: {{ template "drupal.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "drupal.fullname" . }}
  labels:
    app: {{ template "drupal.fullname" . }}
    chart: {{ template "drupal.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      app: {{ template "drupal.fullname" . }}
      release: "{{ .Release.Name }}"
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ template "drupal.fullname" . }}
        chart: {{ template "drupal.chart" . }}
        release: "{{ .Release.Name }}"
{{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
  {{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
  {{- end }}
  {{- if .Values.metrics.podAnnotations }}
{{ toYaml .Values.metrics.podAnnotations | indent 8 }}
  {{- end }}
{{- end }}
    spec:
{{- include "drupal.imagePullSecrets" . | indent 6 }}
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "status.localhost"
      containers:
      - name: drupal
        image: {{ template "drupal.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: ALLOW_EMPTY_PASSWORD
        {{- if .Values.allowEmptyPassword }}
          value: "yes"
        {{- else }}
          value: "no"
        {{- end }}
        - name: MARIADB_HOST
        {{- if .Values.mariadb.enabled }}
          value: {{ template "drupal.mariadb.fullname" . }}
        {{- else }}
          value: {{ default "" .Values.externalDatabase.host | quote }}
        {{- end }}
        - name: MARIADB_PORT_NUMBER
          value: "3306"
        - name: DRUPAL_DATABASE_NAME
        {{- if .Values.mariadb.enabled }}
          value: {{ default "" .Values.mariadb.db.name | quote }}
        {{- else }}
          value: {{ default "" .Values.externalDatabase.database | quote }}
        {{- end }}
        - name: DRUPAL_DATABASE_USER
        {{- if .Values.mariadb.enabled }}
          value: {{ default "" .Values.mariadb.db.user | quote }}
        {{- else }}
          value: {{ default "" .Values.externalDatabase.user | quote }}
        {{- end }}
        - name: DRUPAL_DATABASE_PASSWORD
        {{- if .Values.mariadb.enabled }}
          valueFrom:
            secretKeyRef:
              name: {{ template "drupal.mariadb.fullname" . }}
              key: mariadb-password
        {{- else }}
          value: {{ default "" .Values.externalDatabase.password | quote }}
        {{- end }}
        {{- if .Values.drupalProfile }}
        - name: DRUPAL_PROFILE
          value: {{ .Values.drupalProfile | quote }}
        {{- end }}
        {{- if .Values.drupalUsername }}
        - name: DRUPAL_USERNAME
          value: {{ .Values.drupalUsername | quote }}
        {{- end }}
        - name: DRUPAL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "drupal.fullname" . }}
              key: drupal-password
        {{- if .Values.drupalEmail }}
        - name: DRUPAL_EMAIL
          value: {{ .Values.drupalEmail | quote }}
        {{- end }}
{{- if .Values.extraVars }}
{{ toYaml .Values.extraVars | indent 8 }}
{{- end }}
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        livenessProbe:
{{ toYaml .Values.livenessProbe | indent 10 }}
        readinessProbe:
{{ toYaml .Values.readinessProbe | indent 10 }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: drupal-data
          mountPath: {{ .Values.volumeMounts.drupal.mountPath }}
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ template "drupal.metrics.image" . }}
        imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
        command: [ '/bin/apache_exporter', '-scrape_uri', 'http://status.localhost:80/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
        resources:
  {{ toYaml .Values.metrics.resources | indent 10 }}
{{- end }}
      volumes:
      - name: drupal-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.drupal.existingClaim | default (printf "%s-drupal" (include "drupal.fullname" .)) }}
      {{- else }}
        emptyDir: {}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
