Skip to content

Commit 2c74f03

Browse files
authored
Merge pull request #45 from kcl-lang/feat-add-more-examples
feat: add more examples
2 parents 84e28e1 + 393f745 commit 2c74f03

File tree

6 files changed

+382
-5
lines changed

6 files changed

+382
-5
lines changed

web/examples/abstration/main.k

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import manifests
2+
3+
schema App:
4+
"""The application model."""
5+
name: str
6+
replicas: int = 1
7+
labels?: {str:str} = {app = name}
8+
service?: Service
9+
containers?: {str:Container}
10+
11+
schema Service:
12+
"""The service model."""
13+
$type?: str
14+
ports: [Port]
15+
16+
schema Port:
17+
"""The port model."""
18+
port: int
19+
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
20+
targetPort?: int | str
21+
22+
schema Container:
23+
"""The container model."""
24+
image: str
25+
command?: [str]
26+
args?: [str]
27+
env?: [Env]
28+
volumes?: [Volume]
29+
resources?: Resource
30+
ports: [ContainerPort]
31+
32+
schema ContainerPort:
33+
"""The container port model."""
34+
name?: str
35+
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
36+
containerPort: int
37+
38+
check:
39+
1 <= containerPort <= 65535, "containerPort must be between 1 and 65535, inclusive"
40+
41+
schema Env:
42+
name: str
43+
value: str
44+
45+
schema Volume:
46+
source: str
47+
path: str
48+
target: str
49+
readOnly?: bool = False
50+
51+
schema Resource:
52+
limits?: {str:}
53+
requests?: {str:}
54+
55+
kubernetesRender = lambda a: App {
56+
# Construct the deployment manifest.
57+
deployment = {
58+
apiVersion = "apps/v1"
59+
kind = "Deployment"
60+
metadata.name = a.name
61+
metadata.labels = a.labels
62+
spec = {
63+
replicas = a.replicas
64+
selector.matchLabels = a.labels
65+
template.metadata.labels = a.labels
66+
template.spec.containers = [{
67+
name = name
68+
image = c.image
69+
command = c.command
70+
args = c.args
71+
env = c.env
72+
volumeMounts = c.volumes
73+
resources: c.resources
74+
ports = c.ports
75+
} for name, c in a.containers]
76+
}
77+
}
78+
# Construct the service manifest.
79+
service = {
80+
apiVersion = "v1"
81+
kind = "Service"
82+
metadata.name = a.name
83+
metadata.labels = a.labels
84+
spec = {
85+
type = a.service?.$type
86+
selector = a.labels
87+
ports = a.service?.ports
88+
}
89+
}
90+
# Returns Kubernetes manifests
91+
[
92+
deployment
93+
if a.service:
94+
service
95+
96+
]
97+
}
98+
99+
app1 = App {
100+
name = "app"
101+
containers.nginx = {
102+
image = "nginx"
103+
ports = [{containerPort = 80}]
104+
}
105+
service.ports = [{port = 80}]
106+
}
107+
108+
manifests.yaml_stream(sum([kubernetesRender(a) for a in App.instances()], []))

web/examples/kubernetes/main.k

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion = "apps/v1"
2+
kind = "Deployment"
3+
metadata = {
4+
name = "nginx"
5+
labels.app = "nginx"
6+
}
7+
spec = {
8+
replicas = 3
9+
selector.matchLabels = metadata.labels
10+
template.metadata.labels = metadata.labels
11+
template.spec.containers = [
12+
{
13+
name = metadata.name
14+
image = "nginx:1.14.2"
15+
ports = [{ containerPort = 80 }]
16+
}
17+
]
18+
}

web/examples/mutation/main.k

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import yaml
2+
3+
resource = yaml.decode("""\
4+
apiVersion: apps/v1
5+
kind: Deployment
6+
metadata:
7+
name: nginx-deployment
8+
labels:
9+
app: nginx
10+
spec:
11+
replicas: 3
12+
selector:
13+
matchLabels:
14+
app: nginx
15+
template:
16+
metadata:
17+
labels:
18+
app: nginx
19+
spec:
20+
containers:
21+
- name: nginx
22+
image: nginx:1.14.2
23+
ports:
24+
- containerPort: 80
25+
""")
26+
27+
set_replicas = lambda item: {str:}, replicas: int {
28+
item | {
29+
if item?.kind == "Deployment":
30+
spec.replicas = replicas
31+
32+
}
33+
}
34+
35+
new_resource = set_replicas(resource, 5)

web/examples/validation/main.k

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import yaml
2+
import json
3+
4+
schema Server:
5+
ports: [int]
6+
7+
check:
8+
all p in ports {
9+
0 < p < 65535
10+
}
11+
12+
server1: Server = yaml.decode("""\
13+
ports:
14+
- 80
15+
- 8080
16+
""")
17+
server2: Server = json.decode("""\
18+
{
19+
"ports": [80, 8000]
20+
}
21+
""")

0 commit comments

Comments
 (0)