{{- if .Values.operator.cleanupCRD }}
apiVersion: batch/v1
kind: Job
metadata:
  name: gpu-operator-cleanup-crd
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-weight": "1"
    "helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation
  labels:
    {{- include "gpu-operator.labels" . | nindent 4 }}
    app.kubernetes.io/component: "gpu-operator"
spec:
  template:
    metadata:
      name: gpu-operator-cleanup-crd
      labels:
        {{- include "gpu-operator.labels" . | nindent 8 }}
        app.kubernetes.io/component: "gpu-operator"
    spec:
      serviceAccountName: gpu-operator
      {{- if .Values.operator.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.operator.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      {{- with .Values.operator.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: cleanup-crd
          image: {{ include "gpu-operator.fullimage" . }}
          imagePullPolicy: {{ .Values.operator.imagePullPolicy }}
          command:
          - /bin/sh
          - -c
          - >
              kubectl delete clusterpolicy cluster-policy;
              kubectl delete crd clusterpolicies.nvidia.com;
              kubectl delete crd nvidiadrivers.nvidia.com --ignore-not-found=true;
            {{- if .Values.nfd.enabled -}}
              kubectl delete crd nodefeatures.nfd.k8s-sigs.io --ignore-not-found=true;
              kubectl delete crd nodefeaturegroups.nfd.k8s-sigs.io --ignore-not-found=true;
              kubectl delete crd nodefeaturerules.nfd.k8s-sigs.io --ignore-not-found=true;
            {{- end }}
      restartPolicy: OnFailure
{{- end }}
