Skip to content

Commit

Permalink
add kind_exists helper
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <[email protected]>
  • Loading branch information
miklezzzz committed Sep 11, 2024
1 parent 88fc24f commit ce30311
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion charts/helm_lib/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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."
18 changes: 18 additions & 0 deletions charts/helm_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** |
Expand Down Expand Up @@ -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 . "<kind-name>") }} `

#### Arguments

list:
- Template context with .Values, .Chart, etc
- Kind name portion

## Kube Rbac Proxy

### helm_lib_kube_rbac_proxy_ca_certificate
Expand Down
15 changes: 15 additions & 0 deletions charts/helm_lib/templates/_kind_exists.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- /* Usage: {{ include "helm_lib_kind_exists" (list . "<kind-name>") }} */ -}}
{{- /* 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 -}}
1 change: 1 addition & 0 deletions tests/templates/helm_lib_kind_exists.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
result: {{ include "helm_lib_kind_exists" (list . "validatingadmissionpolicy") }}
37 changes: 37 additions & 0 deletions tests/tests/helm_lib_kind_exists_test.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit ce30311

Please sign in to comment.