From f370bfad22a89bf209087cec5d92b975b1cf7759 Mon Sep 17 00:00:00 2001 From: peefy Date: Wed, 25 Oct 2023 14:37:00 +0800 Subject: [PATCH 1/2] feat: add set-annotations models Signed-off-by: peefy --- set-annotations/README.md | 30 ++++++++++++++++++++++++++++++ set-annotations/kcl.mod | 3 +++ set-annotations/main.k | 6 ++++++ set-annotations/suite/config.yaml | 9 +++++++++ set-annotations/suite/good.yaml | 25 +++++++++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 set-annotations/README.md create mode 100644 set-annotations/kcl.mod create mode 100644 set-annotations/main.k create mode 100644 set-annotations/suite/config.yaml create mode 100644 set-annotations/suite/good.yaml diff --git a/set-annotations/README.md b/set-annotations/README.md new file mode 100644 index 00000000..e23b070a --- /dev/null +++ b/set-annotations/README.md @@ -0,0 +1,30 @@ +## Introduction + +`set-annotations` is a kcl mutation package, which can be used to add `metadata.annotations` attributes for the Kubernetes resources. + +The KCL code is as follows: + +```python +params = option("params") or {} +# Use `k = v` to override existing annotations +annotations: {str:str} = {k = v for k, v in params.annotations or {}} +items = [item | { + metadata.annotations: annotations +} for item in option("items")] +``` + +## How to Use + +Add the source into your `KCLRun` resource and use the [kubectl kcl plugin](https://kcl-lang.io/docs/user_docs/guides/working-with-k8s/mutate-manifests/kubectl-kcl-plugin) or the [kcl operator](https://kcl-lang.io/docs/user_docs/guides/working-with-k8s/mutate-manifests/kcl-operator) to integrate this model. + +```yaml +apiVersion: krm.kcl.dev/v1alpha1 +kind: KCLRun +metadata: + name: set-annotations +spec: + params: + annotations: + config.kubernetes.io/local-config: "true" + source: oci://ghcr.io/kcl-lang/set-annotations +``` diff --git a/set-annotations/kcl.mod b/set-annotations/kcl.mod new file mode 100644 index 00000000..1dcf80ec --- /dev/null +++ b/set-annotations/kcl.mod @@ -0,0 +1,3 @@ +[package] +name = "set-annotations" +version = "0.1.0" diff --git a/set-annotations/main.k b/set-annotations/main.k new file mode 100644 index 00000000..559dc95e --- /dev/null +++ b/set-annotations/main.k @@ -0,0 +1,6 @@ +params = option("params") or {} +# Use `k = v` to override existing annotations +annotations: {str:str} = {k = v for k, v in params.annotations or {}} +items = [item | { + metadata.annotations: annotations +} for item in option("items")] diff --git a/set-annotations/suite/config.yaml b/set-annotations/suite/config.yaml new file mode 100644 index 00000000..eef8a587 --- /dev/null +++ b/set-annotations/suite/config.yaml @@ -0,0 +1,9 @@ +apiVersion: krm.kcl.dev/v1alpha1 +kind: KCLRun +metadata: + name: set-annotations +spec: + params: + annotations: + config.kubernetes.io/local-config: "true" + source: oci://ghcr.io/kcl-lang/set-annotations diff --git a/set-annotations/suite/good.yaml b/set-annotations/suite/good.yaml new file mode 100644 index 00000000..bebd7e91 --- /dev/null +++ b/set-annotations/suite/good.yaml @@ -0,0 +1,25 @@ +apiVersion: krm.kcl.dev/v1alpha1 +kind: KCLRun +metadata: + name: set-annotations + annotations: + krm.kcl.dev/version: 0.0.1 + krm.kcl.dev/type: mutation + documentation: >- + Set annotations +spec: + params: + annotations: + config.kubernetes.io/local-config: "true" + source: ./examples/mutation/set-annotations/main.k +--- +apiVersion: v1 +kind: Pod +metadata: + name: nginx +spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 From 27cb603a4f9e3839084117bc55e4179a649e33e5 Mon Sep 17 00:00:00 2001 From: peefy Date: Wed, 25 Oct 2023 14:44:05 +0800 Subject: [PATCH 2/2] docs: add description for set-annotation packages Signed-off-by: peefy --- set-annotations/kcl.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/set-annotations/kcl.mod b/set-annotations/kcl.mod index 1dcf80ec..e94d29f7 100644 --- a/set-annotations/kcl.mod +++ b/set-annotations/kcl.mod @@ -1,3 +1,4 @@ [package] name = "set-annotations" version = "0.1.0" +description = "`set-annotations` is a kcl mutation package, which can be used to add `metadata.annotations` attributes for the Kubernetes resources."