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)
| 1034 | // buildPortsRelatedCreateEndpointOptions returns the appropriate endpoint options to apply config related to port |
| 1035 | // mapping and exposed ports. |
| 1036 | func 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 |
no test coverage detected
searching dependent graphs…