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