Skip to content

Commit 82f5255

Browse files
committed
fix(vsnap-crd): keep the CRDs when the release is uninstalled
The tenant addon HelmRelease remediates a failed install by uninstalling the release, and an uninstall deletes whatever the chart rendered. Deleting a CRD makes the API server garbage-collect every custom resource of that kind, so a failed reinstall takes every VolumeSnapshot and VolumeSnapshotContent in the tenant cluster with it — the objects that carry the only reference to a snapshot on the storage backend. Stamp helm.sh/resource-policy: keep on each CustomResourceDefinition and teach `make update` to reapply it, since that target rebuilds templates/ from upstream and would otherwise drop the annotation silently. Removing the release on purpose now leaves the definitions behind to be deleted by hand. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 5d5d8f5 commit 82f5255

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎packages/system/vsnap-crd/Makefile‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@ export NAMESPACE=cozy-$(NAME)
33

44
include ../../../hack/package.mk
55

6+
test:
7+
helm unittest .
8+
9+
# Stamp helm.sh/resource-policy: keep on every CustomResourceDefinition so a
10+
# chart uninstall never garbage-collects the CRDs and, with them, every
11+
# VolumeSnapshot and VolumeSnapshotContent in the cluster — the objects that
12+
# hold the only reference to a snapshot on the storage backend.
13+
# The stamp goes into the CRD's existing metadata.annotations block; a CRD that
14+
# arrives without one is skipped, and the chart's helm-unittest suite is what
15+
# catches that.
616
update:
717
rm -rf templates
818
mkdir templates
919
wget -O ./templates/volumesnapshotclasses.yaml https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/refs/tags/v8.3.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
1020
wget -O ./templates/volumesnapshotcontents.yaml https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/refs/tags/v8.3.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
1121
wget -O ./templates/volumesnapshots.yaml https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/refs/tags/v8.3.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml
22+
@set -e; tmp=$$(mktemp); for f in ./templates/*.yaml; do \
23+
awk '/^ helm.sh\/resource-policy: keep$$/{next} /^kind: /{kind=$$2} /^ annotations:$$/ && kind=="CustomResourceDefinition"{print; print " helm.sh/resource-policy: keep"; next} {print}' "$$f" > "$$tmp"; \
24+
cat "$$tmp" > "$$f"; \
25+
done; rm -f "$$tmp"

‎packages/system/vsnap-crd/templates/volumesnapshotclasses.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6+
helm.sh/resource-policy: keep
67
api-approved.kubernetes.io: "https://github.com/kubernetes-csi/external-snapshotter/pull/814"
78
controller-gen.kubebuilder.io/version: v0.15.0
89
name: volumesnapshotclasses.snapshot.storage.k8s.io

‎packages/system/vsnap-crd/templates/volumesnapshotcontents.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6+
helm.sh/resource-policy: keep
67
controller-gen.kubebuilder.io/version: v0.15.0
78
api-approved.kubernetes.io: "https://github.com/kubernetes-csi/external-snapshotter/pull/955"
89
name: volumesnapshotcontents.snapshot.storage.k8s.io

‎packages/system/vsnap-crd/templates/volumesnapshots.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6+
helm.sh/resource-policy: keep
67
controller-gen.kubebuilder.io/version: v0.15.0
78
api-approved.kubernetes.io: "https://github.com/kubernetes-csi/external-snapshotter/pull/814"
89
name: volumesnapshots.snapshot.storage.k8s.io
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# The tenant addon HelmRelease that ships this chart remediates a failed
2+
# install by uninstalling the release, and an uninstall deletes whatever the
3+
# chart rendered. Deleting a CRD makes the API server garbage-collect every
4+
# custom resource of that kind, so an unannotated CRD here costs the cluster
5+
# every VolumeSnapshot it holds — the objects that carry the only reference to
6+
# a snapshot on the storage backend.
7+
#
8+
# The first case scopes to no template at all, so it holds every document the
9+
# chart renders, including a CRD that arrives in a file `make update` starts
10+
# fetching later. The named cases below pin which CRD each file carries, so an
11+
# assertion cannot pass against a document it was not meant to inspect.
12+
suite: volumesnapshot CRDs survive a release uninstall
13+
tests:
14+
- it: annotates every CustomResourceDefinition the chart renders
15+
asserts:
16+
- equal:
17+
path: kind
18+
value: CustomResourceDefinition
19+
- equal:
20+
path: metadata.annotations["helm.sh/resource-policy"]
21+
value: keep
22+
23+
- it: keeps the VolumeSnapshotClass CRD
24+
template: templates/volumesnapshotclasses.yaml
25+
asserts:
26+
- hasDocuments:
27+
count: 1
28+
- equal:
29+
path: metadata.name
30+
value: volumesnapshotclasses.snapshot.storage.k8s.io
31+
- equal:
32+
path: metadata.annotations["helm.sh/resource-policy"]
33+
value: keep
34+
35+
- it: keeps the VolumeSnapshotContent CRD
36+
template: templates/volumesnapshotcontents.yaml
37+
asserts:
38+
- hasDocuments:
39+
count: 1
40+
- equal:
41+
path: metadata.name
42+
value: volumesnapshotcontents.snapshot.storage.k8s.io
43+
- equal:
44+
path: metadata.annotations["helm.sh/resource-policy"]
45+
value: keep
46+
47+
- it: keeps the VolumeSnapshot CRD
48+
template: templates/volumesnapshots.yaml
49+
asserts:
50+
- hasDocuments:
51+
count: 1
52+
- equal:
53+
path: metadata.name
54+
value: volumesnapshots.snapshot.storage.k8s.io
55+
- equal:
56+
path: metadata.annotations["helm.sh/resource-policy"]
57+
value: keep

0 commit comments

Comments
 (0)