-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: peefy <xpf6677@163.com>
- Loading branch information
Showing
6 changed files
with
382 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], [])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
""") |
Oops, something went wrong.