You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
KCL, which is specifically used for configuration writing and policy verification in cloud-native and Kubernetes scenarios. However, it is obviously not enough to use only one language for Kubernetes configuration management until we find Helmfile.
One of the core issues to be addressed by KCL is to enhance configuration tools and provide declarative configuration verification and editing capabilities, serving to a certain extent as a glue layer for Configuration as Data (CaC) and Configuration as Data (CaD). Besides, KCL is a Python-like domain language with enhancement schema models and multiple override strategies for transforming configuration data without out-of-band access to the host filesystem and networking.
For example, the following image shows how KCL can be applied to cloud-native configuration and policy scenarios to generate, mutate, and validate Kubernetes manifests.
Core Concepts of KCL
The core concepts of KCL are Config, Schema, Lambda, and Rule.
Config
Schema
Lambda
Rule
In KCL, lambda is designed to be "pure" (for example, it prohibits modifying features such as closure variables), which means that executing the same function multiple times will yield the same results, which naturally conforms to some design specifications of the KRM Function.
In addition, we have completed initial KCL plugin support for KPT, Helm, and Kustomize.
# kcl-fn-config.yamlapiVersion: krm.kcl.dev/v1alpha1kind: KCLRunmetadata:
name: set-annotationspec:
# EDIT THE SOURCE! source: | # One line KCL transformer. [item | {metadata.annotations: {"managed-by" = "helmfile-kcl"}} for item in option("resource_list").items]
Besides, we can use a ConfigMap to store the KCL source. To use a ConfigMap as the functionConfig, the KCL script source must be specified in the data.source field. Additional parameters can be specified in the data field.
Here's an example:
apiVersion: v1kind: ConfigMapmetadata:
name: set-replicasdata:
replicas: "5"source: | resources = option("resource_list") setReplicas = lambda items, replicas { [item | { spec.replicas = int(replicas) } if item.kind == "Deployment" and item.apiVersion == "apps/v1" else {} for item in items] } setReplicas(resources.items or [], resources.functionConfig.data.replicas)
In the example above, the script accesses the replicas parameters using option("resource_list").functionConfig.data.replicas.
To use a KCLRun as the functionConfig, the KCL source must be specified in the source field. Additional parameters can be specified in the params field. The params field supports any complex data structure as long as it can be represented in YAML.
apiVersion: krm.kcl.dev/v1alpha1kind: KCLRunmetadata:
name: conditionally-add-annotationsparams:
toMatch:
config.kubernetes.io/local-config: "true"toAdd:
configmanagement.gke.io/managed: disabledspec:
source: | resource = option("resource_list") items = resource.items params = resource.functionConfig.params toMatch = params.toMatch toAdd = params.toAdd [item | { # If all annotations are matched, patch more annotations if all key, value in toMatch { item.metadata.annotations[key] == value }: metadata.annotations: toAdd } for item in items]
repositories:
- name: prometheus-communityurl: https://prometheus-community.github.io/helm-chartsreleases:
- name: prom-norbac-ubuntunamespace: prometheuschart: prometheus-community/prometheusset:
- name: rbac.createvalue: falsetransformers:
# Use KRM KCL Plugin to mutate or validate Kubernetes manifests.
- apiVersion: krm.kcl.dev/v1alpha1kind: KCLRunmetadata:
annotations:
config.kubernetes.io/function: | container: image: docker.io/peefyxpf/kustomize-kcl:v0.1.1# EDIT THE SOURCE!# This should be your KCL code which preloads the `ResourceList` to `option("resource_list").spec:
# code sourcesource: | [resource | {if resource.kind == "Deployment": metadata.annotations: {"managed-by" = "helmfile-kcl"}} for resource in option("resource_list").items]# source: path/to/file# source: oci://# source: git://
Besides, the source of KCL code can come from files, gits, and oci sources. Usually, users do not need to write too much KCL code. We provide many out-of-the-box models, including label/annotation mutation, sidecar injection, and so on
Background
KCL, which is specifically used for configuration writing and policy verification in cloud-native and Kubernetes scenarios. However, it is obviously not enough to use only one language for Kubernetes configuration management until we find Helmfile.
One of the core issues to be addressed by KCL is to enhance configuration tools and provide declarative configuration verification and editing capabilities, serving to a certain extent as a glue layer for Configuration as Data (CaC) and Configuration as Data (CaD). Besides, KCL is a Python-like domain language with enhancement schema models and multiple override strategies for transforming configuration data without out-of-band access to the host filesystem and networking.
For example, the following image shows how KCL can be applied to cloud-native configuration and policy scenarios to generate, mutate, and validate Kubernetes manifests.
Core Concepts of KCL
The core concepts of KCL are Config, Schema, Lambda, and Rule.
In KCL, lambda is designed to be "pure" (for example, it prohibits modifying features such as closure variables), which means that executing the same function multiple times will yield the same results, which naturally conforms to some design specifications of the KRM Function.
In addition, we have completed initial KCL plugin support for KPT, Helm, and Kustomize.
For these tools, KCL has the same KRM KCL user interface.
Design
KRM KCL Function
A KRM KCL function contains as follows
where:
KRM KCL Spec follows the KRM Function Spec
Function Config
Besides, we can use a
ConfigMap
to store the KCL source. To use a ConfigMap as the functionConfig, the KCL script source must be specified in the data.source field. Additional parameters can be specified in the data field.Here's an example:
In the example above, the script accesses the replicas parameters using
option("resource_list").functionConfig.data.replicas
.To use a KCLRun as the functionConfig, the KCL source must be specified in the source field. Additional parameters can be specified in the params field. The params field supports any complex data structure as long as it can be represented in YAML.
Helmfile KCL Plugin User Interface
Besides, the source of KCL code can come from files, gits, and oci sources. Usually, users do not need to write too much KCL code. We provide many out-of-the-box models, including label/annotation mutation, sidecar injection, and so on
Helmfile KCL Plugin Implementation
TODO:
Reference
The text was updated successfully, but these errors were encountered: