Skip to content

Commit 6005f65

Browse files
authored
Merge pull request #147 from Peefy/kcl-operator-guide
feat: add kcl operator documents
2 parents 35003cb + 7c01712 commit 6005f65

File tree

2 files changed

+184
-0
lines changed
  • docs/user_docs/guides/working-with-k8s/3-mutate-manifests
  • versioned_docs/version-0.5.5/user_docs/guides/working-with-k8s/3-mutate-manifests

2 files changed

+184
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: "KCL Operator"
3+
sidebar_position: 6
4+
---
5+
6+
## Introduction
7+
8+
[KCL Operator](https://github.com/kcl-lang/kcl-operator) provides cluster integration, allowing you to use Access Webhook to generate, mutate, or validate resources based on KCL configuration when apply resources to the cluster. Webhook will capture creation, application, and editing operations, and execute `KCLRun` resource on the configuration associated with each operation, and the KCL programming language can be used to
9+
10+
+ **Add** labels or annotations based on a condition.
11+
+ **Inject** a sidecar container in all KRM resources that contain a PodTemplate.
12+
+ **Validate** all KRM resources using KCL schema.
13+
+ Use an abstract model to **generate** KRM resources.
14+
15+
## Prerequisites
16+
17+
+ Install Kubectl
18+
+ Prepare a Kubernetes cluster
19+
20+
## Quick Start
21+
22+
Let’s write a KCL function which add annotation `managed-by=kcl-operator` only to Pod resources at runtime.
23+
24+
### 1. Install KCL Operator
25+
26+
```bash
27+
kubectl apply -f https://raw.githubusercontent.com/kcl-lang/kcl-operator/main/config/all.yaml
28+
```
29+
30+
Use the following command to watch and wait the pod status is Running.
31+
32+
```shell
33+
kubectl get po
34+
```
35+
36+
### 2. Deploy the KCL source
37+
38+
```bash
39+
kubectl apply -f- << EOF
40+
apiVersion: krm.kcl.dev/v1alpha1
41+
kind: KCLRun
42+
metadata:
43+
name: set-annotation
44+
spec:
45+
source: |
46+
items = [item | {
47+
metadata.annotations: {
48+
"managed-by" = "kcl-operator"
49+
}
50+
} for item in option("items")]
51+
EOF
52+
```
53+
54+
### 3. Validate the result
55+
56+
Validate the mutation result by creating a nginx Pod YAML.
57+
58+
```shell
59+
kubectl apply -f- << EOF
60+
apiVersion: v1
61+
kind: Pod
62+
metadata:
63+
name: nginx
64+
annotations:
65+
app: nginx
66+
spec:
67+
containers:
68+
- name: nginx
69+
image: nginx:1.14.2
70+
ports:
71+
- containerPort: 80
72+
EOF
73+
kubectl get po nginx -o yaml | grep kcl-operator
74+
```
75+
76+
The output is
77+
78+
```shell
79+
managed-by: kcl-operator
80+
```
81+
82+
## Guides for Developing KCL
83+
84+
Here's what you can do in the KCL code:
85+
86+
+ Read resources from `option("resource_list")`. The `option("resource_list")` complies with the [KRM Functions Specification](https://kpt.dev/book/05-developing-functions/01-functions-specification). You can read the input resources from `option("items")` and the params from `option("params")`.
87+
+ Return a KPM list for output resources.
88+
+ Return an error using `assert {condition}, {error_message}`.
89+
90+
## More Documents and Examples
91+
92+
+ [KRM KCL Spec](https://github.com/kcl-lang/krm-kcl)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: "KCL Operator"
3+
sidebar_position: 6
4+
---
5+
6+
## Introduction
7+
8+
[KCL Operator](https://github.com/kcl-lang/kcl-operator) provides cluster integration, allowing you to use Access Webhook to generate, mutate, or validate resources based on KCL configuration when apply resources to the cluster. Webhook will capture creation, application, and editing operations, and execute `KCLRun` resource on the configuration associated with each operation, and the KCL programming language can be used to
9+
10+
+ **Add** labels or annotations based on a condition.
11+
+ **Inject** a sidecar container in all KRM resources that contain a PodTemplate.
12+
+ **Validate** all KRM resources using KCL schema.
13+
+ Use an abstract model to **generate** KRM resources.
14+
15+
## Prerequisites
16+
17+
+ Install Kubectl
18+
+ Prepare a Kubernetes cluster
19+
20+
## Quick Start
21+
22+
Let’s write a KCL function which add annotation `managed-by=kcl-operator` only to Pod resources at runtime.
23+
24+
### 1. Install KCL Operator
25+
26+
```bash
27+
kubectl apply -f https://raw.githubusercontent.com/kcl-lang/kcl-operator/main/config/all.yaml
28+
```
29+
30+
Use the following command to watch and wait the pod status is Running.
31+
32+
```shell
33+
kubectl get po
34+
```
35+
36+
### 2. Deploy the KCL source
37+
38+
```bash
39+
kubectl apply -f- << EOF
40+
apiVersion: krm.kcl.dev/v1alpha1
41+
kind: KCLRun
42+
metadata:
43+
name: set-annotation
44+
spec:
45+
source: |
46+
items = [item | {
47+
metadata.annotations: {
48+
"managed-by" = "kcl-operator"
49+
}
50+
} for item in option("items")]
51+
EOF
52+
```
53+
54+
### 3. Validate the result
55+
56+
Validate the mutation result by creating a nginx Pod YAML.
57+
58+
```shell
59+
kubectl apply -f- << EOF
60+
apiVersion: v1
61+
kind: Pod
62+
metadata:
63+
name: nginx
64+
annotations:
65+
app: nginx
66+
spec:
67+
containers:
68+
- name: nginx
69+
image: nginx:1.14.2
70+
ports:
71+
- containerPort: 80
72+
EOF
73+
kubectl get po nginx -o yaml | grep kcl-operator
74+
```
75+
76+
The output is
77+
78+
```shell
79+
managed-by: kcl-operator
80+
```
81+
82+
## Guides for Developing KCL
83+
84+
Here's what you can do in the KCL code:
85+
86+
+ Read resources from `option("resource_list")`. The `option("resource_list")` complies with the [KRM Functions Specification](https://kpt.dev/book/05-developing-functions/01-functions-specification). You can read the input resources from `option("items")` and the params from `option("params")`.
87+
+ Return a KPM list for output resources.
88+
+ Return an error using `assert {condition}, {error_message}`.
89+
90+
## More Documents and Examples
91+
92+
+ [KRM KCL Spec](https://github.com/kcl-lang/krm-kcl)

0 commit comments

Comments
 (0)