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