{{- if .Values.operator.upgradeCRD }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: gpu-operator-upgrade-crd-hook-sa
  annotations:
    helm.sh/hook: pre-upgrade
    helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
    helm.sh/hook-weight: "0"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: gpu-operator-upgrade-crd-hook-role
  annotations:
    helm.sh/hook: pre-upgrade
    helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
    helm.sh/hook-weight: "0"
rules:
  - apiGroups:
      - apiextensions.k8s.io
    resources:
      - customresourcedefinitions
    verbs:
      - create
      - get
      - list
      - watch
      - patch
      - update
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: gpu-operator-upgrade-crd-hook-binding
  annotations:
    helm.sh/hook: pre-upgrade
    helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
    helm.sh/hook-weight: "0"
subjects:
  - kind: ServiceAccount
    name: gpu-operator-upgrade-crd-hook-sa
    namespace: {{ .Release.Namespace }}
roleRef:
  kind: ClusterRole
  name: gpu-operator-upgrade-crd-hook-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1
kind: Job
metadata:
  name: gpu-operator-upgrade-crd
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": pre-upgrade
    "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-upgrade-crd
      labels:
        {{- include "gpu-operator.labels" . | nindent 8 }}
        app.kubernetes.io/component: "gpu-operator"
    spec:
      serviceAccountName: gpu-operator-upgrade-crd-hook-sa
      {{- if .Values.operator.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.operator.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      {{- with .Values.operator.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: upgrade-crd
          image: {{ include "gpu-operator.fullimage" . }}
          imagePullPolicy: {{ .Values.operator.imagePullPolicy }}
          command:
          - /bin/sh
          - -c
          - >
            kubectl apply -f /opt/gpu-operator/nvidia.com_clusterpolicies.yaml;
            kubectl apply -f /opt/gpu-operator/nvidia.com_nvidiadrivers.yaml;
        {{- if .Values.nfd.enabled }}
            kubectl apply -f /opt/gpu-operator/nfd-api-crds.yaml;
        {{- end }}
      restartPolicy: OnFailure
{{- end }}
