MCPcopy
hub / github.com/moby/moby / buildPortsRelatedCreateEndpointOptions

Function buildPortsRelatedCreateEndpointOptions

daemon/network.go:1036–1106  ·  view source on GitHub ↗

buildPortsRelatedCreateEndpointOptions returns the appropriate endpoint options to apply config related to port mapping and exposed ports.

(c *container.Container, n *libnetwork.Network, sb *libnetwork.Sandbox)

Source from the content-addressed store, hash-verified

1034// buildPortsRelatedCreateEndpointOptions returns the appropriate endpoint options to apply config related to port
1035// mapping and exposed ports.
1036func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwork.Network, sb *libnetwork.Sandbox) ([]libnetwork.EndpointOption, error) {
1037 // Port-mapping rules belong to the container & applicable only to non-internal networks.
1038 //
1039 // TODO(thaJeztah): Look if we can provide a more minimal function for getPortMapInfo, as it does a lot, and we only need the "length".
1040 if n.Internal() || len(getPortMapInfo(sb)) > 0 {
1041 return nil, nil
1042 }
1043
1044 var (
1045 exposedPorts []lntypes.TransportPort
1046 publishedPorts []lntypes.PortBinding
1047 )
1048
1049 ports := c.HostConfig.PortBindings
1050 if c.HostConfig.PublishAllPorts && len(c.Config.ExposedPorts) > 0 {
1051 // Add exposed ports to a copy of the map to make sure a "publishedPorts" entry is created
1052 // for each exposed port, even if there's no specific binding.
1053 ports = maps.Clone(c.HostConfig.PortBindings)
1054 if ports == nil {
1055 ports = networktypes.PortMap{}
1056 }
1057 for p := range c.Config.ExposedPorts {
1058 if _, exists := ports[p]; !exists {
1059 ports[p] = nil
1060 }
1061 }
1062 }
1063
1064 for p, bindings := range ports {
1065 protocol := lntypes.ParseProtocol(string(p.Proto()))
1066 exposedPorts = append(exposedPorts, lntypes.TransportPort{
1067 Proto: protocol,
1068 Port: p.Num(),
1069 })
1070
1071 for _, binding := range bindings {
1072 var (
1073 portRange networktypes.PortRange
1074 err error
1075 )
1076
1077 // Empty HostPort means to map to an ephemeral port.
1078 if binding.HostPort != "" {
1079 portRange, err = networktypes.ParsePortRange(binding.HostPort)
1080 if err != nil {
1081 return nil, fmt.Errorf("error parsing HostPort value(%s):%v", binding.HostPort, err)
1082 }
1083 }
1084
1085 publishedPorts = append(publishedPorts, lntypes.PortBinding{
1086 Proto: protocol,
1087 Port: p.Num(),
1088 HostIP: binding.HostIP.AsSlice(),
1089 HostPort: portRange.Start(),
1090 HostPortEnd: portRange.End(),
1091 })
1092 }
1093

Callers 1

Calls 11

CreateOptionPortMappingFunction · 0.92
CreateOptionExposedPortsFunction · 0.92
getPortMapInfoFunction · 0.85
NumMethod · 0.80
ErrorfMethod · 0.80
AsSliceMethod · 0.80
InternalMethod · 0.65
StartMethod · 0.65
CloneMethod · 0.45
ProtoMethod · 0.45
EndMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…