{{- if and (include "mediawiki.host" .) (or .Values.mariadb.enabled .Values.externalDatabase.host) -}}
apiVersion: {{ template "mediawiki.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "mediawiki.fullname" . }}
  labels:
    app: {{ template "mediawiki.name" . }}
    chart: {{ template "mediawiki.chart" . }}
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  selector:
    matchLabels:
      app: {{ template "mediawiki.name" . }}
      release: "{{ .Release.Name }}"
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ template "mediawiki.name" . }}
        chart: {{ template "mediawiki.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:
{{- include "mediawiki.imagePullSecrets" . | indent 6 }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.podSecurityContext.fsGroup }}
      {{- end }}
      hostAliases:
        - ip: "127.0.0.1"
          hostnames:
            - "status.localhost"
      containers:
        - name: {{ template "mediawiki.fullname" . }}
          image: {{ template "mediawiki.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.containerSecurityContext.runAsUser }}
          {{- end }}
          env:
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ .Values.allowEmptyPassword | quote }}
            - name: MARIADB_HOST
            {{- if .Values.mariadb.enabled }}
              value: {{ template "mediawiki.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: MEDIAWIKI_DATABASE_NAME
            {{- if .Values.mariadb.enabled }}
              value: {{ .Values.mariadb.db.name | quote }}
            {{- else }}
              value: {{ .Values.externalDatabase.database | quote }}
            {{- end }}
            - name: MEDIAWIKI_DATABASE_USER
            {{- if .Values.mariadb.enabled }}
              value: {{ .Values.mariadb.db.user | quote }}
            {{- else }}
              value: {{ .Values.externalDatabase.user | quote }}
            {{- end }}
            - name: MEDIAWIKI_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                {{- if .Values.mariadb.enabled }}
                  name: {{ template "mediawiki.mariadb.fullname" . }}
                  key: mariadb-password
                {{- else }}
                  name: {{ printf "%s-%s" .Release.Name "externaldb" }}
                  key: db-password
                {{- end }}
            {{- $port:=.Values.service.port | toString }}
            - name: MEDIAWIKI_HOST
              value: "{{ include "mediawiki.host" . }}{{- if ne $port "80" }}:{{ .Values.service.port }}{{ end }}"
            - name: MEDIAWIKI_USERNAME
              value: {{ .Values.mediawikiUser | quote }}
            - name: MEDIAWIKI_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mediawiki.fullname" . }}
                  key: mediawiki-password
            - name: MEDIAWIKI_EMAIL
              value: {{ .Values.mediawikiEmail | quote }}
            - name: MEDIAWIKI_WIKI_NAME
              value: {{ .Values.mediawikiName | quote }}
            {{- if .Values.smtpHostID }}
            - name: SMTP_HOST_ID
              value: {{ .Values.smtpHostID | 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 "mediawiki.fullname" . }}
                  key: smtp-password
            {{- end }}
          ports:
            - name: http
              containerPort: 8080
          {{- if .Values.service.httpsPort }}
            - name: https
              containerPort: 443
          {{ end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /api.php?action=query&meta=siteinfo&format=none
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "mediawiki.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: /api.php?action=query&meta=siteinfo&format=none
              port: http
              httpHeaders:
                - name: Host
                  value: {{ include "mediawiki.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 | nindent 12 }}
          volumeMounts:
            - mountPath: /bitnami/mediawiki
              name: mediawiki-data
              subPath: mediawiki
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ template "mediawiki.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: mediawiki-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ template "mediawiki.fullname" . }}-mediawiki{{- end }}
          {{- else }}
          emptyDir: {}
          {{- end }}
      {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
{{- end }}
