apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ template "cdi.crUninstallHook.name" . }}
  {{ template "cdi.crUninstallHook.annotations" (dict "hookWeight" 1) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ template "cdi.crUninstallHook.name" . }}
  {{ template "cdi.crUninstallHook.annotations" (dict "hookWeight" 1) }}
rules:
  - apiGroups: [ "cdi.kubevirt.io" ]
    resources: [ "cdis" ]
    resourceNames:
      - "cdi"
    verbs: [ "delete" ]
  - apiGroups: [ "apps" ]
    resources: [ "deployments" ]
    verbs: [ "get", "list" ]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ template "cdi.crUninstallHook.name" . }}
  {{ template "cdi.crUninstallHook.annotations" (dict "hookWeight" 2) }}
subjects:
  - kind: ServiceAccount
    namespace: {{ .Release.Namespace }}
    name: {{ template "cdi.crUninstallHook.name" . }}
roleRef:
  kind: ClusterRole
  name: {{ template "cdi.crUninstallHook.name" . }}
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1
kind: Job
metadata:
  namespace: {{ .Release.Namespace }}
  name: {{ template "cdi.crUninstallHook.name" . }}
  {{ template "cdi.crUninstallHook.annotations" (dict "hookWeight" 3) }}
spec:
  template:
    metadata:
      name: {{ template "cdi.crUninstallHook.name" . }}
    spec:
      serviceAccountName: {{ template "cdi.crUninstallHook.name" . }}
      restartPolicy: {{ .Values.hookRestartPolicy }}
      containers:
        - name: {{ template "cdi.crUninstallHook.name" . }}
          image: {{ .Values.hookImage }}
          securityContext:
            {{- toYaml .Values.hookSecurityContext | nindent 12 }}
          args:
            - delete
            - cdi
            - cdi
        - name: {{ template "cdi.crUninstallHook.name" . }}-cleanup
          image: {{ .Values.hookImage }}
          securityContext:
            {{- toYaml .Values.hookSecurityContext | nindent 12 }}
          args:
            - wait
            - --for=delete
            - deployments/cdi-apiserver
            - deployments/cdi-deployment
            - deployments/cdi-uploadproxy
            - --timeout=60s