Add KCL deployment configuration and application setup for nginx

This commit is contained in:
suyiiyii 2025-03-14 11:05:33 +08:00
parent 6d793e89b3
commit c4a1f3aefd
Signed by: suyiiyii
GPG Key ID: 044704CB29B8AD85
4 changed files with 68 additions and 18 deletions

8
air.toml Normal file
View File

@ -0,0 +1,8 @@
# air.toml
root = "." # 监听的项目目录
[build]
cmd = "echo 'File changed, running KCL...' && kcl main.k" # 自定义命令
bin = "" # 不生成二进制文件
exclude_dir = ["vendor", "node_modules"] # 排除某些目录
include_ext = ["k"] # 监听 `.k` 文件

View File

@ -1,22 +1,44 @@
import k8s.api.apps.v1 as appsv1 import k8s.api.apps.v1 as appsv1
import k8s.api.core.v1 as corev1
a = appsv1.Deployment { schema App:
apiVersion = "apps/v1" """App schema"""
kind = "Deployment" name: str
metadata = { replicas: int = 1
name = "nginx" image: str
labels.app = "nginx" port: int
appRender = lambda a: App {
deployment = appsv1.Deployment {
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = a.name
labels.app = a.name
}
spec = {
replicas = a.replicas
selector.matchLabels = metadata.labels
template.metadata.labels = metadata.labels
template.spec.containers = [
{
name = a.name
image = a.image
ports = [{containerPort = a.port}]
}
]
}
} }
spec = { service = corev1.Service {
replicas = 3 apiVersion = "v1"
selector.matchLabels = metadata.labels kind = "Service"
template.metadata.labels = metadata.labels metadata = {
template.spec.containers = [ name = a.name
{ }
name = metadata.name spec = {
image = "nginx:1.14.2" selector.app = a.name
ports = [{ containerPort = 80 }] ports = [{port = a.port, targetPort = a.port}]
} }
]
} }
[deployment, service]
} }

21
main.k
View File

@ -1 +1,20 @@
The_first_kcl_program = 'Hello World!' import base
import manifests
base.App {
name = "myapp"
replicas = 2
image = "myimage"
port = 8080
}
base.App {
name = "myapp122"
replicas = 2
image = "myimage"
port = 8080
}
# base.appRender(a)
manifests.yaml_stream([base.appRender(a) for a in base.App.instances()])

1
tmp/build-errors.log Normal file
View File

@ -0,0 +1 @@
exit status 1exit status 2exit status 1exit status 1exit status 1exit status 1