{{- $name := include "karmada.name" . -}}
{{- $namespace := include "karmada.namespace" . -}}
{{- if eq .Values.installMode "host" }}
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ $name }}-post-install"
  namespace: {{ $namespace }}
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ $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": post-install
    "helm.sh/hook-weight": "0"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  parallelism: 1
  completions: 1
  template:
    metadata:
      name: {{ $name }}
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ $name | quote }}
        helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    spec:
      {{- with .Values.postInstallJob.tolerations}}
      tolerations:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.postInstallJob.nodeSelector }}
      nodeSelector:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      restartPolicy: Never
      containers:
      - name: post-install
        image: {{ template "karmada.kubectl.image" . }}
        imagePullPolicy: {{ .Values.kubectl.image.pullPolicy }}
        command:
        - /bin/sh
        - -c
        - |
          bash <<'EOF'
          set -ex
          kubectl apply -k /crds --kubeconfig /etc/kubeconfig
          kubectl apply -f /static-resources --kubeconfig /etc/kubeconfig
          EOF
        volumeMounts:
          - name: {{ $name }}-crds-kustomization
            mountPath: /crds
          - name: {{ $name }}-crds-patches
            mountPath: /crds/patches
          - name: {{ $name }}-crds-autoscaling-bases
            mountPath: /crds/bases/autoscaling
          - name: {{ $name }}-crds-config-bases
            mountPath: /crds/bases/config
          - name: {{ $name }}-crds-multicluster-bases
            mountPath: /crds/bases/multicluster
          - name: {{ $name }}-crds-networking-bases
            mountPath: /crds/bases/networking
          - name: {{ $name }}-crds-policy-bases
            mountPath: /crds/bases/policy
          - name: {{ $name }}-crds-work-bases
            mountPath: /crds/bases/work
          - name: {{ $name }}-static-resources
            mountPath: /static-resources
          {{ include "karmada.kubeconfig.volumeMount" . | nindent 10 }}
      volumes:
        - name: {{ $name }}-crds-kustomization
          configMap:
            name: {{ $name }}-crds-kustomization
        - name: {{ $name }}-crds-patches
          configMap:
            name: {{ $name }}-crds-patches
        - name: {{ $name }}-crds-autoscaling-bases
          configMap:
            name: {{ $name }}-crds-autoscaling-bases
        - name: {{ $name }}-crds-config-bases
          configMap:
            name: {{ $name }}-crds-config-bases
        - name: {{ $name }}-crds-multicluster-bases
          configMap:
            name: {{ $name }}-crds-multicluster-bases
        - name: {{ $name }}-crds-networking-bases
          configMap:
            name: {{ $name }}-crds-networking-bases
        - name: {{ $name }}-crds-policy-bases
          configMap:
            name: {{ $name }}-crds-policy-bases
        - name: {{ $name }}-crds-work-bases
          configMap:
            name: {{ $name }}-crds-work-bases
        - name: {{ $name }}-static-resources
          configMap:
            name: {{ $name }}-static-resources
        {{ include "karmada.kubeconfig.volume" . | nindent 8 }}
{{- end }}
