From ba9a6e37629b6e4ddbffc13ee8fdde82c53a3a98 Mon Sep 17 00:00:00 2001 From: Scott Rigby Date: Tue, 8 Jul 2025 16:18:20 -0400 Subject: [PATCH] Minimum Helm version pattern Signed-off-by: Scott Rigby --- patterns/min-helm-version/README.md | 39 +++++++++++++++++++ patterns/min-helm-version/mychart/Chart.yaml | 5 +++ .../mychart/templates/_helpers.tpl | 8 ++++ .../mychart/templates/secret.yaml | 7 ++++ 4 files changed, 59 insertions(+) create mode 100644 patterns/min-helm-version/README.md create mode 100644 patterns/min-helm-version/mychart/Chart.yaml create mode 100644 patterns/min-helm-version/mychart/templates/_helpers.tpl create mode 100644 patterns/min-helm-version/mychart/templates/secret.yaml diff --git a/patterns/min-helm-version/README.md b/patterns/min-helm-version/README.md new file mode 100644 index 00000000..62a16f7f --- /dev/null +++ b/patterns/min-helm-version/README.md @@ -0,0 +1,39 @@ +# Define the minimum Helm version for a chart + +The [mychart](./mychart/) chart contains a simple pattern that you can use to enforce a minimum version of the Helm client for any chart. + +1. Add `annotations.minimumHelmVersion` to your `Chart.yaml`. For example: + + ```yaml + apiVersion: v2 + name: mychart + version: 0.1.0 + annotations: + minimumHelmVersion: 3.18.2 + ``` + _See [mychart/Chart.yaml](./mychart/Chart.yaml)._ +1. Add a helper function to your chart's `templates/_helpers.tpl` file, like the `mychart.validateHelmVersion` function below. Replace `mychart` with the name of your chart. + + ```tpl + {{- define "mychart.validateHelmVersion" -}} + {{- $minVersion := .Chart.Annotations.minimumHelmVersion }} + {{- $helmVersion := trimPrefix "v" .Capabilities.HelmVersion.Version }} + {{- if lt $helmVersion $minVersion }} + {{- $message := printf "\n\nThis chart requires a minimum version of Helm %s. Please upgrade your Helm version." $minVersion }} + {{- fail $message }} + {{- end }} + {{- end }} + ``` + _See [mychart/templates/_helpers.tpl](./mychart/templates/_helpers.tpl)._ +1. Include your helper function at the top of any template. Make sure this include statement is outside of any rendering conditional logic if you want to always enfore the minimum Helm version. + + ```yaml + {{- include "mychart.validateHelmVersion" . }} + apiVersion: v1 + kind: Secret + metadata: + name: mysecret + data: + foo: YmFyCg== + ``` + _See [mychart/templates/secret.yaml](./mychart/templates/secret.yaml)._ diff --git a/patterns/min-helm-version/mychart/Chart.yaml b/patterns/min-helm-version/mychart/Chart.yaml new file mode 100644 index 00000000..6051b0bf --- /dev/null +++ b/patterns/min-helm-version/mychart/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v2 +name: mychart +version: 0.1.0 +annotations: + minimumHelmVersion: 3.18.2 diff --git a/patterns/min-helm-version/mychart/templates/_helpers.tpl b/patterns/min-helm-version/mychart/templates/_helpers.tpl new file mode 100644 index 00000000..b6a2cb2d --- /dev/null +++ b/patterns/min-helm-version/mychart/templates/_helpers.tpl @@ -0,0 +1,8 @@ +{{- define "mychart.validateHelmVersion" -}} + {{- $minVersion := .Chart.Annotations.minimumHelmVersion }} + {{- $helmVersion := trimPrefix "v" .Capabilities.HelmVersion.Version }} + {{- if lt $helmVersion $minVersion }} + {{- $message := printf "\n\nThis chart requires a minimum version of Helm %s. Please upgrade your Helm version." $minVersion }} + {{- fail $message }} + {{- end }} +{{- end }} diff --git a/patterns/min-helm-version/mychart/templates/secret.yaml b/patterns/min-helm-version/mychart/templates/secret.yaml new file mode 100644 index 00000000..9b194961 --- /dev/null +++ b/patterns/min-helm-version/mychart/templates/secret.yaml @@ -0,0 +1,7 @@ +{{- include "mychart.validateHelmVersion" . }} +apiVersion: v1 +kind: Secret +metadata: + name: mysecret +data: + foo: YmFyCg==