{{- if and (include "magento.host" .) (or .Values.mariadb.enabled .Values.externalDatabase.host) -}}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if .Values.updateStrategy }}
  strategy: {{- include "common.tplvalues.render" (dict "value" .Values.updateStrategy "context" $ ) | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "magento.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "magento.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - bash
          args:
            - -ec
            - |
              mkdir -p "/bitnami/magento"
              chown "/bitnami/magento"
              find "/bitnami/magento" -mindepth 1 -maxdepth 1 -not -name "config" -not -name ".snapshot" -not -name "lost+found" | xargs chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: magento-data
              mountPath: /bitnami/magento
              {{- if .Values.persistence.subPath }}
              subPath: {{ .Values.persistence.subPath }}
              {{- end }}
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- include "certificates.initContainer" . | nindent 8 }}
      containers:
        - name: magento
          image: {{ include "magento.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ ternary "yes" "no" .Values.allowEmptyPassword | quote }}
            - name: APACHE_HTTP_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: APACHE_HTTPS_PORT_NUMBER
              value: {{ .Values.containerPorts.https | quote }}
            - name: MAGENTO_DATABASE_HOST
              value: {{ include "magento.databaseHost" . | quote }}
            - name: MAGENTO_DATABASE_PORT_NUMBER
              value: {{ include "magento.databasePort" . | quote }}
            - name: MAGENTO_DATABASE_NAME
              value: {{ include "magento.databaseName" . | quote }}
            - name: MAGENTO_DATABASE_USER
              value: {{ include "magento.databaseUser" . | quote }}
            - name: MAGENTO_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "magento.databaseSecretName" . }}
                  key: mariadb-password
            - 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_SKIP_BOOTSTRAP
              value: {{ ternary "yes" "no" .Values.magentoSkipInstall | quote }}
            {{- $port:=.Values.service.ports.http | toString }}
            - name: MAGENTO_HOST
              value: "{{ include "magento.host" . }}{{- if ne $port "80" }}:{{ .Values.service.ports.http }}{{ end }}"
            - name: MAGENTO_USERNAME
              value: {{ .Values.magentoUsername | quote }}
            - name: MAGENTO_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.names.fullname" . }}
                  key: magento-password
            {{- if .Values.magentoEmail }}
            - name: MAGENTO_EMAIL
              value: {{ .Values.magentoEmail | quote }}
            {{- end }}
            {{- if .Values.magentoFirstName }}
            - name: MAGENTO_FIRST_NAME
              value: {{ .Values.magentoFirstName | quote }}
            {{- end }}
            {{- if .Values.magentoLastName }}
            - name: MAGENTO_LAST_NAME
              value: {{ .Values.magentoLastName | quote }}
            {{- end }}
            {{- if .Values.magentoMode }}
            - name: MAGENTO_MODE
              value: {{ .Values.magentoMode | quote }}
            {{- end }}
            {{- if .Values.magentoExtraInstallArgs }}
            - name: MAGENTO_EXTRA_INSTALL_ARGS
              value: {{ .Values.magentoExtraInstallArgs | quote }}
            {{- end }}
            {{- if .Values.magentoAdminUri }}
            - name: MAGENTO_ADMIN_URL_PREFIX
              value: {{ .Values.magentoAdminUri | quote }}
            {{- end }}
            - name: MAGENTO_ENABLE_HTTPS
              value: {{ ternary "yes" "no" .Values.magentoUseHttps | quote }}
            - name: MAGENTO_ENABLE_ADMIN_HTTPS
              value: {{ ternary "yes" "no" .Values.magentoUseSecureAdmin | quote }}
            - name: MAGENTO_DEPLOY_STATIC_CONTENT
              value: {{ ternary "yes" "no" .Values.magentoDeployStaticContent | quote }}
            - name: MAGENTO_SKIP_REINDEX
              value: {{ ternary "yes" "no" .Values.magentoSkipReindex | quote }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
            - name: https
              containerPort: {{ .Values.containerPorts.https }}
            {{- if .Values.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /index.php
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "magento.host" . | quote }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /index.php
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "magento.host" . | quote }}
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: magento-data
              mountPath: /bitnami/magento
              {{- if .Values.persistence.subPath }}
              subPath: {{ .Values.persistence.subPath }}
              {{- end }}
            {{- include "certificates.volumeMounts" . | nindent 12 }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "magento.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          command:
            - /bin/apache_exporter
          args:
            - --scrape_uri
            - {{ printf "http://status.localhost:%d/server-status/?auto" (int .Values.containerPorts.http) }}
          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 | nindent 12 }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: magento-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (printf "%s-magento" (include "common.names.fullname" .)) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- include "certificates.volumes" . | indent 8 }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end -}}
