func (container *Container) buildPortMapInfo(ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) { if ep == nil { return nil, derr.ErrorCodeEmptyEndpoint } if networkSettings == nil { return nil, derr.ErrorCodeEmptyNetwork } driverInfo, err := ep.DriverInfo() if err != nil { return nil, err } if driverInfo == nil { // It is not an error for epInfo to be nil return networkSettings, nil } if mac, ok := driverInfo[netlabel.MacAddress]; ok { networkSettings.MacAddress = mac.(net.HardwareAddr).String() } networkSettings.Ports = nat.PortMap{} if expData, ok := driverInfo[netlabel.ExposedPorts]; ok { if exposedPorts, ok := expData.([]types.TransportPort); ok { for _, tp := range exposedPorts { natPort, err := nat.NewPort(tp.Proto.String(), strconv.Itoa(int(tp.Port))) if err != nil { return nil, derr.ErrorCodeParsingPort.WithArgs(tp.Port, err) } networkSettings.Ports[natPort] = nil } } } mapData, ok := driverInfo[netlabel.PortMap] if !ok { return networkSettings, nil } if portMapping, ok := mapData.([]types.PortBinding); ok { for _, pp := range portMapping { natPort, err := nat.NewPort(pp.Proto.String(), strconv.Itoa(int(pp.Port))) if err != nil { return nil, err } natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))} networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg) } } return networkSettings, nil }
func (container *Container) buildPortMapInfo(n libnetwork.Network, ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) { if ep == nil { return nil, fmt.Errorf("invalid endpoint while building port map info") } if networkSettings == nil { return nil, fmt.Errorf("invalid networksettings while building port map info") } driverInfo, err := ep.DriverInfo() if err != nil { return nil, err } if driverInfo == nil { // It is not an error for epInfo to be nil return networkSettings, nil } if mac, ok := driverInfo[netlabel.MacAddress]; ok { networkSettings.MacAddress = mac.(net.HardwareAddr).String() } networkSettings.Ports = nat.PortMap{} if expData, ok := driverInfo[netlabel.ExposedPorts]; ok { if exposedPorts, ok := expData.([]types.TransportPort); ok { for _, tp := range exposedPorts { natPort := nat.NewPort(tp.Proto.String(), strconv.Itoa(int(tp.Port))) networkSettings.Ports[natPort] = nil } } } mapData, ok := driverInfo[netlabel.PortMap] if !ok { return networkSettings, nil } if portMapping, ok := mapData.([]types.PortBinding); ok { for _, pp := range portMapping { natPort := nat.NewPort(pp.Proto.String(), strconv.Itoa(int(pp.Port))) natBndg := nat.PortBinding{HostIp: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))} networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg) } } return networkSettings, nil }