apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "erp.fullname" . }}
  labels:
    {{- include "erp.labels" . | nindent 4 }}
  annotations:
    configmap-hash: {{ include (print .Template.BasePath "/config.yaml") . | sha256sum }}
    configmap2-hash: {{ include (print .Template.BasePath "/scripts-config.yaml") . | sha256sum }}
    configmap3-hash: {{ include (print .Template.BasePath "/script-entrypoint-config.yaml") . | sha256sum }}
spec:
  revisionHistoryLimit: 5
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "erp.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "erp.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "erp.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          command: ["/bin/bash", "/usr/local/bin/custom-entrypoint.sh", "apache2-foreground"]
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
            - configMapRef:
                name: {{ include "erp.name" . }}-config
            - secretRef:
                name: secretkv
          env:
            - name: DOLI_DB_USER
              valueFrom:
                secretKeyRef:
                  name: vso-db-credentials
                  key: username
            - name: DOLI_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: vso-db-credentials
                  key: password
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.livenessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - name: dolibarr
            mountPath: /var/www/documents
            subPath: documents
          - name: dolibarr
            mountPath: /var/www/html/custom
            subPath: custom
          - name: dolibarr
            mountPath: /var/backups
            subPath: backups
          - name: before-start-scripts
            mountPath: /var/www/scripts/before-starting.d
          - name: custom-entrypoint-script
            mountPath: /usr/local/bin/custom-entrypoint.sh
            subPath: custom-entrypoint.sh
      volumes:
      - name: dolibarr
        persistentVolumeClaim:
          claimName: {{ include "erp.fullname" . }}
      - name: before-start-scripts
        configMap:
          name: dolibarr-before-start-scripts
      - name: custom-entrypoint-script
        configMap:
          name: dolibarr-custom-entrypoint-script
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
