github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/cue/testdata/comprehensions/nested.txtar (about)

     1  -- in.cue --
     2  package kube
     3  
     4  service: "node-exporter": {
     5  	spec: {
     6  		ports: [{
     7  			name:     "metrics"
     8  			port:     9100
     9  			protocol: "TCP"
    10  		}]
    11  	}
    12  }
    13  deployment: "node-exporter": {
    14  	spec: template: {
    15  		spec: {
    16  			containers: [{
    17  				ports: [{
    18  					containerPort: 9100
    19  					name:          "scrape"
    20  				}]
    21  			}]
    22  		}
    23  	}
    24  }
    25  service: [ID=string]: {
    26  	spec: {
    27  		ports: [...{
    28  			port:     int
    29  			protocol: *"TCP" | "UDP"
    30  			name:     string | *"client"
    31  		}]
    32  	}
    33  }
    34  deployment: [ID=string]: {}
    35  deployment: [ID=_]: _spec & {}
    36  _spec: {
    37  	_name: string
    38  	spec: template: {
    39  		spec: containers: [{name: _name}]
    40  	}
    41  }
    42  _spec: spec: template: spec: containers: [...{
    43  	ports: [...{
    44  		_export: *true | false // include the port in the service
    45  	}]
    46  }]
    47  for k, v in deployment {
    48  	service: "\(k)": {
    49  		spec: selector: v.spec.template.metadata.labels
    50  		spec: ports: [
    51  			for c in v.spec.template.spec.containers
    52  			for p in c.ports
    53  			if p._export {
    54  				let Port = p.containerPort // Port is an alias
    55  				port:       *Port | int
    56  				targetPort: *Port | int
    57  			},
    58  		]
    59  	}
    60  }
    61  -- out/eval --
    62  (struct){
    63    service: (struct){
    64      "node-exporter": (struct){
    65        spec: (struct){
    66          ports: (#list){
    67            0: (struct){
    68              name: (string){ "metrics" }
    69              port: (int){ 9100 }
    70              protocol: (string){ "TCP" }
    71              targetPort: (int){ |(*(int){ 9100 }, (int){ int }) }
    72            }
    73          }
    74          selector: (_|_){
    75            // [incomplete] service."node-exporter".spec.selector: undefined field: metadata:
    76            //     ./in.cue:48:35
    77          }
    78        }
    79      }
    80    }
    81    deployment: (struct){
    82      "node-exporter": (struct){
    83        spec: (struct){
    84          template: (struct){
    85            spec: (struct){
    86              containers: (#list){
    87                0: (struct){
    88                  ports: (#list){
    89                    0: (struct){
    90                      containerPort: (int){ 9100 }
    91                      name: (string){ "scrape" }
    92                      _export(:kube): (bool){ |(*(bool){ true }, (bool){ false }) }
    93                    }
    94                  }
    95                  name: (string){ string }
    96                }
    97              }
    98            }
    99          }
   100        }
   101        _name(:kube): (string){ string }
   102      }
   103    }
   104    _spec(:kube): (struct){
   105      _name(:kube): (string){ string }
   106      spec: (struct){
   107        template: (struct){
   108          spec: (struct){
   109            containers: (#list){
   110              0: (struct){
   111                name: (string){ string }
   112                ports: (list){
   113                }
   114              }
   115            }
   116          }
   117        }
   118      }
   119    }
   120  }
   121  -- out/compile --
   122  --- in.cue
   123  {
   124    service: {
   125      "node-exporter": {
   126        spec: {
   127          ports: [
   128            {
   129              name: "metrics"
   130              port: 9100
   131              protocol: "TCP"
   132            },
   133          ]
   134        }
   135      }
   136    }
   137    deployment: {
   138      "node-exporter": {
   139        spec: {
   140          template: {
   141            spec: {
   142              containers: [
   143                {
   144                  ports: [
   145                    {
   146                      containerPort: 9100
   147                      name: "scrape"
   148                    },
   149                  ]
   150                },
   151              ]
   152            }
   153          }
   154        }
   155      }
   156    }
   157    service: {
   158      [string]: {
   159        spec: {
   160          ports: [
   161            ...{
   162              port: int
   163              protocol: (*"TCP"|"UDP")
   164              name: (string|*"client")
   165            },
   166          ]
   167        }
   168      }
   169    }
   170    deployment: {
   171      [string]: {}
   172    }
   173    deployment: {
   174      [_]: (〈1;_spec〉 & {})
   175    }
   176    _spec: {
   177      _name: string
   178      spec: {
   179        template: {
   180          spec: {
   181            containers: [
   182              {
   183                name: 〈5;_name〉
   184              },
   185            ]
   186          }
   187        }
   188      }
   189    }
   190    _spec: {
   191      spec: {
   192        template: {
   193          spec: {
   194            containers: [
   195              ...{
   196                ports: [
   197                  ...{
   198                    _export: (*true|false)
   199                  },
   200                ]
   201              },
   202            ]
   203          }
   204        }
   205      }
   206    }
   207    for k, v in 〈0;deployment〉 {
   208      service: {
   209        "\(〈2;k〉)": {
   210          spec: {
   211            selector: 〈4;v〉.spec.template.metadata.labels
   212          }
   213          spec: {
   214            ports: [
   215              for _, c in 〈5;v〉.spec.template.spec.containers for _, p in 〈0;c〉.ports if 〈0;p〉._export {
   216                port: (*〈0;let Port〉|int)
   217                targetPort: (*〈0;let Port〉|int)
   218              },
   219            ]
   220          }
   221        }
   222      }
   223    }
   224  }