apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "suitecrm.fullname" . }}
  labels:

  labels:
    {{- include "suitecrm.labels" . | nindent 4 }}
    # app.kubernetes.io/name: {{ include "suitecrm.name" . }}
    # app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "suitecrm.selectorLabels" . | nindent 6 }}
      # app.kubernetes.io/name: {{ include "suitecrm.name" . }}
      # app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        {{- include "suitecrm.selectorLabels" . | nindent 8 }}
        # app.kubernetes.io/name: {{ include "suitecrm.name" . }}
        # app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      securityContext:
        fsGroup: {{ .Values.podSecurityContext.fsGroup }}
      containers:
        - name: web
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.targetPort }}
          env:
            # Bitnami-Image-Variablen
            - name: SUITECRM_HOST
              value: {{ .Values.suitecrm.siteURL | quote }}
            - name: SUITECRM_ENABLE_HTTPS
              value: {{ ternary "yes" "no" .Values.suitecrm.httpsEnabled | quote }}
            - name: SUITECRM_EXTERNAL_HTTP_PORT_NUMBER
              value: "80"
            - name: SUITECRM_EXTERNAL_HTTPS_PORT_NUMBER
              value: "443"

            - name: SUITECRM_DATABASE_HOST
              # value: "{{ include "suitecrm.fullname" . }}-mariadb"
              value: "{{ .Release.Name }}-mariadb"
            - name: SUITECRM_DATABASE_NAME
              value: {{ .Values.mariadb.auth.database | quote }}
            - name: SUITECRM_DATABASE_USER
              value: {{ .Values.mariadb.auth.username | quote }}
            - name: SUITECRM_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-mariadb
                  # name: {{ include "suitecrm.fullname" . }}-mariadb
                  key: mariadb-password

            # Admin/SMPP etc. aus Secret
            - name: SUITECRM_USERNAME
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_USERNAME } }
            - name: SUITECRM_PASSWORD
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_PASSWORD } }
            - name: SUITECRM_EMAIL
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_EMAIL } }

            - name: SUITECRM_SMTP_HOST
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_SMTP_HOST } }
            - name: SUITECRM_SMTP_PORT_NUMBER
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_SMTP_PORT_NUMBER } }
            - name: SUITECRM_SMTP_USER
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_SMTP_USER } }
            - name: SUITECRM_SMTP_PASSWORD
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_SMTP_PASSWORD } }
            - name: SUITECRM_SMTP_PROTOCOL
              valueFrom: { secretKeyRef: { name: {{ include "suitecrm.fullname" . }}-app, key: SUITECRM_SMTP_PROTOCOL } }

          volumeMounts:
            - name: data
              mountPath: /bitnami/suitecrm   # Persistenzpfad der Bitnami-Images
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 10
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 60
            periodSeconds: 20
            timeoutSeconds: 5
            failureThreshold: 6

        {{- if .Values.scheduler.enabled }}
        - name: scheduler
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["bash", "-lc"]
          args:
            - >
              while true; do
                php /bitnami/suitecrm/bin/console schedulers:run || true;
                sleep {{ .Values.scheduler.intervalSeconds }};
              done
          volumeMounts:
            - name: data
              mountPath: /bitnami/suitecrm
          env:
            - name: SUITECRM_HOST
              value: {{ .Values.suitecrm.siteURL | quote }}
            - name: SUITECRM_DATABASE_HOST
              # value: "{{ include "suitecrm.fullname" . }}-mariadb"
              value: "{{ .Release.Name }}-mariadb"
            - name: SUITECRM_DATABASE_NAME
              value: {{ .Values.mariadb.auth.database | quote }}
            - name: SUITECRM_DATABASE_USER
              value: {{ .Values.mariadb.auth.username | quote }}
            - name: SUITECRM_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  # name: {{ include "suitecrm.fullname" . }}-mariadb
                  name: {{ .Release.Name }}-mariadb
                  key: mariadb-password
        {{- end }}
      volumes:
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{ else }}{{ include "suitecrm.fullname" . }}{{ end }}
          {{- else }}
          emptyDir: {}
          {{- end }}

