Skip to content

Commit 2d7e6c2

Browse files
twilight0620STARRY-S
authored andcommitted
fix: add pre-delete-hook to delete clusterset when installation type is global
1 parent b164a69 commit 2d7e6c2

8 files changed

Lines changed: 212 additions & 2 deletions

File tree

1.09 KB
Binary file not shown.

charts/mcs-ext-chart/0.0.1/charts/mcs-addon/templates/controller-rbac-global.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rules:
1919
- watch
2020
- update
2121
- patch
22+
- delete
2223
- apiGroups: [ rbac.authorization.k8s.io ]
2324
resources:
2425
- rolebindings
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{{- if eq .Values.global.installationType "global" }}
2+
{{- if .Values.cleanupOnUninstall | default true }}
3+
apiVersion: batch/v1
4+
kind: Job
5+
metadata:
6+
name: mcs-addon-pre-delete-cleanup-clusterset
7+
namespace: {{ .Release.Namespace }}
8+
annotations:
9+
"helm.sh/hook": pre-delete
10+
"helm.sh/hook-weight": "-5"
11+
"helm.sh/hook-delete-policy": hook-succeeded,hook-failed
12+
labels:
13+
{{- include "mcs-addon.chart_labels" . | nindent 4 }}
14+
component: cleanup
15+
spec:
16+
activeDeadlineSeconds: {{ .Values.cleanupTimeout | default 300 }}
17+
backoffLimit: 2
18+
template:
19+
metadata:
20+
labels:
21+
{{- include "mcs-addon.chart_labels" . | nindent 8 }}
22+
component: cleanup
23+
spec:
24+
serviceAccountName: {{ template "mcs-addon.component_name" (dict "Chart" .Chart "componentName" "controller" "Release" .Release "Values" .Values) }}
25+
restartPolicy: Never
26+
containers:
27+
- name: cleanup-clusterset-crd
28+
image: "{{ template "system_default_registry" . }}{{ .Values.kubectl.repository }}:{{ .Values.kubectl.tag }}"
29+
imagePullPolicy: IfNotPresent
30+
command:
31+
- /bin/sh
32+
- -c
33+
- |
34+
set -e
35+
CRD_NAME="clustersets.multicluster.pandaria.io"
36+
DELETE_TIMEOUT={{ .Values.cleanupTimeout | default 300 }}
37+
38+
echo "===================================================================="
39+
echo "NOTE: Deleting ClusterSet CRD will automatically delete all ClusterSet resources"
40+
echo "Delete Timeout: ${DELETE_TIMEOUT}s"
41+
echo "===================================================================="
42+
43+
# Check if CRD exists
44+
if ! kubectl get crd "$CRD_NAME" &>/dev/null; then
45+
echo "CRD '$CRD_NAME' not found, nothing to clean up."
46+
exit 0
47+
fi
48+
49+
echo ""
50+
echo "CRD '$CRD_NAME' exists, listing ClusterSet resources..."
51+
CLUSTERSETS=$(kubectl get clustersets -o jsonpath='{.items[*].metadata.name}' 2>/dev/null || echo "")
52+
if [ -n "$CLUSTERSETS" ]; then
53+
echo "Found ClusterSet resources: $CLUSTERSETS"
54+
echo "These will be automatically deleted when CRD is removed."
55+
else
56+
echo "No ClusterSet resources found."
57+
fi
58+
59+
echo ""
60+
echo "--- Deleting CRD: $CRD_NAME ---"
61+
echo "NOTE: ClusterSet resources do not use finalizers, deletion will be immediate."
62+
63+
# Delete CRD with timeout
64+
echo "Deleting CRD..."
65+
if kubectl delete crd "$CRD_NAME" --timeout=60s --ignore-not-found=true 2>&1; then
66+
echo "CRD '$CRD_NAME' deleted successfully."
67+
else
68+
echo "WARNING: CRD deletion failed or timed out, attempting force cleanup..."
69+
kubectl delete crd "$CRD_NAME" \
70+
--force \
71+
--grace-period=0 \
72+
--ignore-not-found=true 2>&1 || echo "Warning: Force delete failed"
73+
fi
74+
75+
# Give controller a few seconds to process OnRemove handlers
76+
echo ""
77+
echo "Waiting 5 seconds for controller to process cleanup handlers..."
78+
sleep 5
79+
80+
# Verify CRD is deleted
81+
if kubectl get crd "$CRD_NAME" &>/dev/null; then
82+
echo "WARNING: CRD '$CRD_NAME' still exists!"
83+
echo "Manual cleanup may be required: kubectl delete crd $CRD_NAME"
84+
else
85+
echo "CRD '$CRD_NAME' successfully removed."
86+
fi
87+
88+
echo ""
89+
echo "===================================================================="
90+
echo "Pre-delete cleanup hook completed"
91+
echo "===================================================================="
92+
93+
# Always exit 0 to allow Helm uninstall to proceed
94+
exit 0
95+
{{- end }}
96+
{{- end }}

charts/mcs-ext-chart/0.0.1/charts/mcs-addon/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ global:
5353
cattle:
5454
systemDefaultRegistry: ""
5555
provisioningNamespace: "fleet-default"
56+
# Cleanup settings for Helm hooks
57+
cleanupOnUninstall: true
58+
# Cleanup timeout settings (in seconds)
59+
cleanupTimeout: 300
60+
# Kubectl image for cleanup hooks (must have shell and kubectl)
61+
kubectl:
62+
repository: "cnrancher/mirrored-suse-kubectl"
63+
tag: "latest"

index.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ entries:
1111
catalog.cattle.io/ui-component: mcs-ext-chart
1212
apiVersion: v2
1313
appVersion: 0.0.1
14-
created: "2026-01-07T17:08:16.326761+08:00"
14+
created: "2026-01-16T16:01:49.026337+08:00"
1515
dependencies:
1616
- name: mcs-addon
1717
repository: file://./charts/mcs-addon
@@ -23,7 +23,7 @@ entries:
2323
repository: file://./charts/submariner-operator
2424
version: 0.0.1
2525
description: Multi-cluster Service Extension Chart
26-
digest: ceae19a785043baf073038b3c17e45456e01ffe8097f89ea3ec3f456e5536b0c
26+
digest: 61d9f12213693e0dabdf0b68c2822d71f365e4565d8b0f5e32b77f55eda53883
2727
icon: https://pub-68160d88f4744423960a290fc7a5031d.r2.dev/mcs-icon.png
2828
name: mcs-ext-chart
2929
urls:

packages/mcs-addon/v0.0.1/charts/templates/controller-rbac-global.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rules:
1919
- watch
2020
- update
2121
- patch
22+
- delete
2223
- apiGroups: [ rbac.authorization.k8s.io ]
2324
resources:
2425
- rolebindings
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{{- if eq .Values.global.installationType "global" }}
2+
{{- if .Values.cleanupOnUninstall | default true }}
3+
apiVersion: batch/v1
4+
kind: Job
5+
metadata:
6+
name: mcs-addon-pre-delete-cleanup-clusterset
7+
namespace: {{ .Release.Namespace }}
8+
annotations:
9+
"helm.sh/hook": pre-delete
10+
"helm.sh/hook-weight": "-5"
11+
"helm.sh/hook-delete-policy": hook-succeeded,hook-failed
12+
labels:
13+
{{- include "mcs-addon.chart_labels" . | nindent 4 }}
14+
component: cleanup
15+
spec:
16+
activeDeadlineSeconds: {{ .Values.cleanupTimeout | default 300 }}
17+
backoffLimit: 2
18+
template:
19+
metadata:
20+
labels:
21+
{{- include "mcs-addon.chart_labels" . | nindent 8 }}
22+
component: cleanup
23+
spec:
24+
serviceAccountName: {{ template "mcs-addon.component_name" (dict "Chart" .Chart "componentName" "controller" "Release" .Release "Values" .Values) }}
25+
restartPolicy: Never
26+
containers:
27+
- name: cleanup-clusterset-crd
28+
image: "{{ template "system_default_registry" . }}{{ .Values.kubectl.repository }}:{{ .Values.kubectl.tag }}"
29+
imagePullPolicy: IfNotPresent
30+
command:
31+
- /bin/sh
32+
- -c
33+
- |
34+
set -e
35+
CRD_NAME="clustersets.multicluster.pandaria.io"
36+
DELETE_TIMEOUT={{ .Values.cleanupTimeout | default 300 }}
37+
38+
echo "===================================================================="
39+
echo "NOTE: Deleting ClusterSet CRD will automatically delete all ClusterSet resources"
40+
echo "Delete Timeout: ${DELETE_TIMEOUT}s"
41+
echo "===================================================================="
42+
43+
# Check if CRD exists
44+
if ! kubectl get crd "$CRD_NAME" &>/dev/null; then
45+
echo "CRD '$CRD_NAME' not found, nothing to clean up."
46+
exit 0
47+
fi
48+
49+
echo ""
50+
echo "CRD '$CRD_NAME' exists, listing ClusterSet resources..."
51+
CLUSTERSETS=$(kubectl get clustersets -o jsonpath='{.items[*].metadata.name}' 2>/dev/null || echo "")
52+
if [ -n "$CLUSTERSETS" ]; then
53+
echo "Found ClusterSet resources: $CLUSTERSETS"
54+
echo "These will be automatically deleted when CRD is removed."
55+
else
56+
echo "No ClusterSet resources found."
57+
fi
58+
59+
echo ""
60+
echo "--- Deleting CRD: $CRD_NAME ---"
61+
echo "NOTE: ClusterSet resources do not use finalizers, deletion will be immediate."
62+
63+
# Delete CRD with timeout
64+
echo "Deleting CRD..."
65+
if kubectl delete crd "$CRD_NAME" --timeout=60s --ignore-not-found=true 2>&1; then
66+
echo "CRD '$CRD_NAME' deleted successfully."
67+
else
68+
echo "WARNING: CRD deletion failed or timed out, attempting force cleanup..."
69+
kubectl delete crd "$CRD_NAME" \
70+
--force \
71+
--grace-period=0 \
72+
--ignore-not-found=true 2>&1 || echo "Warning: Force delete failed"
73+
fi
74+
75+
# Give controller a few seconds to process OnRemove handlers
76+
echo ""
77+
echo "Waiting 5 seconds for controller to process cleanup handlers..."
78+
sleep 5
79+
80+
# Verify CRD is deleted
81+
if kubectl get crd "$CRD_NAME" &>/dev/null; then
82+
echo "WARNING: CRD '$CRD_NAME' still exists!"
83+
echo "Manual cleanup may be required: kubectl delete crd $CRD_NAME"
84+
else
85+
echo "CRD '$CRD_NAME' successfully removed."
86+
fi
87+
88+
echo ""
89+
echo "===================================================================="
90+
echo "Pre-delete cleanup hook completed"
91+
echo "===================================================================="
92+
93+
# Always exit 0 to allow Helm uninstall to proceed
94+
exit 0
95+
{{- end }}
96+
{{- end }}

packages/mcs-addon/v0.0.1/charts/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ global:
5353
cattle:
5454
systemDefaultRegistry: ""
5555
provisioningNamespace: "fleet-default"
56+
# Cleanup settings for Helm hooks
57+
cleanupOnUninstall: true
58+
# Cleanup timeout settings (in seconds)
59+
cleanupTimeout: 300
60+
# Kubectl image for cleanup hooks (must have shell and kubectl)
61+
kubectl:
62+
repository: "cnrancher/mirrored-suse-kubectl"
63+
tag: "latest"

0 commit comments

Comments
 (0)