apiVersion: v1
kind: ServiceAccount
metadata:
  name: post-upgrade-job
  namespace: '{{ .Values.rancherTurtles.namespace }}'
  annotations:
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "1"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: post-upgrade-job-delete-capi-operator-resources
  annotations:
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "1"
rules:
- apiGroups:
  - operator.cluster.x-k8s.io
  resources:
  - addonproviders
  - bootstrapproviders
  - controlplaneproviders
  - coreproviders
  - infrastructureproviders
  - ipamproviders
  - runtimeextensionproviders
  verbs:
  - get
  - watch
  - list
  - delete
  - patch
- apiGroups:
  - apiextensions.k8s.io
  resources:
  - customresourcedefinitions
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: post-upgrade-job-capi-operator-resources-cleanup
  annotations:
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "1"
subjects:
  - kind: ServiceAccount
    name: post-upgrade-job
    namespace: '{{ .Values.rancherTurtles.namespace }}'
roleRef:
  kind: ClusterRole
  name: post-upgrade-job-delete-capi-operator-resources
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cluster-api-operator-resources-cleanup-script
  namespace: '{{ .Values.rancherTurtles.namespace }}'
  annotations:
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "1"
data:
  cleanup.sh: |
    #!/usr/bin/env bash

    set -euo pipefail

    remove_finalizers_and_delete() {
      local resource_type="$1"
      if kubectl get crd $resource_type > /dev/null 2>&1; then
        kubectl get $resource_type --all-namespaces --no-headers --ignore-not-found | awk '{print $1 " " $2}' | xargs -r -n2 bash -c 'kubectl patch '"${resource_type}"' "$1" -n "$0" --type merge -p "{\"metadata\":{\"finalizers\":null}}"'
        kubectl delete $resource_type --all --all-namespaces
      else
        echo "Resource type $resource_type does not exist, skipping cleanup."
      fi
    }

    resource_types=(
      "addonproviders.operator.cluster.x-k8s.io"
      "bootstrapproviders.operator.cluster.x-k8s.io"
      "controlplaneproviders.operator.cluster.x-k8s.io"
      "coreproviders.operator.cluster.x-k8s.io"
      "infrastructureproviders.operator.cluster.x-k8s.io"
      "ipamproviders.operator.cluster.x-k8s.io"
      "runtimeextensionproviders.operator.cluster.x-k8s.io"
    )

    for resource_type in "${resource_types[@]}"; do
      remove_finalizers_and_delete "$resource_type"
    done
---
apiVersion: batch/v1
kind: Job
metadata:
  name: cluster-api-operator-resources-cleanup
  namespace: '{{ .Values.rancherTurtles.namespace }}'
  annotations:
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "2"
spec:
  ttlSecondsAfterFinished: 300
  template:
    spec:
      serviceAccountName: post-upgrade-job
      containers:
        - name: cluster-api-operator-resources-cleanup
          image: {{ index .Values "rancherTurtles" "shellImage" }}
          command: ["/bin/bash"]
          args:
          - "-c"
          - "/scripts/cleanup.sh"
          volumeMounts:
            - name: script
              mountPath: /scripts
      volumes:
        - name: script
          configMap:
            name: cluster-api-operator-resources-cleanup-script
            defaultMode: 0777
      restartPolicy: Never
---