From 7e178514c6f7e4cd658ff98af72af33f37303eaa Mon Sep 17 00:00:00 2001 From: Myasnikov Daniil Date: Mon, 26 Oct 2026 14:36:41 +0000 Subject: [PATCH] fix(cozy-lib): make the cozyconfig helpers survive uninjected values `_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 rather than returning their documented empty default. Two spellings, one cause. `index .Values._cluster "root-host"` raises `index of untyped nil`, and `.Values._namespace.host` raises `nil pointer evaluating interface {}.host`. In both cases the trailing `| default ""` never runs: 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. Guard the map instead, 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. With the map present, `| default dict` returns it untouched. This changes no rendered output. Every one of the 34 charts that symlink cozy-lib was rendered before and after, both with the injected values populated and with them set to empty maps, and every result is byte-identical. Eight charts differ between any two runs because they generate passwords; rendering the unchanged tree twice reproduces exactly those eight, which is how they are excluded. What this does not do is fix a chart that fails today. The charts that cannot render standalone read the injected values directly in their own templates rather than through these helpers, so they are unaffected by a change to the library and are being addressed where they live. Twelve of the thirteen helpers here have no callers anywhere in packages/ at present; 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: they are the library's public surface, they are what a chart author copies when reaching for cluster configuration, and the broken spelling propagates by being read. `cozy-lib.image` is called from four templates, so these helpers do get adopted once they exist. Add a suite in cozy-lib-tests, which is where library behaviour is pinned because a library chart renders nothing of its own. One case covers the uninjected state, which needs no mocking since neither key is in values.yaml, and one covers the injected state so the guard cannot quietly change what a populated map returns. The injected case includes `scheduling` and `branding` specifically: those two are the helpers whose body was restructured rather than wrapped, so they are the ones whose present-map behaviour most needs pinning. Against the unguarded helpers the first case fails with `index of untyped nil`. (cherry picked from commit 93ffb38a9bb679caddce9fb86e6f043065f18c41) On release-1.6 one of these helpers does have a caller that fails today, so the two paragraphs above about callers do not hold here: tenant.ciliumPolicies calls cozy-lib.bundle-name since the isp-hosted Cilium guard was backported in 2f820f7e6. Every tenant suite that renders templates/networkpolicy.yaml without setting _cluster aborts with `index of untyped nil` in the helper, which is how the vm-exportproxy suite has failed on this branch since then. With the map guarded, bundle-name returns "" there and the policies render as on every non-isp-hosted bundle. The patch applies unchanged. Assisted-by: LLM Signed-off-by: Myasnikov Daniil --- .../cozy-lib/templates/_cozyconfig.tpl | 30 +++---- .../templates/tests/cozyconfig.yaml | 18 +++++ .../cozy-lib-tests/tests/cozyconfig_test.yaml | 81 +++++++++++++++++++ 3 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 packages/tests/cozy-lib-tests/templates/tests/cozyconfig.yaml create mode 100644 packages/tests/cozy-lib-tests/tests/cozyconfig_test.yaml diff --git a/packages/library/cozy-lib/templates/_cozyconfig.tpl b/packages/library/cozy-lib/templates/_cozyconfig.tpl index 04c891c28..3e96ab9bb 100644 --- a/packages/library/cozy-lib/templates/_cozyconfig.tpl +++ b/packages/library/cozy-lib/templates/_cozyconfig.tpl @@ -8,7 +8,7 @@ Get the root host for the cluster. Usage: {{ include "cozy-lib.root-host" . }} */}} {{- define "cozy-lib.root-host" -}} -{{- (index .Values._cluster "root-host") | default "" }} +{{- (index (.Values._cluster | default dict) "root-host") | default "" }} {{- end }} {{/* @@ -16,7 +16,7 @@ Get the bundle name for the cluster. Usage: {{ include "cozy-lib.bundle-name" . }} */}} {{- define "cozy-lib.bundle-name" -}} -{{- (index .Values._cluster "bundle-name") | default "" }} +{{- (index (.Values._cluster | default dict) "bundle-name") | default "" }} {{- end }} {{/* @@ -53,7 +53,7 @@ Get the ipv4 cluster CIDR. Usage: {{ include "cozy-lib.ipv4-cluster-cidr" . }} */}} {{- define "cozy-lib.ipv4-cluster-cidr" -}} -{{- (index .Values._cluster "ipv4-cluster-cidr") | default "" }} +{{- (index (.Values._cluster | default dict) "ipv4-cluster-cidr") | default "" }} {{- end }} {{/* @@ -61,7 +61,7 @@ Get the ipv4 service CIDR. Usage: {{ include "cozy-lib.ipv4-service-cidr" . }} */}} {{- define "cozy-lib.ipv4-service-cidr" -}} -{{- (index .Values._cluster "ipv4-service-cidr") | default "" }} +{{- (index (.Values._cluster | default dict) "ipv4-service-cidr") | default "" }} {{- end }} {{/* @@ -69,7 +69,7 @@ Get the ipv4 join CIDR. Usage: {{ include "cozy-lib.ipv4-join-cidr" . }} */}} {{- define "cozy-lib.ipv4-join-cidr" -}} -{{- (index .Values._cluster "ipv4-join-cidr") | default "" }} +{{- (index (.Values._cluster | default dict) "ipv4-join-cidr") | default "" }} {{- end }} {{/* @@ -78,8 +78,9 @@ Usage: {{ include "cozy-lib.scheduling" . }} Returns: YAML string of scheduling configuration */}} {{- define "cozy-lib.scheduling" -}} -{{- if .Values._cluster.scheduling }} -{{- .Values._cluster.scheduling | toYaml }} +{{- $scheduling := (.Values._cluster | default dict).scheduling }} +{{- if $scheduling }} +{{- $scheduling | toYaml }} {{- end }} {{- end }} @@ -89,8 +90,9 @@ Usage: {{ include "cozy-lib.branding" . }} Returns: YAML string of branding configuration */}} {{- define "cozy-lib.branding" -}} -{{- if .Values._cluster.branding }} -{{- .Values._cluster.branding | toYaml }} +{{- $branding := (.Values._cluster | default dict).branding }} +{{- if $branding }} +{{- $branding | toYaml }} {{- end }} {{- end }} @@ -104,7 +106,7 @@ Get the host for this namespace. Usage: {{ include "cozy-lib.ns-host" . }} */}} {{- define "cozy-lib.ns-host" -}} -{{- .Values._namespace.host | default "" }} +{{- (.Values._namespace | default dict).host | default "" }} {{- end }} {{/* @@ -112,7 +114,7 @@ Get the etcd namespace reference. Usage: {{ include "cozy-lib.ns-etcd" . }} */}} {{- define "cozy-lib.ns-etcd" -}} -{{- .Values._namespace.etcd | default "" }} +{{- (.Values._namespace | default dict).etcd | default "" }} {{- end }} {{/* @@ -120,7 +122,7 @@ Get the ingress namespace reference. Usage: {{ include "cozy-lib.ns-ingress" . }} */}} {{- define "cozy-lib.ns-ingress" -}} -{{- .Values._namespace.ingress | default "" }} +{{- (.Values._namespace | default dict).ingress | default "" }} {{- end }} {{/* @@ -128,7 +130,7 @@ Get the monitoring namespace reference. Usage: {{ include "cozy-lib.ns-monitoring" . }} */}} {{- define "cozy-lib.ns-monitoring" -}} -{{- .Values._namespace.monitoring | default "" }} +{{- (.Values._namespace | default dict).monitoring | default "" }} {{- end }} {{/* @@ -136,7 +138,7 @@ Get the seaweedfs namespace reference. Usage: {{ include "cozy-lib.ns-seaweedfs" . }} */}} {{- define "cozy-lib.ns-seaweedfs" -}} -{{- .Values._namespace.seaweedfs | default "" }} +{{- (.Values._namespace | default dict).seaweedfs | default "" }} {{- end }} {{/* diff --git a/packages/tests/cozy-lib-tests/templates/tests/cozyconfig.yaml b/packages/tests/cozy-lib-tests/templates/tests/cozyconfig.yaml new file mode 100644 index 000000000..dbcbb0f07 --- /dev/null +++ b/packages/tests/cozy-lib-tests/templates/tests/cozyconfig.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name }}-cozyconfig +data: + root-host: {{ include "cozy-lib.root-host" . | quote }} + bundle-name: {{ include "cozy-lib.bundle-name" . | quote }} + images-registry: {{ include "cozy-lib.images-registry" . | quote }} + ipv4-cluster-cidr: {{ include "cozy-lib.ipv4-cluster-cidr" . | quote }} + ipv4-service-cidr: {{ include "cozy-lib.ipv4-service-cidr" . | quote }} + ipv4-join-cidr: {{ include "cozy-lib.ipv4-join-cidr" . | quote }} + scheduling: {{ include "cozy-lib.scheduling" . | quote }} + branding: {{ include "cozy-lib.branding" . | quote }} + ns-host: {{ include "cozy-lib.ns-host" . | quote }} + ns-etcd: {{ include "cozy-lib.ns-etcd" . | quote }} + ns-ingress: {{ include "cozy-lib.ns-ingress" . | quote }} + ns-monitoring: {{ include "cozy-lib.ns-monitoring" . | quote }} + ns-seaweedfs: {{ include "cozy-lib.ns-seaweedfs" . | quote }} diff --git a/packages/tests/cozy-lib-tests/tests/cozyconfig_test.yaml b/packages/tests/cozy-lib-tests/tests/cozyconfig_test.yaml new file mode 100644 index 000000000..85dbb3217 --- /dev/null +++ b/packages/tests/cozy-lib-tests/tests/cozyconfig_test.yaml @@ -0,0 +1,81 @@ +# ./tests/cozyconfig_test.yaml +suite: cozyconfig helpers survive an uninjected _cluster and _namespace + +templates: + - templates/tests/cozyconfig.yaml + +# `_cluster` and `_namespace` are injected by the platform through valuesFrom on +# the cozystack-values Secret. A chart rendered on its own therefore has neither, +# which is the state of `helm lint`, a plain `helm template`, and any suite that +# does not set them. These helpers must degrade to their empty defaults there +# rather than abort the render, because every consumer of this library inherits +# whatever they do. + +tests: + - it: returns empty defaults when the platform injected nothing + # No `set:` at all: this is the uninjected state, and it needs no mocking. + asserts: + - equal: {path: 'data["root-host"]', value: ""} + - equal: {path: 'data["bundle-name"]', value: ""} + - equal: {path: 'data["images-registry"]', value: ""} + - equal: {path: 'data["ipv4-cluster-cidr"]', value: ""} + - equal: {path: 'data["ipv4-service-cidr"]', value: ""} + - equal: {path: 'data["ipv4-join-cidr"]', value: ""} + - equal: {path: 'data["scheduling"]', value: ""} + - equal: {path: 'data["branding"]', value: ""} + - equal: {path: 'data["ns-host"]', value: ""} + - equal: {path: 'data["ns-etcd"]', value: ""} + - equal: {path: 'data["ns-ingress"]', value: ""} + - equal: {path: 'data["ns-monitoring"]', value: ""} + - equal: {path: 'data["ns-seaweedfs"]', value: ""} + + - it: returns the injected values unchanged when the platform did inject them + # The other half of the guard: making the helpers survive an absent map must + # not change what they return when the map is present. + set: + _cluster: + root-host: example.com + bundle-name: paas-full + images-registry: registry.example.com + ipv4-cluster-cidr: 10.244.0.0/16 + ipv4-service-cidr: 10.96.0.0/16 + ipv4-join-cidr: 100.64.0.0/16 + scheduling: + nodeSelector: + node-role.kubernetes.io/worker: "" + branding: + logo: + light: /logos/light.svg + _namespace: + host: tenant.example.com + etcd: etcd-ns + ingress: ingress-ns + monitoring: monitoring-ns + seaweedfs: seaweedfs-ns + asserts: + - equal: {path: 'data["root-host"]', value: "example.com"} + - equal: {path: 'data["bundle-name"]', value: "paas-full"} + - equal: {path: 'data["images-registry"]', value: "registry.example.com"} + - equal: {path: 'data["ipv4-cluster-cidr"]', value: "10.244.0.0/16"} + - equal: {path: 'data["ipv4-service-cidr"]', value: "10.96.0.0/16"} + - equal: {path: 'data["ipv4-join-cidr"]', value: "100.64.0.0/16"} + - equal: {path: 'data["ns-host"]', value: "tenant.example.com"} + - equal: {path: 'data["ns-etcd"]', value: "etcd-ns"} + - equal: {path: 'data["ns-ingress"]', value: "ingress-ns"} + - equal: {path: 'data["ns-monitoring"]', value: "monitoring-ns"} + - equal: {path: 'data["ns-seaweedfs"]', value: "seaweedfs-ns"} + # scheduling and branding are the two helpers whose body was rewritten + # rather than wrapped, so they need the present-map half most. Their + # value is a whole YAML document, and it is pinned as one: a substring + # match here would survive the loss of the `toYaml` that produces it, + # because the key name also appears in Go's default map formatting. + - equal: + path: 'data["scheduling"]' + value: |- + nodeSelector: + node-role.kubernetes.io/worker: "" + - equal: + path: 'data["branding"]' + value: |- + logo: + light: /logos/light.svg