Skip to content

Commit 93ffb38

Browse files
authored
fix(cozy-lib): make the cozyconfig helpers survive uninjected values (#3647)
<!-- Thank you for making a contribution! Here are some tips for you: - Use Conventional Commits for the PR title: `type(scope): description` - Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore - Scopes are not an exhaustive list — pick the most specific scope for the change and extend the list when a genuinely new area appears. Examples: - System components: dashboard, platform, operator, cilium, kube-ovn, linstor, fluxcd, cluster-api - Managed apps: postgres, mariadb, redis, kafka, clickhouse, virtual-machine, kubernetes - Development and maintenance: api, hack, tests, ci, docs, maintenance - Breaking changes: append `!` after type/scope (`feat(api)!: ...`) or add a `BREAKING CHANGE:` footer - If it's a work in progress, consider creating this PR as a draft. - Don't hesistate to ask for opinion and review in the community chats, even if it's still a draft. - Add the label `backport` if it's a bugfix that needs to be backported to a previous version. --> ## What this PR does `_cluster` and `_namespace` reach a chart through valuesFrom on the cozystack-values Secret, so they are absent whenever a chart is rendered on its own: `helm lint`, a plain `helm template`, and any helm-unittest suite that does not set them. Twelve of the helpers in `_cozyconfig.tpl` read straight through the missing map and abort the render instead of returning the empty default they document. Two spellings, one cause. `index .Values._cluster "root-host"` raises `index of untyped nil`; `.Values._namespace.host` raises `nil pointer evaluating interface {}.host`. In both, the trailing `| default ""` never runs, because it applies to the result and there is no result, only an error. That default covered a key missing from a map that exists, never a missing map. The guard goes on the map, which is what `cozy-lib.images-registry` in this same file already did: ``` (index (.Values._cluster | default dict) "root-host") | default "" ``` `index` on an empty map returns nil harmlessly, so the trailing default applies as it always read, and with the map present `| default dict` returns it untouched. **This fixes no chart that is failing today.** The charts that cannot render standalone read the injected values directly in their own templates rather than through these helpers, so a change to the library does not reach them; they are handled where they live. Worth being blunt about the same point from the other side: twelve of the thirteen helpers here currently have no callers anywhere in `packages/`. Only `cozy-lib.images-registry` is reached, from `cozy-lib.image`, and it was already guarded. The guard is still worth having on the rest, because they are the library's public surface, they are what a chart author copies when reaching for cluster configuration, and a broken spelling propagates by being read. `cozy-lib.image` is called from four templates, so these helpers do get adopted once they exist. **Rendered output does not change, and that is measured rather than argued.** All 34 charts that symlink cozy-lib were rendered before and after, both with the injected values populated and with them set to empty maps, and every result is byte-identical. That check needs a control: the first comparison showed eight charts differing, which looks like a regression in a library change. Rendering the *unchanged* tree twice reproduces exactly those eight, because they generate passwords. Subtracting the control leaves zero real differences. The suite lives in `cozy-lib-tests`, which is where library behaviour is pinned, since a library chart renders nothing of its own. One case covers the uninjected state, which needs no mocking because neither key appears in any values file, and one covers the injected state so the guard cannot quietly change what a populated map returns. The injected case pins `scheduling` and `branding` as whole YAML documents rather than by substring: those two had their bodies restructured rather than wrapped, and a substring assertion survives the loss of the `toYaml` that produces the document, because the key name also appears in Go's default map formatting. Against the unguarded helpers the first case fails with `index of untyped nil`. Three things left deliberately undone, so they are not mistaken for oversights. The header comments in `_cozyconfig.tpl` say where these values come from but not what the helpers now do when they are absent, which is the contract this change creates and would be better stated next to the helpers than only in the suite. `Chart.yaml` in cozy-lib-tests still carries `name: quota` from when that chart covered one helper family, so the runner prints `### Chart [ quota ]` for a suite that now covers three. And the mixed state, one map injected and the other not, has no case of its own; the two guards are independent so composition covers it, and a third case would pin nothing new. Relates to #3646. ### Screenshots <!-- REQUIRED for UI changes: attach screenshots or screen recordings demonstrating the visual impact of your changes. PRs with UI changes without screenshots will not be merged. --> ### Downstream repositories <!-- Cozystack is upstream for repositories that are NOT kept in sync with it automatically, and nothing in CI compares the two sides, so a change here can break them silently. Which change forces what, and which file to touch, is the trigger map in docs/agents/contributing.md under "Downstream Repositories". Walk it before you tick anything. IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM TO FILL IN: 1. Walk the trigger map against the actual diff of this PR, file by file. Do not decide from the PR title or from your memory of what you changed. 2. For every repository you tick, open the follow-up PR there and link it on the same line. A ticked box with no link claims work that does not exist, which is worse than an empty box. Search that repository for an open PR or issue covering it first, and link that instead of filing a duplicate. 3. When the follow-up is out of scope, or needs a decision that is not yours to make, open an issue in that repository instead, link that, and say so in the PR body. Do not push a speculative PR into another repository just to fill a line here. 4. Do not tick "No downstream repository is affected" to make the checklist go away. If you are unsure, leave every box empty and say so in the PR body, so a human decides. --> - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: ### Release note <!-- Write a release note: - Explain what has changed internally and for users. - Start with the same `type(scope):` prefix as in the PR title - Follow the guidelines at https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md. --> ```release-note fix(cozy-lib): the cozyconfig helpers now return their documented empty defaults when the platform has not injected `_cluster` / `_namespace`, instead of aborting the render, so a chart using them can be rendered and unit-tested on its own ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved configuration handling when cluster or namespace settings are missing, preventing template rendering errors. - Preserved existing configuration values for scheduling, branding, networking, registries, and component namespaces. - **Tests** - Added coverage for configurations with missing platform values. - Added validation that injected cluster and namespace settings render correctly. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents db590b4 + 85fd592 commit 93ffb38

3 files changed

Lines changed: 115 additions & 14 deletions

File tree

‎packages/library/cozy-lib/templates/_cozyconfig.tpl‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Get the root host for the cluster.
88
Usage: {{ include "cozy-lib.root-host" . }}
99
*/}}
1010
{{- define "cozy-lib.root-host" -}}
11-
{{- (index .Values._cluster "root-host") | default "" }}
11+
{{- (index (.Values._cluster | default dict) "root-host") | default "" }}
1212
{{- end }}
1313

1414
{{/*
1515
Get the bundle name for the cluster.
1616
Usage: {{ include "cozy-lib.bundle-name" . }}
1717
*/}}
1818
{{- define "cozy-lib.bundle-name" -}}
19-
{{- (index .Values._cluster "bundle-name") | default "" }}
19+
{{- (index (.Values._cluster | default dict) "bundle-name") | default "" }}
2020
{{- end }}
2121

2222
{{/*
@@ -53,23 +53,23 @@ Get the ipv4 cluster CIDR.
5353
Usage: {{ include "cozy-lib.ipv4-cluster-cidr" . }}
5454
*/}}
5555
{{- define "cozy-lib.ipv4-cluster-cidr" -}}
56-
{{- (index .Values._cluster "ipv4-cluster-cidr") | default "" }}
56+
{{- (index (.Values._cluster | default dict) "ipv4-cluster-cidr") | default "" }}
5757
{{- end }}
5858

5959
{{/*
6060
Get the ipv4 service CIDR.
6161
Usage: {{ include "cozy-lib.ipv4-service-cidr" . }}
6262
*/}}
6363
{{- define "cozy-lib.ipv4-service-cidr" -}}
64-
{{- (index .Values._cluster "ipv4-service-cidr") | default "" }}
64+
{{- (index (.Values._cluster | default dict) "ipv4-service-cidr") | default "" }}
6565
{{- end }}
6666

6767
{{/*
6868
Get the ipv4 join CIDR.
6969
Usage: {{ include "cozy-lib.ipv4-join-cidr" . }}
7070
*/}}
7171
{{- define "cozy-lib.ipv4-join-cidr" -}}
72-
{{- (index .Values._cluster "ipv4-join-cidr") | default "" }}
72+
{{- (index (.Values._cluster | default dict) "ipv4-join-cidr") | default "" }}
7373
{{- end }}
7474

7575
{{/*
@@ -78,8 +78,9 @@ Usage: {{ include "cozy-lib.scheduling" . }}
7878
Returns: YAML string of scheduling configuration
7979
*/}}
8080
{{- define "cozy-lib.scheduling" -}}
81-
{{- if .Values._cluster.scheduling }}
82-
{{- .Values._cluster.scheduling | toYaml }}
81+
{{- $scheduling := (.Values._cluster | default dict).scheduling }}
82+
{{- if $scheduling }}
83+
{{- $scheduling | toYaml }}
8384
{{- end }}
8485
{{- end }}
8586

@@ -89,8 +90,9 @@ Usage: {{ include "cozy-lib.branding" . }}
8990
Returns: YAML string of branding configuration
9091
*/}}
9192
{{- define "cozy-lib.branding" -}}
92-
{{- if .Values._cluster.branding }}
93-
{{- .Values._cluster.branding | toYaml }}
93+
{{- $branding := (.Values._cluster | default dict).branding }}
94+
{{- if $branding }}
95+
{{- $branding | toYaml }}
9496
{{- end }}
9597
{{- end }}
9698

@@ -104,39 +106,39 @@ Get the host for this namespace.
104106
Usage: {{ include "cozy-lib.ns-host" . }}
105107
*/}}
106108
{{- define "cozy-lib.ns-host" -}}
107-
{{- .Values._namespace.host | default "" }}
109+
{{- (.Values._namespace | default dict).host | default "" }}
108110
{{- end }}
109111

110112
{{/*
111113
Get the etcd namespace reference.
112114
Usage: {{ include "cozy-lib.ns-etcd" . }}
113115
*/}}
114116
{{- define "cozy-lib.ns-etcd" -}}
115-
{{- .Values._namespace.etcd | default "" }}
117+
{{- (.Values._namespace | default dict).etcd | default "" }}
116118
{{- end }}
117119

118120
{{/*
119121
Get the ingress namespace reference.
120122
Usage: {{ include "cozy-lib.ns-ingress" . }}
121123
*/}}
122124
{{- define "cozy-lib.ns-ingress" -}}
123-
{{- .Values._namespace.ingress | default "" }}
125+
{{- (.Values._namespace | default dict).ingress | default "" }}
124126
{{- end }}
125127

126128
{{/*
127129
Get the monitoring namespace reference.
128130
Usage: {{ include "cozy-lib.ns-monitoring" . }}
129131
*/}}
130132
{{- define "cozy-lib.ns-monitoring" -}}
131-
{{- .Values._namespace.monitoring | default "" }}
133+
{{- (.Values._namespace | default dict).monitoring | default "" }}
132134
{{- end }}
133135

134136
{{/*
135137
Get the seaweedfs namespace reference.
136138
Usage: {{ include "cozy-lib.ns-seaweedfs" . }}
137139
*/}}
138140
{{- define "cozy-lib.ns-seaweedfs" -}}
139-
{{- .Values._namespace.seaweedfs | default "" }}
141+
{{- (.Values._namespace | default dict).seaweedfs | default "" }}
140142
{{- end }}
141143

142144
{{/*
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: {{ .Release.Name }}-cozyconfig
5+
data:
6+
root-host: {{ include "cozy-lib.root-host" . | quote }}
7+
bundle-name: {{ include "cozy-lib.bundle-name" . | quote }}
8+
images-registry: {{ include "cozy-lib.images-registry" . | quote }}
9+
ipv4-cluster-cidr: {{ include "cozy-lib.ipv4-cluster-cidr" . | quote }}
10+
ipv4-service-cidr: {{ include "cozy-lib.ipv4-service-cidr" . | quote }}
11+
ipv4-join-cidr: {{ include "cozy-lib.ipv4-join-cidr" . | quote }}
12+
scheduling: {{ include "cozy-lib.scheduling" . | quote }}
13+
branding: {{ include "cozy-lib.branding" . | quote }}
14+
ns-host: {{ include "cozy-lib.ns-host" . | quote }}
15+
ns-etcd: {{ include "cozy-lib.ns-etcd" . | quote }}
16+
ns-ingress: {{ include "cozy-lib.ns-ingress" . | quote }}
17+
ns-monitoring: {{ include "cozy-lib.ns-monitoring" . | quote }}
18+
ns-seaweedfs: {{ include "cozy-lib.ns-seaweedfs" . | quote }}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# ./tests/cozyconfig_test.yaml
2+
suite: cozyconfig helpers survive an uninjected _cluster and _namespace
3+
4+
templates:
5+
- templates/tests/cozyconfig.yaml
6+
7+
# `_cluster` and `_namespace` are injected by the platform through valuesFrom on
8+
# the cozystack-values Secret. A chart rendered on its own therefore has neither,
9+
# which is the state of `helm lint`, a plain `helm template`, and any suite that
10+
# does not set them. These helpers must degrade to their empty defaults there
11+
# rather than abort the render, because every consumer of this library inherits
12+
# whatever they do.
13+
14+
tests:
15+
- it: returns empty defaults when the platform injected nothing
16+
# No `set:` at all: this is the uninjected state, and it needs no mocking.
17+
asserts:
18+
- equal: {path: 'data["root-host"]', value: ""}
19+
- equal: {path: 'data["bundle-name"]', value: ""}
20+
- equal: {path: 'data["images-registry"]', value: ""}
21+
- equal: {path: 'data["ipv4-cluster-cidr"]', value: ""}
22+
- equal: {path: 'data["ipv4-service-cidr"]', value: ""}
23+
- equal: {path: 'data["ipv4-join-cidr"]', value: ""}
24+
- equal: {path: 'data["scheduling"]', value: ""}
25+
- equal: {path: 'data["branding"]', value: ""}
26+
- equal: {path: 'data["ns-host"]', value: ""}
27+
- equal: {path: 'data["ns-etcd"]', value: ""}
28+
- equal: {path: 'data["ns-ingress"]', value: ""}
29+
- equal: {path: 'data["ns-monitoring"]', value: ""}
30+
- equal: {path: 'data["ns-seaweedfs"]', value: ""}
31+
32+
- it: returns the injected values unchanged when the platform did inject them
33+
# The other half of the guard: making the helpers survive an absent map must
34+
# not change what they return when the map is present.
35+
set:
36+
_cluster:
37+
root-host: example.com
38+
bundle-name: paas-full
39+
images-registry: registry.example.com
40+
ipv4-cluster-cidr: 10.244.0.0/16
41+
ipv4-service-cidr: 10.96.0.0/16
42+
ipv4-join-cidr: 100.64.0.0/16
43+
scheduling:
44+
nodeSelector:
45+
node-role.kubernetes.io/worker: ""
46+
branding:
47+
logo:
48+
light: /logos/light.svg
49+
_namespace:
50+
host: tenant.example.com
51+
etcd: etcd-ns
52+
ingress: ingress-ns
53+
monitoring: monitoring-ns
54+
seaweedfs: seaweedfs-ns
55+
asserts:
56+
- equal: {path: 'data["root-host"]', value: "example.com"}
57+
- equal: {path: 'data["bundle-name"]', value: "paas-full"}
58+
- equal: {path: 'data["images-registry"]', value: "registry.example.com"}
59+
- equal: {path: 'data["ipv4-cluster-cidr"]', value: "10.244.0.0/16"}
60+
- equal: {path: 'data["ipv4-service-cidr"]', value: "10.96.0.0/16"}
61+
- equal: {path: 'data["ipv4-join-cidr"]', value: "100.64.0.0/16"}
62+
- equal: {path: 'data["ns-host"]', value: "tenant.example.com"}
63+
- equal: {path: 'data["ns-etcd"]', value: "etcd-ns"}
64+
- equal: {path: 'data["ns-ingress"]', value: "ingress-ns"}
65+
- equal: {path: 'data["ns-monitoring"]', value: "monitoring-ns"}
66+
- equal: {path: 'data["ns-seaweedfs"]', value: "seaweedfs-ns"}
67+
# scheduling and branding are the two helpers whose body was rewritten
68+
# rather than wrapped, so they need the present-map half most. Their
69+
# value is a whole YAML document, and it is pinned as one: a substring
70+
# match here would survive the loss of the `toYaml` that produces it,
71+
# because the key name also appears in Go's default map formatting.
72+
- equal:
73+
path: 'data["scheduling"]'
74+
value: |-
75+
nodeSelector:
76+
node-role.kubernetes.io/worker: ""
77+
- equal:
78+
path: 'data["branding"]'
79+
value: |-
80+
logo:
81+
light: /logos/light.svg

0 commit comments

Comments
 (0)