Skip to content

Commit

Permalink
Generate helm KCL schemas with go generate
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Jan 9, 2025
1 parent 6a17f6c commit f7e36d3
Show file tree
Hide file tree
Showing 19 changed files with 660 additions and 242 deletions.
7 changes: 7 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ tasks:
export CC=$CC_{{.C_ENV}} CXX=$CXX_{{.C_ENV}}
go test -ldflags="-s -w" -bench=. -benchmem -tags={{.BUILD_TAGS}} {{.FLAGS}} {{.PKG}}
go-gen:
desc: Generates Go code
cmds:
- |
export CC=$CC_{{.C_ENV}} CXX=$CXX_{{.C_ENV}}
go generate ./...
go-build:
desc: Builds Go binaries
vars:
Expand Down
53 changes: 53 additions & 0 deletions cmd/gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/MacroPower/kclipper/pkg/helmmodels/pluginmodule"
)

func main() {
basePath := "modules"
if err := generate(basePath); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

func generate(path string) error {
modPath := filepath.Join(path, "helm")

fcb, err := os.Create(filepath.Join(modPath, "chart_base.k"))
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer fcb.Close()
pcb := &pluginmodule.ChartBase{}
if err = pcb.GenerateKCL(fcb); err != nil {
return fmt.Errorf("failed to generate KCL: %w", err)
}

fcc, err := os.Create(filepath.Join(modPath, "chart_config.k"))
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer fcc.Close()
pcc := &pluginmodule.ChartConfig{}
if err = pcc.GenerateKCL(fcc); err != nil {
return fmt.Errorf("failed to generate KCL: %w", err)
}

fc, err := os.Create(filepath.Join(modPath, "chart.k"))
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer fc.Close()
pc := &pluginmodule.Chart{}
if err = pc.GenerateKCL(fc); err != nil {
return fmt.Errorf("failed to generate KCL: %w", err)
}

return nil
}
3 changes: 3 additions & 0 deletions gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate go run -tags=netgo cmd/gen/main.go
23 changes: 23 additions & 0 deletions modules/helm/chart.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

schema Chart(ChartBase):
r"""
Defines a Helm chart.

Attributes
----------
values : any, optional
Helm values to be passed to Helm template. These take precedence over valueFiles.
valueFiles : [str], optional
Helm value files to be passed to Helm template.
postRenderer : ({str:}) -> {str:}, optional
Lambda function to modify the Helm template output. Evaluated for each resource in the Helm template output.
"""

values?: any
valueFiles?: [str]
postRenderer?: ({str:}) -> {str:}

38 changes: 38 additions & 0 deletions modules/helm/chart_base.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

schema ChartBase:
r"""
Represents attributes common in `helm.Chart` and `helm.ChartConfig`.

Attributes
----------
chart : str, required
Helm chart name.
repoURL : str, required
URL of the Helm chart repository.
targetRevision : str, required
Semver tag for the chart's version.
releaseName : str, optional
Helm release name to use. If omitted the chart name will be used.
namespace : str, optional
Optional namespace to template with.
skipCRDs : bool, optional
Set to `True` to skip the custom resource definition installation step (Helm's `--skip-crds`).
passCredentials : bool, optional
Set to `True` to pass credentials to all domains (Helm's `--pass-credentials`).
schemaValidator : "KCL" | "HELM", optional
Validator to use for the Values schema.
"""

chart: str
repoURL: str
targetRevision: str
releaseName?: str
namespace?: str
skipCRDs?: bool
passCredentials?: bool
schemaValidator?: "KCL" | "HELM"

20 changes: 20 additions & 0 deletions modules/helm/chart_config.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

schema ChartConfig(ChartBase):
r"""
Configuration that can be defined in `charts.k`, in addition to those specified in `helm.ChartBase`.

Attributes
----------
schemaGenerator : "AUTO" | "VALUE-INFERENCE" | "URL" | "CHART-PATH" | "LOCAL-PATH" | "NONE", optional
Schema generator to use for the Values schema.
schemaPath : str, optional
Path to the schema to use, when relevant for the selected schemaGenerator.
"""

schemaGenerator?: "AUTO" | "VALUE-INFERENCE" | "URL" | "CHART-PATH" | "LOCAL-PATH" | "NONE"
schemaPath?: str

73 changes: 0 additions & 73 deletions modules/helm/main.k
Original file line number Diff line number Diff line change
@@ -1,80 +1,10 @@
"""
This module provides an interface for the kclipper Helm plugin.
"""
import regex
import file
import yaml
import kcl_plugin.helm as helm_plugin

schema ChartBase:
r"""Helm chart resource.

Attributes
----------
chart: str
The Helm chart name.
repoURL: str
The URL of the Helm chart repository.
targetRevision: str
TargetRevision defines the semver tag for the chart's version.
releaseName: str, optional.
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.
skipCRDs: bool, default is False, optional.
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`).
schemaValidator : "KCL" | "HELM", default is "KCL", optional.
The schema validator to use.
"""
chart: str
repoURL: str
targetRevision: str
releaseName?: str
namespace?: str
skipCRDs?: bool = False
passCredentials?: bool = False
schemaValidator?: "KCL" | "HELM"

check:
not regex.match(repoURL, r"^oci://"), \
"Invalid repoURL: ${repoURL}. OCI registries must not include a scheme (e.g. `oci://`)"

schema Chart(ChartBase):
"""Helm chart resource.

Attributes
----------
values: any, default is {}, optional.
Specifies Helm values to be passed to Helm template. These take precedence over valueFiles.
valueFiles: [str], default is [], optional.
Specifies Helm value files to be passed to Helm template.
preRenderer: (Chart) -> Chart, optional.
Lambda function to modify Chart before rendering the Helm template.
postRenderer: ({str:}) -> {str:}, optional.
Lambda function to modify the Helm template output. Evaluated for each resource in the Helm template output.
"""
values?: any = {}
valueFiles?: [str] = []
preRenderer?: (Chart) -> Chart
postRenderer?: ({str:}) -> {str:}

schema ChartConfig(ChartBase):
r"""
Helm Chart Configuration

Attributes
----------
schemaGenerator : "AUTO" | "VALUE-INFERENCE" | "URL" | "CHART-PATH" | "LOCAL-PATH" | "NONE", optional, default is "AUTO"
The generator to use for the Values schema.
schemaPath : str, optional.
The path to the JSON Schema to use when schemaGenerator is "URL", "CHART-PATH", or "LOCAL-PATH".
"""
schemaGenerator?: "AUTO" | "VALUE-INFERENCE" | "URL" | "CHART-PATH" | "LOCAL-PATH" | "NONE"
schemaPath?: str

type Charts = {str:ChartConfig}

template = lambda chart: Chart -> [{str:}] {
Expand All @@ -97,9 +27,6 @@ template = lambda chart: Chart -> [{str:}] {
_chart = chart
_values: {str:} = {}

if chart.preRenderer:
_chart = chart.preRenderer(_chart)

if _chart.valueFiles and len(_chart.valueFiles) > 0:
_values = {
k: v
Expand Down
3 changes: 0 additions & 3 deletions modules/helm/main_test.k
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ test_Chart = lambda {
chart = "test-oci"
repoURL = "example.com"
targetRevision = "0.1.0"
preRenderer = lambda c: Chart {
c
}
postRenderer = lambda r: {str:} {
r
}
Expand Down
124 changes: 0 additions & 124 deletions pkg/helmmodels/chart.go

This file was deleted.

Loading

0 comments on commit f7e36d3

Please sign in to comment.