Skip to content

Commit

Permalink
Improve helm module types and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Dec 21, 2024
1 parent d9309b8 commit 44f9d6c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
78 changes: 51 additions & 27 deletions modules/helm/main.k
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module provides an interface for the kclx Helm plugin.
"""
import regex
import kcl_plugin.helm as helm_plugin

schema Chart:
Expand All @@ -9,50 +10,73 @@ schema Chart:
Attributes
----------
chart: str
The name of the chart to install.
The Helm chart name.
repoURL: str
The URL of the chart repository.
The URL of the Helm chart repository.
targetRevision: str
Version of the chart to install.
namespace: str, optional.
Namespace to install the chart into.
TargetRevision defines the semver tag for the chart's version.
releaseName: str, optional.
Name of the release.
The Helm release name to use. If omitted it will use the chart name.
namespace: str, optional.
Namespace is an optional namespace to template with.
project: str, optional.
Project is a reference to the project this chart's releases belong to,
e.g. the Argo AppProject. This is used to segregate Helm chart caches.
helmVersion: str, default is "v3", optional.
HelmVersion is the version of Helm to use. One of: [v2, v3].
skipCRDs: bool, default is False, optional.
Set to `True` to skip installing CRDs.
Set to `True` to skip the custom resource definition installation step
(Helm's `--skip-crds`).
passCredentials: bool, default is False, optional.
Set to `True` to pass credentials to all domains (Helm's `--pass-credentials`).
values: any, default is {}, optional.
Chart values.
Specifies Helm values to be passed to helm template.
"""
chart: str
repoURL: str
targetRevision: str
releaseName?: str
namespace?: str
project?: str
helmVersion?: str = "v3"
skipCRDs?: bool = False
passCredentials?: bool = False
values?: any = {}

check:
not regex.match(repoURL, r"^oci://"), \
"Invalid repoURL: ${repoURL}. OCI registries must not include a scheme (e.g. `oci://`)"
any x in ["v2", "v3"] {
x == helmVersion
}, "Invalid helmVersion: ${helmVersion}. Must be one of: [v2, v3]"

template = lambda chart: Chart -> [{str:}] {
"""Render Helm chart templates using `kclx`'s `kcl_plugin.helm.template`.

Examples
--------
```kcl
helm.template(helm.Chart {
chart = "my-chart"
targetRevision = "1.0.0"
repoURL = "https://jacobcolvin.com/helm-charts"
targetRevision = "1.0.0"
values = {
foo = "bar"
bar = "foo"
}
})
```
"""
chart: str
targetRevision: str
repoURL: str
releaseName?: str
namespace?: str
project?: str
skipCRDs?: bool = False
values?: any = {}

template = lambda c: Chart -> {str:} {
helm_plugin.template(
chart=c.chart,
target_revision=c.targetRevision,
repo_url=c.repoURL,
release_name=c.releaseName,
namespace=c.namespace,
project=c.project,
skip_crds=c.skipCRDs,
values=c.values,
chart=chart.chart,
repo_url=chart.repoURL,
target_revision=chart.targetRevision,
release_name=chart.releaseName,
namespace=chart.namespace,
project=chart.project,
helm_version=chart.helmVersion,
skip_crds=chart.skipCRDs,
pass_credentials=chart.passCredentials,
values=chart.values,
)
}
21 changes: 21 additions & 0 deletions modules/helm/main_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_Chart = lambda {
https = Chart {
chart = "test-https"
repoURL = "https://example.com"
targetRevision = "0.1.0"
helmVersion = "v3"
}

oci = Chart {
chart = "test-oci"
repoURL = "example.com"
targetRevision = "0.1.0"
}

helmV2 = Chart {
chart = "test-helm-v2"
repoURL = "https://example.com"
targetRevision = "0.1.0"
helmVersion = "v2"
}
}
2 changes: 1 addition & 1 deletion pkg/helm/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
"pass_credentials": "bool",
"values": "{str:any}",
},
ResultType: "{str:any}",
ResultType: "[{str:any}]",
},
Body: func(args *plugin.MethodArgs) (*plugin.MethodResult, error) {
safeArgs := pluginutil.SafeMethodArgs{Args: args}
Expand Down

0 comments on commit 44f9d6c

Please sign in to comment.