Skip to content

Commit

Permalink
Merge pull request #13 from kcl-lang/add-set-annotations-package
Browse files Browse the repository at this point in the history
feat: add set-annotations models
  • Loading branch information
Peefy authored Oct 25, 2023
2 parents 3f57fc4 + 27cb603 commit 99cd6fb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
30 changes: 30 additions & 0 deletions set-annotations/README.md
Original file line number Diff line number Diff line change
@@ -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
```
4 changes: 4 additions & 0 deletions set-annotations/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +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."
6 changes: 6 additions & 0 deletions set-annotations/main.k
Original file line number Diff line number Diff line change
@@ -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")]
9 changes: 9 additions & 0 deletions set-annotations/suite/config.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions set-annotations/suite/good.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 99cd6fb

Please sign in to comment.