github.com/oam-dev/kubevela@v1.9.11/vela-templates/definitions/internal/trait/container-ports.cue (about) 1 import ( 2 "strconv" 3 "strings" 4 ) 5 6 "container-ports": { 7 type: "trait" 8 annotations: {} 9 labels: {} 10 description: "Expose on the host and bind the external port to host to enable web traffic for your component." 11 attributes: { 12 podDisruptive: true 13 appliesToWorkloads: ["deployments.apps", "statefulsets.apps", "daemonsets.apps", "jobs.batch"] 14 } 15 } 16 17 template: { 18 #PatchParams: { 19 // +usage=Specify the name of the target container, if not set, use the component name 20 containerName: *"" | string 21 // +usage=Specify ports you want customer traffic sent to 22 ports: *[] | [...{ 23 // +usage=Number of port to expose on the pod's IP address 24 containerPort: int 25 // +usage=Protocol for port. Must be UDP, TCP, or SCTP 26 protocol: *"TCP" | "UDP" | "SCTP" 27 // +usage=Number of port to expose on the host 28 hostPort?: int 29 // +usage=What host IP to bind the external port to. 30 hostIP?: string 31 }] 32 } 33 34 PatchContainer: { 35 _params: #PatchParams 36 name: _params.containerName 37 _baseContainers: context.output.spec.template.spec.containers 38 _matchContainers_: [ for _container_ in _baseContainers if _container_.name == name {_container_}] 39 _baseContainer: *_|_ | {...} 40 if len(_matchContainers_) == 0 { 41 err: "container \(name) not found" 42 } 43 if len(_matchContainers_) > 0 { 44 _baseContainer: _matchContainers_[0] 45 _basePorts: _baseContainer.ports 46 if _basePorts == _|_ { 47 // +patchStrategy=replace 48 ports: [ for port in _params.ports { 49 containerPort: port.containerPort 50 protocol: port.protocol 51 if port.hostPort != _|_ { 52 hostPort: port.hostPort 53 } 54 if port.hostIP != _|_ { 55 hostIP: port.hostIP 56 } 57 }] 58 } 59 if _basePorts != _|_ { 60 _basePortsMap: {for _basePort in _basePorts {(strings.ToLower(_basePort.protocol) + strconv.FormatInt(_basePort.containerPort, 10)): _basePort}} 61 _portsMap: {for port in _params.ports {(strings.ToLower(port.protocol) + strconv.FormatInt(port.containerPort, 10)): port}} 62 // +patchStrategy=replace 63 ports: [ for portVar in _basePorts { 64 containerPort: portVar.containerPort 65 protocol: portVar.protocol 66 name: portVar.name 67 _uniqueKey: strings.ToLower(portVar.protocol) + strconv.FormatInt(portVar.containerPort, 10) 68 if _portsMap[_uniqueKey] != _|_ { 69 if _portsMap[_uniqueKey].hostPort != _|_ { 70 hostPort: _portsMap[_uniqueKey].hostPort 71 } 72 if _portsMap[_uniqueKey].hostIP != _|_ { 73 hostIP: _portsMap[_uniqueKey].hostIP 74 } 75 } 76 }] + [ for port in _params.ports if _basePortsMap[strings.ToLower(port.protocol)+strconv.FormatInt(port.containerPort, 10)] == _|_ { 77 if port.containerPort != _|_ { 78 containerPort: port.containerPort 79 } 80 if port.protocol != _|_ { 81 protocol: port.protocol 82 } 83 if port.hostPort != _|_ { 84 hostPort: port.hostPort 85 } 86 if port.hostIP != _|_ { 87 hostIP: port.hostIP 88 } 89 }] 90 } 91 } 92 } 93 94 patch: spec: template: spec: { 95 if parameter.containers == _|_ { 96 // +patchKey=name 97 containers: [{ 98 PatchContainer & {_params: { 99 if parameter.containerName == "" { 100 containerName: context.name 101 } 102 if parameter.containerName != "" { 103 containerName: parameter.containerName 104 } 105 ports: parameter.ports 106 }} 107 }] 108 } 109 if parameter.containers != _|_ { 110 // +patchKey=name 111 containers: [ for c in parameter.containers { 112 if c.containerName == "" { 113 err: "container name must be set for containers" 114 } 115 if c.containerName != "" { 116 PatchContainer & {_params: c} 117 } 118 }] 119 } 120 } 121 122 parameter: *#PatchParams | close({ 123 // +usage=Specify the container ports for multiple containers 124 containers: [...#PatchParams] 125 }) 126 127 errs: [ for c in patch.spec.template.spec.containers if c.err != _|_ {c.err}] 128 }