Skip to content

Commit 0ae8a4f

Browse files
committed
add kcl-openapi binary install script and update kcl-openapi doc
1 parent 73ac718 commit 0ae8a4f

File tree

36 files changed

+921
-1219
lines changed

36 files changed

+921
-1219
lines changed

docs/tools/cli/openapi/quick-start.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,39 @@ sidebar_position: 1
66

77
## 1. Install KCLOpenAPI Tool
88

9-
You can install the kcl-openapi tool with go install:
9+
The kcl-openapi tool can be installed in following ways:
10+
11+
- [go install](#11-go-install)
12+
- [curl|sh install (MacOS & Linux)](#12-curlsh-install-macos--linux)
13+
- [download from release](#13-dowload-from-release)
14+
15+
## 1.1 go install
16+
17+
```shell
18+
go install kcl-lang.io/kcl-openapi@latest
19+
```
20+
21+
## 1.2 Curl|sh install (MacOS & Linux)
22+
23+
If you don't have go, you can install the CLI with this one-liner:
1024

1125
```shell
12-
go install kusionstack.io/kcl-openapi@latest
26+
curl https://kcl-lang.io/scripts/install-kcl-openapi.sh | sh
1327
```
1428

15-
To verify the installation, you can run the command: `kcl-openapi generate model -h` and the following information indicates your successful installation:
29+
## 1.3 Dowload from release
30+
31+
```shell
32+
# 1. download the released binary from:
33+
# https://github.com/kcl-lang/kcl-openapi/releases
34+
35+
# 2. Unzip the package and add the binary location to PATH
36+
export PATH="<Your directory to store KCLOpenapi binary>:$PATH"
37+
```
38+
39+
## 1.4 Verify your installation
40+
41+
- To verify the installation, you can run the command: `kcl-openapi generate model -h` and the following information indicates your successful installation:
1642

1743
```shell
1844
kcl-openapi command helps you to generate KCL schema structure from K8s CRD YAML/JSON file.

docs/user_docs/guides/working-with-k8s/_from_kubernetes.md

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Adopt From Kubernetes to KCL
2+
3+
## 1. Kubernetes OpenAPI Spec
4+
5+
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.
6+
7+
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.
8+
9+
## 2. KCL OpenAPI Support
10+
11+
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/)
12+
13+
## 3. Migrate From Kubernetes To KCL
14+
15+
### 3.1 Write configurations based on the Kusion_Models package
16+
``
17+
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).
18+
``
19+
### 3.2 Build Your Custom Frontend Models
20+
21+
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.
22+
23+
#### 3.2.1 Get the k8s package
24+
25+
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).
26+
27+
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).
28+
29+
#### 3.2.2 Design Custom Frontend Models
30+
31+
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).
32+
33+
#### 3.2.3 Migrate The Configuration Data
34+
35+
You can develop your custom scripts to migrate your configuration data automatically. KCL will later provide writing scaffolding and writing guidelines for this script.
36+
37+
## 4. Migrate From Kubernetes CRD
38+
39+
If you developed CRDs, you can generate the KCL version of the CRD schemas and declare CRs based on that.
40+
41+
* Generate KCL Schema from CRD
42+
43+
```
44+
kcl-openapi generate model --crd --skip-validation -f <your_crd.yaml>
45+
```
46+
47+
* Define CR based on CRDs in KCL
48+
49+
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.

i18n/zh-CN/docusaurus-plugin-content-docs/current/tools/cli/openapi/quick-start.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@ sidebar_position: 1
66

77
## 1. 安装 KCLOpenAPI 工具
88

9-
您可通过 go install 快速安装 kcl-openapi:
9+
目前有多种安装方式可选:
10+
11+
- [通过 go install 安装](#11-通过-go-install-安装)
12+
- [通过 curl|sh 安装(仅限 MacOS & Linux)](#12-通过-curlsh-安装(仅限-MacOS-Linux))
13+
- [下载发布包](#13-下载发布包)
14+
15+
## 1.1 通过 go install 安装
16+
17+
```shell
18+
go install kcl-lang.io/kcl-openapi@latest
19+
```
20+
21+
## 1.2 通过 curl|sh 安装(仅限 MacOS & Linux)
1022

1123
```shell
12-
go install kusionstack.io/kcl-openapi@latest
24+
curl https://kcl-lang.io/scripts/install-kcl-openapi.sh | sh
25+
```
26+
27+
## 1.3 下载发布包
28+
29+
```shell
30+
# 1. 下载二进制程序
31+
# https://github.com/kcl-lang/kcl-openapi/releases
32+
33+
# 2. 解压发布包,并将命令添加至 PATH
34+
export PATH="<Your directory to store KCLOpenapi binary>:$PATH"
1335
```
1436

15-
## 1.2 验证安装结果
37+
## 1.4 验证安装结果
1638

1739
安装完成后,您可执行 `kcl-openapi generate model -h`,如果看到如下信息则说明安装成功:
1840

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# From Kubernetes
2+
3+
## 1. Kubernetes OpenAPI Spec
4+
5+
从 Kubernetes 1.4 开始,引入了对 OpenAPI 规范(在捐赠给 Open API Initiative 之前称为 swagger 2.0)的 alpha 支持,API 描述遵循 [OpenAPI 规范 2.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md),从 Kubernetes 1.5 开始,Kubernetes 能够直接从[源码自动地提取模型并生成 OpenAPI 规范](https://github.com/kubernetes/kube-openapi),自动化地保证了规范和文档与操作/模型的更新完全同步。
6+
7+
此外,Kubernetes CRD 使用 [OpenAPI v3.0 validation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) 来描述(除内置属性 apiVersion、kind、metadata 之外的)自定义 schema,在 CR 的创建和更新阶段,APIServer 会使用这个 schema 对 CR 的内容进行校验。
8+
9+
## 2. KCL OpenAPI 支持
10+
11+
KCLOpenAPI 工具支持从 OpenAPI/CRD 定义提取并生成 KCL schema. 在[KCLOpenapi Spec](/docs/tools/cli/openapi/spec)中明确定义了 OpenAPI 规范与 KCL 语言之间的映射关系。
12+
13+
## 3. 从 Kubernetes 模型迁移到 KCL
14+
15+
### 3.1 基于 kusion_models 编写配置
16+
17+
我们为你提供了一个开箱即用的 `kusion_models` 包,让你可以快速开始。其中包含一个精心设计的前端模型,称为[服务器模型](https://github.com/KusionStack/konfig/blob/main/base/pkg/kusion_models/kube/frontend/server.k)(Server schema)。你可以通过初始化 `Server schema` 来声明其配置。有关模式及其属性的说明和用法,请参阅 [Server schema 文档](https://kusionstack.io/docs/reference/model/kusion_models/kube/frontend/doc_server)
18+
19+
### 3.2 创建自定义的 models 前端模型
20+
21+
现有的 `kusion_models` 模型可能无法满足你的特定业务需求,那么你也可以设计自定义前端模型包。 您可基于预先生成的 Kubernetes KCL 包自定义您的前端模型。您还可以仿照`kube2kcl` 工具的模式,开发自定义脚本,完成配置数据的迁移。
22+
23+
#### 3.2.1 获取 Kubernetes KCL 模型
24+
25+
我们提供了预生成的各版本[Kubernetes KCL模型](https://github.com/orgs/KusionStack/packages/container/package/k8s),您可以在项目下执行 `kpm add k8s:<version>` 来获得它。有关 kpm 使用的详细信息,请参考 [kpm快速入门指南](https://github.com/kcl-lang/kpm#quick-start)
26+
27+
或者,如果您需要自己生成这些包,请参考[从 Kubernetes OpenAPI 文件生成 KCL 包](https://github.com/kcl-lang/kcl-openapi/blob/main/docs/generate_from_k8s_spec.md)
28+
29+
#### 3.2.2 编写自定义前端模型
30+
31+
由于 Kubernetes 内置模型较为原子化和复杂,我们推荐以 Kubernetes 原生模型作为后端输出的模型,对其进一步抽象,而向用户暴露一份更为友好和简单的前端模型界面,具体您可参照 Konfig 仓库中 [kusion_models Server](https://github.com/KusionStack/konfig/blob/main/base/pkg/kusion_models/kube/frontend/server.k) 模型的设计方式进行。
32+
33+
#### 3.2.3 迁移配置数据
34+
35+
对于存量的 Kubernetes 配置数据,您可以仿照 kube2Kcl 工具的做法,编写自定义的转换脚本,进行一键迁移。Kusion 后续将提供该脚本的编写脚手架和编写指南。
36+
37+
## 4. 从 Kubernetes CRD 迁移
38+
39+
如果您的项目中使用了 CRD,也可以采用类似的模式,生成 CRD 对应的 KCL schema,并基于该 schema 声明 CR。
40+
41+
* 从 CRD 生成 KCL Schema
42+
43+
```
44+
kcl-openapi generate model --crd --skip-validation -f your_crd.yaml
45+
```
46+
47+
* 使用 KCL 声明 CR
48+
49+
使用 KCL 声明 CR 的模式与声明 Kubernetes 内置模型配置的模式相同,在此不做赘述。

0 commit comments

Comments
 (0)