From 425f4428ab0832f6d51cc5d56dc6dc09079314f2 Mon Sep 17 00:00:00 2001 From: Bernd Schorgers Date: Wed, 13 Mar 2024 14:26:40 +0100 Subject: [PATCH] fix(common): Release v3.0.3 (#291) Co-authored-by: Gabe Cook --- .../tests/chart/validations_test.yaml | 22 +++++++ .../tests/container/probes_test.yaml | 62 +++++++++++++++++++ .../tests/service/validations_test.yaml | 14 +++++ charts/library/common/Chart.yaml | 26 ++------ charts/library/common/README.md | 4 +- charts/library/common/schemas/service.json | 3 + .../common/templates/lib/chart/_validate.tpl | 17 +++++ .../lib/controller/_getByIdentifier.tpl | 14 +++++ .../lib/service/_primaryForController.tpl | 25 +++++--- .../templates/lib/service/_validate.tpl | 5 ++ .../common/templates/loader/_generate.tpl | 3 + charts/library/common/values.schema.json | 2 +- 12 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 charts/library/common-test/tests/chart/validations_test.yaml create mode 100644 charts/library/common-test/tests/service/validations_test.yaml create mode 100644 charts/library/common/templates/lib/chart/_validate.tpl create mode 100644 charts/library/common/templates/lib/controller/_getByIdentifier.tpl diff --git a/charts/library/common-test/tests/chart/validations_test.yaml b/charts/library/common-test/tests/chart/validations_test.yaml new file mode 100644 index 00000000..2a137ff6 --- /dev/null +++ b/charts/library/common-test/tests/chart/validations_test.yaml @@ -0,0 +1,22 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: Chart validations +templates: + - common.yaml +tests: + - it: advancedMounts invalid controller reference should fail + set: + persistence: + data: + type: configMap + name: myConfigMap + advancedMounts: + test: + main: + - path: /data/config.yaml + readOnly: false + mountPropagation: HostToContainer + subPath: config.yaml + asserts: + - failedTemplate: + errorMessage: "No enabled controller found with this identifier. (persistence item: 'data', controller: 'test')" diff --git a/charts/library/common-test/tests/container/probes_test.yaml b/charts/library/common-test/tests/container/probes_test.yaml index b39ca2b9..14484632 100644 --- a/charts/library/common-test/tests/container/probes_test.yaml +++ b/charts/library/common-test/tests/container/probes_test.yaml @@ -146,6 +146,68 @@ tests: port: 80 timeoutSeconds: 1 + - it: multiple services should pass + set: + controllers: + other: + containers: + main: + image: + repository: ghcr.io/mendhak/http-https-echo + tag: 31 + pullPolicy: IfNotPresent + probes: + liveness: + enabled: true + readiness: + enabled: true + startup: + enabled: true + service: + other: + controller: other + ports: + http: + enabled: true + port: &secondDeploymentPort 8080 + main: + controller: main + ports: + http: + enabled: true + port: &firstDeploymentPort 80 + asserts: + - documentIndex: &firstDeploymentDocument 0 + isKind: + of: Deployment + - documentIndex: *firstDeploymentDocument + equal: + path: spec.template.spec.containers[0].livenessProbe.tcpSocket.port + value: *firstDeploymentPort + - documentIndex: *firstDeploymentDocument + equal: + path: spec.template.spec.containers[0].readinessProbe.tcpSocket.port + value: *firstDeploymentPort + - documentIndex: *firstDeploymentDocument + equal: + path: spec.template.spec.containers[0].startupProbe.tcpSocket.port + value: *firstDeploymentPort + - documentIndex: &secondDeploymentDocument 1 + isKind: + of: Deployment + - documentIndex: *secondDeploymentDocument + equal: + path: spec.template.spec.containers[0].livenessProbe.tcpSocket.port + value: *secondDeploymentPort + - documentIndex: *secondDeploymentDocument + equal: + path: spec.template.spec.containers[0].readinessProbe.tcpSocket.port + value: *secondDeploymentPort + - documentIndex: *secondDeploymentDocument + equal: + path: spec.template.spec.containers[0].startupProbe.tcpSocket.port + value: *secondDeploymentPort + - it: disabled service should pass set: service: diff --git a/charts/library/common-test/tests/service/validations_test.yaml b/charts/library/common-test/tests/service/validations_test.yaml new file mode 100644 index 00000000..d7a1292f --- /dev/null +++ b/charts/library/common-test/tests/service/validations_test.yaml @@ -0,0 +1,14 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: Service validations +templates: + - common.yaml +tests: + - it: invalid controller reference should fail + set: + service: + main: + controller: other + asserts: + - failedTemplate: + errorMessage: "No enabled controller found with this identifier. (service: 'main', controller: 'other')" diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 966c3d02..7e2afd7a 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.0.2 +version: 3.0.3 kubeVersion: ">=1.22.0-0" keywords: - common @@ -16,25 +16,7 @@ annotations: artifacthub.io/changes: |- - kind: fixed description: |- - Fixed nameOverride logic to prevent duplicated name - - kind: changed + Fixed probes intermittently choosing the wrong service + - kind: fixed description: |- - BREAKING: Default objects (they used to be called main) have been commented out and will therefore no longer provide any (both expected and unexpected) default values. - - kind: changed - description: |- - BREAKING: `enableServiceLinks` is now disabled by default - - kind: changed - description: |- - BREAKING: Referencing services under Ingress paths has been separated in explicit `name` and `identifier` keys. - - kind: added - description: |- - Added support for restartPolicy field on container level. This enables Kubernetes 1.29 sidecar containers - links: - - name: Reference documentation - url: https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/ - - kind: added - description: |- - Added json-schema validation to the chart - - kind: added - description: |- - Allow referencing secrets and configMaps by identifier in persistence section + Prevent potential incorrect controller references diff --git a/charts/library/common/README.md b/charts/library/common/README.md index 62f599ed..b61bcf42 100644 --- a/charts/library/common/README.md +++ b/charts/library/common/README.md @@ -1,6 +1,6 @@ # common -![Version: 3.0.2](https://img.shields.io/badge/Version-3.0.2-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) +![Version: 3.0.3](https://img.shields.io/badge/Version-3.0.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.0.2 + version: 3.0.3 repository: https://bjw-s.github.io/helm-charts/ ``` diff --git a/charts/library/common/schemas/service.json b/charts/library/common/schemas/service.json index c70e179a..cff7f79b 100644 --- a/charts/library/common/schemas/service.json +++ b/charts/library/common/schemas/service.json @@ -101,6 +101,9 @@ "targetPort": { "type": ["string", "integer"] }, + "nodePort": { + "type": ["string", "integer"] + }, "appProtocol": { "type": "string" } diff --git a/charts/library/common/templates/lib/chart/_validate.tpl b/charts/library/common/templates/lib/chart/_validate.tpl new file mode 100644 index 00000000..0e16e203 --- /dev/null +++ b/charts/library/common/templates/lib/chart/_validate.tpl @@ -0,0 +1,17 @@ +{{/* +Validate global chart values +*/}} +{{- define "bjw-s.common.lib.chart.validate" -}} + {{- $rootContext := . -}} + + {{- /* Validate persistence values */ -}} + {{- range $persistenceKey, $persistenceValues := .Values.persistence }} + {{- /* Make sure that any advancedMounts controller references actually resolve */ -}} + {{- range $key, $advancedMount := $persistenceValues.advancedMounts -}} + {{- $mountController := include "bjw-s.common.lib.controller.getByIdentifier" (dict "rootContext" $rootContext "id" $key) -}} + {{- if empty $mountController -}} + {{- fail (printf "No enabled controller found with this identifier. (persistence item: '%s', controller: '%s')" $persistenceKey $key) -}} + {{- end -}} + {{- end -}} + {{- end -}} +{{- end -}} diff --git a/charts/library/common/templates/lib/controller/_getByIdentifier.tpl b/charts/library/common/templates/lib/controller/_getByIdentifier.tpl new file mode 100644 index 00000000..b1faaa6a --- /dev/null +++ b/charts/library/common/templates/lib/controller/_getByIdentifier.tpl @@ -0,0 +1,14 @@ +{{/* +Return a controller by its identifier. +*/}} +{{- define "bjw-s.common.lib.controller.getByIdentifier" -}} + {{- $rootContext := .rootContext -}} + {{- $identifier := .id -}} + + {{- $enabledControllers := include "bjw-s.common.lib.controller.enabledControllers" (dict "rootContext" $rootContext) | fromYaml -}} + {{- $controllerValues := get $enabledControllers $identifier -}} + + {{- if not (empty $controllerValues) -}} + {{- include "bjw-s.common.lib.controller.valuesToObject" (dict "rootContext" $rootContext "id" $identifier "values" $controllerValues) -}} + {{- end -}} +{{- end -}} diff --git a/charts/library/common/templates/lib/service/_primaryForController.tpl b/charts/library/common/templates/lib/service/_primaryForController.tpl index ebc07067..a72891b7 100644 --- a/charts/library/common/templates/lib/service/_primaryForController.tpl +++ b/charts/library/common/templates/lib/service/_primaryForController.tpl @@ -11,18 +11,27 @@ Return the primary service object for a controller {{- /* Loop over all enabled services */ -}} {{- $enabledServices := (include "bjw-s.common.lib.service.enabledServices" (dict "rootContext" $rootContext) | fromYaml ) }} {{- if $enabledServices -}} + {{- /* We are only interested in services for the specified controller */ -}} + {{- $enabledServicesForController := dict -}} {{- range $name, $service := $enabledServices -}} - {{- /* Determine the Service that has been marked as primary */ -}} - {{- if and (hasKey $service "primary") $service.primary -}} - {{- $identifier = $name -}} - {{- $result = $service -}} + {{- if eq $service.controller $controllerIdentifier -}} + {{- $_ := set $enabledServicesForController $name $service -}} {{- end -}} {{- end -}} - {{- /* Return the first Service if none has been explicitly marked as primary */ -}} - {{- if not $result -}} - {{- $identifier = keys $enabledServices | first -}} - {{- $result = get $enabledServices $identifier -}} + {{- range $name, $service := $enabledServicesForController -}} + {{- /* Determine the Service that has been marked as primary */ -}} + {{- if $service.primary -}} + {{- $identifier = $name -}} + {{- $result = $service -}} + {{- end -}} + + {{- /* Return the first Service if none has been explicitly marked as primary */ -}} + {{- if not $result -}} + {{- $firstServiceKey := keys $enabledServicesForController | first -}} + {{- $result = get $enabledServicesForController $firstServiceKey -}} + {{- $identifier = $result.identifier -}} + {{- end -}} {{- end -}} {{- include "bjw-s.common.lib.service.valuesToObject" (dict "rootContext" $rootContext "id" $identifier "values" $result) -}} diff --git a/charts/library/common/templates/lib/service/_validate.tpl b/charts/library/common/templates/lib/service/_validate.tpl index e9558cea..45fdbc4b 100644 --- a/charts/library/common/templates/lib/service/_validate.tpl +++ b/charts/library/common/templates/lib/service/_validate.tpl @@ -9,6 +9,11 @@ Validate Service values {{- fail (printf "controller field is required for Service. (service: %s)" $serviceObject.identifier) -}} {{- end -}} + {{- $serviceController := include "bjw-s.common.lib.controller.getByIdentifier" (dict "rootContext" $rootContext "id" $serviceObject.controller) -}} + {{- if empty $serviceController -}} + {{- fail (printf "No enabled controller found with this identifier. (service: '%s', controller: '%s')" $serviceObject.identifier $serviceObject.controller) -}} + {{- end -}} + {{- /* Validate Service type */ -}} {{- $validServiceTypes := (list "ClusterIP" "LoadBalancer" "NodePort" "ExternalName" "ExternalIP") -}} {{- if and $serviceObject.type (not (mustHas $serviceObject.type $validServiceTypes)) -}} diff --git a/charts/library/common/templates/loader/_generate.tpl b/charts/library/common/templates/loader/_generate.tpl index 43750162..8788d4d1 100644 --- a/charts/library/common/templates/loader/_generate.tpl +++ b/charts/library/common/templates/loader/_generate.tpl @@ -2,6 +2,9 @@ Secondary entrypoint and primary loader for the common chart */}} {{- define "bjw-s.common.loader.generate" -}} + {{- /* Run global chart validations */ -}} + {{- include "bjw-s.common.lib.chart.validate" . -}} + {{- /* Build the templates */ -}} {{- include "bjw-s.common.render.pvcs" . | nindent 0 -}} {{- include "bjw-s.common.render.serviceAccount" . | nindent 0 -}} diff --git a/charts/library/common/values.schema.json b/charts/library/common/values.schema.json index 4d7889c8..58fb0fbb 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.0.2/charts/library/common/values.schema.json", + "$id": "https://raw.githubusercontent.com/bjw-s/helm-charts/common-3.0.3/charts/library/common/values.schema.json", "type": "object", "properties": {