{{- if .Values.dataintegration.enabled }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "cmem.name-di" . }}
  labels:
    {{- include "cmem.labels-di" . | nindent 4 }}
spec:
  {{- if not .Values.dataintegration.autoscaling.enabled }}
  replicas: {{ .Values.dataintegration.replicas }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "cmem.selectorLabels-di" . | nindent 6 }}
  updateStrategy: {{ toYaml .Values.dataintegration.updateStrategy | nindent 4 }}
  template:
    metadata:
      annotations:
        checksum/configFiles: {{ include (print $.Template.BasePath "/di-configmap-files.yaml") . | sha256sum }}
        checksum/configEnvs: {{ include (print $.Template.BasePath "/di-configmap.yaml") . | sha256sum }}
        checksum/secretEnvs: {{ include (print $.Template.BasePath "/di-secret.yaml") . | sha256sum }}
        {{- if .Values.global.customCACerts }}
        checksum/configCustomCACert: {{ include (print $.Template.BasePath "/custom-cacert.yaml") . | sha256sum }}
        {{- end }}
      {{- with .Values.dataintegration.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cmem.selectorLabels-di" . | nindent 8 }}
        {{- with .Values.global.additionalPodLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      automountServiceAccountToken: {{ .Values.dataintegration.automountServiceAccountToken | default false }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.dataintegration.podSecurityContext | nindent 8 }}
      {{- if .Values.dataintegration.initContainer.enabled }}
      initContainers:
        {{- if .Values.dataintegration.initContainer.provisioningData.enabled }}
        - name: provisioning-data
          image: {{ include "cmem.image" (dict "context" . "name" "dataintegration" "values" .Values.dataintegration.image) }}
          imagePullPolicy: {{ .Values.dataintegration.image.pullPolicy }}
          command:
            {{- if .Values.dataintegration.initContainer.provisioningData.command }}
            {{- toYaml .Values.dataintegration.initContainer.provisioningData.command | nindent 12 }}
            {{- else }}
            - bash
            - -c
            - |
              set -x
              echo "create plugin-folder"
              mkdir -p {{ .Values.dataintegration.config.pluginFolder }}
              echo "create python-plugin-folder"
              mkdir -p {{ .Values.dataintegration.config.pythonPluginFolder }}
              {{- if eq .Values.dataintegration.config.executionReportManager.plugin "file" }}
              echo "create reports-folder"
              mkdir -p {{ .Values.dataintegration.config.executionReportManager.folder }}
              {{- end }}
              ls -la {{ .Values.dataintegration.config.home }}
            {{- end}}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.dataintegration.config.home }}
          {{- if .Values.global.initContainerResources }}
          resources: {{ toYaml .Values.global.initContainerResources | nindent 12 }}
          {{- end }}
          {{- with .Values.dataintegration.initContainer.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.dataintegration.initContainer.pythonPluginImageInstall.enabled }}
        - name: python-image-install
          image: {{ include "cmem.image" (dict "context" . "name" "dataintegration-python-plugins" "values" .Values.dataintegration.initContainer.pythonPluginImageInstall.image) }}
          imagePullPolicy: {{ .Values.dataintegration.initContainer.pythonPluginImageInstall.image.pullPolicy }}
          command:
            {{- if .Values.dataintegration.initContainer.pythonPluginImageInstall.command }}
            {{- toYaml .Values.dataintegration.initContainer.pythonPluginImageInstall.command | nindent 12 }}
            {{- else }}
            - bash
            - -c
            - |
              ls -lah /data/python-packages-install/
              rm -rf /python-packages-local{{ .Values.dataintegration.config.pythonPluginFolder }}/* 
              cp -Rv /data/python-packages/* /python-packages-local{{ .Values.dataintegration.config.pythonPluginFolder }}
            {{- end}}
          volumeMounts:
            - name: data
              mountPath: /python-packages-local{{ .Values.dataintegration.config.home }}
          {{- with .Values.dataintegration.initContainer.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.global.customCACerts }}
        {{- if .Values.dataintegration.initContainer.customCACerts.enabled }}
        - name: add-custom-cacert
          image: {{ include "cmem.image" (dict "context" . "name" "dataintegration" "values" .Values.dataintegration.image) }}
          imagePullPolicy: {{ .Values.dataintegration.image.pullPolicy }}
          command:
            {{- if .Values.dataintegration.initContainer.customCACerts.command }}
            {{- toYaml .Values.dataintegration.initContainer.customCACerts.command | nindent 12 }}
            {{- else }}
            - bash
            - -c
            - |
              set -x
              id
              {{- range $i, $cert := .Values.global.customCACerts }}
              cp /custom-cacert/{{ $i }}.crt /cacerts/{{ $i }}.crt
              {{- end }}
              ls -laF /cacerts /custom-cacert
              echo "Copy Python certs"
              cp -pur {{ include "cmem.di-ca-bundle-path" . }} /cacerts/pythoncacerts
              cat /custom-cacert/*.crt >> /cacerts/pythoncacerts
            {{- end }}
          volumeMounts:
            - name: custom-cacert
              mountPath: /custom-cacert
            - name: cacerts
              mountPath: /cacerts
          {{- if .Values.global.initContainerResources }}
          resources: {{ toYaml .Values.global.initContainerResources | nindent 12 }}
          {{- end }}
          {{- with .Values.dataintegration.initContainer.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- end }}
        {{- if .Values.dataintegration.customInitContainers }}
        {{ toYaml .Values.dataintegration.customInitContainers | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        {{- with .Values.dataintegration.extraContainers }}
          {{ tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
        - name: dataintegration
          image: {{ include "cmem.image" (dict "context" . "name" "dataintegration" "values" .Values.dataintegration.image) }}
          imagePullPolicy: {{ .Values.dataintegration.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.dataintegration.service.port }}
            {{- if .Values.dataintegration.config.metrics.enabled }}
            - name: metrics
              containerPort: {{ .Values.dataintegration.config.metrics.port }}
            {{- end }}
          envFrom:
          - configMapRef:
              name: {{ template "cmem.name-di" . }}
          - secretRef:
              name: {{ template "cmem.name-di" . }}
          volumeMounts:
            {{- if .Values.dataintegration.cache.existentPvc.enabled }}
            - name: data
            {{ else }}
            - name: cache
            {{- end }}
              mountPath: {{ .Values.dataintegration.cache.cacheBasePath }}
            - name: tmp
              mountPath: "/tmp"
              subPath: tmp
            - name: tmp
              mountPath: "{{ .Values.dataintegration.tmp.silkPath }}"
              subPath: silk
            - name: tmp
              mountPath: "/home/cmem/.cache"
              subPath: cmem-cache
            - name: tmp
              mountPath: "/home/cmem/.local"
              subPath: cmem-local
            {{- if .Values.dataintegration.persistence.enabled }}
            - name: data
              mountPath: {{ .Values.dataintegration.config.home }}
              {{- if .Values.dataintegration.persistence.subPath }}
              subPath: {{ .Values.dataintegration.persistence.subPath }}
              {{- end }}
            {{- end }}
            - name: config-files-volume
              mountPath: /opt/cmem/eccenca-DataIntegration/dist/etc/dataintegration/conf
              readOnly: true
            - name: cacerts
              mountPath: /cacerts
            {{- if .Values.global.customCACerts }}
            - name: custom-cacert
              mountPath: /custom-cacert
            {{- end }}
            {{- if .Values.dataintegration.customMounts }}
            {{ toYaml .Values.dataintegration.customMounts | nindent 12 }}
            {{- end }}
          {{- if .Values.dataintegration.customCommand }}
          command:
            {{ toYaml .Values.dataintegration.customCommand | nindent 12 }}
          {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.dataintegration.config.contextPath }}/health
              port: {{ .Values.dataintegration.service.port }}
            initialDelaySeconds: {{ .Values.dataintegration.probes.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.dataintegration.probes.livenessProbe.periodSeconds }}
            failureThreshold: {{ .Values.dataintegration.probes.livenessProbe.failureThreshold }}
          readinessProbe:
            httpGet:
              path: {{ .Values.dataintegration.config.contextPath }}/health
              port: {{ .Values.dataintegration.service.port }}
            initialDelaySeconds: {{ .Values.dataintegration.probes.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.dataintegration.probes.readinessProbe.periodSeconds }}
            failureThreshold: {{ .Values.dataintegration.probes.readinessProbe.failureThreshold }}
          {{- if .Values.dataintegration.resources }}
          resources: {{ toYaml .Values.dataintegration.resources | nindent 12 }}
          {{- end }}
          {{- with .Values.dataintegration.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
      restartPolicy: Always
      volumes:
      - name: tmp
      {{- if .Values.dataintegration.tmp.pvcenabled }}
        persistentVolumeClaim:
          claimName: {{ template "cmem.name-di" . }}-tmp
      {{- else }}
        emptyDir:
          sizeLimit: {{ .Values.dataintegration.tmp.cacheSize }}
      {{- end }}
      {{- if not .Values.dataintegration.cache.existentPvc.enabled }}
      - name: cache
      {{- if .Values.dataintegration.cache.pvcenabled }}
        persistentVolumeClaim:
          claimName: {{ template "cmem.name-di" . }}-cache
      {{- else }}
        emptyDir:
          sizeLimit: {{ .Values.dataintegration.cache.cacheSize }}
      {{- end }}
      {{- end }}
      {{- if .Values.dataintegration.persistence.enabled }}
      - name: data
        persistentVolumeClaim:
          {{- if .Values.dataintegration.persistence.existentPvc.enabled }}
          claimName: {{ .Values.dataintegration.persistence.existentPvc.name }}
          {{- else }}
          claimName: {{ template "cmem.name-di" . }}-data
          {{- end }}
      {{- end }}
      {{- if .Values.global.customCACerts }}
      - name: custom-cacert
        configMap:
          name: {{ template "cmem.name" . }}-custom-cacert
      {{- end }}
      - name: cacerts
        emptyDir: {}
      - name: config-files-volume
        configMap:
          name: {{ template "cmem.name-di" . }}-files
      {{- if .Values.dataintegration.initContainer.pythonPluginImageInstall.enabled }}
      - name: pythonpluginemptydir
        emptyDir:
          sizeLimit: {{ .Values.dataintegration.initContainer.pythonPluginImageInstall.emptyDirSize }}
      {{- end }}
      {{- if .Values.dataintegration.customVolumes }}
      {{ toYaml .Values.dataintegration.customVolumes | nindent 6 }}
      {{- end }}
      {{- if .Values.dataintegration.nodeSelector }}
      nodeSelector: {{ toYaml .Values.dataintegration.nodeSelector | nindent 8 }}
      {{- end }}
      {{- with .Values.dataintegration.tolerations }}
      tolerations: {{- tpl (toYaml .) $ | nindent 8 }}
      {{- end }}
      {{- if .Values.dataintegration.affinity }}
      affinity: {{ toYaml .Values.dataintegration.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.dataintegration.extraHosts }}
      hostAliases: {{ toYaml .Values.dataintegration.extraHosts | nindent 8 }}
      {{- end }}
      {{- if .Values.dataintegration.serviceAccount.enabled }}
      serviceAccount: {{ toYaml .Values.dataintegration.serviceAccount.name | quote }}
      {{- end }}
{{- end }}
