apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "crm-chart.fullname" . }}
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "crm-chart.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "crm-chart.selectorLabels" . | nindent 6 }}
  # Recreate to avoid multi attach error, we have only 1 pod   
  strategy:
    type: Recreate
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "crm-chart.selectorLabels" . | nindent 8 }}
        app: webserver
    spec:
      {{- with .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: uploads
          persistentVolumeClaim:
            claimName: uploads
#        - name: public-uploads
#          persistentVolumeClaim:
#            claimName: public-uploads
        - name: app-logs
          persistentVolumeClaim:
            claimName: app-logs
#        - name: media
#          persistentVolumeClaim:
#            claimName: media
        - name: sessions
          persistentVolumeClaim:
            claimName: sessions
        - name: backup
          persistentVolumeClaim:
            claimName: backup
        - name: contabo-crm-backup
          secret:
            secretName: contabo-crm-backup
      containers:
        - name: {{ .Chart.Name }}
          {{- if .Values.crm.image.repo }}
          # image: "{{ .Values.crm.image.repo }}:{{ .Values.crm.image.tag }}"
          {{- else }}
          image: "{{ .Values.crm.image.local }}"
          {{- end }}
          imagePullPolicy: {{ .Values.crm.image.pullPolicy }}
          volumeMounts:
            - name: uploads
              mountPath: /uploads
#            - name: public-uploads
#              mountPath: /arhivare/public/uploads
            - name: app-logs
              mountPath: /application/logs
#            - name: media
#              mountPath: /media
            - name: sessions
              mountPath: /sessions
            - name: backup
              mountPath: /backup
            - name: contabo-crm-backup
              mountPath: /root/rclone
              readOnly: true  

          env:
            - name: APP_BASE_URL
              value: "https://{{ .Values.ingress.hostname }}/"
            - name: APP_DB_HOSTNAME
              value: {{ include "crm-chart.databaseHost" . | quote }}
            - name: APP_DB_USERNAME
              value: "{{ .Values.crm.db.username }}"
            - name: APP_DB_PASSWORD
              value: "{{ .Values.crm.db.password }}"
            - name: APP_DB_NAME
              value: "{{ .Values.crm.db.name }}"
            - name: ENROLL_EMAIL
              value: "{{ .Values.crm.app_env.enroll_email }}"
            - name: CONTACT_FN
              value: "{{ .Values.crm.app_env.contact_fn }}"
            - name: CONTACT_LN
              value: "{{ .Values.crm.app_env.contact_ln }}"
            - name: CRM_ADMIN_PASS
              value: "{{ .Values.crm.app_env.crm_admin_pass }}"
            - name: active_package
              value: "{{ .Values.crm.app_env.active_package }}"
            - name: HUB_TOKEN
              value: "{{ .Values.crm.app_env.hub_token }}"
            - name: APP_ENC_KEY
              value: "{{ .Values.crm.app_env.app_enc_key }}"
            - name: CI_ENV
              value: "{{ .Values.crm.app_env.ci_env }}"
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: {{ .Values.crm.ports.containerPort }}
              protocol: TCP
      initContainers:
      - name: init-wait-for-dependencies
        image: docker.io/kubeark/wait-for:latest
        command: ['sh', '-c', '/docker-entrypoint.sh wait_for mysql:{{ include "crm-chart.databaseHost" .}}']
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: uploads
  namespace: {{ .Values.global.namespace }}
spec:
  storageClassName: {{ .Values.global.storageClass }}
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
#apiVersion: v1
#kind: PersistentVolumeClaim
#metadata:
#  name: public-uploads
#  namespace: {{ .Values.global.namespace }}
#spec:
#  storageClassName: {{ .Values.global.storageClass }}
#  accessModes:
#  - ReadWriteMany
#  resources:
#    requests:
#      storage: 5Gi
#---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app-logs
  namespace: {{ .Values.global.namespace }}
spec:
  storageClassName: {{ .Values.global.storageClass }}
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Mi
---
#apiVersion: v1
#kind: PersistentVolumeClaim
#metadata:
#  name: media
#  namespace: {{ .Values.global.namespace }}
#spec:
#  storageClassName: {{ .Values.global.storageClass }}
#  accessModes:
#  - ReadWriteMany
#  resources:
#    requests:
#      storage: 1Gi
#---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sessions
  namespace: {{ .Values.global.namespace }}
spec:
  storageClassName: {{ .Values.global.storageClass }}
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 20Mi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data
  namespace: {{ .Values.global.namespace }}
spec:
  storageClassName: {{ .Values.global.storageClass }}
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: backup
  namespace: {{ .Values.global.namespace }}
spec:
  storageClassName: {{ .Values.global.storageClass }}
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi