apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "moodle.fullname" . }}
  labels:
    app: {{ template "moodle.fullname" . }}
    chart: {{ template "moodle.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      app: {{ template "moodle.fullname" . }}
      release: "{{ .Release.Name }}"
  template:
    metadata:
      labels:
        app: {{ template "moodle.fullname" . }}
        chart: {{ template "moodle.chart" . }}
        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:
      {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "status.localhost"
      containers:
      - name: moodle
        image: {{ template "moodle.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: ALLOW_EMPTY_PASSWORD
          value: {{ .Values.allowEmptyPassword | quote }}
        {{- if .Values.mariadb.enabled }}
        - name: MARIADB_HOST
          value: {{ template "moodle.mariadb.fullname" . }}
        - name: MARIADB_PORT_NUMBER
          value: "3306"
        - name: MOODLE_DATABASE_NAME
          value: {{ .Values.mariadb.db.name | quote }}
        - name: MOODLE_DATABASE_USER
          value: {{ .Values.mariadb.db.user | quote }}
        - name: MOODLE_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "moodle.mariadb.fullname" . }}
              key: mariadb-password
        {{- else }}
        - name: MARIADB_HOST
          value: {{ .Values.externalDatabase.host | quote }}
        - name: MARIADB_PORT_NUMBER
          value: {{ .Values.externalDatabase.port | quote }}
        - name: MOODLE_DATABASE_NAME
          value: {{ .Values.externalDatabase.database | quote }}
        - name: MOODLE_DATABASE_USER
          value: {{ .Values.externalDatabase.user | quote }}
        - name: MOODLE_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "externaldb" }}
              key: db-password
        {{- end }}
        - name: MOODLE_USERNAME
          value: {{ default "" .Values.moodleUsername | quote }}
        - name: MOODLE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "moodle.fullname" . }}
              key: moodle-password
        - name: MOODLE_EMAIL
          value: {{ default "" .Values.moodleEmail | quote }}
        - name: SMTP_HOST
          value: {{ default "" .Values.smtpHost | quote }}
        - name: SMTP_PORT
          value: {{ default "" .Values.smtpPort | quote }}
        - name: SMTP_USER
          value: {{ default "" .Values.smtpUser | quote }}
        - name: SMTP_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "moodle.fullname" . }}
              key: smtp-password
        - name: SMTP_PROTOCOL
          value: {{ default "" .Values.smtpProtocol | quote }}
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: /login/index.php
            port: http
          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: /login/index.php
            port: http
          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: moodle-data
          mountPath: /bitnami
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ template "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: moodle-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default (include "moodle.moodle.fullname" . ) }}
      {{- else }}
        emptyDir: {}
      {{- end }}
