{{- if .Values.bootstrap.enabled }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: cmemc-bootstrap
  labels:
    {{- include "cmem.labels" . | nindent 4 }}
spec:
  template:
    metadata:
      labels:
        {{- include "cmem.labels" . | nindent 8 }}
        {{- with .Values.global.additionalPodLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- if .Values.bootstrap.force }}
      annotations:
        cmem.rand.forceBootstrap: {{ randAlphaNum 24 | nospace }}
      {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.bootstrap.podSecurityContext | nindent 8 }}
      containers:
        - name: cmemc
          image: {{ include "cmem.image" (dict "context" . "name" "cmemc" "values" .Values.bootstrap.image) }}
          imagePullPolicy: {{ .Values.bootstrap.image.pullPolicy }}
          command:
            - /bin/bash
            - -c
          args:
            - /opt/bootstrap.sh
          volumeMounts:
            - name: bootstrap-files-volume
              mountPath: /opt/
              readOnly: true
            - name: cmemconfig
              mountPath: /home/cmem/.config
              readOnly: false
            {{- if .Values.global.customCACerts }}
            - name: custom-cacert
              mountPath: /custom-cacert/cm
            - name: cacert
              mountPath: /custom-cacert
            {{- end }}
          {{- if .Values.bootstrap.resources }}
          resources: {{ toYaml .Values.bootstrap.resources | nindent 12 }}
          {{- end }}
          {{- with .Values.bootstrap.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
          env:
          - name: CMEM_BASE_URI
            valueFrom:
              configMapKeyRef:
                name: dataintegration
                key: DEPLOY_BASE_URL
          {{- if $.Values.global.disablePythonInstallFromOutside }}
          - name: DI_API_ENDPOINT
            value: "http://{{ include "cmem.servicename-di" . }}:{{ .Values.dataintegration.service.port }}/dataintegration"
          {{- end }}
          - name: KEYCLOAK_BASE_URI
            value: {{ .Values.global.keycloakBaseUrl | quote }}
          - name: OAUTH_TOKEN_URI
            valueFrom:
              configMapKeyRef:
                name: dataintegration
                key: OAUTH_TOKEN_URL
          - name: OAUTH_GRANT_TYPE
            value: client_credentials
          - name: OAUTH_CLIENT_ID
            valueFrom:
              secretKeyRef:
                name: dataintegration
                key: DATAINTEGRATION_CMEM_SERVICE_CLIENT
          - name: OAUTH_CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: dataintegration
                key: DATAINTEGRATION_CMEM_SERVICE_CLIENT_SECRET
          - name: SSL_VERIFY
            value: "{{ .Values.bootstrap.sslVerify }}"
          {{- if or (eq (printf "%v" .Values.bootstrap.sslVerify) "true") (eq (printf "%v" .Values.bootstrap.sslVerify) "True") }}
          {{- if .Values.global.customCACerts }}
          - name: REQUESTS_CA_BUNDLE
            value: /custom-cacert/customcacerts.pem
          {{- end }}
          {{- end }}
          {{- if .Values.bootstrap.defaultUser.enabled }}
          - name: NEW_USER_NAME
            valueFrom:
              secretKeyRef:
                name: cmemc-bootstrap-default-user-credentials
                key: username
          - name: NEW_USER_PASSWORD
            valueFrom:
              secretKeyRef:
                name: cmemc-bootstrap-default-user-credentials
                key: password
          - name: NEW_USER_EMAIL
            valueFrom:
              secretKeyRef:
                name: cmemc-bootstrap-default-user-credentials
                key: email
          {{- end }}
      restartPolicy: Never
      volumes:
        - name: bootstrap-files-volume
          configMap:
            name: {{ .Values.bootstrap.configMapName | default "cmemc-bootstrap-sh-files" }}
            defaultMode: 0777
        - name: cmemconfig
          emptyDir:
            sizeLimit: 1M
        {{- if .Values.global.customCACerts }}
        - name: custom-cacert
          configMap:
            name: {{ template "cmem.name" . }}-custom-cacert
        - name: cacert
          emptyDir:
            sizeLimit: 10M
        {{- end }}
      {{- if .Values.bootstrap.extraHosts }}
      hostAliases: {{ toYaml .Values.bootstrap.extraHosts | nindent 8 }}
      {{- end }}
      {{- with .Values.bootstrap.nodeSelector }}
      nodeSelector: {{- tpl (toYaml .) $ | nindent 8 }}
      {{- end }}
      {{- with .Values.bootstrap.tolerations }}
      tolerations: {{- tpl (toYaml .) $ | nindent 8 }}
      {{- end }}
      {{- if .Values.bootstrap.affinity }}
      affinity: {{ toYaml .Values.bootstrap.affinity | nindent 8 }}
      {{- end }}
  backoffLimit: 3
  activeDeadlineSeconds: {{ .Values.bootstrap.activeDeadlineSeconds | default "600" }}
  completions: 1
  parallelism: 1
  ttlSecondsAfterFinished: {{ .Values.bootstrap.ttlSecondsAfterFinished | default "300" }}
{{- if .Values.bootstrap.defaultUser.enabled }}
---
apiVersion: v1
kind: Secret
metadata:
  name: cmemc-bootstrap-default-user-credentials
  labels:
    {{- include "cmem.labels" . | nindent 4 }}
type: Opaque
stringData:
  username: {{ .Values.bootstrap.defaultUser.name }}
  password: {{ .Values.bootstrap.defaultUser.password }}
  email: {{ .Values.bootstrap.defaultUser.email }}
{{- end }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cmemc-bootstrap-sh-files
  labels:
    {{- include "cmem.labels" . | nindent 4 }}
data:
  bootstrap.sh: |
    {{ tpl (.Files.Get "configuration-files/bootstrap.sh") . | nindent 4 }}
{{- end }}