- Firstly, download and install KCL according to the instructions, and then prepare a Kubernetes environment.
Create a new kcl package named my_package
. And after we have created the package my_package
, we need to go inside the package by cd my_package
to complete the following operations.
kcl mod init my_package && cd my_package
kcl
will create two kcl package configuration files: kcl.mod
and kcl.mod.lock
in the directory where you executed the command.
- my_package
|- kcl.mod
|- kcl.mod.lock
|- # You can write your kcl program directly in this directory.
kcl.mod.lock
is the file generated by kcl
to fix the dependency version. Do not modify this file manually.
kcl
initializes kcl.mod
for an empty project as shown below:
[package]
name = "my_package"
edition = "0.0.1"
version = "0.0.1"
You can then add a dependency to the current kcl package using the kcl mod add
command
As shown below, taking the example of adding a package dependency named k8s
, the version of the package is 1.29
.
kcl mod add k8s
You can see that kcl
adds the dependency you just added to kcl.mod
.
[package]
name = "my_package"
edition = "0.0.1"
version = "0.0.1"
[dependencies]
k8s = "1.28" # The dependency 'k8s' with version '1.29'
Create the main.k
file in the current package.
- my_package
|- kcl.mod
|- kcl.mod.lock
|- main.k # Your KCL program.
And write the following into the main.k
file.
# Import and use the contents of the external dependency 'k8s'.
import k8s.api.core.v1 as k8core
k8core.Pod {
metadata.name = "web-app"
spec.containers = [{
name = "main-container"
image = "nginx"
ports = [{containerPort = 80}]
}]
}
In the my_package
directory, you can use kcl
to compile the main.k
file you just wrote.
kcl run
The output is
apiVersion: v1
kind: Pod
metadata:
name: web-app
spec:
containers:
- image: nginx
name: main-container
ports:
- containerPort: 80