cue/format: output new comprehension format
Change-Id: Id10ae14fa8a3238dec76d766982f09bfc59e2465
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3185
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/doc/tutorial/kubernetes/manual/services/cloud.cue b/doc/tutorial/kubernetes/manual/services/cloud.cue
index 747fab5..55ffe63 100644
--- a/doc/tutorial/kubernetes/manual/services/cloud.cue
+++ b/doc/tutorial/kubernetes/manual/services/cloud.cue
@@ -32,7 +32,11 @@
env <Key>: string
envSpec <Key>: {}
- envSpec: {"\(k)" value: v for k, v in env}
+ envSpec: {
+ for k, v in env {
+ "\(k)" value: v
+ }
+ }
volume <Name>: {
name: string | *Name
@@ -60,17 +64,22 @@
}
// define services implied by deployments
-service "\(k)": {
+for k, spec in deployment if len(spec.expose.port) > 0 {
+ service "\(k)": {
- // Copy over all ports exposed from containers.
- port "\(Name)": {
- // Set default external port to Port. targetPort must be
- // the respective containerPort (Port) if it differs from port.
- port: int | *Port
- targetPort: Port if port != Port
- } for Name, Port in spec.expose.port
+ // Copy over all ports exposed from containers.
+ for Name, Port in spec.expose.port {
+ port "\(Name)": {
+ // Set default external port to Port. targetPort must be
+ // the respective containerPort (Port) if it differs from port.
+ port: int | *Port
+ if port != Port {
+ targetPort: Port
+ }
+ }
+ }
- // Copy over the labels
- label: spec.label
-
-} for k, spec in deployment if len(spec.expose.port) > 0
+ // Copy over the labels
+ label: spec.label
+ }
+}
diff --git a/doc/tutorial/kubernetes/manual/services/k8s.cue b/doc/tutorial/kubernetes/manual/services/k8s.cue
index adf88f7..74e7228 100644
--- a/doc/tutorial/kubernetes/manual/services/k8s.cue
+++ b/doc/tutorial/kubernetes/manual/services/k8s.cue
@@ -1,17 +1,18 @@
package kube
kubernetes services: {
- "\(k)": x.kubernetes & {
- apiVersion: "v1"
- kind: "Service"
+ for k, x in service {
+ "\(k)": x.kubernetes & {
+ apiVersion: "v1"
+ kind: "Service"
- metadata name: x.name
- metadata labels: x.label
- spec selector: x.label
+ metadata name: x.name
+ metadata labels: x.label
+ spec selector: x.label
- spec ports: [ p for p in x.port ]
-// jba: how does [p for p in x.port ] differ from x.port?
- } for k, x in service
+ spec ports: [ p for p in x.port ] // convert struct to list
+ }
+ }
// Note that we cannot write
// kubernetes services "\(k)": {} for k, x in service
// "service" is also a field comprehension and the spec prohibits one field
@@ -26,37 +27,45 @@
// This would look nicer and would allow for superior type checking.
kubernetes deployments: {
- "\(k)": (_k8sSpec & {X: x}).X.kubernetes & {
- apiVersion: "extensions/v1beta1"
- kind: "Deployment"
- spec replicas: x.replicas
- } for k, x in deployment if x.kind == "deployment"
+ for k, x in deployment if x.kind == "deployment" {
+ "\(k)": (_k8sSpec & {X: x}).X.kubernetes & {
+ apiVersion: "extensions/v1beta1"
+ kind: "Deployment"
+ spec replicas: x.replicas
+ }
+ }
}
kubernetes statefulSets: {
- "\(k)": (_k8sSpec & {X: x}).X.kubernetes & {
- apiVersion: "apps/v1beta1"
- kind: "StatefulSet"
- spec replicas: x.replicas
- } for k, x in deployment if x.kind == "stateful"
+ for k, x in deployment if x.kind == "stateful" {
+ "\(k)": (_k8sSpec & {X: x}).X.kubernetes & {
+ apiVersion: "apps/v1beta1"
+ kind: "StatefulSet"
+ spec replicas: x.replicas
+ }
+ }
}
kubernetes daemonSets: {
- "\(k)": (_k8sSpec & {X: x}).X.kubernetes & {
- apiVersion: "extensions/v1beta1"
- kind: "DaemonSet"
- } for k, x in deployment if x.kind == "daemon"
+ for k, x in deployment if x.kind == "daemon" {
+ "\(k)": (_k8sSpec & {X: x}).X.kubernetes & {
+ apiVersion: "extensions/v1beta1"
+ kind: "DaemonSet"
+ }
+ }
}
kubernetes configMaps: {
- "\(k)": {
- apiVersion: "v1"
- kind: "ConfigMap"
+ for k, v in configMap {
+ "\(k)": {
+ apiVersion: "v1"
+ kind: "ConfigMap"
- metadata name: k
- metadata labels component: _base.label.component
- data: v
- } for k, v in configMap
+ metadata name: k
+ metadata labels component: _base.label.component
+ data: v
+ }
+ }
}
// _k8sSpec injects Kubernetes definitions into a deployment
@@ -74,7 +83,9 @@
name: X.name
image: X.image
args: X.args
- env: [ {name: k} & v for k, v in X.envSpec ] if len(X.envSpec) > 0
+ if len(X.envSpec) > 0 {
+ env: [ {name: k} & v for k, v in X.envSpec ]
+ }
ports: [ {
name: k
@@ -85,20 +96,28 @@
// Volumes
spec template spec: {
- volumes: [
- v.kubernetes & {name: v.name} for v in X.volume
- ] if len(X.volume) > 0
+ if len(X.volume) > 0 {
+ volumes: [
+ v.kubernetes & {name: v.name} for v in X.volume
+ ]
+ }
containers: [{
// TODO: using conversions this would look like:
// volumeMounts: [ k8s.VolumeMount(v) for v in d.volume ]
- volumeMounts: [ {
- name: v.name
- mountPath: v.mountPath
- subPath: v.subPath if v.subPath != null | true
- readOnly: v.readOnly if v.readOnly
- } for v in X.volume
- ] if len(X.volume) > 0
+ if len(X.volume) > 0 {
+ volumeMounts: [ {
+ name: v.name
+ mountPath: v.mountPath
+ if v.subPath != null | true {
+ subPath: v.subPath
+ }
+ if v.readOnly {
+ readOnly: v.readOnly
+ }
+ } for v in X.volume
+ ]
+ }
}]
}
}