Skip to content

Commit

Permalink
feat: add more examples
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Aug 27, 2024
1 parent 84e28e1 commit 393f745
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 5 deletions.
108 changes: 108 additions & 0 deletions web/examples/abstration/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import manifests

schema App:
"""The application model."""
name: str
replicas: int = 1
labels?: {str:str} = {app = name}
service?: Service
containers?: {str:Container}

schema Service:
"""The service model."""
$type?: str
ports: [Port]

schema Port:
"""The port model."""
port: int
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
targetPort?: int | str

schema Container:
"""The container model."""
image: str
command?: [str]
args?: [str]
env?: [Env]
volumes?: [Volume]
resources?: Resource
ports: [ContainerPort]

schema ContainerPort:
"""The container port model."""
name?: str
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
containerPort: int

check:
1 <= containerPort <= 65535, "containerPort must be between 1 and 65535, inclusive"

schema Env:
name: str
value: str

schema Volume:
source: str
path: str
target: str
readOnly?: bool = False

schema Resource:
limits?: {str:}
requests?: {str:}

kubernetesRender = lambda a: App {
# Construct the deployment manifest.
deployment = {
apiVersion = "apps/v1"
kind = "Deployment"
metadata.name = a.name
metadata.labels = a.labels
spec = {
replicas = a.replicas
selector.matchLabels = a.labels
template.metadata.labels = a.labels
template.spec.containers = [{
name = name
image = c.image
command = c.command
args = c.args
env = c.env
volumeMounts = c.volumes
resources: c.resources
ports = c.ports
} for name, c in a.containers]
}
}
# Construct the service manifest.
service = {
apiVersion = "v1"
kind = "Service"
metadata.name = a.name
metadata.labels = a.labels
spec = {
type = a.service?.$type
selector = a.labels
ports = a.service?.ports
}
}
# Returns Kubernetes manifests
[
deployment
if a.service:
service

]
}

app1 = App {
name = "app"
containers.nginx = {
image = "nginx"
ports = [{containerPort = 80}]
}
service.ports = [{port = 80}]
}

manifests.yaml_stream(sum([kubernetesRender(a) for a in App.instances()], []))
18 changes: 18 additions & 0 deletions web/examples/kubernetes/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "nginx"
labels.app = "nginx"
}
spec = {
replicas = 3
selector.matchLabels = metadata.labels
template.metadata.labels = metadata.labels
template.spec.containers = [
{
name = metadata.name
image = "nginx:1.14.2"
ports = [{ containerPort = 80 }]
}
]
}
35 changes: 35 additions & 0 deletions web/examples/mutation/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import yaml

resource = yaml.decode("""\
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
""")

set_replicas = lambda item: {str:}, replicas: int {
item | {
if item?.kind == "Deployment":
spec.replicas = replicas

}
}

new_resource = set_replicas(resource, 5)
21 changes: 21 additions & 0 deletions web/examples/validation/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import yaml
import json

schema Server:
ports: [int]

check:
all p in ports {
0 < p < 65535
}

server1: Server = yaml.decode("""\
ports:
- 80
- 8080
""")
server2: Server = json.decode("""\
{
"ports": [80, 8000]
}
""")
Loading

0 comments on commit 393f745

Please sign in to comment.