apiVersion: {{ template "jasperreports.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "jasperreports.fullname" . }}
  labels:
    app: "{{ template "jasperreports.fullname" . }}"
    chart: "{{ template "jasperreports.chart" . }}"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  selector:
    matchLabels:
      app: "{{ template "jasperreports.fullname" . }}"
      release: {{ .Release.Name | quote }}
  template:
    metadata:
      labels:
        app: "{{ template "jasperreports.fullname" . }}"
        chart: "{{ template "jasperreports.chart" . }}"
        release: {{ .Release.Name | quote }}
    spec:
{{- include "jasperreports.imagePullSecrets" . | indent 6 }}
      containers:
      - name: {{ template "jasperreports.fullname" . }}
        image: {{ template "jasperreports.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: ALLOW_EMPTY_PASSWORD
          value: {{ .Values.allowEmptyPassword | quote }}
        {{- if .Values.mariadb.enabled }}
        - name: MARIADB_HOST
          value: {{ template "jasperreports.mariadb.fullname" . }}
        - name: MARIADB_PORT_NUMBER
          value: "3306"
        - name: JASPERREPORTS_DATABASE_NAME
          value: {{ .Values.mariadb.db.name | quote }}
        - name: JASPERREPORTS_DATABASE_USER
          value: {{ .Values.mariadb.db.user | quote }}
        - name: JASPERREPORTS_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "jasperreports.mariadb.fullname" . }}
              key: mariadb-password
        {{- else }}
        - name: MARIADB_HOST
          value: {{ .Values.externalDatabase.host | quote }}
        - name: MARIADB_PORT_NUMBER
          value: {{ .Values.externalDatabase.port | quote }}
        - name: JASPERREPORTS_DATABASE_NAME
          value: {{ .Values.externalDatabase.database | quote }}
        - name: JASPERREPORTS_DATABASE_USER
          value: {{ .Values.externalDatabase.user | quote }}
        - name: JASPERREPORTS_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "externaldb" }}
              key: db-password
        {{- end }}
        - name: JASPERREPORTS_USERNAME
          value: {{ .Values.jasperreportsUsername | quote }}
        - name: JASPERREPORTS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "jasperreports.fullname" . }}
              key: jasperreports-password
        - name: JASPERREPORTS_EMAIL
          value: {{ .Values.jasperreportsEmail | quote }}
        {{- if .Values.smtpHost }}
        - name: SMTP_HOST
          value: {{ .Values.smtpHost | quote }}
        {{- end }}
        {{- if .Values.smtpPort }}
        - name: SMTP_PORT
          value: {{ .Values.smtpPort | quote }}
        {{- end }}
        {{- if .Values.smtpEmail }}
        - name: SMTP_EMAIL
          value: {{ .Values.smtpEmail| quote }}
        {{- end }}
        {{- if .Values.smtpUser }}
        - name: SMTP_USER
          value: {{ .Values.smtpUser | quote }}
        {{- end }}
        {{- if .Values.smtpPassword }}
        - name: SMTP_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "jasperreports.fullname" . }}
              key: smtp-password
        {{- end }}
        {{- if .Values.smtpProtocol }}
        - name: SMTP_PROTOCOL
          value: {{ .Values.smtpProtocol | quote }}
        {{- end }}
        ports:
        - name: http
          containerPort: 8080
        livenessProbe:
          httpGet:
            path: /jasperserver/login.html
            port: http
          initialDelaySeconds: 300
          timeoutSeconds: 5
          failureThreshold: 6
        readinessProbe:
          httpGet:
            path: /jasperserver/login.html
            port: http
          initialDelaySeconds: 30
          timeoutSeconds: 3
          periodSeconds: 5
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: jasperreports-data
          mountPath: /bitnami/jasperreports
      volumes:
      - name: jasperreports-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "jasperreports.fullname" . }}
      {{- else }}
        emptyDir: {}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
