{{ if .Values.pre_install_hook.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: "preinstall-check"
#  labels:
#    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
#    app.kubernetes.io/instance: {{ .Release.Name | quote }}
#    app.kubernetes.io/version: {{ .Chart.AppVersion }}
#    helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": pre-install,pre-upgrade
#    "helm.sh/hook-weight": "-5"
#    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  ttlSecondsAfterFinished: 6000000
  template:
#    metadata:
#      name: "{{ .Release.Name }}"
#      labels:
#        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
#        app.kubernetes.io/instance: {{ .Release.Name | quote }}
#        helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    spec:
      restartPolicy: Never
      containers:
      - name: post-install-job
        image: {{.Values.pre_install_hook.image}}
        volumeMounts:
        - mountPath: /customer
          name: customer
#          subPath: /

#        volumeMounts:
#        - mountPath: /customer
#          name: customer
        env:
{{ if .Values.local_user_id }}
          - name: LOCAL_USER_ID
            value: "{{.Values.local_user_id}}"
{{ end }}
{{ if .Values.local_user_name }}
          - name: LOCAL_USER_NAME
            value: "{{.Values.local_user_name}}"
{{ end }}
        command: ["entrypoint.sh", "bash", "-c"]
        args:
          - NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace) &&
            kubectl --namespace $NAMESPACE get secret secret-service &&
            kubectl --namespace $NAMESPACE get secret secret-admin &&
            sudo chown $LOCAL_USER_NAME /customer &&
            mkdir -p /customer/data &&
{{ if .Values.front.enabled }}
            mkdir -p /customer/data/static &&
{{ end }}
{{ if .Values.coder.enabled }}
            mkdir -p /customer/data/coder &&
{{ end }}
{{ if .Values.packman.enabled }}
            mkdir -p /customer/data/packman &&
{{ end }}
{{ if .Values.jupyter.enabled }}
            mkdir -p /customer/data/jupyter &&
{{ end }}
{{ if .Values.jupyterhub.enabled }}
            mkdir -p /customer/data/jupyter/hub/users &&
            mkdir -p /customer/data/jupyter/hub/common &&
{{ end }}
{{ if .Values.retail.enabled }}
            mkdir -p /customer/data/storage &&
            mkdir -p /customer/in/int/retail/.cache &&
{{ end }}
            mkdir -p /customer/in/int/modeling &&
{{ if .Values.server_api.enabled }}
            mkdir -p /customer/data/server-api &&
{{ end }}
{{ if .Values.api_router.enabled }}
            mkdir -p /customer/data/api-router &&
{{ end }}
{{ if .Values.api_router_schema_updater.enabled }}
            mkdir -p /customer/data/api-router &&
{{ end }}
{{ if .Values.assets_inlet.enabled }}
            mkdir -p /customer/data/assets-inlet &&
{{ end }}
{{ if .Values.camunda2.enabled }}
            kubectl --namespace $NAMESPACE get secret camunda-db-credentials &&
{{ if eq "postgres" .Values.camunda2.camunda_db }}
            mkdir -p /customer/camunda/postgres &&
{{ else if eq "mysql" .Values.camunda2.camunda_db }}
            mkdir -p /customer/camunda/mysql &&
{{ end }}
{{ end }}
{{ if .Values.pim20_data_loader.enabled }}
            mkdir -p /customer/out/ext &&
{{ end }}
{{ if .Values.loki_stack.enabled }}
            mkdir -p /customer/data/loki-stack &&
            sudo chmod -R 777 /customer/data/loki-stack
{{ end }}
            echo "iuhiuhi" > /tmp/termination-log
        terminationMessagePolicy: "File"
        terminationMessagePath: "/tmp/termination-log"
      volumes:
      - name: customer
        {{ include "app.customer_volume" . | nindent 8 }}
      imagePullSecrets:
      - name: regcred
      {{ include "app.affinity" . | nindent 6}}
  backoffLimit: 0
{{ end }}