From ce30311bbb6eea09dbc943fe57716ac5a0c680b9 Mon Sep 17 00:00:00 2001 From: Mikhail Scherba Date: Wed, 11 Sep 2024 18:13:18 +0300 Subject: [PATCH] add kind_exists helper Signed-off-by: Mikhail Scherba --- charts/helm_lib/Chart.yaml | 2 +- charts/helm_lib/README.md | 18 +++++++++++ charts/helm_lib/templates/_kind_exists.tpl | 15 +++++++++ tests/templates/helm_lib_kind_exists.yaml | 1 + tests/tests/helm_lib_kind_exists_test.yaml | 37 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 charts/helm_lib/templates/_kind_exists.tpl create mode 100644 tests/templates/helm_lib_kind_exists.yaml create mode 100644 tests/tests/helm_lib_kind_exists_test.yaml diff --git a/charts/helm_lib/Chart.yaml b/charts/helm_lib/Chart.yaml index 4d78c5f..6336d07 100644 --- a/charts/helm_lib/Chart.yaml +++ b/charts/helm_lib/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 type: library name: deckhouse_lib_helm -version: 1.29.0 +version: 1.30.0 description: "Helm utils template definitions for Deckhouse modules." diff --git a/charts/helm_lib/README.md b/charts/helm_lib/README.md index d17d11b..c4fdb06 100644 --- a/charts/helm_lib/README.md +++ b/charts/helm_lib/README.md @@ -11,6 +11,8 @@ | **High Availability** | | [helm_lib_is_ha_to_value](#helm_lib_is_ha_to_value) | | [helm_lib_ha_enabled](#helm_lib_ha_enabled) | +| **Kind Exists** | +| [helm_lib_kind_exists](#helm_lib_kind_exists) | | **Kube Rbac Proxy** | | [helm_lib_kube_rbac_proxy_ca_certificate](#helm_lib_kube_rbac_proxy_ca_certificate) | | **Module Documentation Uri** | @@ -162,6 +164,22 @@ list: - Template context with .Values, .Chart, etc +## Kind Exists + +### helm_lib_kind_exists + + returns true if the specified resource kind (case-insensitive) is represented in the cluster + +#### Usage + +`{{ include "helm_lib_kind_exists" (list . "") }} ` + +#### Arguments + +list: +- Template context with .Values, .Chart, etc +- Kind name portion + ## Kube Rbac Proxy ### helm_lib_kube_rbac_proxy_ca_certificate diff --git a/charts/helm_lib/templates/_kind_exists.tpl b/charts/helm_lib/templates/_kind_exists.tpl new file mode 100644 index 0000000..3e827df --- /dev/null +++ b/charts/helm_lib/templates/_kind_exists.tpl @@ -0,0 +1,15 @@ +{{- /* Usage: {{ include "helm_lib_kind_exists" (list . "") }} */ -}} +{{- /* returns true if the specified resource kind (case-insensitive) is represented in the cluster */ -}} +{{- define "helm_lib_kind_exists" }} + {{- $context := index . 0 -}} {{- /* Template context with .Values, .Chart, etc */ -}} + {{- $kind_name := index . 1 -}} {{- /* Kind name portion */ -}} + {{- if eq (len $context.Capabilities.APIVersions) 0 }} + {{- fail "Helm reports no capabilities" }} + {{- end }} + {{ range $cap := $context.Capabilities.APIVersions }} + {{- if hasSuffix (lower (printf "/%s" $kind_name)) (lower $cap) }} + found + {{- break }} + {{- end }} + {{- end }} +{{- end -}} diff --git a/tests/templates/helm_lib_kind_exists.yaml b/tests/templates/helm_lib_kind_exists.yaml new file mode 100644 index 0000000..c8f15e4 --- /dev/null +++ b/tests/templates/helm_lib_kind_exists.yaml @@ -0,0 +1 @@ +result: {{ include "helm_lib_kind_exists" (list . "validatingadmissionpolicy") }} diff --git a/tests/tests/helm_lib_kind_exists_test.yaml b/tests/tests/helm_lib_kind_exists_test.yaml new file mode 100644 index 0000000..f968d94 --- /dev/null +++ b/tests/tests/helm_lib_kind_exists_test.yaml @@ -0,0 +1,37 @@ +suite: helm_lib_kind_exists +templates: + - helm_lib_kind_exists.yaml +tests: + - it: returns found for the `ValidatingAdmissionPolicy` kind + capabilities: + apiVersions: + - apps/v1/ValidatingAdmissionPolicy + asserts: + - equal: + path: "result" + value: "found" + + - it: returns found for the `ValidatingAdmissionPolicy` kind + capabilities: + apiVersions: + - apps/v1/validatingadmissionpolicy + asserts: + - equal: + path: "result" + value: "found" + + - it: returns "" for the `ValidatingAdmissionPolicy` kind + capabilities: + apiVersions: + - apps/v1/SomeOtherPolicy + asserts: + - equal: + path: "result" + value: null + + - it: returns errors for empt apiVersions + capabilities: + apiVersions: + asserts: + - failedTemplate: + errorMessage: "Helm reports no capabilities"