Skip to content

Commit 6d25092

Browse files
committed
refactor: judge kubernetes user guides.
1 parent 0559eff commit 6d25092

File tree

55 files changed

+779
-299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+779
-299
lines changed

versioned_docs/version-0.5.0/user_docs/guides/working-with-k8s/adopt_from_kubernetes.md renamed to docs/user_docs/guides/working-with-k8s/1-adopt-from-kubernetes.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
1-
# Adopt From Kubernetes to KCL
1+
---
2+
title: "Adopt From Kubernetes"
3+
sidebar_position: 1
4+
---
25

3-
## 1. Kubernetes OpenAPI Spec
6+
## Introduction
7+
8+
KCL provides many out of the box support for Kubernetes configuration. Through KCL tools, we can integrate Kubernetes Schema and configuration into KCL. This section will introduce how to use KCL to integrate Kubernetes.
9+
10+
## Quick Start
11+
12+
### 1. Kubernetes OpenAPI Spec
413

514
Starting from Kubernetes 1.4, the alpha support for the OpenAPI specification (known as Swagger 2.0 before it was donated to the OpenAPI Initiative) was introduced, and the API descriptions follow the [OpenAPI Spec 2.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md). And since Kubernetes 1.5, Kubernetes supports [directly extracting models from source code and then generating the OpenAPI spec file](https://github.com/kubernetes/kube-openapi) to automatically keep the specifications and documents up to date with the operation and models.
615

716
In addition, Kubernetes CRD uses [OpenAPI V3.0 validation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) to describe a custom schema (in addition to the built-in attributes apiVersion, Kind, and metadata), that APIServer uses to validate the CR during the resource creation and update phases.
817

9-
## 2. KCL OpenAPI Support
18+
### 2. KCL OpenAPI Support
1019

1120
The `kcl-openapi` tool supports extracting and generating KCL schemas from Kubernetes OpenAPI/CRD. the [KCL OpenAPI Spec](/docs/tools/cli/openapi/spec) defines the mapping between the OpenAPI specification and the KCL language features. For a quick start with the tool, see [KCL OpenAPI tool](/docs/tools/cli/openapi/)
1221

13-
## 3. Migrate From Kubernetes To KCL
22+
### 3. Migrate From Kubernetes To KCL
1423

15-
### 3.1 Write configurations based on the Kusion_Models package
24+
#### 3.1 Write configurations based on the Kusion_Models package
1625
``
1726
We provide an out-of-the-box `kusion_models` package for you to quickly start. It contains a well-designed frontend model called [`Server schema`](https://github.com/KusionStack/konfig/blob/main/base/pkg/kusion_models/kube/frontend/server.k). You can declare the configurations by initializing the `Server schema`. For the description and usage of the schema and its attributes, please refer to the [Server schema documentation](https://kusionstack.io/docs/reference/model/kusion_models/kube/frontend/doc_server).
1827
``
19-
### 3.2 Build Your Custom Frontend Models
28+
#### 3.2 Build Your Custom Frontend Models
2029

2130
The existing KCL Models may not meet your specific business requirements, then you can also design your custom frontend model package. You can design your custom models based on the pre-generated Kubernetes KCL models among all versions. And you can also develop your custom scripts to migrate your configuration data as what `kube2kcl` tool does.
2231

23-
#### 3.2.1 Get the k8s package
32+
##### 3.2.1 Get the k8s package
2433

2534
The [Kubernetes KCL models](https://github.com/orgs/KusionStack/packages/container/package/k8s) among all versions are pre-generated, you get it by executing `kpm add k8s:<version>` under your project. For detailed information about kpm usage, please refer to [kpm quick start guide](https://github.com/kcl-lang/kpm#quick-start).
2635

2736
Alternatively, if you may want to generate them yourself, please refer to [Generate KCL Packages from Kubernetes OpenAPI Specs](https://github.com/kcl-lang/kcl-openapi/blob/main/docs/generate_from_k8s_spec.md).
2837

29-
#### 3.2.2 Design Custom Frontend Models
38+
##### 3.2.2 Design Custom Frontend Models
3039

3140
Since the Kubernetes built-in models are atomistic and kind of complex to beginners, we recommend taking the native model of Kubernetes as the backend output model and designing a batch of frontend models which could become a more abstract, friendlier and simpler interface to the user. You can refer to the design pattern in the [`Server Schema in the Konfig repo`](https://github.com/KusionStack/konfig/blob/main/base/pkg/kusion_models/kube/frontend/server.k).
3241

33-
#### 3.2.3 Migrate The Configuration Data
42+
##### 3.2.3 Migrate The Configuration Data
3443

3544
You can develop your custom scripts to migrate your configuration data automatically. KCL will later provide writing scaffolding and writing guidelines for this script.
3645

37-
## 4. Migrate From Kubernetes CRD
46+
### 4. Migrate From Kubernetes CRD
3847

3948
If you developed CRDs, you can generate the KCL version of the CRD schemas and declare CRs based on that.
4049

@@ -47,3 +56,7 @@ If you developed CRDs, you can generate the KCL version of the CRD schemas and d
4756
* Define CR based on CRDs in KCL
4857
4958
You can initialize the CRD schema to define a CR, or further, you can use the generated schema as a backend model and design a frontend interface for users to initialize. The practice is similar to what `KCL Models` does on Kubernetes built-in models.
59+
60+
## Summary
61+
62+
This section provides a quick start guide for using KCL with OpenAPI and Custom Resource Definitions (CRD). KCL also supports OpenAPI through the `kcl-openapi tool`, which maps OpenAPI specifications to KCL language features.

versioned_docs/version-0.5.0/user_docs/guides/working-with-k8s/generate_k8s_manifests.md renamed to docs/user_docs/guides/working-with-k8s/2-generate-k8s-manifests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Generate and Manage Kubernetes Resource"
2+
title: "Generate Kubernetes Manifests"
33
sidebar_position: 2
44
---
55

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: "Kubectl KCL Plugin"
3+
sidebar_position: 1
4+
---
5+
6+
## Introduction
7+
8+
[Kubectl](https://kubernetes.io/docs/reference/kubectl/) is a command line tool for communicating with a Kubernetes cluster's control plane, using the Kubernetes API. You can use the `Kubectl-KCL-Plugin` to
9+
10+
+ Edit the YAML configuration in a hook way to separate data and logic for the Kubernetes manifests management.
11+
+ For multi-environment and multi-tenant scenarios, you can maintain these configurations gracefully rather than simply copy and paste.
12+
+ Validate all KRM resources using the KCL schema.
13+
14+
## Prerequisites
15+
16+
+ Install [Kubectl](https://github.com/kubernetes/kubectl)
17+
+ Install [Kubectl KCL Plugin](https://github.com/kcl-lang/kubectl-kcl)
18+
19+
## Quick Start
20+
21+
Let’s write a KCL function which add annotation `managed-by=krm-kcl` only to `Deployment` resources in the helm chart.
22+
23+
### 1. Get the Example
24+
25+
```bash
26+
git clone https://github.com/kcl-lang/kubectl-kcl.git/
27+
cd ./kubectl-kcl/examples/
28+
```
29+
30+
### 2. Test and Run
31+
32+
Run the KCL code via the `Kubectl KCL Plugin`.
33+
34+
```bash
35+
kubectl kcl run -f ./kcl-run.yaml
36+
```
37+
38+
The output yaml is
39+
40+
```yaml
41+
apiVersion: config.kubernetes.io/v1
42+
kind: ResourceList
43+
items:
44+
- apiVersion: apps/v1
45+
kind: Deployment
46+
metadata:
47+
annotations:
48+
managed-by: krm-kcl
49+
labels:
50+
app: nginx
51+
name: nginx-deployment
52+
spec:
53+
replicas: 3
54+
selector:
55+
matchLabels:
56+
app: nginx
57+
template:
58+
metadata:
59+
labels:
60+
app: nginx
61+
spec:
62+
containers:
63+
- image: "nginx:1.14.2"
64+
name: nginx
65+
ports:
66+
- containerPort: 80
67+
- apiVersion: v1
68+
kind: Service
69+
metadata:
70+
name: test
71+
spec:
72+
ports:
73+
- port: 80
74+
protocol: TCP
75+
targetPort: 9376
76+
selector:
77+
app: MyApp
78+
functionConfig:
79+
# kcl-fn-config.yaml
80+
apiVersion: krm.kcl.dev/v1alpha1
81+
kind: KCLRun
82+
metadata:
83+
# EDIT THE SOURCE!
84+
# This should be your KCL code which preloads the `ResourceList` to `option("resource_list")
85+
spec:
86+
source: |
87+
[resource | {if resource.kind == "Deployment": metadata.annotations: {"managed-by" = "krm-kcl"}} for resource in option("resource_list").items]
88+
```
89+
90+
## Guides for Developing KCL
91+
92+
Here's what you can do in the KCL code:
93+
94+
+ 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("resource_list")["items"]` and the `functionConfig` from `option("resource_list")["functionConfig"]`.
95+
+ Return a KPM list for output resources.
96+
+ Return an error using `assert {condition}, {error_message}`.
97+
98+
## More Documents and Examples
99+
100+
+ [Kubectl KCL Plugin](https://github.com/kcl-lang/kubectl-kcl)

versioned_docs/version-0.5.0/user_docs/guides/working-with-k8s/helm_kcl_plugin.md renamed to docs/user_docs/guides/working-with-k8s/3-mutate-manifests/2-helm-kcl-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Helm KCL Plugin"
3-
sidebar_position: 4
3+
sidebar_position: 2
44
---
55

66
## Introduction

docs/user_docs/guides/working-with-k8s/kustomize_kcl_plugin.md renamed to docs/user_docs/guides/working-with-k8s/3-mutate-manifests/3-kustomize-kcl-plugin.md

File renamed without changes.

docs/user_docs/guides/working-with-k8s/kpt_kcl_sdk.md renamed to docs/user_docs/guides/working-with-k8s/3-mutate-manifests/4-kpt-kcl-sdk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "KPT KCL SDK"
3-
sidebar_position: 5
3+
sidebar_position: 4
44
---
55

66
## Introduction
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Mutate or Validate Kubernetes Manifests",
3+
"position": 3
4+
}

docs/user_docs/guides/_working-with-kusion/_category_.json renamed to docs/user_docs/guides/working-with-kusion/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"label": "KusionStack",
3-
"position": 11
3+
"position": 13
44
}
File renamed without changes.

i18n/en/docusaurus-plugin-content-docs/current.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,5 +722,13 @@
722722
"sidebar.user_docs.category.CI Integration": {
723723
"message": "CI Integration",
724724
"description": "The label for category CI Integration in sidebar user_docs"
725+
},
726+
"sidebar.docs.category.Mutate or Validate Kubernetes Manifests": {
727+
"message": "Mutate or Validate Kubernetes Manifests",
728+
"description": "The label for category Mutate or Validate Kubernetes Manifests in sidebar docs"
729+
},
730+
"sidebar.user_docs.category.Mutate or Validate Kubernetes Manifests": {
731+
"message": "Mutate or Validate Kubernetes Manifests",
732+
"description": "The label for category Mutate or Validate Kubernetes Manifests in sidebar user_docs"
725733
}
726734
}

0 commit comments

Comments
 (0)