apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "phpmyadmin.fullname" . }}
  labels:
    app: {{ template "phpmyadmin.name" . }}
    chart: {{ template "phpmyadmin.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "phpmyadmin.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "phpmyadmin.name" . }}
        chart: {{ template "phpmyadmin.chart" . }}
        release: {{ .Release.Name }}
  {{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
  {{- end }}
{{- 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 "phpmyadmin.imagePullSecrets" . | indent 6 }}
      {{- with .Values.nodeSelector }}
      nodeSelector: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations: {{ toYaml . | nindent 8 }}
      {{- end }}
      hostAliases:
        - ip: "127.0.0.1"
          hostnames:
            - "status.localhost"
      containers:
        - name: {{ .Chart.Name }}
          image: {{ template "phpmyadmin.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: DATABASE_PORT_NUMBER
              value: {{ .Values.db.port | quote }}
            {{- if .Values.db.chartName }}
            - name: DATABASE_HOST
              value: "{{ template "phpmyadmin.dbfullname" . }}"
            {{- else if .Values.db.bundleTestDB }}
            - name: DATABASE_HOST
              value: "{{ template "mariadb.fullname" . }}"
            {{- else }}
            - name: DATABASE_HOST
              value: {{ .Values.db.host | quote }}
            {{- end }}
            {{- if and (not .Values.db.chartName) (not .Values.db.host) }}
            - name: PHPMYADMIN_ALLOW_NO_PASSWORD
              value: "true"
            - name: PHPMYADMIN_ALLOW_ARBITRARY_SERVER
              value: "true"
            {{- else }}
            - name: PHPMYADMIN_ALLOW_NO_PASSWORD
              value: "false"
            {{- end }}
            - name: DATABASE_ENABLE_SSL
              value: {{ ternary "yes" "no" .Values.db.enableSsl | quote }}
            {{- if .Values.db.enableSsl }}
            {{- if not (empty .Values.db.ssl.clientKey) }}
            - name: DATABASE_SSL_KEY
              value: "/db_certs/server_key.pem"
            {{- end }}
            {{- if not (empty .Values.db.ssl.clientCertificate) }}
            - name: DATABASE_SSL_CERT
              value: "/db_certs/server_certificate.pem"
            {{- end }}
            {{- if not (empty .Values.db.ssl.caCertificate) }}
            - name: DATABASE_SSL_CA
              value: "/db_certs/ca_certificate.pem"
            {{- end }}
            {{- if .Values.db.ssl.ciphers }}
            - name: DATABASE_SSL_CIPHERS
              values: {{ .Values.db.ssl.ciphers | quote }}
            {{- end }}
            - name: DATABASE_SSL_VERIFY
              value: {{ ternary "yes" "no" .Values.db.ssl.verify | quote }}
            {{- end }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
            - name: https
              containerPort: 443
              protocol: TCP
          {{- if .Values.probesEnabled }}
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          {{- end }}
          {{- if .Values.resources }}
          resources: {{ toYaml .Values.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.db.enableSsl }}
          volumeMounts:
          - name: ssl-certs
            mountPath: /db_certs
          {{- end }}
{{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ template "phpmyadmin.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
          {{- if .Values.metrics.resources }}
          resources: {{ toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
{{- end }}
{{- if .Values.db.enableSsl }}
      volumes:
        - name: ssl-certs
          secret:
            secretName: {{ template "phpmyadmin.fullname" . }}-certs
            items:
              {{- if not (empty .Values.db.ssl.clientKey) }}
              - key: server_key.pem
                path: server_key.pem
              {{- end }}
              {{- if not (empty .Values.db.ssl.clientCertificate) }}
              - key: server_certificate.pem
                path: server_certificate.pem
              {{- end }}
              {{- if not (empty .Values.db.ssl.caCertificate) }}
              - key: ca_certificate.pem
                path: ca_certificate.pem
              {{- end }}
{{- end }}
