{{- if and (include "magento.host" .) (or .Values.mariadb.enabled .Values.externalDatabase.host) -}}
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: {{ template "magento.fullname" . }}
  labels:
    app: {{ template "magento.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  template:
    metadata:
      labels:
        app: {{ template "magento.fullname" . }}
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      containers:
      - name: {{ template "magento.fullname" . }}
        image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: MARIADB_HOST
        {{- if .Values.mariadb.enabled }}
          value: {{ template "magento.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: MAGENTO_DATABASE_NAME
        {{- if .Values.mariadb.enabled }}
          value: {{ .Values.mariadb.mariadbDatabase | quote }}
        {{- else }}
          value: {{ .Values.externalDatabase.database | quote }}
        {{- end }}
        - name: MAGENTO_DATABASE_USER
        {{- if .Values.mariadb.enabled }}
          value: {{ .Values.mariadb.mariadbUser | quote }}
        {{- else }}
          value: {{ .Values.externalDatabase.user | quote }}
        {{- end }}
        - name: MAGENTO_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              {{- if .Values.mariadb.enabled }}
              name: {{ template "magento.mariadb.fullname" . }}
              key: mariadb-password
              {{- else }}
              name: {{ template "magento.fullname" . }}-externaldb
              key: db-password
              {{- end }}
        - name: MAGENTO_HOST
          value: {{ include "magento.host" . | quote }}
        - name: MAGENTO_USERNAME
          value: {{ .Values.magentoUsername | quote }}
        - name: MAGENTO_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "magento.fullname" . }}
              key: magento-password
        - name: MAGENTO_EMAIL
          value: {{ .Values.magentoEmail | quote }}
        - name: MAGENTO_ADMINURI
          value: {{ .Values.magentoAdminUri | quote }}
        - name: MAGENTO_FIRSTNAME
          value: {{ .Values.magentoFirstName | quote }}
        - name: MAGENTO_LASTNAME
          value: {{ .Values.magentoLastName | quote }}
        - name: MAGENTO_MODE
          value: {{ .Values.magentoMode | quote }}
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        livenessProbe:
          httpGet:
            path: /index.php
            port: http
            httpHeaders:
            - name: Host
              value: {{ include "magento.host" . | quote }}
          initialDelaySeconds: 420
          timeoutSeconds: 5
          failureThreshold: 6
        readinessProbe:
          httpGet:
            path: /index.php
            port: http
            httpHeaders:
            - name: Host
              value: {{ include "magento.host" . | quote }}
          initialDelaySeconds: 30
          timeoutSeconds: 3
          periodSeconds: 5
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: magento-data
          mountPath: /bitnami/magento
        - name: apache-data
          mountPath: /bitnami/apache
      volumes:
      - name: magento-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "magento.fullname" . }}-magento
      {{- else }}
        emptyDir: {}
      {{- end }}
      - name: apache-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "magento.fullname" . }}-apache
      {{- else }}
        emptyDir: {}
      {{- end }}
{{- end -}}
