[Backport release-1.6] fix(cozy-lib): make the cozyconfig helpers survive uninjected values (#5141)

## What this PR does

Backport of #3647 to release-1.6.

Since #5080 (backport of #4710) `tenant.ciliumPolicies` calls
`cozy-lib.bundle-name`, and on release-1.6 that helper still does `index
.Values._cluster "bundle-name"`, which fails with `index of untyped nil`
when `_cluster` is not set. Tenant export-proxy suite
(`tests/networkpolicy_vm_exportproxy_test.yaml`) sets no `_cluster`, so
all four cases fail and unit tests are red on every PR against
release-1.6, including batch #5089. #5080 set `_cluster: {}` only in its
own suite, that's why it passed there.

#3647 guards the map in every cozyconfig helper and adds cozy-lib-tests
suite for injected and uninjected state. It applies cleanly, diff is
identical to main.

Checked locally (helm-unittest is not installed here, CI runs it):

- export-proxy suite renders fail on current branch and pass with this
change;
- new cozyconfig suite passes, its uninjected case fails against old
helpers;
- all 34 charts using cozy-lib render the same before and after with
realistic `_cluster` / `_namespace` (apart from generated passwords),
only tenant chart with nothing injected changes, it doesn't fail in
`networkpolicy.yaml` anymore.

Plain `helm template` of tenant chart without `_cluster` still fails in
`keycloakgroups.yaml`, main fixed it separately in #3636. Unit tests are
not affected, suite renders only templates it names.

### Screenshots

Not a UI change.

### Downstream repositories

- [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

```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 commit is contained in:
myasnikovdaniil authored and GitHub committed 2026-10-30 11:18:44 +05:00
commit d7c280ca74
3 files changed
+115 -14

No files matched your search

@@ -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 }}
{{/*
@@ -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 }}
@@ -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