Marcel van Lohuizen | 02173f8 | 2018-12-20 13:27:07 +0100 | [diff] [blame] | 1 | package kube |
| 2 | |
| 3 | kubernetes services: { |
| 4 | "\(k)": x.kubernetes & { |
| 5 | apiVersion: "v1" |
| 6 | kind: "Service" |
| 7 | |
| 8 | metadata name: x.name |
| 9 | metadata labels: x.label |
| 10 | spec selector: x.label |
| 11 | |
| 12 | spec ports: [ p for p in x.port ] |
| 13 | } for k, x in service |
| 14 | // Note that we cannot write |
| 15 | // kubernetes services "\(k)": {} for k, x in service |
| 16 | // "service" is also a field comprehension and the spec prohibits one field |
| 17 | // comprehension referencing another within the same struct. |
| 18 | // In general it is good practice to define a comprehension in the smallest |
| 19 | // struct possible. |
| 20 | } |
| 21 | |
| 22 | // TODO: with type conversions and types, if implemented: |
| 23 | // deployments :: k8s.Deployment |
| 24 | // deployments: _k8sSpec(X: x) for x in deployment |
| 25 | // This would look nicer and would allow fpr superior type checking. |
| 26 | |
| 27 | kubernetes deployments: { |
| 28 | "\(k)": (_k8sSpec & {X: x}).X.kubernetes & { |
| 29 | apiVersion: "extensions/v1beta1" |
| 30 | kind: "Deployment" |
| 31 | spec replicas: x.replicas |
| 32 | } for k, x in deployment if x.kind == "deployment" |
| 33 | } |
| 34 | |
| 35 | kubernetes statefulSets: { |
| 36 | "\(k)": (_k8sSpec & {X: x}).X.kubernetes & { |
| 37 | apiVersion: "apps/v1beta1" |
| 38 | kind: "StatefulSet" |
| 39 | spec replicas: x.replicas |
| 40 | } for k, x in deployment if x.kind == "stateful" |
| 41 | } |
| 42 | |
| 43 | kubernetes daemonSets: { |
| 44 | "\(k)": (_k8sSpec & {X: x}).X.kubernetes & { |
| 45 | apiVersion: "extensions/v1beta1" |
| 46 | kind: "DaemonSet" |
| 47 | } for k, x in deployment if x.kind == "daemon" |
| 48 | } |
| 49 | |
| 50 | kubernetes configMaps: { |
| 51 | "\(k)": { |
| 52 | apiVersion: "v1" |
| 53 | kind: "ConfigMap" |
| 54 | |
| 55 | metadata name: k |
| 56 | metadata labels component: _base.label.component |
| 57 | data: v |
| 58 | } for k, v in configMap |
| 59 | } |
| 60 | |
| 61 | // _k8sSpec injects Kubernetes definitions into a deployment |
| 62 | // Unify the deployment at X and read out kubernetes to obtain |
| 63 | // the conversion. |
| 64 | // TODO: use alias |
| 65 | _k8sSpec X kubernetes: { |
| 66 | metadata name: X.name |
| 67 | metadata labels component: X.label.component |
| 68 | |
| 69 | spec template: { |
| 70 | metadata labels: X.label |
| 71 | |
| 72 | spec containers: [{ |
| 73 | name: X.name |
| 74 | image: X.image |
| 75 | args: X.args |
| 76 | env: [ {name: k} & v for k, v in X.envSpec ] if len(X.envSpec) > 0 |
| 77 | |
| 78 | ports: [ { |
| 79 | name: k |
| 80 | containerPort: p |
| 81 | } for k, p in X.expose.port & X.port ] |
| 82 | }] |
| 83 | } |
| 84 | |
| 85 | // Volumes |
| 86 | spec template spec: { |
| 87 | volumes: [ |
| 88 | v.kubernetes & {name: v.name} for v in X.volume |
| 89 | ] if len(X.volume) > 0 |
| 90 | |
| 91 | containers: [{ |
| 92 | // TODO: using conversions this would look like: |
| 93 | // volumeMounts: [ k8s.VolumeMount(v) for v in d.volume ] |
| 94 | volumeMounts: [ { |
| 95 | name: v.name |
| 96 | mountPath: v.mountPath |
| 97 | subPath: v.subPath if v.subPath != null | true |
| 98 | readOnly: v.readOnly if v.readOnly |
| 99 | } for v in X.volume |
| 100 | ] if len(X.volume) > 0 |
| 101 | }] |
| 102 | } |
| 103 | } |