Quick Start
0. Prerequisite
- Install kpm
1. Init an Empty KCL Package
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.
kpm init my_package && cd my_package
kpm
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 kpm
to fix the dependency version. Do not modify this file manually.
kpm
initializes kcl.mod
for an empty project as shown below:
[package]
name = "my_package"
edition = "0.0.1"
version = "0.0.1"
2. Add a Dependency from OCI Registry
You can then add a dependency to the current kcl package using the kpm add
command
As shown below, taking the example of adding a package dependency named k8s
, the version of the package is 1.27.2
.
kpm add k8s:1.27.2
You can see that kpm
adds the dependency you just added to kcl.mod.
[package]
name = "my_package"
edition = "0.0.1"
version = "0.0.1"
[dependencies]
k8s = "1.27.2" # The dependency 'k8s' with version '1.27.2'
Write a kcl program that uses the content in k8s
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}]
}]
}
3. Run the KCL Code
In the my_package
directory, you can use kpm
to compile the main.k
file you just wrote.
kpm run
The output is
apiVersion: v1
kind: Pod
metadata:
name: web-app
spec:
containers:
- image: nginx
name: main-container
ports:
- containerPort: 80