{{- if index .Values "rancherTurtles" "rancherInstalled" }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: pre-delete-job
  namespace: '{{ .Values.rancherTurtles.namespace }}'
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-weight": "-2"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: pre-delete-job-delete-capiproviders
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-weight": "-2"
rules:
- apiGroups:
  - turtles-capi.cattle.io
  resources:
  - capiproviders
  verbs:
  - list
  - delete
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: pre-delete-job-capiprovider-cleanup
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-weight": "-2"
subjects:
  - kind: ServiceAccount
    name: pre-delete-job
    namespace: '{{ .Values.rancherTurtles.namespace }}'
roleRef:
  kind: ClusterRole
  name: pre-delete-job-delete-capiproviders
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1
kind: Job
metadata:
  name: rancher-capiprovider-cleanup
  namespace: '{{ .Values.rancherTurtles.namespace }}'
  annotations:
    "helm.sh/hook": pre-delete
    "helm.sh/hook-weight": "-1"
spec:
  ttlSecondsAfterFinished: 300
  template:
    spec:
      serviceAccountName: pre-delete-job
      containers:
        - name: rancher-capiprovider-cleanup
          image: {{ index .Values "rancherTurtles" "kubectlImage" }}
          args:
          - delete
          - capiproviders
          - -A
          - --all
          - --cascade=foreground
      restartPolicy: Never
{{- end }}
