Skip to content

Commit fdd4ce4

Browse files
committed
CKS/CSI: fix PVC deletion script to avoid '(standard input)' and use kubectl -o name/jsonpath
1 parent 3d6cafe commit fdd4ce4

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

plugins/integrations/kubernetes-service/src/main/resources/script/delete-pv-reclaimpolicy-delete

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,33 @@ delete_workloads_using_pvc() {
3333
local deleted_count=0
3434

3535
# Find & delete any deployment using the PVC
36-
/opt/bin/kubectl get deployments -n "$namespace" -o json 2>/dev/null | grep -l "$pvc_name" | \
37-
while IFS= read -r deployment; do
38-
if [ -n "$deployment" ]; then
39-
deployment_name=$(echo "$deployment" | cut -d'/' -f2)
40-
echo "$(timestamp) - Deleting Deployment: $deployment_name"
41-
/opt/bin/kubectl delete deployment "$deployment_name" -n "$namespace" --ignore-not-found=true
36+
# Iterate resource names and check the volumes via jsonpath to avoid grep -l on JSON (which prints "(standard input)")
37+
/opt/bin/kubectl get deployments -n "$namespace" -o name 2>/dev/null | \
38+
while IFS= read -r resource; do
39+
if [ -z "$resource" ]; then
40+
continue
41+
fi
42+
name=${resource#*/}
43+
dep_volumes=$(/opt/bin/kubectl get deployment "$name" -n "$namespace" -o jsonpath='{.spec.template.spec.volumes[*].persistentVolumeClaim.claimName}' 2>/dev/null || echo "")
44+
if [ -n "$dep_volumes" ] && echo "$dep_volumes" | grep -F -q "$pvc_name"; then
45+
echo "$(timestamp) - Deleting Deployment: $name"
46+
/opt/bin/kubectl delete deployment "$name" -n "$namespace" --ignore-not-found=true
4247
deleted_count=$((deleted_count + 1))
4348
fi
4449
done
4550

4651
# Find and delete any StatefulSet using the PVC
47-
/opt/bin/kubectl get statefulsets -n "$namespace" -o json 2>/dev/null | grep -l "$pvc_name" | \
48-
while IFS= read -r sts; do
49-
if [ -n "$sts" ]; then
50-
sts_name=$(echo "$sts" | cut -d'/' -f2)
51-
echo "$(timestamp) - Deleting StatefulSet: $sts_name"
52-
/opt/bin/kubectl delete statefulset "$sts_name" -n "$namespace" --ignore-not-found=true
52+
/opt/bin/kubectl get statefulsets -n "$namespace" -o name 2>/dev/null | \
53+
while IFS= read -r resource; do
54+
if [ -z "$resource" ]; then
55+
continue
56+
fi
57+
name=${resource#*/}
58+
# Check both template volumes and volumeClaimTemplates
59+
sts_volumes=$(/opt/bin/kubectl get statefulset "$name" -n "$namespace" -o jsonpath='{.spec.template.spec.volumes[*].persistentVolumeClaim.claimName} {.spec.volumeClaimTemplates[*].metadata.name}' 2>/dev/null || echo "")
60+
if [ -n "$sts_volumes" ] && echo "$sts_volumes" | grep -F -q "$pvc_name"; then
61+
echo "$(timestamp) - Deleting StatefulSet: $name"
62+
/opt/bin/kubectl delete statefulset "$name" -n "$namespace" --ignore-not-found=true
5363
deleted_count=$((deleted_count + 1))
5464
fi
5565
done
@@ -71,34 +81,46 @@ delete_workloads_using_pvc() {
7181
done
7282

7383
# Find and delete any DaemonSet using the PVC
74-
/opt/bin/kubectl get daemonsets -n "$namespace" -o json 2>/dev/null | grep -l "$pvc_name" | \
75-
while IFS= read -r ds; do
76-
if [ -n "$ds" ]; then
77-
ds_name=$(echo "$ds" | cut -d'/' -f2)
78-
echo "$(timestamp) - Deleting DaemonSet: $ds_name"
79-
/opt/bin/kubectl delete daemonset "$ds_name" -n "$namespace" --ignore-not-found=true
84+
/opt/bin/kubectl get daemonsets -n "$namespace" -o name 2>/dev/null | \
85+
while IFS= read -r resource; do
86+
if [ -z "$resource" ]; then
87+
continue
88+
fi
89+
name=${resource#*/}
90+
ds_volumes=$(/opt/bin/kubectl get daemonset "$name" -n "$namespace" -o jsonpath='{.spec.template.spec.volumes[*].persistentVolumeClaim.claimName}' 2>/dev/null || echo "")
91+
if [ -n "$ds_volumes" ] && echo "$ds_volumes" | grep -F -q "$pvc_name"; then
92+
echo "$(timestamp) - Deleting DaemonSet: $name"
93+
/opt/bin/kubectl delete daemonset "$name" -n "$namespace" --ignore-not-found=true
8094
deleted_count=$((deleted_count + 1))
8195
fi
8296
done
8397

8498
# Find and delete any Job using the PVC
85-
/opt/bin/kubectl get jobs -n "$namespace" -o json 2>/dev/null | grep -l "$pvc_name" | \
86-
while IFS= read -r job; do
87-
if [ -n "$job" ]; then
88-
job_name=$(echo "$job" | cut -d'/' -f2)
89-
echo "$(timestamp) - Deleting Job: $job_name"
90-
/opt/bin/kubectl delete job "$job_name" -n "$namespace" --ignore-not-found=true
99+
/opt/bin/kubectl get jobs -n "$namespace" -o name 2>/dev/null | \
100+
while IFS= read -r resource; do
101+
if [ -z "$resource" ]; then
102+
continue
103+
fi
104+
name=${resource#*/}
105+
job_volumes=$(/opt/bin/kubectl get job "$name" -n "$namespace" -o jsonpath='{.spec.template.spec.volumes[*].persistentVolumeClaim.claimName}' 2>/dev/null || echo "")
106+
if [ -n "$job_volumes" ] && echo "$job_volumes" | grep -F -q "$pvc_name"; then
107+
echo "$(timestamp) - Deleting Job: $name"
108+
/opt/bin/kubectl delete job "$name" -n "$namespace" --ignore-not-found=true
91109
deleted_count=$((deleted_count + 1))
92110
fi
93111
done
94112

95113
# Find and delete any CronJobs using the PVC
96-
/opt/bin/kubectl get cronjobs -n "$namespace" -o json 2>/dev/null | grep -l "$pvc_name" | \
97-
while IFS= read -r cronjob; do
98-
if [ -n "$cronjob" ]; then
99-
cronjob_name=$(echo "$cronjob" | cut -d'/' -f2)
100-
echo "$(timestamp) - Deleting CronJob: $cronjob_name"
101-
/opt/bin/kubectl delete cronjob "$cronjob_name" -n "$namespace" --ignore-not-found=true
114+
/opt/bin/kubectl get cronjobs -n "$namespace" -o name 2>/dev/null | \
115+
while IFS= read -r resource; do
116+
if [ -z "$resource" ]; then
117+
continue
118+
fi
119+
name=${resource#*/}
120+
cron_volumes=$(/opt/bin/kubectl get cronjob "$name" -n "$namespace" -o jsonpath='{.spec.jobTemplate.spec.template.spec.volumes[*].persistentVolumeClaim.claimName}' 2>/dev/null || echo "")
121+
if [ -n "$cron_volumes" ] && echo "$cron_volumes" | grep -F -q "$pvc_name"; then
122+
echo "$(timestamp) - Deleting CronJob: $name"
123+
/opt/bin/kubectl delete cronjob "$name" -n "$namespace" --ignore-not-found=true
102124
deleted_count=$((deleted_count + 1))
103125
fi
104126
done

0 commit comments

Comments
 (0)