From 245e1631c9ec2fcda03817b94cbc516dd0258971 Mon Sep 17 00:00:00 2001 From: Bernd Schorgers Date: Fri, 14 Mar 2025 10:38:35 +0100 Subject: [PATCH] feat(common): Release 3.7.3 (#393) --- charts/library/common/Chart.yaml | 9 ++- charts/library/common/README.md | 4 +- .../common/templates/classes/_route.tpl | 14 ++-- .../common/templates/lib/role/_validate.tpl | 2 +- .../templates/lib/rolebinding/_validate.tpl | 2 +- .../templates/lib/routes/_enabled_routes.tpl | 23 ++++++ .../common/templates/lib/routes/_primary.tpl | 21 ------ .../templates/lib/routes/_valuesToObject.tpl | 3 +- .../common/templates/render/_routes.tpl | 33 ++++----- .../unittests/rbac/validations_test.yaml | 5 +- .../unittests/route/names_test.yaml | 74 +++++++++++++++++++ .../unittests/route/presence_test.yaml | 66 ++++++++--------- charts/library/common/values.schema.json | 2 +- charts/other/app-template/Chart.yaml | 8 +- 14 files changed, 167 insertions(+), 99 deletions(-) create mode 100644 charts/library/common/templates/lib/routes/_enabled_routes.tpl delete mode 100644 charts/library/common/templates/lib/routes/_primary.tpl create mode 100644 charts/library/common/test-chart/unittests/route/names_test.yaml diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 82f450fe..4a599f08 100644 --- a/charts/library/common/Chart.yaml +++ b/charts/library/common/Chart.yaml @@ -3,7 +3,7 @@ apiVersion: v2 name: common description: Function library for Helm charts type: library -version: 3.7.2 +version: 3.7.3 kubeVersion: ">=1.22.0-0" keywords: - common @@ -12,12 +12,13 @@ home: https://github.com/bjw-s/helm-charts/tree/main/charts/library/common maintainers: - name: bjw-s email: me@bjw-s.dev +sources: + - https://github.com/bjw-s/helm-charts annotations: - org.opencontainers.image.source: "https://github.com/bjw-s/helm-charts" artifacthub.io/changes: |- - kind: fixed description: |- - The Apache 2.0 LICENSE file has been added to the chart. + Fixed name suffix for HTTPRoute - kind: fixed description: |- - Fixed non-deterministic selection of primary service and port. + Fixed incorrect default backendRefs values in HTTPRoute diff --git a/charts/library/common/README.md b/charts/library/common/README.md index 1686cf7a..bca0395b 100644 --- a/charts/library/common/README.md +++ b/charts/library/common/README.md @@ -1,6 +1,6 @@ # common -![Version: 3.7.2](https://img.shields.io/badge/Version-3.7.2-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) +![Version: 3.7.3](https://img.shields.io/badge/Version-3.7.3-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) Function library for Helm charts @@ -27,7 +27,7 @@ Include this chart as a dependency in your `Chart.yaml` e.g. # Chart.yaml dependencies: - name: common - version: 3.7.2 + version: 3.7.3 repository: https://bjw-s.github.io/helm-charts/ ``` diff --git a/charts/library/common/templates/classes/_route.tpl b/charts/library/common/templates/classes/_route.tpl index 7c369570..f57b70b0 100644 --- a/charts/library/common/templates/classes/_route.tpl +++ b/charts/library/common/templates/classes/_route.tpl @@ -43,8 +43,8 @@ metadata: spec: parentRefs: {{- range $routeObject.parentRefs }} - - group: {{ default "gateway.networking.k8s.io" .group }} - kind: {{ default "Gateway" .kind }} + - group: {{ .group | default "gateway.networking.k8s.io" }} + kind: {{ .kind | default "Gateway" }} name: {{ required (printf "parentRef name is required for %v %v" $routeKind $routeObject.name) .name }} namespace: {{ required (printf "parentRef namespace is required for %v %v" $routeKind $routeObject.name) .namespace }} {{- if .sectionName }} @@ -66,11 +66,11 @@ spec: {{ if $service -}} {{ $servicePrimaryPort = include "bjw-s.common.lib.service.primaryPort" (dict "rootContext" $rootContext "serviceObject" $service) | fromYaml -}} {{- end }} - - group: {{ default "" .group | quote}} - kind: {{ default "Service" .kind }} - name: {{ default .name $service.name }} - namespace: {{ default $rootContext.Release.Namespace .namespace }} - port: {{ default .port $servicePrimaryPort.port }} + - group: {{ .group | default "" | quote}} + kind: {{ .kind | default "Service" }} + name: {{ $service.name | default .name }} + namespace: {{ .namespace | default $rootContext.Release.Namespace }} + port: {{ .port | default $servicePrimaryPort.port }} weight: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" .weight "default" 1) }} {{- end }} {{- if or (eq $routeKind "HTTPRoute") (eq $routeKind "GRPCRoute") }} diff --git a/charts/library/common/templates/lib/role/_validate.tpl b/charts/library/common/templates/lib/role/_validate.tpl index eae77db8..2516800d 100644 --- a/charts/library/common/templates/lib/role/_validate.tpl +++ b/charts/library/common/templates/lib/role/_validate.tpl @@ -9,7 +9,7 @@ Validate Role values {{- $rules := $roleValues.rules -}} {{- if not (mustHas $type $typeList) -}} - {{- fail (printf "You selected: `%s`. Type must be one of:\n%s\n" $type ($typeList|toYaml)) -}} + {{- fail (printf "\nYou selected: `%s`. Type must be one of:\n%s\n" $type ($typeList|toYaml)) -}} {{- end -}} {{- if not $rules -}} {{- fail "Rules can't be empty" -}} diff --git a/charts/library/common/templates/lib/rolebinding/_validate.tpl b/charts/library/common/templates/lib/rolebinding/_validate.tpl index 928bdc3f..27113d64 100644 --- a/charts/library/common/templates/lib/rolebinding/_validate.tpl +++ b/charts/library/common/templates/lib/rolebinding/_validate.tpl @@ -10,7 +10,7 @@ Validate RoleBinding values {{- $roleRef := required "A roleRef is required" $roleBindingValues.roleRef -}} {{- if not (mustHas $type $typeList) -}} - {{- fail (printf "You selected: `%s`. Type must be one of:\n%s\n" $type ($typeList|toYaml)) -}} + {{- fail (printf "\nYou selected: `%s`. Type must be one of:\n%s\n" $type ($typeList|toYaml)) -}} {{- end -}} {{- if not (hasKey $roleRef "identifier") -}} diff --git a/charts/library/common/templates/lib/routes/_enabled_routes.tpl b/charts/library/common/templates/lib/routes/_enabled_routes.tpl new file mode 100644 index 00000000..e7473a9b --- /dev/null +++ b/charts/library/common/templates/lib/routes/_enabled_routes.tpl @@ -0,0 +1,23 @@ +{{/* +Return the enabled routes. +*/}} +{{- define "bjw-s.common.lib.route.enabledRoutes" -}} + {{- $rootContext := .rootContext -}} + {{- $enabledRoutes := dict -}} + + {{- range $name, $route := $rootContext.Values.route -}} + {{- if kindIs "map" $route -}} + {{- /* Enable Route by default, but allow override */ -}} + {{- $routeEnabled := true -}} + {{- if hasKey $route "enabled" -}} + {{- $routeEnabled = $route.enabled -}} + {{- end -}} + + {{- if $routeEnabled -}} + {{- $_ := set $enabledRoutes $name . -}} + {{- end -}} + {{- end -}} + {{- end -}} + + {{- $enabledRoutes | toYaml -}} +{{- end -}} diff --git a/charts/library/common/templates/lib/routes/_primary.tpl b/charts/library/common/templates/lib/routes/_primary.tpl deleted file mode 100644 index ba6cd1a4..00000000 --- a/charts/library/common/templates/lib/routes/_primary.tpl +++ /dev/null @@ -1,21 +0,0 @@ -{{/* Return the name of the primary route object */}} -{{- define "bjw-s.common.lib.route.primary" -}} - {{- $enabledRoutes := dict -}} - {{- range $name, $route := .Values.route -}} - {{- if $route.enabled -}} - {{- $_ := set $enabledRoutes $name . -}} - {{- end -}} - {{- end -}} - - {{- $result := "" -}} - {{- range $name, $route := $enabledRoutes -}} - {{- if and (hasKey $route "primary") $route.primary -}} - {{- $result = $name -}} - {{- end -}} - {{- end -}} - - {{- if not $result -}} - {{- $result = keys $enabledRoutes | first -}} - {{- end -}} - {{- $result -}} -{{- end -}} diff --git a/charts/library/common/templates/lib/routes/_valuesToObject.tpl b/charts/library/common/templates/lib/routes/_valuesToObject.tpl index 66a9a476..61382bf1 100644 --- a/charts/library/common/templates/lib/routes/_valuesToObject.tpl +++ b/charts/library/common/templates/lib/routes/_valuesToObject.tpl @@ -15,7 +15,8 @@ Convert Route values to an object {{- $objectName = printf "%s-%s" $objectName $override -}} {{- end -}} {{- else -}} - {{- if ne $identifier (include "bjw-s.common.lib.route.primary" $rootContext) -}} + {{- $enabledRoutes := (include "bjw-s.common.lib.route.enabledRoutes" (dict "rootContext" $rootContext) | fromYaml ) }} + {{- if and (not $objectValues.primary) (gt (len $enabledRoutes) 1) -}} {{- if not (eq $objectName $identifier) -}} {{- $objectName = printf "%s-%s" $objectName $identifier -}} {{- end -}} diff --git a/charts/library/common/templates/render/_routes.tpl b/charts/library/common/templates/render/_routes.tpl index 12e89caa..ae3e709c 100644 --- a/charts/library/common/templates/render/_routes.tpl +++ b/charts/library/common/templates/render/_routes.tpl @@ -1,24 +1,19 @@ -{{/* Renders the Route objects required by the chart */}} +{{/* +Renders the Route objects required by the chart +*/}} {{- define "bjw-s.common.render.routes" -}} {{- /* Generate named routes as required */ -}} - {{- range $key, $route := .Values.route }} - {{- /* Enable Route by default, but allow override */ -}} - {{- $routeEnabled := true -}} - {{- if hasKey $route "enabled" -}} - {{- $routeEnabled = $route.enabled -}} - {{- end -}} + {{- $enabledRoutes := (include "bjw-s.common.lib.route.enabledRoutes" (dict "rootContext" $) | fromYaml ) -}} + {{- range $key, $route := $enabledRoutes -}} + {{- $routeValues := (mustDeepCopy $route) -}} - {{- if $routeEnabled -}} - {{- $routeValues := (mustDeepCopy $route) -}} + {{- /* Create object from the raw Route values */ -}} + {{- $routeObject := (include "bjw-s.common.lib.route.valuesToObject" (dict "rootContext" $ "id" $key "values" $routeValues)) | fromYaml -}} - {{- /* Create object from the raw Route values */ -}} - {{- $routeObject := (include "bjw-s.common.lib.route.valuesToObject" (dict "rootContext" $ "id" $key "values" $routeValues)) | fromYaml -}} + {{- /* Perform validations on the Route before rendering */ -}} + {{- include "bjw-s.common.lib.route.validate" (dict "rootContext" $ "object" $routeObject) -}} - {{- /* Perform validations on the Route before rendering */ -}} - {{- include "bjw-s.common.lib.route.validate" (dict "rootContext" $ "object" $routeObject) -}} - - {{- /* Include the Route class */ -}} - {{- include "bjw-s.common.class.route" (dict "rootContext" $ "object" $routeObject) | nindent 0 -}} - {{- end }} - {{- end }} -{{- end }} + {{- /* Include the Route class */ -}} + {{- include "bjw-s.common.class.route" (dict "rootContext" $ "object" $routeObject) | nindent 0 -}} + {{- end -}} +{{- end -}} diff --git a/charts/library/common/test-chart/unittests/rbac/validations_test.yaml b/charts/library/common/test-chart/unittests/rbac/validations_test.yaml index 16575909..3a3091fe 100644 --- a/charts/library/common/test-chart/unittests/rbac/validations_test.yaml +++ b/charts/library/common/test-chart/unittests/rbac/validations_test.yaml @@ -19,7 +19,10 @@ tests: rules: {} asserts: - failedTemplate: - errorMessage: "You selected: `InvalidRole`. Type must be one of:" + errorMessage: | + You selected: `InvalidRole`. Type must be one of: + - Role + - ClusterRole - it: role rules can't be empty set: diff --git a/charts/library/common/test-chart/unittests/route/names_test.yaml b/charts/library/common/test-chart/unittests/route/names_test.yaml new file mode 100644 index 00000000..f76c5858 --- /dev/null +++ b/charts/library/common/test-chart/unittests/route/names_test.yaml @@ -0,0 +1,74 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: route names +templates: + - common.yaml +values: + - ../_values/controllers_main_default_container.yaml +tests: + - it: default name + set: + route.main: + enabled: true + parentRefs: + - name: test + namespace: test + documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME + asserts: + - exists: + path: metadata.name + + - it: custom name suffix + set: + route.main: + enabled: true + parentRefs: + - name: test + namespace: test + nameOverride: http + documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME-http + asserts: + - exists: + path: metadata.name + + - it: custom name suffix with template + set: + route.main: + enabled: true + parentRefs: + - name: test + namespace: test + nameOverride: "{{ .Chart.Name }}" + documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME-common-test + asserts: + - exists: + path: metadata.name + + - it: multiple routes + set: + route: + main: + parentRefs: + - name: main + namespace: main + test: + parentRefs: + - name: test + namespace: test + asserts: + - documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME-main + exists: + path: metadata.name + - documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME-test + exists: + path: metadata.name diff --git a/charts/library/common/test-chart/unittests/route/presence_test.yaml b/charts/library/common/test-chart/unittests/route/presence_test.yaml index 4c9c3224..cf4ef812 100644 --- a/charts/library/common/test-chart/unittests/route/presence_test.yaml +++ b/charts/library/common/test-chart/unittests/route/presence_test.yaml @@ -6,45 +6,39 @@ templates: values: - ../_values/controllers_main_default_container.yaml tests: - - it: default should pass + - it: an HTTPRoute is not created by default asserts: - - hasDocuments: - count: 1 - - documentIndex: 0 - not: true - isKind: - of: HTTPRoute + - not: true + containsDocument: + kind: HTTPRoute + apiVersion: gateway.networking.k8s.io/v1alpha2 + name: RELEASE-NAME - - it: explicitly disabled should pass + - it: an HTTPRoute is not created when disabled set: route.main.enabled: false asserts: - - hasDocuments: - count: 1 - - documentIndex: 0 - not: true - isKind: - of: HTTPRoute + - not: true + containsDocument: + kind: HTTPRoute + apiVersion: gateway.networking.k8s.io/v1alpha2 + name: RELEASE-NAME - - it: explicitly enabled should pass + - it: an HTTPRoute is created when explicitly enabled set: route.main: enabled: true parentRefs: - name: test namespace: test + documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME asserts: - - hasDocuments: - count: 2 - - documentIndex: 0 - not: true - isKind: - of: HTTPRoute - - documentIndex: 1 - isKind: - of: HTTPRoute + - exists: + path: metadata.name - - it: multiple enabled should pass + - it: multiple HTTPRoute are created when configured set: route: main: @@ -56,15 +50,13 @@ tests: - name: test namespace: test asserts: - - hasDocuments: - count: 3 - - documentIndex: 0 - not: true - isKind: - of: HTTPRoute - - documentIndex: 1 - isKind: - of: HTTPRoute - - documentIndex: 2 - isKind: - of: HTTPRoute + - documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME-main + exists: + path: metadata.name + - documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: RELEASE-NAME-test + exists: + path: metadata.name diff --git a/charts/library/common/values.schema.json b/charts/library/common/values.schema.json index d83b2d3e..faf683d7 100644 --- a/charts/library/common/values.schema.json +++ b/charts/library/common/values.schema.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", - "$id": "https://raw.githubusercontent.com/bjw-s/helm-charts/common-3.7.2/charts/library/common/values.schema.json", + "$id": "https://raw.githubusercontent.com/bjw-s/helm-charts/common-3.7.3/charts/library/common/values.schema.json", "type": "object", "properties": { "global": { diff --git a/charts/other/app-template/Chart.yaml b/charts/other/app-template/Chart.yaml index 557427cd..a07121f0 100644 --- a/charts/other/app-template/Chart.yaml +++ b/charts/other/app-template/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 description: A common powered chart template. This can be useful for small projects that don't have their own chart. name: app-template -version: 3.7.2 +version: 3.7.3 kubeVersion: ">=1.22.0-0" maintainers: - name: bjw-s @@ -10,14 +10,14 @@ maintainers: dependencies: - name: common repository: https://bjw-s.github.io/helm-charts - version: 3.7.2 + version: 3.7.3 sources: - https://github.com/bjw-s/helm-charts annotations: artifacthub.io/changes: |- - kind: fixed description: |- - The Apache 2.0 LICENSE file has been added to the chart. + Fixed name suffix for HTTPRoute - kind: fixed description: |- - Fixed non-deterministic selection of primary service and port. + Fixed incorrect default backendRefs values in HTTPRoute