Kustomize KCL Function

This is an example of implementing a KCL function for Kustomize. KCL is a constraint-based record & functional domain language. Full documents of KCL can be found here.
Function Implementation
The function is implemented as an image, and built using make image.
The function is implemented as a go program, which reads a collection of input Resource configuration, passing them to KCL.
Function Configuration
See the API struct definition in main.go for documentation.
source - the KCL function code.
params - top-level arguments for KCL
Function invocation
The function is invoked by authoring a local Resource with metadata.annotations.[config.kubernetes.io/function] and running:
sudo kustomize fn run examples/set-annotation/local-resource/ --as-current-user --dry-run
This exists non-zero if the KCL code has no errors.
Guides for Developing KCL
Here's what you can do in the KCL script:
- Read resources from
option("resource_list"). The option("resource_list") complies with the KRM Functions Specification. You can read the input resources from option("resource_list")["items"] and the functionConfig from option("resource_list")["functionConfig"].
- Return a KPM list for output resources.
- Read the environment variables. e.g.
option("PATH").
- Read the OpenAPI schema. e.g.
option("open_api")["definitions"]["io.k8s.api.apps.v1.Deployment"]
- Return an error using
assert {condition}, {error_message}.
Library
You can directly use KCL standard libraries without importing them, such as regex.match, math.log.