{{- if or .Values.mariadb.enabled .Values.externalDatabase.host -}}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: "{{ template "phpbb.fullname" . }}"
  labels:
    app: "{{ template "phpbb.fullname" . }}"
    chart: "{{ template "phpbb.chart" . }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  selector:
    matchLabels:
      app: "{{ template "phpbb.fullname" . }}"
      release: {{ .Release.Name | quote }}
  replicas: 1
  template:
    metadata:
      labels:
        app: "{{ template "phpbb.fullname" . }}"
        chart: "{{ template "phpbb.chart" . }}"
        release: {{ .Release.Name | quote }}
{{- 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:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "status.localhost"
      containers:
      - name: "{{ template "phpbb.fullname" . }}"
        image: {{ template "phpbb.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: ALLOW_EMPTY_PASSWORD
          value: {{ .Values.allowEmptyPassword | quote }}
        - name: MARIADB_HOST
        {{- if .Values.mariadb.enabled }}
          value: {{ template "phpbb.mariadb.fullname" . }}
        {{- else }}
          value: {{ .Values.externalDatabase.host | quote }}
        {{- end }}
        - name: MARIADB_PORT_NUMBER
        {{- if .Values.mariadb.enabled }}
          value: "3306"
        {{- else }}
          value: {{ .Values.externalDatabase.port | quote }}
        {{- end }}
        - name: PHPBB_DATABASE_NAME
        {{- if .Values.mariadb.enabled }}
          value: {{ .Values.mariadb.db.name | quote }}
        {{- else }}
          value: {{ .Values.externalDatabase.database | quote }}
        {{- end }}
        - name: PHPBB_DATABASE_USER
        {{- if .Values.mariadb.enabled }}
          value: {{ .Values.mariadb.db.user | quote }}
        {{- else }}
          value: {{ .Values.externalDatabase.user | quote }}
        {{- end }}
        - name: PHPBB_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.mariadb.enabled }}
              name: {{ template "phpbb.mariadb.fullname" . }}
              key: mariadb-password
            {{- else }}
              name: {{ printf "%s-%s" .Release.Name "externaldb" }}
              key: db-password
            {{- end }}
        - name: PHPBB_USERNAME
          value: {{ .Values.phpbbUser | quote }}
        - name: PHPBB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: "{{ template "phpbb.fullname" . }}"
              key: phpbb-password
        - name: PHPBB_EMAIL
          value: {{ .Values.phpbbEmail | quote }}
        {{- if .Values.smtpHost }}
        - name: SMTP_HOST
          value: {{ .Values.smtpHost | quote }}
        {{- end }}
        {{- if .Values.smtpPort }}
        - name: SMTP_PORT
          value: {{ .Values.smtpPort | quote }}
        {{- end }}
        {{- if .Values.smtpUser }}
        - name: SMTP_USER
          value: {{ .Values.smtpUser | quote }}
        {{- end }}
        {{- if .Values.smtpPassword }}
        - name: SMTP_PASSWORD
          valueFrom:
            secretKeyRef:
              name: "{{ template "phpbb.fullname" . }}"
              key: smtp-password
        {{- end }}
        {{- if .Values.smtpProtocol }}
        - name: SMTP_PROTOCOL
          value: {{ default "" .Values.smtpProtocol | quote }}
        {{- end }}
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        livenessProbe:
          httpGet:
            path: /ucp.php
            port: http
          initialDelaySeconds: 120
        readinessProbe:
          httpGet:
            path: /ucp.php
            port: http
          initialDelaySeconds: 30
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: phpbb-data
          mountPath: /bitnami/phpbb
        - name: apache-data
          mountPath: /bitnami/apache
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ template "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: phpbb-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: "{{ template "phpbb.fullname" . }}-phpbb"
      {{- else }}
        emptyDir: {}
      {{- end }}
      - name: apache-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: "{{ template "phpbb.fullname" . }}-apache"
      {{- else }}
        emptyDir: {}
      {{- end }}
{{- end }}
