- Firstly, download and install KCL according to the instructions, and then prepare a Kubernetes environment.
We can run the following command to show the config.
cat nginx.k
The output is
schema Nginx:
"""Schema for Nginx configuration files"""
http: Http
schema Http:
server: Server
schema Server:
listen: int | str # The attribute `listen` can be int type or a string type.
location?: Location # Optional, but must be non-empty when specified
schema Location:
root: str
index: str
nginx = Nginx {
http.server = {
listen = 80
location = {
root = "/var/www/html"
index = "index.html"
}
}
}
Run the following command
kcl nginx.k
We can get the output YAML
nginx:
http:
server:
listen: 80
location:
root: /var/www/html
index: index.html
Besides, we can dynamically receive external parameters through the KCL builtin function option
. For example, for the following KCL file (db.k), we can use the KCL command line -D
flag to receive an external dynamic parameter.
cat db.k
env: str = option("env") or "dev" # The attribute `env` has a default value "dev"
database: str = option("database")
hosts = {
dev = "postgres.dev"
stage = "postgres.stage"
prod = "postgres.prod"
}
dbConfig = {
host = hosts[env]
database = database
port = "2023"
conn = "postgres://${host}:${port}/${database}"
}
# Use the `-D` flag to input external parameters.
kcl db.k -D database="foo"
The output is
env: dev
database: foo
hosts:
dev: postgres.dev
stage: postgres.stage
prod: postgres.prod
dbConfig:
host: postgres.dev
database: foo
port: "2023"
conn: "postgres://postgres.dev:2023/foo"