Skip to content

Commit b7c06bc

Browse files
committed
fix(mariadb): clean up orphaned operator-generated secrets (#3056)
The mariadb-operator creates <release>-metrics-password and <release>-repl-password Secrets as a side effect of metrics/replication being enabled. These are not Helm-owned and survive `helm uninstall`, leaving orphaned Secrets in the tenant namespace after the app CR is deleted. Extend the existing post-delete cleanup hook to sweep them: grant the cleanup Role RBAC on secrets and delete both password Secrets by name. Add a helm-unittest suite locking in the behaviour (and a `test` Makefile target so the package's tests run in CI). Signed-off-by: Алексей Артамонов <[email protected]>
1 parent 374cac1 commit b7c06bc

3 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎packages/apps/mariadb/Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ update:
1111
hack/update-versions.sh
1212
make generate
1313

14+
.PHONY: test
15+
test:
16+
helm unittest .
17+
1418
image:
1519
docker buildx build images/mariadb-backup \
1620
$(call image-tags,mariadb-backup,$(MARIADB_BACKUP_TAG)) \

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,42 @@ spec:
1818
spec:
1919
serviceAccountName: {{ .Release.Name }}-cleanup
2020
restartPolicy: Never
21+
securityContext:
22+
runAsNonRoot: true
23+
runAsUser: 65534
24+
runAsGroup: 65534
25+
seccompProfile:
26+
type: RuntimeDefault
2127
containers:
2228
- name: cleanup
2329
image: docker.io/clastix/kubectl:v1.32
30+
securityContext:
31+
allowPrivilegeEscalation: false
32+
readOnlyRootFilesystem: true
33+
capabilities:
34+
drop: ["ALL"]
35+
env:
36+
- name: HOME
37+
value: /tmp
2438
command:
2539
- /bin/sh
2640
- -c
2741
- |
2842
echo "Deleting orphaned PVCs for {{ .Release.Name }}..."
29-
kubectl delete pvc -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }} || true
43+
kubectl delete pvc -n {{ .Release.Namespace }} -l app.kubernetes.io/instance={{ .Release.Name }} --ignore-not-found \
44+
|| echo "WARNING: PVC cleanup failed for {{ .Release.Name }}; orphaned PVCs may remain" >&2
3045
echo "PVC cleanup complete."
46+
echo "Deleting orphaned operator-generated Secrets for {{ .Release.Name }}..."
47+
kubectl delete secret -n {{ .Release.Namespace }} \
48+
{{ .Release.Name }}-metrics-password {{ .Release.Name }}-repl-password --ignore-not-found \
49+
|| echo "WARNING: Secret cleanup failed for {{ .Release.Name }}; orphaned Secrets may remain" >&2
50+
echo "Secret cleanup complete."
51+
volumeMounts:
52+
- name: tmp
53+
mountPath: /tmp
54+
volumes:
55+
- name: tmp
56+
emptyDir: {}
3157
---
3258
apiVersion: v1
3359
kind: ServiceAccount
@@ -51,9 +77,18 @@ metadata:
5177
"helm.sh/hook-weight": "5"
5278
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
5379
rules:
80+
# PVCs are deleted by label selector, which requires list.
5481
- apiGroups: [""]
5582
resources: ["persistentvolumeclaims"]
5683
verbs: ["get", "list", "delete"]
84+
# Operator-generated Secrets are deleted by name, so scope the rule to
85+
# exactly those two resources (least privilege — no namespace-wide secret access).
86+
- apiGroups: [""]
87+
resources: ["secrets"]
88+
resourceNames:
89+
- {{ .Release.Name }}-metrics-password
90+
- {{ .Release.Name }}-repl-password
91+
verbs: ["get", "delete"]
5792
---
5893
apiVersion: rbac.authorization.k8s.io/v1
5994
kind: RoleBinding
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
suite: post-delete cleanup hook
2+
3+
# Locks in the post-delete cleanup behaviour. The mariadb-operator creates
4+
# <release>-metrics-password and <release>-repl-password Secrets as a side
5+
# effect of metrics/replication being enabled. These Secrets are not
6+
# Helm-owned and survive `helm uninstall`, so the post-delete hook must
7+
# sweep them (issue #3056) alongside the orphaned PVCs. The hook Role also
8+
# needs RBAC on secrets to do so.
9+
10+
templates:
11+
- templates/hooks/cleanup-pvc.yaml
12+
13+
release:
14+
name: mariadb-test
15+
namespace: tenant-test
16+
17+
tests:
18+
- it: "cleanup Job deletes the operator-generated password Secrets by name"
19+
documentSelector:
20+
path: kind
21+
value: Job
22+
asserts:
23+
- matchRegex:
24+
path: spec.template.spec.containers[0].command[2]
25+
pattern: "mariadb-test-metrics-password mariadb-test-repl-password --ignore-not-found"
26+
27+
- it: "cleanup Role grants get/list/delete on persistentvolumeclaims"
28+
documentSelector:
29+
path: kind
30+
value: Role
31+
asserts:
32+
- contains:
33+
path: rules
34+
content:
35+
apiGroups: [""]
36+
resources: ["persistentvolumeclaims"]
37+
verbs: ["get", "list", "delete"]
38+
39+
- it: "cleanup Role scopes secret access to the two named secrets only (least privilege)"
40+
documentSelector:
41+
path: kind
42+
value: Role
43+
asserts:
44+
- contains:
45+
path: rules
46+
content:
47+
apiGroups: [""]
48+
resources: ["secrets"]
49+
resourceNames:
50+
- mariadb-test-metrics-password
51+
- mariadb-test-repl-password
52+
verbs: ["get", "delete"]

0 commit comments

Comments
 (0)