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