{{- if and (include "magento.host" .) (or .Values.mariadb.enabled .Values.externalDatabase.host) -}}
apiVersion: {{ template "magento.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "magento.fullname" . }}
  labels:
    app: {{ template "magento.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      app: {{ template "magento.fullname" . }}
      release: "{{ .Release.Name }}"
  template:
    metadata:
      labels:
        app: {{ template "magento.fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        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 "magento.imagePullSecrets" . | indent 6 }}
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "status.localhost"
      containers:
      - name: {{ template "magento.fullname" . }}
        image: {{ template "magento.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        {{- if .Values.image.debug}}
        - name: BASH_DEBUG
          value: "1"
        - name: NAMI_DEBUG
          value: "1"
        {{- end }}
        - 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: ELASTICSEARCH_HOST
        {{- if .Values.elasticsearch.enabled }}
          value: {{ template "magento.elasticsearch.fullname" . }}
        {{- else if .Values.externalElasticsearch.host }}
          value: {{ .Values.externalElasticsearch.host | quote }}
        {{- else }}
          value: ""
        {{- end }}
        - name: ELASTICSEARCH_PORT_NUMBER
        {{- if .Values.elasticsearch.enabled }}
          value: "9200"
        {{- else if .Values.externalElasticsearch.port }}
          value: {{ .Values.externalElasticsearch.port | quote }}
        {{- else }}
          value: ""
        {{- end }}
        - name: MAGENTO_DATABASE_NAME
        {{- if .Values.mariadb.enabled }}
          value: {{ .Values.mariadb.db.name | quote }}
        {{- else }}
          value: {{ .Values.externalDatabase.database | quote }}
        {{- end }}
        - name: MAGENTO_DATABASE_USER
        {{- if .Values.mariadb.enabled }}
          value: {{ .Values.mariadb.db.user | 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 }}
        {{- $port:=.Values.service.port | toString }}
        - name: MAGENTO_HOST
          value: "{{ include "magento.host" . }}{{- if ne $port "80" }}:{{ .Values.service.port }}{{ end }}"
        - 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
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /index.php
            port: http
            httpHeaders:
            - name: Host
              value: {{ include "magento.host" . | quote }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
        {{- end }}
        {{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /index.php
            port: http
            httpHeaders:
            - name: Host
              value: {{ include "magento.host" . | quote }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
        {{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: magento-data
          mountPath: /bitnami/magento
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ template "magento.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: magento-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "magento.fullname" . }}-magento
      {{- else }}
        emptyDir: {}
      {{- end }}
{{- end -}}
