apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ template "fullname" . }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        release: "{{ .Release.Name }}"
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      containers:
      - name: {{ template "fullname" . }}
        image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: ALLOW_EMPTY_PASSWORD
          value: {{ .Values.allowEmptyPassword | quote }}
        {{- if .Values.mariadb.enabled }}
        - name: MARIADB_HOST
          value: {{ template "testlink.mariadb.fullname" . }}
        - name: MARIADB_PORT_NUMBER
          value: "3306"
        - name: TESTLINK_DATABASE_NAME
          value: {{ .Values.mariadb.db.name | quote }}
        - name: TESTLINK_DATABASE_USER
          value: {{ .Values.mariadb.db.user | quote }}
        - name: TESTLINK_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "testlink.mariadb.fullname" . }}
              key: mariadb-password
        {{- else }}
        - name: MARIADB_HOST
          value: {{ .Values.externalDatabase.host | quote }}
        - name: MARIADB_PORT_NUMBER
          value: {{ .Values.externalDatabase.port | quote }}
        - name: TESTLINK_DATABASE_NAME
          value: {{ .Values.externalDatabase.database | quote }}
        - name: TESTLINK_DATABASE_USER
          value: {{ .Values.externalDatabase.user | quote }}
        - name: TESTLINK_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "externaldb" }}
              key: db-password
        {{- end }}
        - name: TESTLINK_USERNAME
          value: {{ default "" .Values.testlinkUsername | quote }}
        - name: TESTLINK_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: testlink-password
        - name: TESTLINK_EMAIL
          value: {{ default "" .Values.testlinkEmail | quote }}
        - name: TESTLINK_LANGUAGE
          value: {{ default "" .Values.testlinkLanguage | quote }}
        - name: SMTP_ENABLE
          value: {{ .Values.smtpEnable | quote }}
        - name: SMTP_CONNECTION_MODE
          value: {{ .Values.smtpConnectionMode | quote }}
        - name: SMTP_HOST
          value: {{ .Values.smtpHost | quote }}
        - name: SMTP_PORT
          value: {{ .Values.smtpPort | quote }}
        - name: SMTP_USER
          value: {{ .Values.smtpUser | quote }}
        - name: SMTP_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: smtp-password
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        livenessProbe:
          httpGet:
            path: /login.php
            port: http
          initialDelaySeconds: 120
        readinessProbe:
          httpGet:
            path: /login.php
            port: http
          initialDelaySeconds: 30
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: testlink-data
          mountPath: /bitnami/testlink
        - name: apache-data
          mountPath: /bitnami/apache
      volumes:
      - name: testlink-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "fullname" . }}-testlink
      {{- else }}
        emptyDir: {}
      {{- end }}
      - name: apache-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "fullname" . }}-apache
      {{- else }}
        emptyDir: {}
      {{- end }}
