Merge pull request #51094 from austinvazquez/consolidate-api-port-types

Consolidate api port types
This commit is contained in:
Sebastiaan van Stijn
2025-10-06 12:35:15 +02:00
committed by GitHub
49 changed files with 352 additions and 363 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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

View File

@@ -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 "<portnum>/[<proto>]".
//
// 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))
}

View File

@@ -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")

View File

@@ -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"`