Skip to content

Commit 178b836

Browse files
committed
fix(qdrant): clean up orphaned data PVC on delete (#3059)
The StatefulSet data PVC (qdrant-storage-<release>-0) was left orphaned after a Qdrant app CR was deleted. The vendored upstream chart does not support persistentVolumeClaimRetentionPolicy, so add a post-delete PVC cleanup hook (modeled on the MariaDB hook) and pass persistence.additionalLabels through the HelmRelease so the data PVC carries a release-unique app.kubernetes.io/instance label the hook selects on. The cleanup Job runs non-root with a read-only root fs and dropped capabilities; failures are logged but never block namespace teardown. Signed-off-by: Алексей Артамонов <[email protected]>
1 parent 374cac1 commit 178b836

4 files changed

Lines changed: 184 additions & 0 deletions

File tree

‎packages/apps/qdrant/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ generate:
44
cozyvalues-gen -m 'qdrant' -v values.yaml -s values.schema.json -r README.md -g ../../../api/apps/v1alpha1/qdrant/types.go
55
../../../hack/update-crd.sh
66

7+
.PHONY: test
78
test:
89
helm unittest .
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
apiVersion: batch/v1
3+
kind: Job
4+
metadata:
5+
name: {{ .Release.Name }}-qdrant-cleanup
6+
labels:
7+
app.kubernetes.io/instance: {{ .Release.Name }}
8+
annotations:
9+
"helm.sh/hook": post-delete
10+
"helm.sh/hook-weight": "10"
11+
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
12+
spec:
13+
template:
14+
metadata:
15+
labels:
16+
app.kubernetes.io/instance: {{ .Release.Name }}
17+
policy.cozystack.io/allow-to-apiserver: "true"
18+
spec:
19+
serviceAccountName: {{ .Release.Name }}-qdrant-cleanup
20+
restartPolicy: Never
21+
securityContext:
22+
runAsNonRoot: true
23+
runAsUser: 65534
24+
runAsGroup: 65534
25+
seccompProfile:
26+
type: RuntimeDefault
27+
containers:
28+
- name: cleanup
29+
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
38+
resources:
39+
requests:
40+
cpu: 10m
41+
memory: 64Mi
42+
limits:
43+
cpu: 200m
44+
memory: 128Mi
45+
command:
46+
- /bin/sh
47+
- -c
48+
- |
49+
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."
53+
volumeMounts:
54+
- name: tmp
55+
mountPath: /tmp
56+
volumes:
57+
- name: tmp
58+
emptyDir: {}
59+
---
60+
apiVersion: v1
61+
kind: ServiceAccount
62+
metadata:
63+
name: {{ .Release.Name }}-qdrant-cleanup
64+
labels:
65+
app.kubernetes.io/instance: {{ .Release.Name }}
66+
annotations:
67+
"helm.sh/hook": post-delete
68+
helm.sh/hook-weight: "0"
69+
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
70+
---
71+
apiVersion: rbac.authorization.k8s.io/v1
72+
kind: Role
73+
metadata:
74+
name: {{ .Release.Name }}-qdrant-cleanup
75+
labels:
76+
app.kubernetes.io/instance: {{ .Release.Name }}
77+
annotations:
78+
"helm.sh/hook": post-delete
79+
"helm.sh/hook-weight": "5"
80+
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
81+
rules:
82+
- apiGroups: [""]
83+
resources: ["persistentvolumeclaims"]
84+
verbs: ["get", "list", "delete"]
85+
---
86+
apiVersion: rbac.authorization.k8s.io/v1
87+
kind: RoleBinding
88+
metadata:
89+
name: {{ .Release.Name }}-qdrant-cleanup
90+
labels:
91+
app.kubernetes.io/instance: {{ .Release.Name }}
92+
annotations:
93+
"helm.sh/hook": post-delete
94+
helm.sh/hook-weight: "5"
95+
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
96+
roleRef:
97+
apiGroup: rbac.authorization.k8s.io
98+
kind: Role
99+
name: {{ .Release.Name }}-qdrant-cleanup
100+
subjects:
101+
- kind: ServiceAccount
102+
name: {{ .Release.Name }}-qdrant-cleanup

‎packages/apps/qdrant/templates/qdrant.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ spec:
3434
{{- with .Values.storageClass }}
3535
storageClassName: {{ . }}
3636
{{- end }}
37+
# Stamp a release-unique label onto the StatefulSet data PVC so the
38+
# post-delete cleanup hook can target only this release's PVC. The
39+
# upstream chart does NOT support persistentVolumeClaimRetentionPolicy
40+
# and its volumeClaimTemplate otherwise carries only `app: qdrant`
41+
# (the chart name, shared by every Qdrant in the namespace). See #3059.
42+
additionalLabels:
43+
app.kubernetes.io/instance: {{ .Release.Name }}
3744
metrics:
3845
serviceMonitor:
3946
enabled: true
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
suite: qdrant data PVC cleanup on delete
2+
3+
# Guards the fix for issue #3059: the upstream Qdrant chart does NOT support
4+
# persistentVolumeClaimRetentionPolicy, so its StatefulSet-templated data PVC
5+
# (qdrant-storage-<release>-0) is orphaned when the StatefulSet is deleted.
6+
#
7+
# The fix has two parts:
8+
# 1. The HelmRelease passes persistence.additionalLabels through to the
9+
# upstream chart so the data PVC carries a release-unique label
10+
# (app.kubernetes.io/instance=<release>). Without it the volumeClaimTemplate
11+
# only stamps `app: qdrant` (the chart name), shared by every Qdrant in the
12+
# namespace.
13+
# 2. A post-delete hook deletes PVCs matching that label so the data is
14+
# reclaimed on release deletion.
15+
16+
release:
17+
name: qdrant-test
18+
namespace: tenant-test
19+
20+
tests:
21+
#####################################################
22+
# PVC label passthrough on the HelmRelease values #
23+
#####################################################
24+
25+
- it: "stamps a release-unique instance label onto the data PVC"
26+
templates:
27+
- templates/qdrant.yaml
28+
documentSelector:
29+
path: kind
30+
value: HelmRelease
31+
asserts:
32+
- equal:
33+
path: spec.values.qdrant.persistence.additionalLabels["app.kubernetes.io/instance"]
34+
value: qdrant-test
35+
36+
#####################################################
37+
# post-delete cleanup hook #
38+
#####################################################
39+
40+
- it: "renders a post-delete Job that deletes the data PVC by instance label"
41+
templates:
42+
- templates/hooks/cleanup-pvc.yaml
43+
documentSelector:
44+
path: kind
45+
value: Job
46+
asserts:
47+
- isKind:
48+
of: Job
49+
- equal:
50+
path: metadata.annotations["helm.sh/hook"]
51+
value: post-delete
52+
- equal:
53+
path: spec.template.spec.serviceAccountName
54+
value: qdrant-test-qdrant-cleanup
55+
- matchRegex:
56+
path: spec.template.spec.containers[0].command[2]
57+
pattern: "kubectl delete pvc -n tenant-test -l app\\.kubernetes\\.io/instance=qdrant-test --ignore-not-found"
58+
59+
- it: "creates qdrant-scoped RBAC for the cleanup hook"
60+
templates:
61+
- templates/hooks/cleanup-pvc.yaml
62+
documentSelector:
63+
path: kind
64+
value: Role
65+
asserts:
66+
- equal:
67+
path: metadata.name
68+
value: qdrant-test-qdrant-cleanup
69+
- contains:
70+
path: rules
71+
content:
72+
apiGroups: [""]
73+
resources: ["persistentvolumeclaims"]
74+
verbs: ["get", "list", "delete"]

0 commit comments

Comments
 (0)