Skip to content

Commit ea52cc1

Browse files
committed
fix(platform): refuse a networking variant change on a live cluster
Changing bundles.system.variant on a running cluster can re-render the cozystack.networking Package with another variant. The operator then deletes every HelmRelease of that Package the new variant does not install, so isp-full -> isp-hosted removes Cilium and Kube-OVN and the cluster loses its CNI; isp-full -> isp-slim removes Kube-OVN. The platform render now looks up the live Package and fails when its variant differs from the one about to be rendered, unless networking.acknowledgeVariantChange names the new variant. The acknowledgement is a variant name rather than a boolean so that a value left behind after one switch does not cover a later one. The check is inert where lookup returns nothing (helm template, client-side dry-run, first install) and skipped when cozystack.networking is in bundles.disabledPackages, since the platform then does not render it. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 6addef4 commit ea52cc1

3 files changed

Lines changed: 223 additions & 0 deletions

File tree

‎packages/core/platform/templates/_helpers.tpl‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
{{- end -}}
99
{{- $disabled := default (list) $root.Values.bundles.disabledPackages -}}
1010
{{- if not (has $name $disabled) -}}
11+
{{- if eq $name "cozystack.networking" -}}
12+
{{- include "cozystack.platform.networking.variantGuard" (list $root $variant) -}}
13+
{{- end }}
1114
---
1215
apiVersion: cozystack.io/v1alpha1
1316
kind: Package
@@ -24,6 +27,29 @@ spec:
2427
{{- end }}
2528
{{ end }}
2629

30+
{{/*
31+
Refuses to change the variant of a live cozystack.networking Package. The
32+
operator deletes every HelmRelease of a Package that its new variant does not
33+
install, so isp-full -> isp-hosted removes cilium and kubeovn and leaves the
34+
cluster without a CNI. The acknowledgement names the target variant rather than
35+
being a boolean, so a value left set after one switch does not cover the next.
36+
Inert where lookup returns nothing: helm template, client-side dry-run, first
37+
install.
38+
Call as (list $ <variant about to be rendered>).
39+
*/}}
40+
{{- define "cozystack.platform.networking.variantGuard" -}}
41+
{{- $root := index . 0 -}}
42+
{{- $variant := index . 1 -}}
43+
{{- $live := lookup "cozystack.io/v1alpha1" "Package" "" "cozystack.networking" -}}
44+
{{- if $live -}}
45+
{{- $liveVariant := default "default" (dig "spec" "variant" "" $live) -}}
46+
{{- $ack := dig "acknowledgeVariantChange" "" ($root.Values.networking | default dict) | toString -}}
47+
{{- if and (ne $liveVariant $variant) (ne $ack $variant) -}}
48+
{{- fail (printf "the live cozystack.networking Package runs variant %q and this render would switch it to %q (bundles.system.variant: %s). The operator then deletes every HelmRelease of that Package the new variant does not install: isp-full to isp-hosted removes Cilium and Kube-OVN and leaves every pod without networking, isp-full to isp-slim removes Kube-OVN. Either restore bundles.system.variant, or, once the cluster has another CNI or no longer needs the removed one, set networking.acknowledgeVariantChange: %q to confirm this exact switch. To manage the networking Package yourself instead, add cozystack.networking to bundles.disabledPackages." $liveVariant $variant $root.Values.bundles.system.variant $variant) -}}
49+
{{- end -}}
50+
{{- end -}}
51+
{{- end }}
52+
2753
{{- define "cozystack.platform.package.default" -}}
2854
{{- $name := index . 0 -}}
2955
{{- $root := index . 1 -}}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
suite: networking variant change guard (lookup of the live cozystack.networking Package)
2+
3+
# A new networking variant is not a migration. The operator reconciles the
4+
# Package against the new variant and deletes every HelmRelease of the Package
5+
# that the new variant does not install: isp-full -> isp-hosted (noop) removes
6+
# cilium and kubeovn, isp-full -> isp-slim (cilium) removes kubeovn. The guard
7+
# reads the live Package and is inert where lookup returns nothing (helm
8+
# template, client-side dry-run, fresh install).
9+
templates:
10+
- templates/bundles/system.yaml
11+
release:
12+
name: cozystack
13+
namespace: cozy-system
14+
set:
15+
bundles:
16+
system:
17+
enabled: true
18+
variant: isp-full
19+
tests:
20+
- it: refuses isp-full -> isp-hosted on a live kubeovn-cilium cluster and names the acknowledge value
21+
set:
22+
bundles:
23+
system:
24+
variant: isp-hosted
25+
kubernetesProvider:
26+
scheme: &scheme
27+
"cozystack.io/v1alpha1/Package":
28+
gvr: {group: "cozystack.io", version: "v1alpha1", resource: "packages"}
29+
namespaced: false
30+
objects: &liveKubeovnCilium
31+
- kind: Package
32+
apiVersion: cozystack.io/v1alpha1
33+
metadata: {name: cozystack.networking}
34+
spec: {variant: kubeovn-cilium}
35+
asserts:
36+
- failedTemplate:
37+
errorPattern: 'cozystack.networking Package runs variant "kubeovn-cilium".*render would switch it to "noop".*networking.acknowledgeVariantChange: "noop"'
38+
39+
- it: refuses isp-full -> isp-slim on a live kubeovn-cilium cluster
40+
set:
41+
bundles:
42+
system:
43+
variant: isp-slim
44+
kubernetesProvider:
45+
scheme: *scheme
46+
objects: *liveKubeovnCilium
47+
asserts:
48+
- failedTemplate:
49+
errorPattern: 'runs variant "kubeovn-cilium".*switch it to "cilium".*networking.acknowledgeVariantChange: "cilium"'
50+
51+
- it: refuses isp-hosted -> isp-full on a live noop cluster
52+
kubernetesProvider:
53+
scheme: *scheme
54+
objects:
55+
- kind: Package
56+
apiVersion: cozystack.io/v1alpha1
57+
metadata: {name: cozystack.networking}
58+
spec: {variant: noop}
59+
asserts:
60+
- failedTemplate:
61+
errorPattern: 'runs variant "noop".*switch it to "kubeovn-cilium"'
62+
63+
- it: renders when the live variant matches the rendered one
64+
kubernetesProvider:
65+
scheme: *scheme
66+
objects: *liveKubeovnCilium
67+
documentSelector:
68+
path: metadata.name
69+
value: cozystack.networking
70+
asserts:
71+
- equal:
72+
path: spec.variant
73+
value: kubeovn-cilium
74+
75+
- it: renders the change when acknowledgeVariantChange names the new variant
76+
set:
77+
bundles:
78+
system:
79+
variant: isp-hosted
80+
networking:
81+
acknowledgeVariantChange: noop
82+
kubernetesProvider:
83+
scheme: *scheme
84+
objects: *liveKubeovnCilium
85+
documentSelector:
86+
path: metadata.name
87+
value: cozystack.networking
88+
asserts:
89+
- equal:
90+
path: spec.variant
91+
value: noop
92+
93+
# The acknowledgement is bound to one target, so a value left behind after
94+
# one switch does not wave through the next one.
95+
- it: still refuses when acknowledgeVariantChange names a different variant
96+
set:
97+
bundles:
98+
system:
99+
variant: isp-hosted
100+
networking:
101+
acknowledgeVariantChange: cilium
102+
kubernetesProvider:
103+
scheme: *scheme
104+
objects: *liveKubeovnCilium
105+
asserts:
106+
- failedTemplate:
107+
errorPattern: 'networking.acknowledgeVariantChange: "noop"'
108+
109+
- it: still refuses when acknowledgeVariantChange is a bare boolean
110+
set:
111+
bundles:
112+
system:
113+
variant: isp-hosted
114+
networking:
115+
acknowledgeVariantChange: true
116+
kubernetesProvider:
117+
scheme: *scheme
118+
objects: *liveKubeovnCilium
119+
asserts:
120+
- failedTemplate:
121+
errorPattern: 'networking.acknowledgeVariantChange: "noop"'
122+
123+
# A Package left out of the render is not re-rendered, so Helm does not
124+
# touch its variant; helm.sh/resource-policy: keep leaves it as it is.
125+
- it: renders when cozystack.networking is in bundles.disabledPackages
126+
set:
127+
bundles:
128+
system:
129+
variant: isp-hosted
130+
disabledPackages:
131+
- cozystack.networking
132+
kubernetesProvider:
133+
scheme: *scheme
134+
objects: *liveKubeovnCilium
135+
asserts:
136+
- containsDocument:
137+
apiVersion: cozystack.io/v1alpha1
138+
kind: Package
139+
name: cozystack.networking
140+
not: true
141+
documentIndex: -1
142+
143+
- it: renders offline (no live Package) for isp-hosted
144+
set:
145+
bundles:
146+
system:
147+
variant: isp-hosted
148+
documentSelector:
149+
path: metadata.name
150+
value: cozystack.networking
151+
asserts:
152+
- equal:
153+
path: spec.variant
154+
value: noop
155+
156+
- it: renders offline (no live Package) for isp-slim
157+
set:
158+
bundles:
159+
system:
160+
variant: isp-slim
161+
documentSelector:
162+
path: metadata.name
163+
value: cozystack.networking
164+
asserts:
165+
- equal:
166+
path: spec.variant
167+
value: cilium
168+
169+
- it: renders on a cluster that has other Packages but no cozystack.networking yet
170+
set:
171+
bundles:
172+
system:
173+
variant: isp-full-generic
174+
kubernetesProvider:
175+
scheme: *scheme
176+
objects:
177+
- kind: Package
178+
apiVersion: cozystack.io/v1alpha1
179+
metadata: {name: cozystack.cert-manager}
180+
spec: {variant: default}
181+
documentSelector:
182+
path: metadata.name
183+
value: cozystack.networking
184+
asserts:
185+
- equal:
186+
path: spec.variant
187+
value: kubeovn-cilium-generic

‎packages/core/platform/values.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ networking:
9595
# Set this to comma-separated list of master node IPs to override.
9696
kubeovn:
9797
MASTER_NODES: ""
98+
# Confirms a change of the cozystack.networking Package variant on a running
99+
# cluster. Changing bundles.system.variant can change that variant (isp-full
100+
# runs kubeovn-cilium, isp-slim runs cilium, isp-hosted runs noop), and the
101+
# operator then deletes the networking HelmReleases the new variant does not
102+
# install: isp-full -> isp-hosted removes Cilium and Kube-OVN. The platform
103+
# render refuses such a change while the live Package has another variant,
104+
# unless this is set to the NEW networking variant, as named in the render
105+
# error. It confirms that one switch only; a leftover value does not cover a
106+
# later switch to a different variant.
107+
acknowledgeVariantChange: ""
98108
# In-cluster (east-west) transport encryption. Opt-in, OFF by default.
99109
#
100110
# When enabled, pod-to-pod traffic that crosses nodes is encrypted at the

0 commit comments

Comments
 (0)