diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 3c575cb9..7b34ad75 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: 4.0.0 +version: 4.0.1 kubeVersion: ">=1.28.0-0" keywords: - common @@ -46,6 +46,9 @@ annotations: - kind: added description: |- Added support for automatically determining the target service for Ingress paths if there is only one enabled Service + - kind: added + description: |- + Added support for automatically determining the target service for Route backends if there is only one enabled Service - kind: changed description: |- **Breaking**: Standardized resource name logic for all resources. This may cause changes in the generated resource names. diff --git a/charts/library/common/README.md b/charts/library/common/README.md index ec2b539b..5279b8e7 100644 --- a/charts/library/common/README.md +++ b/charts/library/common/README.md @@ -1,6 +1,6 @@ # common -![Version: 4.0.0](https://img.shields.io/badge/Version-4.0.0-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) +![Version: 4.0.1](https://img.shields.io/badge/Version-4.0.1-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) Function library for Helm charts @@ -31,7 +31,7 @@ Include this chart as a dependency in your `Chart.yaml` e.g. # Chart.yaml dependencies: - name: common - version: 4.0.0 + version: 4.0.1 repository: https://bjw-s-labs.github.io/helm-charts/ ``` diff --git a/charts/library/common/templates/lib/routes/_getByIdentifier.tpl b/charts/library/common/templates/lib/routes/_getByIdentifier.tpl index de8d6caa..5edf0839 100644 --- a/charts/library/common/templates/lib/routes/_getByIdentifier.tpl +++ b/charts/library/common/templates/lib/routes/_getByIdentifier.tpl @@ -4,10 +4,26 @@ Return a Route object by its Identifier. {{- define "bjw-s.common.lib.route.getByIdentifier" -}} {{- $rootContext := .rootContext -}} {{- $identifier := .id -}} + + {{- $enabledServices := (include "bjw-s.common.lib.service.enabledServices" (dict "rootContext" $rootContext) | fromYaml ) -}} {{- $enabledRoutes := (include "bjw-s.common.lib.route.enabledRoutes" (dict "rootContext" $rootContext) | fromYaml ) }} {{- if (hasKey $enabledRoutes $identifier) -}} {{- $objectValues := get $enabledRoutes $identifier -}} - {{- include "bjw-s.common.lib.valuesToObject" (dict "rootContext" $rootContext "id" $identifier "values" $objectValues "itemCount" (len $enabledRoutes)) -}} + {{- $object := include "bjw-s.common.lib.valuesToObject" (dict "rootContext" $rootContext "id" $identifier "values" $objectValues "itemCount" (len $enabledRoutes)) | fromYaml -}} + + {{- /* Try to automatically determine the default Service identifier if needed and possible */ -}} + {{- if eq 1 (len $enabledServices) -}} + {{- range $object.rules -}} + {{- range .backendRefs }} + {{- $backendRef := . -}} + {{- if and (empty (dig "name" nil $backendRef)) (empty (dig "identifier" nil $backendRef)) -}} + {{- $_ := set $backendRef "identifier" ($enabledServices | keys | first) -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + + {{- $object | toYaml -}} {{- end -}} {{- end -}} diff --git a/charts/library/common/templates/lib/routes/_validate.tpl b/charts/library/common/templates/lib/routes/_validate.tpl index 776494c0..b83d4972 100644 --- a/charts/library/common/templates/lib/routes/_validate.tpl +++ b/charts/library/common/templates/lib/routes/_validate.tpl @@ -5,6 +5,21 @@ Validate Route values {{- $rootContext := .rootContext -}} {{- $routeObject := .object -}} + {{- $enabledServices := (include "bjw-s.common.lib.service.enabledServices" (dict "rootContext" $rootContext) | fromYaml ) -}} + + {{/* Verify automatic Service detection */}} + {{- if not (eq 1 (len $enabledServices)) -}} + {{- range $routeObject.rules -}} + {{- $rule := . -}} + {{- range $rule.backendRefs }} + {{- $backendRef := . -}} + {{- if and (empty (dig "name" nil $backendRef)) (empty (dig "identifier" nil $backendRef)) -}} + {{- fail (printf "Either name or identifier is required because automatic Service detection is not possible. (route: %s)" $routeObject.identifier) -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{/* Route Types */}} {{- $routeKind := $routeObject.kind | default "HTTPRoute"}} {{- if and (ne $routeKind "GRPCRoute") (ne $routeKind "HTTPRoute") (ne $routeKind "TCPRoute") (ne $routeKind "TLSRoute") (ne $routeKind "UDPRoute") }} diff --git a/charts/library/common/test-chart/unittests/route/field_backendRefs_test.yaml b/charts/library/common/test-chart/unittests/route/field_backendRefs_test.yaml index bdc0916e..0789cc13 100644 --- a/charts/library/common/test-chart/unittests/route/field_backendRefs_test.yaml +++ b/charts/library/common/test-chart/unittests/route/field_backendRefs_test.yaml @@ -60,6 +60,30 @@ tests: port: 8080 weight: 1 + - it: automatic service and port reference should pass + values: + - ../_values/service_main_default.yaml + set: + route.main: + parentRefs: + - name: parentName + namespace: parentNamespace + rules: + - backendRefs: [{}] + documentSelector: + path: $[?(@.kind == "HTTPRoute")].metadata.name + value: release-name + asserts: + - equal: + path: spec.rules[0].backendRefs[0] + value: + group: "" + kind: Service + name: release-name + namespace: NAMESPACE + port: 8081 + weight: 1 + - it: custom service reference should pass set: route.main: diff --git a/charts/library/common/test-chart/unittests/route/validations_test.yaml b/charts/library/common/test-chart/unittests/route/validations_test.yaml index e4467a4f..13c9c321 100644 --- a/charts/library/common/test-chart/unittests/route/validations_test.yaml +++ b/charts/library/common/test-chart/unittests/route/validations_test.yaml @@ -41,3 +41,40 @@ tests: asserts: - failedTemplate: errorMessage: "backend refs and request redirect filters cannot co-exist." + + - it: automatic service determination should fail when no service is enabled + set: + route.main: + parentRefs: + - name: parentName + namespace: parentNamespace + rules: + - backendRefs: + - port: 8080 + asserts: + - failedTemplate: + errorMessage: "Either name or identifier is required because automatic Service detection is not possible. (route: main)" + + - it: automatic service determination should fail when >1 service is enabled + set: + service: + main: + controller: main + ports: + ui: + port: 8082 + second: + controller: main + ports: + ui: + port: 8082 + route.main: + parentRefs: + - name: parentName + namespace: parentNamespace + rules: + - backendRefs: + - port: 8080 + asserts: + - failedTemplate: + errorMessage: "Either name or identifier is required because automatic Service detection is not possible. (route: main)" diff --git a/charts/library/common/test-chart/unittests/service/field_selector_test.yaml b/charts/library/common/test-chart/unittests/service/field_selector_test.yaml new file mode 100644 index 00000000..27e6e6b9 --- /dev/null +++ b/charts/library/common/test-chart/unittests/service/field_selector_test.yaml @@ -0,0 +1,54 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: service - fields - selector +templates: + - common.yaml +values: + - ../_values/controllers_main_default_container.yaml + - ../_values/service_main_default.yaml +tests: + - it: selector is configured by default + documentSelector: + path: $[?(@.kind == "Service")].metadata.name + value: release-name + asserts: + - equal: + path: spec.selector + value: + app.kubernetes.io/controller: main + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: RELEASE-NAME + + - it: additional selector labels can be configured + set: + service: + main: + extraSelectorLabels: + extraLabel: extraValue + documentSelector: + path: $[?(@.kind == "Service")].metadata.name + value: release-name + asserts: + - equal: + path: spec.selector + value: + app.kubernetes.io/controller: main + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: RELEASE-NAME + extraLabel: extraValue + + - it: selector can determine default controller automatically + set: + service: + main: + controller: "" + documentSelector: + path: $[?(@.kind == "Service")].metadata.name + value: release-name + asserts: + - equal: + path: spec.selector + value: + app.kubernetes.io/controller: main + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: RELEASE-NAME diff --git a/charts/library/common/test-chart/unittests/service/validations_test.yaml b/charts/library/common/test-chart/unittests/service/validations_test.yaml index 6a0d663f..984f9ca4 100644 --- a/charts/library/common/test-chart/unittests/service/validations_test.yaml +++ b/charts/library/common/test-chart/unittests/service/validations_test.yaml @@ -42,7 +42,6 @@ tests: - failedTemplate: errorMessage: "controller field is required because automatic controller detection is not possible. (service: main)" - - it: no ports enabled should fail values: - ../_values/service_main_default.yaml diff --git a/charts/library/common/values.schema.json b/charts/library/common/values.schema.json index 72eed6d8..d3d2b49e 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-labs/helm-charts/common-4.0.0/charts/library/common/values.schema.json", + "$id": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.1/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 bddab183..44b660a5 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: 4.0.0 +version: 4.0.1 kubeVersion: ">=1.28.0-0" maintainers: - name: bjw-s @@ -10,16 +10,16 @@ maintainers: dependencies: - name: common repository: https://bjw-s-labs.github.io/helm-charts - version: 4.0.0 + version: 4.0.1 sources: - https://github.com/bjw-s-labs/helm-charts annotations: artifacthub.io/changes: |- - kind: changed description: |- - Upgraded the common library to v4.0.0 + Upgraded the common library to v4.0.1 links: - name: Upgrade notes url: https://bjw-s-labs.github.io/helm-charts/docs/app-template/upgrade-instructions/ - name: Detailed release notes - url: https://github.com/bjw-s-labs/helm-charts/releases/tag/common-4.0.0 + url: https://github.com/bjw-s-labs/helm-charts/releases/tag/common-4.0.1 diff --git a/charts/other/app-template/README.md b/charts/other/app-template/README.md index a4e4bbc0..57fb84ce 100644 --- a/charts/other/app-template/README.md +++ b/charts/other/app-template/README.md @@ -1,6 +1,6 @@ # app-template -![Version: 4.0.0](https://img.shields.io/badge/Version-4.0.0-informational?style=flat-square) +![Version: 4.0.1](https://img.shields.io/badge/Version-4.0.1-informational?style=flat-square) A common powered chart template. This can be useful for small projects that don't have their own chart. @@ -12,7 +12,7 @@ Kubernetes: `>=1.28.0-0` | Repository | Name | Version | |------------|------|---------| -| https://bjw-s-labs.github.io/helm-charts | common | 4.0.0 | +| https://bjw-s-labs.github.io/helm-charts | common | 4.0.1 | ## Installing the Chart diff --git a/charts/other/app-template/schemas/helmrelease-helm-v2.schema.json b/charts/other/app-template/schemas/helmrelease-helm-v2.schema.json index e32dac7e..cb9ab432 100644 --- a/charts/other/app-template/schemas/helmrelease-helm-v2.schema.json +++ b/charts/other/app-template/schemas/helmrelease-helm-v2.schema.json @@ -726,7 +726,7 @@ "values": { "description": "Values holds the values for this Helm release.", "x-kubernetes-preserve-unknown-fields": true, - "$ref": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.0/charts/library/common/values.schema.json" + "$ref": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.1/charts/library/common/values.schema.json" }, "valuesFrom": { "description": "ValuesFrom holds references to resources containing Helm values for this HelmRelease,\nand information about how they should be merged.", diff --git a/charts/other/app-template/schemas/helmrelease-helm-v2beta2.schema.json b/charts/other/app-template/schemas/helmrelease-helm-v2beta2.schema.json index fd53fd47..6989229d 100644 --- a/charts/other/app-template/schemas/helmrelease-helm-v2beta2.schema.json +++ b/charts/other/app-template/schemas/helmrelease-helm-v2beta2.schema.json @@ -727,7 +727,7 @@ "values": { "description": "Values holds the values for this Helm release.", "x-kubernetes-preserve-unknown-fields": true, - "$ref": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.0/charts/library/common/values.schema.json" + "$ref": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.1/charts/library/common/values.schema.json" }, "valuesFrom": { "description": "ValuesFrom holds references to resources containing Helm values for this HelmRelease, and information about how they should be merged.", diff --git a/charts/other/app-template/values.schema.json b/charts/other/app-template/values.schema.json index 00e67961..bdb00822 100644 --- a/charts/other/app-template/values.schema.json +++ b/charts/other/app-template/values.schema.json @@ -1,3 +1,3 @@ { - "$ref": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.0/charts/library/common/values.schema.json" + "$ref": "https://raw.githubusercontent.com/bjw-s-labs/helm-charts/common-4.0.1/charts/library/common/values.schema.json" }