{{- if include "phabricator.host" . -}}
apiVersion: {{ template "phabricator.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "phabricator.fullname" . }}
  labels: {{- include "phabricator.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{- include "phabricator.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "phabricator.labels" . | nindent 8 }}
      {{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- toYaml .Values.podAnnotations | nindent 8 }}
        {{- end }}
        {{- if .Values.metrics.podAnnotations }}
        {{- toYaml .Values.metrics.podAnnotations | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
{{- include "phabricator.imagePullSecrets" . | nindent 6 }}
      hostAliases:
        - ip: "127.0.0.1"
          hostnames:
            - "status.localhost"
      containers:
        - name: {{ template "phabricator.fullname" . }}
          image: {{ template "phabricator.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            - name: MARIADB_HOST
              value: {{ include "phabricator.databaseHost" . | quote }}
            - name: MARIADB_USER
              value: {{ include "phabricator.databaseUser" . | quote }}
            - name: MARIADB_PORT_NUMBER
              value: {{ include "phabricator.databasePort" . | quote }}
            - name: MARIADB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "phabricator.databaseSecretName" . }}
                  key: mariadb-root-password
            {{- $port:=.Values.service.port | toString }}
            - name: PHABRICATOR_HOST
              value: "{{ include "phabricator.host" . }}{{- if ne $port "80" }}:{{ .Values.service.port }}{{ end }}"
            {{- if .Values.phabricatorAlternateFileDomain }}
            - name: PHABRICATOR_ALTERNATE_FILE_DOMAIN
              value: {{ .Values.phabricatorAlternateFileDomain | quote }}
            {{- end }}
            - name: PHABRICATOR_USERNAME
              value: {{ default "" .Values.phabricatorUsername | quote }}
            - name: PHABRICATOR_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "phabricator.fullname" . }}
                  key: phabricator-password
            {{- if .Values.phabricatorEmail }}
            - name: PHABRICATOR_EMAIL
              value: {{ .Values.phabricatorEmail | quote }}
            {{- end }}
            {{- if .Values.phabricatorFirstName }}
            - name: PHABRICATOR_FIRSTNAME
              value: {{ .Values.phabricatorFirstName | quote }}
            {{- end }}
            {{- if .Values.phabricatorLastName }}
            - name: PHABRICATOR_LASTNAME
              value: {{ .Values.phabricatorLastName | quote }}
            {{- end }}
            {{- 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 "phabricator.fullname" . }}
                  key: smtp-password
            {{- end }}
            {{- if .Values.smtpProtocol }}
            - name: SMTP_PROTOCOL
              value: {{ .Values.smtpProtocol | quote }}
            {{- end }}
          ports:
            - name: http
              containerPort: 80
            - name: https
              containerPort: 443
          livenessProbe:
            httpGet:
              path: /auth/
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "phabricator.host" . | quote }}
            initialDelaySeconds: 180
            timeoutSeconds: 5
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /auth/
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "phabricator.host" . | quote }}
            initialDelaySeconds: 30
            timeoutSeconds: 3
            periodSeconds: 5
          resources: {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: phabricator-data
              mountPath: /bitnami/phabricator
  {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ template "phabricator.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 | nindent 12 }}
  {{- end }}
      volumes:
        - name: phabricator-data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "phabricator.fullname" . }}
        {{- else }}
          emptyDir: {}
        {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- toYaml .Values.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- toYaml .Values.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- toYaml .Values.tolerations | nindent 8 }}
      {{- end }}
{{- end -}}
