diff --git a/api/types/container/config.go b/api/types/container/config.go index 12fa6b3102..c5650f10c9 100644 --- a/api/types/container/config.go +++ b/api/types/container/config.go @@ -4,6 +4,7 @@ import ( "time" dockerspec "github.com/moby/docker-image-spec/specs-go/v1" + "github.com/moby/moby/api/types/network" ) // MinimumDuration puts a minimum on user configured duration. @@ -28,7 +29,7 @@ type Config struct { AttachStdin bool // Attach the standard input, makes possible user interaction AttachStdout bool // Attach the standard output AttachStderr bool // Attach the standard error - ExposedPorts PortSet `json:",omitempty"` // List of exposed ports + ExposedPorts network.PortSet `json:",omitempty"` // List of exposed ports Tty bool // Attach standard streams to a tty, including stdin if it is not closed. OpenStdin bool // Open stdin StdinOnce bool // If true, close stdin after the 1 attached client disconnects. diff --git a/api/types/container/hostconfig.go b/api/types/container/hostconfig.go index 6df9d1d63c..0f889c6512 100644 --- a/api/types/container/hostconfig.go +++ b/api/types/container/hostconfig.go @@ -421,7 +421,7 @@ type HostConfig struct { ContainerIDFile string // File (path) where the containerId is written LogConfig LogConfig // Configuration of the logs for this container NetworkMode NetworkMode // Network mode to use for the container - PortBindings PortMap // Port mapping between the exposed port (container) and the host + PortBindings network.PortMap // Port mapping between the exposed port (container) and the host RestartPolicy RestartPolicy // Restart policy to be used for the container AutoRemove bool // Automatically remove container when it exits VolumeDriver string // Name of the volume driver used to mount volumes diff --git a/api/types/container/network_settings.go b/api/types/container/network_settings.go index fb7f59df08..c51c0839d2 100644 --- a/api/types/container/network_settings.go +++ b/api/types/container/network_settings.go @@ -6,10 +6,13 @@ import ( // NetworkSettings exposes the network settings in the api type NetworkSettings struct { - SandboxID string // SandboxID uniquely represents a container's network stack - SandboxKey string // SandboxKey identifies the sandbox - Ports PortMap // Ports is a collection of PortBinding indexed by Port - Networks map[string]*network.EndpointSettings + SandboxID string // SandboxID uniquely represents a container's network stack + SandboxKey string // SandboxKey identifies the sandbox + + // Ports is a collection of [network.PortBinding] indexed by [network.Port] + Ports network.PortMap + + Networks map[string]*network.EndpointSettings } // NetworkSettingsSummary provides a summary of container's networks diff --git a/api/types/container/network.go b/api/types/network/port.go similarity index 92% rename from api/types/container/network.go rename to api/types/network/port.go index 17e90ed746..171d9f51d3 100644 --- a/api/types/container/network.go +++ b/api/types/network/port.go @@ -1,4 +1,4 @@ -package container +package network import ( "errors" @@ -10,24 +10,24 @@ import ( "unique" ) -// NetworkProtocol represents a network protocol for a port. -type NetworkProtocol string +// IPProtocol represents a network protocol for a port. +type IPProtocol string const ( - TCP NetworkProtocol = "tcp" - UDP NetworkProtocol = "udp" - SCTP NetworkProtocol = "sctp" + TCP IPProtocol = "tcp" + UDP IPProtocol = "udp" + SCTP IPProtocol = "sctp" ) // Sentinel port proto value for zero Port and PortRange values. -var protoZero unique.Handle[NetworkProtocol] +var protoZero unique.Handle[IPProtocol] // Port is a type representing a single port number and protocol in the format "/[]". // // The zero port value, i.e. Port{}, is invalid; use [ParsePort] to create a valid Port value. type Port struct { num uint16 - proto unique.Handle[NetworkProtocol] + proto unique.Handle[IPProtocol] } // ParsePort parses s as a [Port]. @@ -64,7 +64,7 @@ func MustParsePort(s string) Port { // PortFrom returns a [Port] with the given number and protocol. // // If no protocol is specified (i.e. proto == ""), then PortFrom returns Port{}, false. -func PortFrom(num uint16, proto NetworkProtocol) (p Port, ok bool) { +func PortFrom(num uint16, proto IPProtocol) (p Port, ok bool) { if proto == "" { return Port{}, false } @@ -78,7 +78,7 @@ func (p Port) Num() uint16 { } // Proto returns p's network protocol. -func (p Port) Proto() NetworkProtocol { +func (p Port) Proto() IPProtocol { return p.proto.Value() } @@ -163,7 +163,7 @@ type PortMap = map[Port][]PortBinding type PortRange struct { start uint16 end uint16 - proto unique.Handle[NetworkProtocol] + proto unique.Handle[IPProtocol] } // ParsePortRange parses s as a [PortRange]. @@ -212,7 +212,7 @@ func MustParsePortRange(s string) PortRange { // PortRangeFrom returns a [PortRange] with the given start and end port numbers and protocol. // // If end < start or no protocol is specified (i.e. proto == ""), then PortRangeFrom returns PortRange{}, false. -func PortRangeFrom(start, end uint16, proto NetworkProtocol) (pr PortRange, ok bool) { +func PortRangeFrom(start, end uint16, proto IPProtocol) (pr PortRange, ok bool) { if end < start || proto == "" { return PortRange{}, false } @@ -231,7 +231,7 @@ func (pr PortRange) End() uint16 { } // Proto returns pr's network protocol. -func (pr PortRange) Proto() NetworkProtocol { +func (pr PortRange) Proto() IPProtocol { return pr.proto.Value() } @@ -335,12 +335,12 @@ func parsePortNumber(rawPort string) (uint16, error) { // normalizePortProto normalizes the protocol string such that "tcp", "TCP", and "tCp" are equivalent. // If proto is not specified, it defaults to "tcp". -func normalizePortProto(proto string) unique.Handle[NetworkProtocol] { +func normalizePortProto(proto string) unique.Handle[IPProtocol] { if proto == "" { return unique.Make(TCP) } proto = strings.ToLower(proto) - return unique.Make(NetworkProtocol(proto)) + return unique.Make(IPProtocol(proto)) } diff --git a/api/types/container/network_test.go b/api/types/network/port_test.go similarity index 98% rename from api/types/container/network_test.go rename to api/types/network/port_test.go index 128787146b..e6cee45385 100644 --- a/api/types/container/network_test.go +++ b/api/types/network/port_test.go @@ -1,4 +1,4 @@ -package container +package network import ( "encoding/json" @@ -51,7 +51,7 @@ func TestPort(t *testing.T) { t.Run("PortFrom", func(t *testing.T) { tests := []struct { num uint16 - proto NetworkProtocol + proto IPProtocol }{ {0, TCP}, {80, TCP}, @@ -80,7 +80,7 @@ func TestPort(t *testing.T) { negativeTests := []struct { num uint16 - proto NetworkProtocol + proto IPProtocol }{ {0, ""}, {80, ""}, @@ -293,7 +293,7 @@ func TestPortRange(t *testing.T) { tests := []struct { start uint16 end uint16 - proto NetworkProtocol + proto IPProtocol }{ {0, 0, TCP}, {0, 1234, TCP}, @@ -325,7 +325,7 @@ func TestPortRange(t *testing.T) { negativeTests := []struct { start uint16 end uint16 - proto NetworkProtocol + proto IPProtocol }{ {1234, 80, TCP}, // end < start {0, 0, ""}, // empty protocol @@ -583,7 +583,7 @@ func BenchmarkPortRangeAll(b *testing.B) { }) } -func portFrom(num uint16, proto NetworkProtocol) Port { +func portFrom(num uint16, proto IPProtocol) Port { p, ok := PortFrom(num, proto) if !ok { panic("invalid port") @@ -591,7 +591,7 @@ func portFrom(num uint16, proto NetworkProtocol) Port { return p } -func portRangeFrom(start, end uint16, proto NetworkProtocol) PortRange { +func portRangeFrom(start, end uint16, proto IPProtocol) PortRange { pr, ok := PortRangeFrom(start, end, proto) if !ok { panic("invalid port range") diff --git a/api/types/swarm/network.go b/api/types/swarm/network.go index 9f181fb00c..5e2517174f 100644 --- a/api/types/swarm/network.go +++ b/api/types/swarm/network.go @@ -32,7 +32,7 @@ const ( // PortConfig represents the config of a port. type PortConfig struct { Name string `json:",omitempty"` - Protocol PortConfigProtocol `json:",omitempty"` + Protocol network.IPProtocol `json:",omitempty"` // TargetPort is the port inside the container TargetPort uint32 `json:",omitempty"` // PublishedPort is the port on the swarm hosts @@ -54,20 +54,6 @@ const ( PortConfigPublishModeHost PortConfigPublishMode = "host" ) -// PortConfigProtocol represents the protocol of a port. -type PortConfigProtocol string - -const ( - // TODO(stevvooe): These should be used generally, not just for PortConfig. - - // PortConfigProtocolTCP TCP - PortConfigProtocolTCP PortConfigProtocol = "tcp" - // PortConfigProtocolUDP UDP - PortConfigProtocolUDP PortConfigProtocol = "udp" - // PortConfigProtocolSCTP SCTP - PortConfigProtocolSCTP PortConfigProtocol = "sctp" -) - // EndpointVirtualIP represents the virtual ip of a port. type EndpointVirtualIP struct { NetworkID string `json:",omitempty"` diff --git a/daemon/builder/dockerfile/dispatchers.go b/daemon/builder/dockerfile/dispatchers.go index c4f820c30f..3346018432 100644 --- a/daemon/builder/dockerfile/dispatchers.go +++ b/daemon/builder/dockerfile/dispatchers.go @@ -21,8 +21,8 @@ import ( "github.com/moby/buildkit/frontend/dockerfile/instructions" "github.com/moby/buildkit/frontend/dockerfile/parser" "github.com/moby/buildkit/frontend/dockerfile/shell" - "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/jsonstream" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/builder" "github.com/moby/moby/v2/daemon/internal/image" "github.com/moby/moby/v2/daemon/internal/netiputil" @@ -533,7 +533,7 @@ func dispatchExpose(ctx context.Context, d dispatchRequest, c *instructions.Expo } if d.state.runConfig.ExposedPorts == nil { - d.state.runConfig.ExposedPorts = make(container.PortSet) + d.state.runConfig.ExposedPorts = make(network.PortSet) } for p := range ps { d.state.runConfig.ExposedPorts[p] = struct{}{} @@ -546,10 +546,10 @@ func dispatchExpose(ctx context.Context, d dispatchRequest, c *instructions.Expo // // parsePortSpecs receives port specs in the format of ip:public:private/proto and parses // these in to the internal types -func parsePortSpecs(ports []string) (map[container.Port]struct{}, map[container.Port][]container.PortBinding, error) { +func parsePortSpecs(ports []string) (map[network.Port]struct{}, network.PortMap, error) { var ( - exposedPorts = make(map[container.Port]struct{}, len(ports)) - bindings = make(map[container.Port][]container.PortBinding) + exposedPorts = make(map[network.Port]struct{}, len(ports)) + bindings = make(network.PortMap) ) for _, p := range ports { portMappings, err := parsePortSpec(p) @@ -571,8 +571,8 @@ func parsePortSpecs(ports []string) (map[container.Port]struct{}, map[container. // Copied and modified from https://github.com/docker/go-connections/blob/c296721c0d56d3acad2973376ded214103a4fd2e/nat/nat.go#L172-L237 // -// parsePortSpec parses a port specification string into a slice of [container.PortMap] -func parsePortSpec(rawPort string) ([]container.PortMap, error) { +// parsePortSpec parses a port specification string into a slice of [network.PortMap] +func parsePortSpec(rawPort string) ([]network.PortMap, error) { ip, hostPort, containerPort := splitParts(rawPort) proto, containerPort := splitProtoPort(containerPort) if containerPort == "" { @@ -597,7 +597,7 @@ func parsePortSpec(rawPort string) ([]container.PortMap, error) { return nil, fmt.Errorf("invalid IP address: %w", err) } - pr, err := container.ParsePortRange(containerPort) + pr, err := network.ParsePortRange(containerPort) if err != nil { return nil, errors.New("invalid containerPort: " + containerPort) } @@ -609,7 +609,7 @@ func parsePortSpec(rawPort string) ([]container.PortMap, error) { var startHostPort, endHostPort uint16 if hostPort != "" { - hostPortRange, err := container.ParsePortRange(hostPort) + hostPortRange, err := network.ParsePortRange(hostPort) if err != nil { return nil, errors.New("invalid hostPort: " + hostPort) } @@ -626,7 +626,7 @@ func parsePortSpec(rawPort string) ([]container.PortMap, error) { } count := endPort - startPort + 1 - ports := make([]container.PortMap, 0, count) + ports := make([]network.PortMap, 0, count) for i := uint16(0); i < count; i++ { hPort := "" @@ -638,8 +638,8 @@ func parsePortSpec(rawPort string) ([]container.PortMap, error) { hPort += "-" + strconv.Itoa(int(endHostPort)) } } - ports = append(ports, container.PortMap{ - container.MustParsePort(fmt.Sprintf("%d/%s", startPort+i, proto)): []container.PortBinding{{HostIP: addr, HostPort: hPort}}, + ports = append(ports, network.PortMap{ + network.MustParsePort(fmt.Sprintf("%d/%s", startPort+i, proto)): []network.PortBinding{{HostIP: addr, HostPort: hPort}}, }) } return ports, nil diff --git a/daemon/builder/dockerfile/dispatchers_test.go b/daemon/builder/dockerfile/dispatchers_test.go index 10fe109024..2416a8bf56 100644 --- a/daemon/builder/dockerfile/dispatchers_test.go +++ b/daemon/builder/dockerfile/dispatchers_test.go @@ -14,6 +14,7 @@ import ( "github.com/moby/buildkit/frontend/dockerfile/parser" "github.com/moby/buildkit/frontend/dockerfile/shell" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/builder" "github.com/moby/moby/v2/daemon/internal/image" "github.com/moby/moby/v2/daemon/pkg/oci" @@ -337,7 +338,7 @@ func TestExpose(t *testing.T) { assert.Assert(t, sb.state.runConfig.ExposedPorts != nil) assert.Assert(t, is.Len(sb.state.runConfig.ExposedPorts, 1)) - assert.Check(t, is.Contains(sb.state.runConfig.ExposedPorts, container.MustParsePort("80/tcp"))) + assert.Check(t, is.Contains(sb.state.runConfig.ExposedPorts, network.MustParsePort("80/tcp"))) } func TestUser(t *testing.T) { @@ -632,29 +633,29 @@ func TestDispatchUnsupportedOptions(t *testing.T) { // Copied and modified from https://github.com/docker/go-connections/blob/c296721c0d56d3acad2973376ded214103a4fd2e/nat/nat_test.go#L390-L499 func TestParsePortSpecs(t *testing.T) { var ( - portMap map[container.Port]struct{} - bindingMap map[container.Port][]container.PortBinding + portSet network.PortSet + bindingMap network.PortMap err error ) - tcp1234 := container.MustParsePort("1234/tcp") - udp2345 := container.MustParsePort("2345/udp") - sctp3456 := container.MustParsePort("3456/sctp") + tcp1234 := network.MustParsePort("1234/tcp") + udp2345 := network.MustParsePort("2345/udp") + sctp3456 := network.MustParsePort("3456/sctp") - portMap, bindingMap, err = parsePortSpecs([]string{tcp1234.String(), udp2345.String(), sctp3456.String()}) + portSet, bindingMap, err = parsePortSpecs([]string{tcp1234.String(), udp2345.String(), sctp3456.String()}) if err != nil { t.Fatalf("Error while processing ParsePortSpecs: %s", err) } - if _, ok := portMap[tcp1234]; !ok { + if _, ok := portSet[tcp1234]; !ok { t.Fatal("1234/tcp was not parsed properly") } - if _, ok := portMap[udp2345]; !ok { + if _, ok := portSet[udp2345]; !ok { t.Fatal("2345/udp was not parsed properly") } - if _, ok := portMap[sctp3456]; !ok { + if _, ok := portSet[sctp3456]; !ok { t.Fatal("3456/sctp was not parsed properly") } @@ -672,20 +673,20 @@ func TestParsePortSpecs(t *testing.T) { } } - portMap, bindingMap, err = parsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp", "3456:3456/sctp"}) + portSet, bindingMap, err = parsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp", "3456:3456/sctp"}) if err != nil { t.Fatalf("Error while processing ParsePortSpecs: %s", err) } - if _, ok := portMap[tcp1234]; !ok { + if _, ok := portSet[tcp1234]; !ok { t.Fatal("1234/tcp was not parsed properly") } - if _, ok := portMap[udp2345]; !ok { + if _, ok := portSet[udp2345]; !ok { t.Fatal("2345/udp was not parsed properly") } - if _, ok := portMap[sctp3456]; !ok { + if _, ok := portSet[sctp3456]; !ok { t.Fatal("3456/sctp was not parsed properly") } @@ -705,20 +706,20 @@ func TestParsePortSpecs(t *testing.T) { } } - portMap, bindingMap, err = parsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp", "0.0.0.0:3456:3456/sctp"}) + portSet, bindingMap, err = parsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp", "0.0.0.0:3456:3456/sctp"}) if err != nil { t.Fatalf("Error while processing ParsePortSpecs: %s", err) } - if _, ok := portMap[tcp1234]; !ok { + if _, ok := portSet[tcp1234]; !ok { t.Fatal("1234/tcp was not parsed properly") } - if _, ok := portMap[udp2345]; !ok { + if _, ok := portSet[udp2345]; !ok { t.Fatal("2345/udp was not parsed properly") } - if _, ok := portMap[sctp3456]; !ok { + if _, ok := portSet[sctp3456]; !ok { t.Fatal("3456/sctp was not parsed properly") } @@ -784,9 +785,9 @@ func TestParsePortSpecFull(t *testing.T) { t.Fatalf("expected nil error, got: %v", err) } - expected := []container.PortMap{ + expected := []network.PortMap{ { - container.MustParsePort("3333/tcp"): []container.PortBinding{ + network.MustParsePort("3333/tcp"): []network.PortBinding{ { HostIP: netip.IPv4Unspecified(), HostPort: "1234", @@ -794,7 +795,7 @@ func TestParsePortSpecFull(t *testing.T) { }, }, { - container.MustParsePort("3334/tcp"): []container.PortBinding{ + network.MustParsePort("3334/tcp"): []network.PortBinding{ { HostIP: netip.IPv4Unspecified(), HostPort: "1235", @@ -813,15 +814,15 @@ func TestPartPortSpecIPV6(t *testing.T) { type test struct { name string spec string - expected []container.PortMap + expected []network.PortMap } cases := []test{ { name: "square angled IPV6 without host port", spec: "[2001:4860:0:2001::68]::333", - expected: []container.PortMap{ + expected: []network.PortMap{ { - container.MustParsePort("333/tcp"): []container.PortBinding{ + network.MustParsePort("333/tcp"): []network.PortBinding{ { HostIP: netip.MustParseAddr("2001:4860:0:2001::68"), HostPort: "", @@ -833,9 +834,9 @@ func TestPartPortSpecIPV6(t *testing.T) { { name: "square angled IPV6 with host port", spec: "[::1]:80:80", - expected: []container.PortMap{ + expected: []network.PortMap{ { - container.MustParsePort("80/tcp"): []container.PortBinding{ + network.MustParsePort("80/tcp"): []network.PortBinding{ { HostIP: netip.IPv6Loopback(), HostPort: "80", @@ -847,9 +848,9 @@ func TestPartPortSpecIPV6(t *testing.T) { { name: "IPV6 without host port", spec: "2001:4860:0:2001::68::333", - expected: []container.PortMap{ + expected: []network.PortMap{ { - container.MustParsePort("333/tcp"): []container.PortBinding{ + network.MustParsePort("333/tcp"): []network.PortBinding{ { HostIP: netip.MustParseAddr("2001:4860:0:2001::68"), HostPort: "", @@ -861,9 +862,9 @@ func TestPartPortSpecIPV6(t *testing.T) { { name: "IPV6 with host port", spec: "::1:80:80", - expected: []container.PortMap{ + expected: []network.PortMap{ { - container.MustParsePort("80/tcp"): []container.PortBinding{ + network.MustParsePort("80/tcp"): []network.PortBinding{ { HostIP: netip.IPv6Loopback(), HostPort: "80", @@ -875,9 +876,9 @@ func TestPartPortSpecIPV6(t *testing.T) { { name: ":: IPV6, without host port", spec: "::::80", - expected: []container.PortMap{ + expected: []network.PortMap{ { - container.MustParsePort("80/tcp"): []container.PortBinding{ + network.MustParsePort("80/tcp"): []network.PortBinding{ { HostIP: netip.IPv6Unspecified(), HostPort: "", @@ -903,25 +904,25 @@ func TestPartPortSpecIPV6(t *testing.T) { // Copied and modified from https://github.com/docker/go-connections/blob/c296721c0d56d3acad2973376ded214103a4fd2e/nat/nat_test.go#L501-L600 func TestParsePortSpecsWithRange(t *testing.T) { var ( - portMap map[container.Port]struct{} - bindingMap map[container.Port][]container.PortBinding + portSet network.PortSet + bindingMap network.PortMap err error ) - portMap, bindingMap, err = parsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp", "3456-3458/sctp"}) + portSet, bindingMap, err = parsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp", "3456-3458/sctp"}) if err != nil { t.Fatalf("Error while processing ParsePortSpecs: %s", err) } - if _, ok := portMap[container.MustParsePort("1235/tcp")]; !ok { + if _, ok := portSet[network.MustParsePort("1235/tcp")]; !ok { t.Fatal("1234-1236/tcp was not parsed properly") } - if _, ok := portMap[container.MustParsePort("2346/udp")]; !ok { + if _, ok := portSet[network.MustParsePort("2346/udp")]; !ok { t.Fatal("2345-2347/udp was not parsed properly") } - if _, ok := portMap[container.MustParsePort("3456/sctp")]; !ok { + if _, ok := portSet[network.MustParsePort("3456/sctp")]; !ok { t.Fatal("3456-3458/sctp was not parsed properly") } @@ -939,20 +940,20 @@ func TestParsePortSpecsWithRange(t *testing.T) { } } - portMap, bindingMap, err = parsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp", "3456-3458:3456-3458/sctp"}) + portSet, bindingMap, err = parsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp", "3456-3458:3456-3458/sctp"}) if err != nil { t.Fatalf("Error while processing ParsePortSpecs: %s", err) } - if _, ok := portMap[container.MustParsePort("1235/tcp")]; !ok { + if _, ok := portSet[network.MustParsePort("1235/tcp")]; !ok { t.Fatal("1234-1236 was not parsed properly") } - if _, ok := portMap[container.MustParsePort("2346/udp")]; !ok { + if _, ok := portSet[network.MustParsePort("2346/udp")]; !ok { t.Fatal("2345-2347 was not parsed properly") } - if _, ok := portMap[container.MustParsePort("3456/sctp")]; !ok { + if _, ok := portSet[network.MustParsePort("3456/sctp")]; !ok { t.Fatal("3456-3458 was not parsed properly") } @@ -971,20 +972,20 @@ func TestParsePortSpecsWithRange(t *testing.T) { } } - portMap, bindingMap, err = parsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp", "0.0.0.0:3456-3458:3456-3458/sctp"}) + portSet, bindingMap, err = parsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp", "0.0.0.0:3456-3458:3456-3458/sctp"}) if err != nil { t.Fatalf("Error while processing ParsePortSpecs: %s", err) } - if _, ok := portMap[container.MustParsePort("1235/tcp")]; !ok { + if _, ok := portSet[network.MustParsePort("1235/tcp")]; !ok { t.Fatal("1234-1236 was not parsed properly") } - if _, ok := portMap[container.MustParsePort("2346/udp")]; !ok { + if _, ok := portSet[network.MustParsePort("2346/udp")]; !ok { t.Fatal("2345-2347 was not parsed properly") } - if _, ok := portMap[container.MustParsePort("3456/sctp")]; !ok { + if _, ok := portSet[network.MustParsePort("3456/sctp")]; !ok { t.Fatal("3456-3458 was not parsed properly") } diff --git a/daemon/builder/dockerfile/internals_test.go b/daemon/builder/dockerfile/internals_test.go index 17eca05a7d..07fc1a588d 100644 --- a/daemon/builder/dockerfile/internals_test.go +++ b/daemon/builder/dockerfile/internals_test.go @@ -8,6 +8,7 @@ import ( "github.com/moby/go-archive" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/builder" "github.com/moby/moby/v2/daemon/builder/remotecontext" "github.com/moby/moby/v2/daemon/internal/image" @@ -135,9 +136,9 @@ func fullMutableRunConfig() *container.Config { return &container.Config{ Cmd: []string{"command", "arg1"}, Env: []string{"env1=foo", "env2=bar"}, - ExposedPorts: container.PortSet{ - container.MustParsePort("1000/tcp"): {}, - container.MustParsePort("1001/tcp"): {}, + ExposedPorts: network.PortSet{ + network.MustParsePort("1000/tcp"): {}, + network.MustParsePort("1001/tcp"): {}, }, Volumes: map[string]struct{}{ "one": {}, @@ -160,7 +161,7 @@ func TestDeepCopyRunConfig(t *testing.T) { ctrCfg.Cmd[1] = "arg2" ctrCfg.Env[1] = "env2=new" - ctrCfg.ExposedPorts[container.MustParsePort("10002")] = struct{}{} + ctrCfg.ExposedPorts[network.MustParsePort("10002")] = struct{}{} ctrCfg.Volumes["three"] = struct{}{} ctrCfg.Entrypoint[1] = "arg2" ctrCfg.OnBuild[0] = "start" diff --git a/daemon/cluster/convert/network.go b/daemon/cluster/convert/network.go index f4fa8563c3..269b54db59 100644 --- a/daemon/cluster/convert/network.go +++ b/daemon/cluster/convert/network.go @@ -144,7 +144,7 @@ func endpointFromGRPC(e *swarmapi.Endpoint) types.Endpoint { func swarmPortConfigToAPIPortConfig(portConfig *swarmapi.PortConfig) types.PortConfig { return types.PortConfig{ Name: portConfig.Name, - Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portConfig.Protocol)])), + Protocol: network.IPProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portConfig.Protocol)])), PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(portConfig.PublishMode)])), TargetPort: portConfig.TargetPort, PublishedPort: portConfig.PublishedPort, diff --git a/daemon/cluster/convert/task.go b/daemon/cluster/convert/task.go index 03c43b4796..ded09dc9b4 100644 --- a/daemon/cluster/convert/task.go +++ b/daemon/cluster/convert/task.go @@ -4,6 +4,7 @@ import ( "strings" gogotypes "github.com/gogo/protobuf/types" + "github.com/moby/moby/api/types/network" types "github.com/moby/moby/api/types/swarm" swarmapi "github.com/moby/swarmkit/v2/api" ) @@ -75,7 +76,7 @@ func TaskFromGRPC(t swarmapi.Task) (types.Task, error) { for _, p := range t.Status.PortStatus.Ports { task.Status.PortStatus.Ports = append(task.Status.PortStatus.Ports, types.PortConfig{ Name: p.Name, - Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])), + Protocol: network.IPProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(p.Protocol)])), PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(p.PublishMode)])), TargetPort: p.TargetPort, PublishedPort: p.PublishedPort, diff --git a/daemon/cluster/executor/container/container.go b/daemon/cluster/executor/container/container.go index 8553a460f0..cffa9a2c0a 100644 --- a/daemon/cluster/executor/container/container.go +++ b/daemon/cluster/executor/container/container.go @@ -143,8 +143,8 @@ func (c *containerConfig) image() string { return reference.FamiliarString(reference.TagNameOnly(ref)) } -func (c *containerConfig) portBindings() container.PortMap { - portBindings := container.PortMap{} +func (c *containerConfig) portBindings() network.PortMap { + portBindings := network.PortMap{} if c.task.Endpoint == nil { return portBindings } @@ -158,12 +158,12 @@ func (c *containerConfig) portBindings() container.PortMap { continue } - port, ok := container.PortFrom(uint16(portConfig.TargetPort), container.NetworkProtocol(portConfig.Protocol.String())) + port, ok := network.PortFrom(uint16(portConfig.TargetPort), network.IPProtocol(portConfig.Protocol.String())) if !ok { continue } - binding := []container.PortBinding{ + binding := []network.PortBinding{ {}, } @@ -188,8 +188,8 @@ func (c *containerConfig) init() *bool { return &init } -func (c *containerConfig) exposedPorts() map[container.Port]struct{} { - exposedPorts := make(map[container.Port]struct{}) +func (c *containerConfig) exposedPorts() map[network.Port]struct{} { + exposedPorts := make(map[network.Port]struct{}) if c.task.Endpoint == nil { return exposedPorts } @@ -203,7 +203,7 @@ func (c *containerConfig) exposedPorts() map[container.Port]struct{} { continue } - port, ok := container.PortFrom(uint16(portConfig.TargetPort), container.NetworkProtocol(portConfig.Protocol.String())) + port, ok := network.PortFrom(uint16(portConfig.TargetPort), network.IPProtocol(portConfig.Protocol.String())) if !ok { continue } diff --git a/daemon/cluster/executor/container/controller.go b/daemon/cluster/executor/container/controller.go index f724d3eeee..f531f042b0 100644 --- a/daemon/cluster/executor/container/controller.go +++ b/daemon/cluster/executor/container/controller.go @@ -11,6 +11,7 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/events" + "github.com/moby/moby/api/types/network" executorpkg "github.com/moby/moby/v2/daemon/cluster/executor" "github.com/moby/moby/v2/daemon/libnetwork" "github.com/moby/swarmkit/v2/agent/exec" @@ -639,17 +640,17 @@ func parsePortStatus(ctnr container.InspectResponse) (*api.PortStatus, error) { return status, nil } -func parsePortMap(portMap container.PortMap) ([]*api.PortConfig, error) { +func parsePortMap(portMap network.PortMap) ([]*api.PortConfig, error) { exposedPorts := make([]*api.PortConfig, 0, len(portMap)) for port, mapping := range portMap { var protocol api.PortConfig_Protocol switch port.Proto() { - case container.TCP: + case network.TCP: protocol = api.ProtocolTCP - case container.UDP: + case network.UDP: protocol = api.ProtocolUDP - case container.SCTP: + case network.SCTP: protocol = api.ProtocolSCTP default: return nil, fmt.Errorf("invalid protocol: %s", port.Proto()) diff --git a/daemon/container.go b/daemon/container.go index b7c32fd3fc..e9762d83d6 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -372,7 +372,7 @@ func validateHealthCheck(healthConfig *containertypes.HealthConfig) error { return nil } -func validatePortBindings(ports containertypes.PortMap) error { +func validatePortBindings(ports networktypes.PortMap) error { for port := range ports { if !port.IsValid() { return errors.Errorf("invalid port specification: %q", port.String()) @@ -384,7 +384,7 @@ func validatePortBindings(ports containertypes.PortMap) error { continue } - if _, err := containertypes.ParsePortRange(pb.HostPort); err != nil { + if _, err := networktypes.ParsePortRange(pb.HostPort); err != nil { return errors.Errorf("invalid port specification: %q", pb.HostPort) } } diff --git a/daemon/container/container.go b/daemon/container/container.go index d564c5f6f2..a918259233 100644 --- a/daemon/container/container.go +++ b/daemon/container/container.go @@ -22,6 +22,7 @@ import ( containertypes "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/events" mounttypes "github.com/moby/moby/api/types/mount" + networktypes "github.com/moby/moby/api/types/network" swarmtypes "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/v2/daemon/internal/image" libcontainerdtypes "github.com/moby/moby/v2/daemon/internal/libcontainerd/types" @@ -651,7 +652,7 @@ func (container *Container) BackfillEmptyPBs() { if len(pb) > 0 || pb == nil { continue } - container.HostConfig.PortBindings[portProto] = []containertypes.PortBinding{ + container.HostConfig.PortBindings[portProto] = []networktypes.PortBinding{ {}, // Backfill an empty PortBinding } } diff --git a/daemon/container/view.go b/daemon/container/view.go index 35c48feaaf..41b8cdeff8 100644 --- a/daemon/container/view.go +++ b/daemon/container/view.go @@ -39,8 +39,8 @@ type Snapshot struct { Running bool Paused bool Managed bool - ExposedPorts container.PortSet - PortBindings container.PortSet + ExposedPorts network.PortSet + PortBindings network.PortSet Health container.HealthStatus HostConfig struct { Isolation string @@ -318,8 +318,8 @@ func (v *View) transform(ctr *Container) *Snapshot { Name: ctr.Name, Pid: ctr.State.Pid, Managed: ctr.Managed, - ExposedPorts: make(container.PortSet), - PortBindings: make(container.PortSet), + ExposedPorts: make(network.PortSet), + PortBindings: make(network.PortSet), Health: health, Running: ctr.State.Running, Paused: ctr.State.Paused, @@ -398,8 +398,8 @@ func (v *View) transform(ctr *Container) *Snapshot { continue } for _, binding := range bindings { - // TODO(thaJeztah): if this is always a port/proto (no range), we can simplify this to [container.ParsePort]. - h, err := container.ParsePortRange(binding.HostPort) + // TODO(thaJeztah): if this is always a port/proto (no range), we can simplify this to [network.ParsePort]. + h, err := network.ParsePortRange(binding.HostPort) if err != nil { log.G(context.TODO()).WithError(err).Warn("invalid host port map") continue diff --git a/daemon/container_operations.go b/daemon/container_operations.go index bc007bcb37..30b862ef30 100644 --- a/daemon/container_operations.go +++ b/daemon/container_operations.go @@ -94,7 +94,7 @@ func buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnet } } - portBindings := make(containertypes.PortMap, len(ctr.HostConfig.PortBindings)) + portBindings := make(networktypes.PortMap, len(ctr.HostConfig.PortBindings)) for p, b := range ctr.HostConfig.PortBindings { portBindings[p] = slices.Clone(b) } @@ -119,13 +119,13 @@ func buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnet for _, binding := range bindings { var ( - portRange containertypes.PortRange + portRange networktypes.PortRange err error ) // Empty HostPort means to map to an ephemeral port. if binding.HostPort != "" { - portRange, err = containertypes.ParsePortRange(binding.HostPort) + portRange, err = networktypes.ParsePortRange(binding.HostPort) if err != nil { return nil, fmt.Errorf("error parsing HostPort value(%s):%v", binding.HostPort, err) } diff --git a/daemon/container_unix_test.go b/daemon/container_unix_test.go index 2fa48b7bba..1b676e840c 100644 --- a/daemon/container_unix_test.go +++ b/daemon/container_unix_test.go @@ -6,6 +6,7 @@ import ( "testing" containertypes "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/config" "gotest.tools/v3/assert" ) @@ -14,12 +15,12 @@ import ( // This should not be tested on Windows because Windows doesn't support "host" network mode. func TestContainerWarningHostAndPublishPorts(t *testing.T) { testCases := []struct { - ports containertypes.PortMap + ports network.PortMap warnings []string }{ - {ports: containertypes.PortMap{}}, - {ports: containertypes.PortMap{ - containertypes.MustParsePort("8080"): []containertypes.PortBinding{{HostPort: "8989"}}, + {ports: network.PortMap{}}, + {ports: network.PortMap{ + network.MustParsePort("8080"): []network.PortBinding{{HostPort: "8989"}}, }, warnings: []string{"Published ports are discarded when using host network mode"}}, } muteLogs(t) diff --git a/daemon/containerd/image_import_test.go b/daemon/containerd/image_import_test.go index de917c3d47..13dc7987aa 100644 --- a/daemon/containerd/image_import_test.go +++ b/daemon/containerd/image_import_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) @@ -11,8 +12,8 @@ import ( // regression test for https://github.com/moby/moby/issues/45904 func TestContainerConfigToDockerImageConfig(t *testing.T) { ociCFG := containerConfigToDockerOCIImageConfig(&container.Config{ - ExposedPorts: container.PortSet{ - container.MustParsePort("80/tcp"): struct{}{}, + ExposedPorts: network.PortSet{ + network.MustParsePort("80/tcp"): struct{}{}, }, }) diff --git a/daemon/containerd/imagespec.go b/daemon/containerd/imagespec.go index 0d578c2ce8..b714df85e3 100644 --- a/daemon/containerd/imagespec.go +++ b/daemon/containerd/imagespec.go @@ -5,6 +5,7 @@ import ( imagespec "github.com/moby/docker-image-spec/specs-go/v1" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/internal/image" "github.com/moby/moby/v2/dockerversion" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -95,9 +96,9 @@ func containerConfigToDockerOCIImageConfig(cfg *container.Config) imagespec.Dock } func dockerOCIImageConfigToContainerConfig(cfg imagespec.DockerOCIImageConfig) *container.Config { - exposedPorts := make(container.PortSet, len(cfg.ExposedPorts)) + exposedPorts := make(network.PortSet, len(cfg.ExposedPorts)) for k := range cfg.ExposedPorts { - if p, err := container.ParsePort(k); err == nil { + if p, err := network.ParsePort(k); err == nil { exposedPorts[p] = struct{}{} } } diff --git a/daemon/daemon_test.go b/daemon/daemon_test.go index dffc715056..cb6d224472 100644 --- a/daemon/daemon_test.go +++ b/daemon/daemon_test.go @@ -11,6 +11,7 @@ import ( cerrdefs "github.com/containerd/errdefs" containertypes "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/container" "github.com/moby/moby/v2/daemon/internal/idtools" "github.com/moby/moby/v2/daemon/libnetwork" @@ -220,9 +221,9 @@ func TestContainerInitDNS(t *testing.T) { func TestMerge(t *testing.T) { configImage := &containertypes.Config{ - ExposedPorts: containertypes.PortSet{ - containertypes.MustParsePort("1111/tcp"): struct{}{}, - containertypes.MustParsePort("2222/tcp"): struct{}{}, + ExposedPorts: network.PortSet{ + network.MustParsePort("1111/tcp"): struct{}{}, + network.MustParsePort("2222/tcp"): struct{}{}, }, Env: []string{"VAR1=1", "VAR2=2"}, Volumes: map[string]struct{}{ @@ -232,9 +233,9 @@ func TestMerge(t *testing.T) { } configUser := &containertypes.Config{ - ExposedPorts: containertypes.PortSet{ - containertypes.MustParsePort("2222/tcp"): struct{}{}, - containertypes.MustParsePort("3333/tcp"): struct{}{}, + ExposedPorts: network.PortSet{ + network.MustParsePort("2222/tcp"): struct{}{}, + network.MustParsePort("3333/tcp"): struct{}{}, }, Env: []string{"VAR2=3", "VAR3=3"}, Volumes: map[string]struct{}{ @@ -273,8 +274,8 @@ func TestMerge(t *testing.T) { } configImage2 := &containertypes.Config{ - ExposedPorts: map[containertypes.Port]struct{}{ - containertypes.MustParsePort("0/tcp"): {}, + ExposedPorts: map[network.Port]struct{}{ + network.MustParsePort("0/tcp"): {}, }, } diff --git a/daemon/inspect.go b/daemon/inspect.go index de68260880..ee4dd7fdea 100644 --- a/daemon/inspect.go +++ b/daemon/inspect.go @@ -34,7 +34,7 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options } // TODO(thaJeztah): do we need a deep copy here? Otherwise we could use maps.Clone (see https://github.com/moby/moby/commit/7917a36cc787ada58987320e67cc6d96858f3b55) - ports := make(containertypes.PortMap, len(ctr.NetworkSettings.Ports)) + ports := make(networktypes.PortMap, len(ctr.NetworkSettings.Ports)) for k, pm := range ctr.NetworkSettings.Ports { ports[k] = pm } diff --git a/daemon/internal/image/cache/compare_test.go b/daemon/internal/image/cache/compare_test.go index ab458b042c..3fae92dcb5 100644 --- a/daemon/internal/image/cache/compare_test.go +++ b/daemon/internal/image/cache/compare_test.go @@ -5,24 +5,25 @@ import ( "testing" "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) func TestCompare(t *testing.T) { - ports1 := container.PortSet{ - container.MustParsePort("1111/tcp"): struct{}{}, - container.MustParsePort("2222/tcp"): struct{}{}, + ports1 := network.PortSet{ + network.MustParsePort("1111/tcp"): struct{}{}, + network.MustParsePort("2222/tcp"): struct{}{}, } - ports2 := container.PortSet{ - container.MustParsePort("3333/tcp"): struct{}{}, - container.MustParsePort("4444/tcp"): struct{}{}, + ports2 := network.PortSet{ + network.MustParsePort("3333/tcp"): struct{}{}, + network.MustParsePort("4444/tcp"): struct{}{}, } - ports3 := container.PortSet{ - container.MustParsePort("1111/tcp"): struct{}{}, - container.MustParsePort("2222/tcp"): struct{}{}, - container.MustParsePort("5555/tcp"): struct{}{}, + ports3 := network.PortSet{ + network.MustParsePort("1111/tcp"): struct{}{}, + network.MustParsePort("2222/tcp"): struct{}{}, + network.MustParsePort("5555/tcp"): struct{}{}, } volumes1 := map[string]struct{}{ "/test1": {}, diff --git a/daemon/links/links.go b/daemon/links/links.go index a03c90f313..d36148b47f 100644 --- a/daemon/links/links.go +++ b/daemon/links/links.go @@ -8,7 +8,7 @@ import ( "slices" "strings" - "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" ) // Link struct holds information about parent/child linked container @@ -22,17 +22,17 @@ type Link struct { // Child environments variables ChildEnvironment []string // Child exposed ports - Ports []container.Port + Ports []network.Port } // EnvVars generates environment variables for the linked container // for the Link with the given options. -func EnvVars(parentIP, childIP, name string, env []string, exposedPorts map[container.Port]struct{}) []string { +func EnvVars(parentIP, childIP, name string, env []string, exposedPorts map[network.Port]struct{}) []string { return NewLink(parentIP, childIP, name, env, exposedPorts).ToEnv() } // NewLink initializes a new Link struct with the provided options. -func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[container.Port]struct{}) *Link { +func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[network.Port]struct{}) *Link { ports := slices.Collect(maps.Keys(exposedPorts)) return &Link{ @@ -55,7 +55,7 @@ func (l *Link) ToEnv() []string { slices.SortFunc(l.Ports, withTCPPriority) env := make([]string, 0, 1+len(l.Ports)*4) - var pStart, pEnd container.Port + var pStart, pEnd network.Port for i, p := range l.Ports { if i == 0 { @@ -111,14 +111,14 @@ func (l *Link) ToEnv() []string { // withTCPPriority prioritizes ports using TCP over other protocols before // comparing port-number and protocol. -func withTCPPriority(ip, jp container.Port) int { +func withTCPPriority(ip, jp network.Port) int { if ip.Proto() == jp.Proto() { return cmp.Compare(ip.Num(), jp.Num()) } - if ip.Proto() == container.TCP { + if ip.Proto() == network.TCP { return -1 } - if jp.Proto() == container.TCP { + if jp.Proto() == network.TCP { return 1 } return cmp.Compare(ip.Proto(), jp.Proto()) diff --git a/daemon/links/links_test.go b/daemon/links/links_test.go index 68356f4b3e..7d76c0c4b7 100644 --- a/daemon/links/links_test.go +++ b/daemon/links/links_test.go @@ -6,13 +6,13 @@ import ( "testing" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "gotest.tools/v3/assert" ) func TestLinkNaming(t *testing.T) { - actual := EnvVars("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, container.PortSet{ - container.MustParsePort("6379/tcp"): struct{}{}, + actual := EnvVars("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, network.PortSet{ + network.MustParsePort("6379/tcp"): struct{}{}, }) expectedEnv := []string{ @@ -29,8 +29,8 @@ func TestLinkNaming(t *testing.T) { } func TestLinkNew(t *testing.T) { - tcp6379 := container.MustParsePort("6379/tcp") - link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, container.PortSet{ + tcp6379 := network.MustParsePort("6379/tcp") + link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, network.PortSet{ tcp6379: struct{}{}, }) @@ -38,15 +38,15 @@ func TestLinkNew(t *testing.T) { Name: "/db/docker", ParentIP: "172.0.17.3", ChildIP: "172.0.17.2", - Ports: []container.Port{tcp6379}, + Ports: []network.Port{tcp6379}, } - assert.DeepEqual(t, expected, link, cmpopts.EquateComparable(container.Port{})) + assert.DeepEqual(t, expected, link, cmpopts.EquateComparable(network.Port{})) } func TestLinkEnv(t *testing.T) { - tcp6379 := container.MustParsePort("6379/tcp") - actual := EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, container.PortSet{ + tcp6379 := network.MustParsePort("6379/tcp") + actual := EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, network.PortSet{ tcp6379: struct{}{}, }) @@ -67,39 +67,39 @@ func TestLinkEnv(t *testing.T) { // TestSortPorts verifies that ports are sorted with TCP taking priority, // and ports with the same protocol to be sorted by port. func TestSortPorts(t *testing.T) { - ports := []container.Port{ - container.MustParsePort("6379/tcp"), - container.MustParsePort("6376/udp"), - container.MustParsePort("6380/tcp"), - container.MustParsePort("6376/sctp"), - container.MustParsePort("6381/tcp"), - container.MustParsePort("6381/udp"), - container.MustParsePort("6375/udp"), - container.MustParsePort("6375/sctp"), + ports := []network.Port{ + network.MustParsePort("6379/tcp"), + network.MustParsePort("6376/udp"), + network.MustParsePort("6380/tcp"), + network.MustParsePort("6376/sctp"), + network.MustParsePort("6381/tcp"), + network.MustParsePort("6381/udp"), + network.MustParsePort("6375/udp"), + network.MustParsePort("6375/sctp"), } - expected := []container.Port{ - container.MustParsePort("6379/tcp"), - container.MustParsePort("6380/tcp"), - container.MustParsePort("6381/tcp"), - container.MustParsePort("6375/sctp"), - container.MustParsePort("6376/sctp"), - container.MustParsePort("6375/udp"), - container.MustParsePort("6376/udp"), - container.MustParsePort("6381/udp"), + expected := []network.Port{ + network.MustParsePort("6379/tcp"), + network.MustParsePort("6380/tcp"), + network.MustParsePort("6381/tcp"), + network.MustParsePort("6375/sctp"), + network.MustParsePort("6376/sctp"), + network.MustParsePort("6375/udp"), + network.MustParsePort("6376/udp"), + network.MustParsePort("6381/udp"), } slices.SortFunc(ports, withTCPPriority) - assert.DeepEqual(t, expected, ports, cmpopts.EquateComparable(container.Port{})) + assert.DeepEqual(t, expected, ports, cmpopts.EquateComparable(network.Port{})) } func TestLinkMultipleEnv(t *testing.T) { - actual := EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, container.PortSet{ - container.MustParsePort("6300/udp"): struct{}{}, - container.MustParsePort("6379/tcp"): struct{}{}, - container.MustParsePort("6380/tcp"): struct{}{}, - container.MustParsePort("6381/tcp"): struct{}{}, - container.MustParsePort("6382/udp"): struct{}{}, + actual := EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, network.PortSet{ + network.MustParsePort("6300/udp"): struct{}{}, + network.MustParsePort("6379/tcp"): struct{}{}, + network.MustParsePort("6380/tcp"): struct{}{}, + network.MustParsePort("6381/tcp"): struct{}{}, + network.MustParsePort("6382/udp"): struct{}{}, }) expectedEnv := []string{ @@ -145,12 +145,12 @@ func BenchmarkLinkMultipleEnv(b *testing.B) { b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { - _ = EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, container.PortSet{ - container.MustParsePort("6300/udp"): struct{}{}, - container.MustParsePort("6379/tcp"): struct{}{}, - container.MustParsePort("6380/tcp"): struct{}{}, - container.MustParsePort("6381/tcp"): struct{}{}, - container.MustParsePort("6382/udp"): struct{}{}, + _ = EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, network.PortSet{ + network.MustParsePort("6300/udp"): struct{}{}, + network.MustParsePort("6379/tcp"): struct{}{}, + network.MustParsePort("6380/tcp"): struct{}{}, + network.MustParsePort("6381/tcp"): struct{}{}, + network.MustParsePort("6382/udp"): struct{}{}, }) } } diff --git a/daemon/list.go b/daemon/list.go index 7129a978ed..113a1bb865 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -13,6 +13,7 @@ import ( "github.com/containerd/log" containertypes "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/filters" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/daemon/container" "github.com/moby/moby/v2/daemon/internal/image" "github.com/moby/moby/v2/daemon/server/backend" @@ -403,7 +404,7 @@ func portOp(key string, filter map[string]bool) func(value string) error { return fmt.Errorf("filter for '%s' should not contain ':': %s", key, value) } // support two formats, original format /[] or /[] - portRange, err := containertypes.ParsePortRange(value) + portRange, err := network.ParsePortRange(value) if err != nil { return fmt.Errorf("error while looking up for %s %s: %s", key, value, err) } diff --git a/daemon/network.go b/daemon/network.go index 5ffca1e8cc..99dedc5dc3 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -1027,13 +1027,13 @@ func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwor for _, binding := range bindings { var ( - portRange containertypes.PortRange + portRange networktypes.PortRange err error ) // Empty HostPort means to map to an ephemeral port. if binding.HostPort != "" { - portRange, err = containertypes.ParsePortRange(binding.HostPort) + portRange, err = networktypes.ParsePortRange(binding.HostPort) if err != nil { return nil, fmt.Errorf("error parsing HostPort value(%s):%v", binding.HostPort, err) } @@ -1063,8 +1063,8 @@ func buildPortsRelatedCreateEndpointOptions(c *container.Container, n *libnetwor } // getPortMapInfo retrieves the current port-mapping programmed for the given sandbox -func getPortMapInfo(sb *libnetwork.Sandbox) containertypes.PortMap { - pm := containertypes.PortMap{} +func getPortMapInfo(sb *libnetwork.Sandbox) networktypes.PortMap { + pm := networktypes.PortMap{} if sb == nil { return pm } @@ -1075,7 +1075,7 @@ func getPortMapInfo(sb *libnetwork.Sandbox) containertypes.PortMap { return pm } -func getEndpointPortMapInfo(pm containertypes.PortMap, ep *libnetwork.Endpoint) { +func getEndpointPortMapInfo(pm networktypes.PortMap, ep *libnetwork.Endpoint) { driverInfo, _ := ep.DriverInfo() if driverInfo == nil { // It is not an error for epInfo to be nil @@ -1085,7 +1085,7 @@ func getEndpointPortMapInfo(pm containertypes.PortMap, ep *libnetwork.Endpoint) if expData, ok := driverInfo[netlabel.ExposedPorts]; ok { if exposedPorts, ok := expData.([]lntypes.TransportPort); ok { for _, tp := range exposedPorts { - natPort, ok := containertypes.PortFrom(tp.Port, containertypes.NetworkProtocol(tp.Proto.String())) + natPort, ok := networktypes.PortFrom(tp.Port, networktypes.IPProtocol(tp.Proto.String())) if !ok { log.G(context.TODO()).Errorf("Invalid exposed port: %s", tp.String()) continue @@ -1105,7 +1105,7 @@ func getEndpointPortMapInfo(pm containertypes.PortMap, ep *libnetwork.Endpoint) if portMapping, ok := mapData.([]lntypes.PortBinding); ok { for _, pp := range portMapping { // Use an empty string for the host natPort if there's no natPort assigned. - natPort, ok := containertypes.PortFrom(pp.Port, containertypes.NetworkProtocol(pp.Proto.String())) + natPort, ok := networktypes.PortFrom(pp.Port, networktypes.IPProtocol(pp.Proto.String())) if !ok { log.G(context.TODO()).Errorf("Invalid port binding: %s", pp.String()) continue @@ -1115,7 +1115,7 @@ func getEndpointPortMapInfo(pm containertypes.PortMap, ep *libnetwork.Endpoint) if pp.HostPort > 0 { hp = strconv.Itoa(int(pp.HostPort)) } - natBndg := containertypes.PortBinding{HostPort: hp} + natBndg := networktypes.PortBinding{HostPort: hp} natBndg.HostIP, _ = netip.AddrFromSlice(pp.HostIP) pm[natPort] = append(pm[natPort], natBndg) } diff --git a/daemon/network/settings.go b/daemon/network/settings.go index f884eacd63..577a53c083 100644 --- a/daemon/network/settings.go +++ b/daemon/network/settings.go @@ -4,7 +4,6 @@ import ( "net" "sync" - "github.com/moby/moby/api/types/container" networktypes "github.com/moby/moby/api/types/network" clustertypes "github.com/moby/moby/v2/daemon/cluster/provider" "github.com/pkg/errors" @@ -17,7 +16,7 @@ type Settings struct { SandboxKey string Networks map[string]*EndpointSettings Service *clustertypes.ServiceConfig - Ports container.PortMap + Ports networktypes.PortMap HasSwarmEndpoint bool } diff --git a/daemon/server/router/container/container_routes.go b/daemon/server/router/container/container_routes.go index 3ca6b55792..953fb02fd4 100644 --- a/daemon/server/router/container/container_routes.go +++ b/daemon/server/router/container/container_routes.go @@ -901,7 +901,7 @@ func handlePortBindingsBC(hostConfig *container.HostConfig, version string) stri emptyPBs = append(emptyPBs, port.String()) } - hostConfig.PortBindings[port] = []container.PortBinding{{}} + hostConfig.PortBindings[port] = []network.PortBinding{{}} } if len(emptyPBs) > 0 { diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 242818ce85..11ee6b95a7 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -504,8 +504,8 @@ func (s *DockerAPISuite) TestContainerAPIBadPort(c *testing.T) { } hostConfig := container.HostConfig{ - PortBindings: container.PortMap{ - container.MustParsePort("8080/tcp"): []container.PortBinding{ + PortBindings: network.PortMap{ + network.MustParsePort("8080/tcp"): []network.PortBinding{ { HostPort: "aa80", }, diff --git a/integration-cli/docker_cli_create_test.go b/integration-cli/docker_cli_create_test.go index 4eae2630e0..f950049900 100644 --- a/integration-cli/docker_cli_create_test.go +++ b/integration-cli/docker_cli_create_test.go @@ -9,7 +9,7 @@ import ( "strings" "testing" - containertypes "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/v2/integration-cli/cli" "github.com/moby/moby/v2/integration-cli/cli/build" "github.com/moby/moby/v2/internal/testutil/fakecontext" @@ -92,7 +92,7 @@ func (s *DockerCLICreateSuite) TestCreateWithPortRange(c *testing.T) { var containers []struct { HostConfig *struct { - PortBindings map[containertypes.Port][]containertypes.PortBinding + PortBindings network.PortMap } } err := json.Unmarshal([]byte(out), &containers) @@ -118,7 +118,7 @@ func (s *DockerCLICreateSuite) TestCreateWithLargePortRange(c *testing.T) { var containers []struct { HostConfig *struct { - PortBindings map[containertypes.Port][]containertypes.PortBinding + PortBindings network.PortMap } } diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index b1c68bc4d8..1c3ba8643c 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -22,7 +22,7 @@ import ( "testing" "time" - "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/client/pkg/stringid" "github.com/moby/moby/v2/integration-cli/cli" @@ -2168,7 +2168,7 @@ func (s *DockerCLIRunSuite) TestRunAllowPortRangeThroughExpose(c *testing.T) { id = strings.TrimSpace(id) portstr := inspectFieldJSON(c, id, "NetworkSettings.Ports") - var ports container.PortMap + var ports network.PortMap if err := json.Unmarshal([]byte(portstr), &ports); err != nil { c.Fatal(err) } @@ -2505,7 +2505,7 @@ func (s *DockerCLIRunSuite) TestRunAllowPortRangeThroughPublish(c *testing.T) { id = strings.TrimSpace(id) portStr := inspectFieldJSON(c, id, "NetworkSettings.Ports") - var ports container.PortMap + var ports network.PortMap err := json.Unmarshal([]byte(portStr), &ports) assert.NilError(c, err, "failed to unmarshal: %v", portStr) for port, binding := range ports { diff --git a/integration/container/daemon_test.go b/integration/container/daemon_test.go index fbf0fee983..cd5a8928eb 100644 --- a/integration/container/daemon_test.go +++ b/integration/container/daemon_test.go @@ -3,7 +3,7 @@ package container import ( "testing" - containertypes "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/v2/integration/internal/container" "github.com/moby/moby/v2/internal/testutil" @@ -71,13 +71,13 @@ func TestNetworkStateCleanupOnDaemonStart(t *testing.T) { defer d.Stop(t) apiClient := d.NewClientT(t) - mappedPort := containertypes.MustParsePort("80/tcp") + mappedPort := network.MustParsePort("80/tcp") // The intention of this container is to ignore stop signals. // Sadly this means the test will take longer, but at least this test can be parallelized. cid := container.Run(ctx, t, apiClient, container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{mappedPort: {{}}}), + container.WithPortMap(network.PortMap{mappedPort: {{}}}), container.WithCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done")) defer func() { err := apiClient.ContainerRemove(ctx, cid, client.ContainerRemoveOptions{Force: true}) diff --git a/integration/container/nat_test.go b/integration/container/nat_test.go index 6e2eb2df9f..d132229bf5 100644 --- a/integration/container/nat_test.go +++ b/integration/container/nat_test.go @@ -10,7 +10,7 @@ import ( "strings" "testing" - containertypes "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/v2/integration/internal/container" "gotest.tools/v3/assert" @@ -122,8 +122,8 @@ func startServerContainer(ctx context.Context, t *testing.T, msg string, port ui container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), container.WithExposedPorts(fmt.Sprintf("%d/tcp", port)), func(c *container.TestContainerConfig) { - c.HostConfig.PortBindings = containertypes.PortMap{ - containertypes.MustParsePort(fmt.Sprintf("%d/tcp", port)): []containertypes.PortBinding{ + c.HostConfig.PortBindings = network.PortMap{ + network.MustParsePort(fmt.Sprintf("%d/tcp", port)): []network.PortBinding{ { HostPort: fmt.Sprintf("%d", port), }, diff --git a/integration/internal/container/ops.go b/integration/internal/container/ops.go index 3a49791765..55a1dffcd0 100644 --- a/integration/internal/container/ops.go +++ b/integration/internal/container/ops.go @@ -74,18 +74,18 @@ func WithSysctls(sysctls map[string]string) func(*TestContainerConfig) { // WithExposedPorts sets the exposed ports of the container func WithExposedPorts(ports ...string) func(*TestContainerConfig) { return func(c *TestContainerConfig) { - c.Config.ExposedPorts = map[container.Port]struct{}{} + c.Config.ExposedPorts = map[network.Port]struct{}{} for _, port := range ports { - p, _ := container.ParsePort(port) + p, _ := network.ParsePort(port) c.Config.ExposedPorts[p] = struct{}{} } } } // WithPortMap sets/replaces port mappings. -func WithPortMap(pm container.PortMap) func(*TestContainerConfig) { +func WithPortMap(pm network.PortMap) func(*TestContainerConfig) { return func(c *TestContainerConfig) { - c.HostConfig.PortBindings = container.PortMap{} + c.HostConfig.PortBindings = network.PortMap{} for p, b := range pm { c.HostConfig.PortBindings[p] = slices.Clone(b) } diff --git a/integration/network/bridge/bridge_linux_test.go b/integration/network/bridge/bridge_linux_test.go index 48a571c0b3..a006999c91 100644 --- a/integration/network/bridge/bridge_linux_test.go +++ b/integration/network/bridge/bridge_linux_test.go @@ -516,19 +516,19 @@ func TestEndpointWithCustomIfname(t *testing.T) { func TestPublishedPortAlreadyInUse(t *testing.T) { ctx := setupTest(t) apiClient := testEnv.APIClient() - mappedPort := containertypes.MustParsePort("80/tcp") + mappedPort := networktypes.MustParsePort("80/tcp") ctr1 := ctr.Run(ctx, t, apiClient, ctr.WithCmd("top"), ctr.WithExposedPorts("80/tcp"), - ctr.WithPortMap(containertypes.PortMap{mappedPort: {{HostPort: "8000"}}})) + ctr.WithPortMap(networktypes.PortMap{mappedPort: {{HostPort: "8000"}}})) defer ctr.Remove(ctx, t, apiClient, ctr1, client.ContainerRemoveOptions{Force: true}) ctr2 := ctr.Create(ctx, t, apiClient, ctr.WithCmd("top"), ctr.WithRestartPolicy(containertypes.RestartPolicyAlways), ctr.WithExposedPorts("80/tcp"), - ctr.WithPortMap(containertypes.PortMap{mappedPort: {{HostPort: "8000"}}})) + ctr.WithPortMap(networktypes.PortMap{mappedPort: {{HostPort: "8000"}}})) defer ctr.Remove(ctx, t, apiClient, ctr2, client.ContainerRemoveOptions{Force: true}) err := apiClient.ContainerStart(ctx, ctr2, client.ContainerStartOptions{}) @@ -563,18 +563,18 @@ func TestAllPortMappingsAreReturned(t *testing.T) { ctrID := ctr.Run(ctx, t, apiClient, ctr.WithExposedPorts("80/tcp", "81/tcp"), - ctr.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8000"}}}), + ctr.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8000"}}}), ctr.WithEndpointSettings("testnetv4", &networktypes.EndpointSettings{}), ctr.WithEndpointSettings("testnetv6", &networktypes.EndpointSettings{})) defer ctr.Remove(ctx, t, apiClient, ctrID, client.ContainerRemoveOptions{Force: true}) inspect := ctr.Inspect(ctx, t, apiClient, ctrID) - assert.DeepEqual(t, inspect.NetworkSettings.Ports, containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): []containertypes.PortBinding{ + assert.DeepEqual(t, inspect.NetworkSettings.Ports, networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): []networktypes.PortBinding{ {HostIP: netip.IPv4Unspecified(), HostPort: "8000"}, {HostIP: netip.IPv6Unspecified(), HostPort: "8000"}, }, - containertypes.MustParsePort("81/tcp"): nil, + networktypes.MustParsePort("81/tcp"): nil, }, cmpopts.EquateComparable(netip.Addr{})) } @@ -602,7 +602,7 @@ func TestFirewalldReloadNoZombies(t *testing.T) { cid := ctr.Run(ctx, t, c, ctr.WithExposedPorts("80/tcp", "81/tcp"), - ctr.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8000"}}})) + ctr.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8000"}}})) defer func() { if !removed { ctr.Remove(ctx, t, c, cid, client.ContainerRemoveOptions{Force: true}) @@ -792,7 +792,7 @@ func TestPortMappingRestore(t *testing.T) { const svrName = "svr" cid := ctr.Run(ctx, t, c, ctr.WithExposedPorts("80/tcp"), - ctr.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {}}), + ctr.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {}}), ctr.WithName(svrName), ctr.WithRestartPolicy(containertypes.RestartPolicyUnlessStopped), ctr.WithCmd("httpd", "-f"), @@ -803,9 +803,9 @@ func TestPortMappingRestore(t *testing.T) { t.Helper() insp := ctr.Inspect(ctx, t, c, cid) assert.Check(t, is.Equal(insp.State.Running, true)) - if assert.Check(t, is.Contains(insp.NetworkSettings.Ports, containertypes.MustParsePort("80/tcp"))) && - assert.Check(t, is.Len(insp.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")], 2)) { - hostPort := insp.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")][0].HostPort + if assert.Check(t, is.Contains(insp.NetworkSettings.Ports, networktypes.MustParsePort("80/tcp"))) && + assert.Check(t, is.Len(insp.NetworkSettings.Ports[networktypes.MustParsePort("80/tcp")], 2)) { + hostPort := insp.NetworkSettings.Ports[networktypes.MustParsePort("80/tcp")][0].HostPort res := ctr.RunAttach(ctx, t, c, ctr.WithExtraHost("thehost:host-gateway"), ctr.WithCmd("wget", "-T3", "http://"+net.JoinHostPort("thehost", hostPort)), @@ -950,7 +950,7 @@ func TestEmptyPortBindingsBC(t *testing.T) { d.StartWithBusybox(ctx, t) defer d.Stop(t) - createInspect := func(t *testing.T, version string, pbs []containertypes.PortBinding) (containertypes.PortMap, []string) { + createInspect := func(t *testing.T, version string, pbs []networktypes.PortBinding) (networktypes.PortMap, []string) { apiClient := d.NewClientT(t, client.WithVersion(version)) defer apiClient.Close() @@ -965,7 +965,7 @@ func TestEmptyPortBindingsBC(t *testing.T) { // Create a container with an empty list of port bindings for container port 80/tcp. config := ctr.NewTestConfig(ctr.WithCmd("top"), ctr.WithExposedPorts("80/tcp"), - ctr.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): pbs})) + ctr.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): pbs})) c, err := apiClient.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Platform, config.Name) assert.NilError(t, err) defer apiClient.ContainerRemove(ctx, c.ID, client.ContainerRemoveOptions{Force: true}) @@ -978,46 +978,46 @@ func TestEmptyPortBindingsBC(t *testing.T) { } t.Run("backfilling on old client version", func(t *testing.T) { - expMappings := containertypes.PortMap{containertypes.MustParsePort("80/tcp"): { + expMappings := networktypes.PortMap{networktypes.MustParsePort("80/tcp"): { {}, // An empty PortBinding is backfilled }} expWarnings := make([]string, 0) - mappings, warnings := createInspect(t, "1.51", []containertypes.PortBinding{}) + mappings, warnings := createInspect(t, "1.51", []networktypes.PortBinding{}) assert.DeepEqual(t, expMappings, mappings, cmpopts.EquateComparable(netip.Addr{})) assert.DeepEqual(t, expWarnings, warnings, cmpopts.EquateComparable(netip.Addr{})) }) t.Run("backfilling on API 1.52, with a warning", func(t *testing.T) { - expMappings := containertypes.PortMap{containertypes.MustParsePort("80/tcp"): { + expMappings := networktypes.PortMap{networktypes.MustParsePort("80/tcp"): { {}, // An empty PortBinding is backfilled }} expWarnings := []string{ "Following container port(s) have an empty list of port-bindings: 80/tcp. Starting with API 1.53, such bindings will be discarded.", } - mappings, warnings := createInspect(t, "1.52", []containertypes.PortBinding{}) + mappings, warnings := createInspect(t, "1.52", []networktypes.PortBinding{}) assert.DeepEqual(t, expMappings, mappings, cmpopts.EquateComparable(netip.Addr{})) assert.DeepEqual(t, expWarnings, warnings, cmpopts.EquateComparable(netip.Addr{})) }) t.Run("no backfilling on API 1.53", func(t *testing.T) { - expMappings := containertypes.PortMap{} + expMappings := networktypes.PortMap{} expWarnings := make([]string, 0) - mappings, warnings := createInspect(t, "1.53", []containertypes.PortBinding{}) + mappings, warnings := createInspect(t, "1.53", []networktypes.PortBinding{}) assert.DeepEqual(t, expMappings, mappings, cmpopts.EquateComparable(netip.Addr{})) assert.DeepEqual(t, expWarnings, warnings, cmpopts.EquateComparable(netip.Addr{})) }) for _, apiVersion := range []string{"1.51", "1.52", "1.53"} { t.Run("no backfilling on API "+apiVersion+" with non-empty bindings", func(t *testing.T) { - expMappings := containertypes.PortMap{containertypes.MustParsePort("80/tcp"): { + expMappings := networktypes.PortMap{networktypes.MustParsePort("80/tcp"): { {HostPort: "8080"}, }} expWarnings := make([]string, 0) - mappings, warnings := createInspect(t, apiVersion, []containertypes.PortBinding{{HostPort: "8080"}}) + mappings, warnings := createInspect(t, apiVersion, []networktypes.PortBinding{{HostPort: "8080"}}) assert.DeepEqual(t, expMappings, mappings, cmpopts.EquateComparable(netip.Addr{})) assert.DeepEqual(t, expWarnings, warnings, cmpopts.EquateComparable(netip.Addr{})) }) @@ -1042,7 +1042,7 @@ func TestPortBindingBackfillingForOlderContainers(t *testing.T) { cid := ctr.Create(ctx, t, c, ctr.WithExposedPorts("80/tcp"), - ctr.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {}})) + ctr.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {}})) defer c.ContainerRemove(ctx, cid, client.ContainerRemoveOptions{Force: true}) // Stop the daemon to safely tamper with the on-disk state. @@ -1051,7 +1051,7 @@ func TestPortBindingBackfillingForOlderContainers(t *testing.T) { d.TamperWithContainerConfig(t, cid, func(container *container.Container) { // Simulate a container created with an older version of the Engine // by setting an empty list of port bindings. - container.HostConfig.PortBindings = containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {}} + container.HostConfig.PortBindings = networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {}} }) // Restart the daemon — it should backfill the empty port binding slice. @@ -1059,7 +1059,7 @@ func TestPortBindingBackfillingForOlderContainers(t *testing.T) { inspect := ctr.Inspect(ctx, t, c, cid) - expMappings := containertypes.PortMap{containertypes.MustParsePort("80/tcp"): { + expMappings := networktypes.PortMap{networktypes.MustParsePort("80/tcp"): { {}, // An empty PortBinding is backfilled }} assert.DeepEqual(t, expMappings, inspect.HostConfig.PortBindings, cmpopts.EquateComparable(netip.Addr{})) diff --git a/integration/network/bridge/iptablesdoc/iptablesdoc_linux_test.go b/integration/network/bridge/iptablesdoc/iptablesdoc_linux_test.go index 304c0434c2..fafff64427 100644 --- a/integration/network/bridge/iptablesdoc/iptablesdoc_linux_test.go +++ b/integration/network/bridge/iptablesdoc/iptablesdoc_linux_test.go @@ -30,7 +30,7 @@ import ( "text/template" "time" - containertypes "github.com/moby/moby/api/types/container" + networktypes "github.com/moby/moby/api/types/network" swarmtypes "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/client" "github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge" @@ -53,7 +53,7 @@ var ( type ctrDesc struct { name string - portMappings containertypes.PortMap + portMappings networktypes.PortMap } type networkDesc struct { @@ -82,7 +82,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -95,7 +95,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -108,7 +108,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -142,7 +142,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -155,7 +155,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -167,7 +167,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -179,7 +179,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostIP: netip.MustParseAddr("127.0.0.1"), HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostIP: netip.MustParseAddr("127.0.0.1"), HostPort: "8080"}}}, }, }, }}, @@ -354,7 +354,7 @@ func createServices(ctx context.Context, t *testing.T, d *daemon.Daemon, section hp, err := strconv.Atoi(hostPort.HostPort) assert.NilError(t, err) portConfig = append(portConfig, swarmtypes.PortConfig{ - Protocol: swarmtypes.PortConfigProtocol(ctrPP.Proto()), + Protocol: ctrPP.Proto(), PublishedPort: uint32(hp), TargetPort: uint32(ctrPP.Num()), }) diff --git a/integration/network/bridge/nftablesdoc/nftablesdoc_linux_test.go b/integration/network/bridge/nftablesdoc/nftablesdoc_linux_test.go index 7be4e353fc..c6510b7f21 100644 --- a/integration/network/bridge/nftablesdoc/nftablesdoc_linux_test.go +++ b/integration/network/bridge/nftablesdoc/nftablesdoc_linux_test.go @@ -28,7 +28,7 @@ import ( "testing" "text/template" - containertypes "github.com/moby/moby/api/types/container" + networktypes "github.com/moby/moby/api/types/network" swarmtypes "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/client" "github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge" @@ -50,7 +50,7 @@ var ( type ctrDesc struct { name string - portMappings containertypes.PortMap + portMappings networktypes.PortMap } type networkDesc struct { @@ -79,7 +79,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -92,7 +92,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -105,7 +105,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -139,7 +139,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -152,7 +152,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}, }, }, }}, @@ -178,7 +178,7 @@ var index = []section{ containers: []ctrDesc{ { name: "c1", - portMappings: containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostIP: netip.MustParseAddr("127.0.0.1"), HostPort: "8080"}}}, + portMappings: networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostIP: netip.MustParseAddr("127.0.0.1"), HostPort: "8080"}}}, }, }, }}, diff --git a/integration/network/overlay/overlay_test.go b/integration/network/overlay/overlay_test.go index 823dfc898d..9151bcb149 100644 --- a/integration/network/overlay/overlay_test.go +++ b/integration/network/overlay/overlay_test.go @@ -82,7 +82,7 @@ func TestHostPortMappings(t *testing.T) { swarm.ServiceWithNetwork(netName), swarm.ServiceWithEndpoint(&swarmtypes.EndpointSpec{ Ports: []swarmtypes.PortConfig{ - {Protocol: swarmtypes.PortConfigProtocolTCP, TargetPort: 80, PublishedPort: 80, PublishMode: swarmtypes.PortConfigPublishModeHost}, + {Protocol: networktypes.TCP, TargetPort: 80, PublishedPort: 80, PublishMode: swarmtypes.PortConfigPublishModeHost}, }, })) defer apiClient.ServiceRemove(ctx, svcID) diff --git a/integration/network/service_test.go b/integration/network/service_test.go index a6fb9ef164..5a591ee6b2 100644 --- a/integration/network/service_test.go +++ b/integration/network/service_test.go @@ -276,7 +276,7 @@ func TestServiceRemoveKeepsIngressNetwork(t *testing.T) { swarm.ServiceWithEndpoint(&swarmtypes.EndpointSpec{ Ports: []swarmtypes.PortConfig{ { - Protocol: swarmtypes.PortConfigProtocolTCP, + Protocol: networktypes.TCP, TargetPort: 80, PublishMode: swarmtypes.PortConfigPublishModeIngress, }, diff --git a/integration/networking/bridge_linux_test.go b/integration/networking/bridge_linux_test.go index d8b9e0a9e8..024e4f2376 100644 --- a/integration/networking/bridge_linux_test.go +++ b/integration/networking/bridge_linux_test.go @@ -17,7 +17,6 @@ import ( "time" "github.com/google/go-cmp/cmp/cmpopts" - containertypes "github.com/moby/moby/api/types/container" networktypes "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge" @@ -388,7 +387,7 @@ func TestBridgeINCRouted(t *testing.T) { container.WithNetworkMode(netName), container.WithName("ctr-"+gwMode), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {}}), ) t.Cleanup(func() { c.ContainerRemove(ctx, ctrId, client.ContainerRemoveOptions{Force: true}) @@ -565,7 +564,7 @@ func TestAccessToPublishedPort(t *testing.T) { container.WithNetworkMode(serverNetName), container.WithName("ctr-server"), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {containertypes.PortBinding{HostPort: "8080"}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {networktypes.PortBinding{HostPort: "8080"}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, ctrId, client.ContainerRemoveOptions{Force: true}) @@ -688,7 +687,7 @@ func TestInterNetworkDirectRouting(t *testing.T) { container.WithNetworkMode(serverNetName), container.WithName("ctr-pub"), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {containertypes.PortBinding{HostPort: "8080"}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {networktypes.PortBinding{HostPort: "8080"}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, ctrPubId, client.ContainerRemoveOptions{Force: true}) @@ -1063,7 +1062,7 @@ func TestDisableIPv6OnInterface(t *testing.T) { container.WithName(ctrName), container.WithNetworkMode(tc.netName), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}), container.WithEndpointSettings(tc.netName, &networktypes.EndpointSettings{ DriverOpts: map[string]string{ netlabel.EndpointSysctls: "net.ipv6.conf.IFNAME.disable_ipv6=1", @@ -1505,7 +1504,7 @@ func TestGatewaySelection(t *testing.T) { container.WithName(ctrName), container.WithNetworkMode(netName4), container.WithExposedPorts("80"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80"): {{HostPort: "8080"}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80"): {{HostPort: "8080"}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, ctrId, client.ContainerRemoveOptions{Force: true}) @@ -1956,7 +1955,7 @@ func TestDropInForwardChain(t *testing.T) { ctrId := container.Run(ctx, t, c, container.WithNetworkMode(netName46), container.WithExposedPorts("80"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80"): {{HostPort: hostPort}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80"): {{HostPort: hostPort}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, ctrId, client.ContainerRemoveOptions{Force: true}) diff --git a/integration/networking/nat_windows_test.go b/integration/networking/nat_windows_test.go index 0d0a3865b1..d0a18e5b1e 100644 --- a/integration/networking/nat_windows_test.go +++ b/integration/networking/nat_windows_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - containertypes "github.com/moby/moby/api/types/container" + networktypes "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/v2/integration/internal/container" "github.com/moby/moby/v2/integration/internal/network" @@ -98,13 +98,13 @@ func TestFlakyPortMappedHairpinWindows(t *testing.T) { serverId := container.Run(ctx, t, c, container.WithNetworkMode(serverNetName), container.WithExposedPorts("80"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80"): {{HostIP: netip.IPv4Unspecified()}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80"): {{HostIP: netip.IPv4Unspecified()}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, serverId, client.ContainerRemoveOptions{Force: true}) inspect := container.Inspect(ctx, t, c, serverId) - hostPort := inspect.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")][0].HostPort + hostPort := inspect.NetworkSettings.Ports[networktypes.MustParsePort("80/tcp")][0].HostPort attachCtx, cancel := context.WithTimeout(ctx, 15*time.Second) defer cancel() diff --git a/integration/networking/port_mapping_linux_test.go b/integration/networking/port_mapping_linux_test.go index b9f2312791..a144567d27 100644 --- a/integration/networking/port_mapping_linux_test.go +++ b/integration/networking/port_mapping_linux_test.go @@ -17,7 +17,6 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/moby/moby/api/pkg/stdcopy" - containertypes "github.com/moby/moby/api/types/container" networktypes "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge" @@ -69,12 +68,12 @@ func TestDisableNAT(t *testing.T) { name string gwMode4 string gwMode6 string - expPortMap containertypes.PortMap + expPortMap networktypes.PortMap }{ { name: "defaults", - expPortMap: containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): []containertypes.PortBinding{ + expPortMap: networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): []networktypes.PortBinding{ {HostIP: netip.MustParseAddr("0.0.0.0"), HostPort: "8080"}, {HostIP: netip.MustParseAddr("::"), HostPort: "8080"}, }, @@ -84,8 +83,8 @@ func TestDisableNAT(t *testing.T) { name: "nat4 routed6", gwMode4: "nat", gwMode6: "routed", - expPortMap: containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): []containertypes.PortBinding{ + expPortMap: networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): []networktypes.PortBinding{ {HostIP: netip.MustParseAddr("0.0.0.0"), HostPort: "8080"}, {HostIP: netip.MustParseAddr("::"), HostPort: ""}, }, @@ -95,8 +94,8 @@ func TestDisableNAT(t *testing.T) { name: "nat6 routed4", gwMode4: "routed", gwMode6: "nat", - expPortMap: containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): []containertypes.PortBinding{ + expPortMap: networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): []networktypes.PortBinding{ {HostIP: netip.MustParseAddr("::"), HostPort: "8080"}, {HostIP: netip.MustParseAddr("0.0.0.0"), HostPort: ""}, }, @@ -125,7 +124,7 @@ func TestDisableNAT(t *testing.T) { id := container.Run(ctx, t, c, container.WithNetworkMode(netName), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}}), ) defer c.ContainerRemove(ctx, id, client.ContainerRemoveOptions{Force: true}) @@ -163,13 +162,13 @@ func TestPortMappedHairpinTCP(t *testing.T) { serverId := container.Run(ctx, t, c, container.WithNetworkMode(serverNetName), container.WithExposedPorts("80"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80"): {{HostIP: netip.MustParseAddr("0.0.0.0")}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80"): {{HostIP: netip.MustParseAddr("0.0.0.0")}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, serverId, client.ContainerRemoveOptions{Force: true}) inspect := container.Inspect(ctx, t, c, serverId) - hostPort := inspect.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")][0].HostPort + hostPort := inspect.NetworkSettings.Ports[networktypes.MustParsePort("80/tcp")][0].HostPort clientCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() @@ -210,13 +209,13 @@ func TestPortMappedHairpinUDP(t *testing.T) { serverId := container.Run(ctx, t, c, container.WithNetworkMode(serverNetName), container.WithExposedPorts("54/udp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("54/udp"): {{HostIP: netip.MustParseAddr("0.0.0.0")}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("54/udp"): {{HostIP: netip.MustParseAddr("0.0.0.0")}}}), container.WithCmd("/bin/sh", "-c", "echo 'foobar.internal 192.168.155.23' | dnsd -c - -p 54"), ) defer c.ContainerRemove(ctx, serverId, client.ContainerRemoveOptions{Force: true}) inspect := container.Inspect(ctx, t, c, serverId) - hostPort := inspect.NetworkSettings.Ports[containertypes.MustParsePort("54/udp")][0].HostPort + hostPort := inspect.NetworkSettings.Ports[networktypes.MustParsePort("54/udp")][0].HostPort // nslookup gets an answer quickly from the dns server, but then tries to // query another DNS server (for some unknown reasons) and times out. Hence, @@ -252,13 +251,13 @@ func TestProxy4To6(t *testing.T) { serverId := container.Run(ctx, t, c, container.WithNetworkMode(netName), container.WithExposedPorts("80"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80"): {{HostIP: netip.MustParseAddr("::1")}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80"): {{HostIP: netip.MustParseAddr("::1")}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, serverId, client.ContainerRemoveOptions{Force: true}) inspect := container.Inspect(ctx, t, c, serverId) - hostPort := inspect.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")][0].HostPort + hostPort := inspect.NetworkSettings.Ports[networktypes.MustParsePort("80/tcp")][0].HostPort var resp *http.Response addr := "http://[::1]:" + hostPort @@ -376,7 +375,7 @@ func TestAccessPublishedPortFromHost(t *testing.T) { serverID := container.Run(ctx, t, c, container.WithName(sanitizeCtrName(t.Name()+"-server")), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: hostPort}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: hostPort}}}), container.WithCmd("httpd", "-f"), container.WithNetworkMode(bridgeName)) defer c.ContainerRemove(ctx, serverID, client.ContainerRemoveOptions{Force: true}) @@ -456,7 +455,7 @@ func TestAccessPublishedPortFromRemoteHost(t *testing.T) { serverID := container.Run(ctx, t, c, container.WithName(sanitizeCtrName(t.Name()+"-server")), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostPort: hostPort}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostPort: hostPort}}}), container.WithCmd("httpd", "-f"), container.WithNetworkMode(bridgeName)) defer c.ContainerRemove(ctx, serverID, client.ContainerRemoveOptions{Force: true}) @@ -554,13 +553,13 @@ func TestAccessPublishedPortFromCtr(t *testing.T) { serverId := container.Run(ctx, t, c, container.WithNetworkMode(netName), container.WithExposedPorts("80"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): {{HostIP: netip.MustParseAddr("0.0.0.0")}}}), + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): {{HostIP: netip.MustParseAddr("0.0.0.0")}}}), container.WithCmd("httpd", "-f"), ) defer c.ContainerRemove(ctx, serverId, client.ContainerRemoveOptions{Force: true}) inspect := container.Inspect(ctx, t, c, serverId) - hostPort := inspect.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")][0].HostPort + hostPort := inspect.NetworkSettings.Ports[networktypes.MustParsePort("80/tcp")][0].HostPort clientCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() @@ -604,8 +603,8 @@ func TestRestartUserlandProxyUnder2MSL(t *testing.T) { ctrOpts := []func(*container.TestContainerConfig){ container.WithName(ctrName), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): {{HostPort: "1780"}}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): {{HostPort: "1780"}}, }), container.WithCmd("httpd", "-f"), container.WithNetworkMode(netName), @@ -703,8 +702,8 @@ func TestDirectRoutingOpenPorts(t *testing.T) { container.WithNetworkMode(netName), container.WithName("ctr-"+gwMode), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): {}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): {}, }), ) t.Cleanup(func() { @@ -984,8 +983,8 @@ func TestRoutedNonGateway(t *testing.T) { ctrId := container.Run(ctx, t, c, container.WithCmd("httpd", "-f"), container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("80/tcp"): {{HostPort: "8080"}}, }), container.WithNetworkMode(natNetName), container.WithNetworkMode(routedNetName), @@ -1140,8 +1139,8 @@ func TestAccessPublishedPortFromAnotherNetwork(t *testing.T) { container.WithName("server"), container.WithCmd("nc", "-lp", "5000"), container.WithExposedPorts("5000/tcp"), - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("5000/tcp"): {{HostPort: "5000"}}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("5000/tcp"): {{HostPort: "5000"}}, }), container.WithNetworkMode(servnet)) defer c.ContainerRemove(ctx, serverID, client.ContainerRemoveOptions{Force: true}) @@ -1338,8 +1337,8 @@ func testDirectRemoteAccessOnExposedPort(t *testing.T, ctx context.Context, d *d container.WithName(sanitizeCtrName(t.Name()+"-server")), container.WithCmd("nc", "-lup", "5000"), container.WithExposedPorts("5000/udp"), - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("5000/udp"): {{HostPort: hostPort}}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("5000/udp"): {{HostPort: hostPort}}, }), container.WithNetworkMode(bridgeName), container.WithEndpointSettings(bridgeName, &networktypes.EndpointSettings{ @@ -1426,8 +1425,8 @@ func TestAccessPortPublishedOnLoopbackAddress(t *testing.T) { container.WithCmd("nc", "-lup", "5000"), container.WithExposedPorts("5000/udp"), // This port is mapped on 127.0.0.2, so it should not be remotely accessible. - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("5000/udp"): {{HostIP: netip.MustParseAddr(loIP), HostPort: hostPort}}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("5000/udp"): {{HostIP: netip.MustParseAddr(loIP), HostPort: hostPort}}, }), container.WithNetworkMode(bridgeName)) defer c.ContainerRemove(ctx, serverID, client.ContainerRemoveOptions{Force: true}) @@ -1539,7 +1538,7 @@ func TestSkipRawRules(t *testing.T) { ctrId := container.Run(ctx, t, c, container.WithExposedPorts("80/tcp"), - container.WithPortMap(containertypes.PortMap{containertypes.MustParsePort("80/tcp"): { + container.WithPortMap(networktypes.PortMap{networktypes.MustParsePort("80/tcp"): { {HostIP: netip.MustParseAddr("127.0.0.1"), HostPort: "8080"}, {HostPort: "8081"}, }}), @@ -1571,10 +1570,10 @@ func TestMixAnyWithSpecificHostAddrs(t *testing.T) { ctrId := container.Run(ctx, t, c, container.WithExposedPorts("80/"+proto, "81/"+proto, "82/"+proto), - container.WithPortMap(containertypes.PortMap{ - containertypes.MustParsePort("81/" + proto): {{}}, - containertypes.MustParsePort("82/" + proto): {{}}, - containertypes.MustParsePort("80/" + proto): {{HostIP: netip.MustParseAddr("127.0.0.1")}}, + container.WithPortMap(networktypes.PortMap{ + networktypes.MustParsePort("81/" + proto): {{}}, + networktypes.MustParsePort("82/" + proto): {{}}, + networktypes.MustParsePort("80/" + proto): {{HostIP: netip.MustParseAddr("127.0.0.1")}}, }), ) defer c.ContainerRemove(ctx, ctrId, client.ContainerRemoveOptions{Force: true}) diff --git a/internal/testutil/fakestorage/storage.go b/internal/testutil/fakestorage/storage.go index 525c25daab..434b9f497e 100644 --- a/internal/testutil/fakestorage/storage.go +++ b/internal/testutil/fakestorage/storage.go @@ -12,6 +12,7 @@ import ( "testing" containertypes "github.com/moby/moby/api/types/container" + "github.com/moby/moby/api/types/network" "github.com/moby/moby/client" "github.com/moby/moby/v2/internal/testutil" "github.com/moby/moby/v2/internal/testutil/environment" @@ -164,7 +165,7 @@ COPY . /static`); err != nil { // Find out the system assigned port i, err := c.ContainerInspect(context.Background(), b.ID) assert.NilError(t, err) - ports, exists := i.NetworkSettings.Ports[containertypes.MustParsePort("80/tcp")] + ports, exists := i.NetworkSettings.Ports[network.MustParsePort("80/tcp")] assert.Assert(t, exists, "unable to find port 80/tcp for %s", ctrName) if len(ports) == 0 { t.Fatalf("no ports mapped for 80/tcp for %s: %#v", ctrName, i.NetworkSettings.Ports) diff --git a/vendor/github.com/moby/moby/api/types/container/config.go b/vendor/github.com/moby/moby/api/types/container/config.go index 12fa6b3102..c5650f10c9 100644 --- a/vendor/github.com/moby/moby/api/types/container/config.go +++ b/vendor/github.com/moby/moby/api/types/container/config.go @@ -4,6 +4,7 @@ import ( "time" dockerspec "github.com/moby/docker-image-spec/specs-go/v1" + "github.com/moby/moby/api/types/network" ) // MinimumDuration puts a minimum on user configured duration. @@ -28,7 +29,7 @@ type Config struct { AttachStdin bool // Attach the standard input, makes possible user interaction AttachStdout bool // Attach the standard output AttachStderr bool // Attach the standard error - ExposedPorts PortSet `json:",omitempty"` // List of exposed ports + ExposedPorts network.PortSet `json:",omitempty"` // List of exposed ports Tty bool // Attach standard streams to a tty, including stdin if it is not closed. OpenStdin bool // Open stdin StdinOnce bool // If true, close stdin after the 1 attached client disconnects. diff --git a/vendor/github.com/moby/moby/api/types/container/hostconfig.go b/vendor/github.com/moby/moby/api/types/container/hostconfig.go index 6df9d1d63c..0f889c6512 100644 --- a/vendor/github.com/moby/moby/api/types/container/hostconfig.go +++ b/vendor/github.com/moby/moby/api/types/container/hostconfig.go @@ -421,7 +421,7 @@ type HostConfig struct { ContainerIDFile string // File (path) where the containerId is written LogConfig LogConfig // Configuration of the logs for this container NetworkMode NetworkMode // Network mode to use for the container - PortBindings PortMap // Port mapping between the exposed port (container) and the host + PortBindings network.PortMap // Port mapping between the exposed port (container) and the host RestartPolicy RestartPolicy // Restart policy to be used for the container AutoRemove bool // Automatically remove container when it exits VolumeDriver string // Name of the volume driver used to mount volumes diff --git a/vendor/github.com/moby/moby/api/types/container/network_settings.go b/vendor/github.com/moby/moby/api/types/container/network_settings.go index fb7f59df08..c51c0839d2 100644 --- a/vendor/github.com/moby/moby/api/types/container/network_settings.go +++ b/vendor/github.com/moby/moby/api/types/container/network_settings.go @@ -6,10 +6,13 @@ import ( // NetworkSettings exposes the network settings in the api type NetworkSettings struct { - SandboxID string // SandboxID uniquely represents a container's network stack - SandboxKey string // SandboxKey identifies the sandbox - Ports PortMap // Ports is a collection of PortBinding indexed by Port - Networks map[string]*network.EndpointSettings + SandboxID string // SandboxID uniquely represents a container's network stack + SandboxKey string // SandboxKey identifies the sandbox + + // Ports is a collection of [network.PortBinding] indexed by [network.Port] + Ports network.PortMap + + Networks map[string]*network.EndpointSettings } // NetworkSettingsSummary provides a summary of container's networks diff --git a/vendor/github.com/moby/moby/api/types/container/network.go b/vendor/github.com/moby/moby/api/types/network/port.go similarity index 92% rename from vendor/github.com/moby/moby/api/types/container/network.go rename to vendor/github.com/moby/moby/api/types/network/port.go index 17e90ed746..171d9f51d3 100644 --- a/vendor/github.com/moby/moby/api/types/container/network.go +++ b/vendor/github.com/moby/moby/api/types/network/port.go @@ -1,4 +1,4 @@ -package container +package network import ( "errors" @@ -10,24 +10,24 @@ import ( "unique" ) -// NetworkProtocol represents a network protocol for a port. -type NetworkProtocol string +// IPProtocol represents a network protocol for a port. +type IPProtocol string const ( - TCP NetworkProtocol = "tcp" - UDP NetworkProtocol = "udp" - SCTP NetworkProtocol = "sctp" + TCP IPProtocol = "tcp" + UDP IPProtocol = "udp" + SCTP IPProtocol = "sctp" ) // Sentinel port proto value for zero Port and PortRange values. -var protoZero unique.Handle[NetworkProtocol] +var protoZero unique.Handle[IPProtocol] // Port is a type representing a single port number and protocol in the format "/[]". // // The zero port value, i.e. Port{}, is invalid; use [ParsePort] to create a valid Port value. type Port struct { num uint16 - proto unique.Handle[NetworkProtocol] + proto unique.Handle[IPProtocol] } // ParsePort parses s as a [Port]. @@ -64,7 +64,7 @@ func MustParsePort(s string) Port { // PortFrom returns a [Port] with the given number and protocol. // // If no protocol is specified (i.e. proto == ""), then PortFrom returns Port{}, false. -func PortFrom(num uint16, proto NetworkProtocol) (p Port, ok bool) { +func PortFrom(num uint16, proto IPProtocol) (p Port, ok bool) { if proto == "" { return Port{}, false } @@ -78,7 +78,7 @@ func (p Port) Num() uint16 { } // Proto returns p's network protocol. -func (p Port) Proto() NetworkProtocol { +func (p Port) Proto() IPProtocol { return p.proto.Value() } @@ -163,7 +163,7 @@ type PortMap = map[Port][]PortBinding type PortRange struct { start uint16 end uint16 - proto unique.Handle[NetworkProtocol] + proto unique.Handle[IPProtocol] } // ParsePortRange parses s as a [PortRange]. @@ -212,7 +212,7 @@ func MustParsePortRange(s string) PortRange { // PortRangeFrom returns a [PortRange] with the given start and end port numbers and protocol. // // If end < start or no protocol is specified (i.e. proto == ""), then PortRangeFrom returns PortRange{}, false. -func PortRangeFrom(start, end uint16, proto NetworkProtocol) (pr PortRange, ok bool) { +func PortRangeFrom(start, end uint16, proto IPProtocol) (pr PortRange, ok bool) { if end < start || proto == "" { return PortRange{}, false } @@ -231,7 +231,7 @@ func (pr PortRange) End() uint16 { } // Proto returns pr's network protocol. -func (pr PortRange) Proto() NetworkProtocol { +func (pr PortRange) Proto() IPProtocol { return pr.proto.Value() } @@ -335,12 +335,12 @@ func parsePortNumber(rawPort string) (uint16, error) { // normalizePortProto normalizes the protocol string such that "tcp", "TCP", and "tCp" are equivalent. // If proto is not specified, it defaults to "tcp". -func normalizePortProto(proto string) unique.Handle[NetworkProtocol] { +func normalizePortProto(proto string) unique.Handle[IPProtocol] { if proto == "" { return unique.Make(TCP) } proto = strings.ToLower(proto) - return unique.Make(NetworkProtocol(proto)) + return unique.Make(IPProtocol(proto)) } diff --git a/vendor/github.com/moby/moby/api/types/swarm/network.go b/vendor/github.com/moby/moby/api/types/swarm/network.go index 9f181fb00c..5e2517174f 100644 --- a/vendor/github.com/moby/moby/api/types/swarm/network.go +++ b/vendor/github.com/moby/moby/api/types/swarm/network.go @@ -32,7 +32,7 @@ const ( // PortConfig represents the config of a port. type PortConfig struct { Name string `json:",omitempty"` - Protocol PortConfigProtocol `json:",omitempty"` + Protocol network.IPProtocol `json:",omitempty"` // TargetPort is the port inside the container TargetPort uint32 `json:",omitempty"` // PublishedPort is the port on the swarm hosts @@ -54,20 +54,6 @@ const ( PortConfigPublishModeHost PortConfigPublishMode = "host" ) -// PortConfigProtocol represents the protocol of a port. -type PortConfigProtocol string - -const ( - // TODO(stevvooe): These should be used generally, not just for PortConfig. - - // PortConfigProtocolTCP TCP - PortConfigProtocolTCP PortConfigProtocol = "tcp" - // PortConfigProtocolUDP UDP - PortConfigProtocolUDP PortConfigProtocol = "udp" - // PortConfigProtocolSCTP SCTP - PortConfigProtocolSCTP PortConfigProtocol = "sctp" -) - // EndpointVirtualIP represents the virtual ip of a port. type EndpointVirtualIP struct { NetworkID string `json:",omitempty"`