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:
"""App schema"""
name: str
replicas: int = 1
image: str
port: int
appRender = lambda a: App {
deployment = appsv1.Deployment {
apiVersion = "apps/v1" apiVersion = "apps/v1"
kind = "Deployment" kind = "Deployment"
metadata = { metadata = {
name = "nginx" name = a.name
labels.app = "nginx" labels.app = a.name
} }
spec = { spec = {
replicas = 3 replicas = a.replicas
selector.matchLabels = metadata.labels selector.matchLabels = metadata.labels
template.metadata.labels = metadata.labels template.metadata.labels = metadata.labels
template.spec.containers = [ template.spec.containers = [
{ {
name = metadata.name name = a.name
image = "nginx:1.14.2" image = a.image
ports = [{ containerPort = 80 }] ports = [{containerPort = a.port}]
} }
] ]
} }
} }
service = corev1.Service {
apiVersion = "v1"
kind = "Service"
metadata = {
name = a.name
}
spec = {
selector.app = a.name
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