Skip to content

Commit c2a8a3b

Browse files
committed
fix(mariadb): fail cleanup hook on real delete errors
The post-delete cleanup hook used `kubectl delete ... --ignore-not-found || echo WARNING`, which swallowed genuine RBAC/API failures and let the Job succeed with orphaned password Secrets still present. Track delete failures and propagate them via a non-zero exit, while --ignore-not-found still tolerates already-absent resources. Add a helm-unittest assertion locking in the non-zero exit-on-failure behaviour. Assisted-By: Claude <[email protected]> Signed-off-by: Алексей Артамонов <[email protected]>
1 parent 7ef2b2f commit c2a8a3b

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎packages/apps/mariadb/templates/hooks/cleanup-pvc.yaml‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,27 @@ spec:
4646
- /bin/sh
4747
- -c
4848
- |
49+
# --ignore-not-found tolerates already-absent resources; only a
50+
# genuine delete failure (RBAC/API error) must fail the hook, so
51+
# track it and propagate via the final exit instead of masking
52+
# errors behind `|| echo ...`.
53+
cleanup_failed=0
4954
echo "Deleting orphaned PVCs for {{ .Release.Name }}..."
50-
kubectl delete pvc -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }} --ignore-not-found \
51-
|| echo "WARNING: PVC cleanup failed for {{ .Release.Name }}; orphaned PVCs may remain" >&2
52-
echo "PVC cleanup complete."
55+
if kubectl delete pvc -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }} --ignore-not-found; then
56+
echo "PVC cleanup complete."
57+
else
58+
echo "WARNING: PVC cleanup failed for {{ .Release.Name }}; orphaned PVCs may remain" >&2
59+
cleanup_failed=1
60+
fi
5361
echo "Deleting orphaned operator-generated Secrets for {{ .Release.Name }}..."
54-
kubectl delete secret -n {{ .Release.Namespace }} \
55-
{{ .Release.Name }}-metrics-password {{ .Release.Name }}-repl-password --ignore-not-found \
56-
|| echo "WARNING: Secret cleanup failed for {{ .Release.Name }}; orphaned Secrets may remain" >&2
57-
echo "Secret cleanup complete."
62+
if kubectl delete secret -n {{ .Release.Namespace }} \
63+
{{ .Release.Name }}-metrics-password {{ .Release.Name }}-repl-password --ignore-not-found; then
64+
echo "Secret cleanup complete."
65+
else
66+
echo "WARNING: Secret cleanup failed for {{ .Release.Name }}; orphaned Secrets may remain" >&2
67+
cleanup_failed=1
68+
fi
69+
exit "${cleanup_failed}"
5870
volumeMounts:
5971
- name: tmp
6072
mountPath: /tmp

‎packages/apps/mariadb/tests/cleanup_hook_test.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ tests:
2424
path: spec.template.spec.containers[0].command[2]
2525
pattern: "mariadb-test-metrics-password mariadb-test-repl-password --ignore-not-found"
2626

27+
- it: "cleanup Job fails (exits non-zero) when a real delete error occurs, so orphaned resources don't pass silently"
28+
documentSelector:
29+
path: kind
30+
value: Job
31+
asserts:
32+
# --ignore-not-found tolerates absent resources, but a genuine RBAC/API
33+
# failure must propagate: the script tracks failures and exits with that
34+
# status instead of masking errors behind `|| echo ...`.
35+
- matchRegex:
36+
path: spec.template.spec.containers[0].command[2]
37+
pattern: 'exit "\$\{cleanup_failed\}"'
38+
2739
- it: "cleanup Role grants get/list/delete on persistentvolumeclaims"
2840
documentSelector:
2941
path: kind

0 commit comments

Comments
 (0)