github.com/oam-dev/kubevela@v1.9.11/charts/vela-core/templates/defwithtemplate/container-ports.yaml (about)

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