replace direct uses of github.com/docker/go-connections/nat types
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@ -414,8 +414,8 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
|
||||
|
||||
publishOpts := copts.publish.GetSlice()
|
||||
var (
|
||||
ports map[nat.Port]struct{}
|
||||
portBindings map[nat.Port][]nat.PortBinding
|
||||
ports map[container.PortRangeProto]struct{}
|
||||
portBindings map[container.PortRangeProto][]container.PortBinding
|
||||
convertedOpts []string
|
||||
)
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
networktypes "github.com/moby/moby/api/types/network"
|
||||
"github.com/spf13/pflag"
|
||||
@ -439,7 +438,7 @@ func TestParseWithExpose(t *testing.T) {
|
||||
"8080-NaN/tcp": `invalid range format for --expose: 8080-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
|
||||
"1234567890-8080/tcp": `invalid range format for --expose: 1234567890-8080/tcp, error: strconv.ParseUint: parsing "1234567890": value out of range`,
|
||||
}
|
||||
valids := map[string][]nat.Port{
|
||||
valids := map[string][]container.PortRangeProto{
|
||||
"8080/tcp": {"8080/tcp"},
|
||||
"8080/udp": {"8080/udp"},
|
||||
"8080/ncp": {"8080/ncp"},
|
||||
@ -473,7 +472,7 @@ func TestParseWithExpose(t *testing.T) {
|
||||
if len(config.ExposedPorts) != 2 {
|
||||
t.Fatalf("Expected 2 exposed ports, got %v", config.ExposedPorts)
|
||||
}
|
||||
ports := []nat.Port{"80/tcp", "81/tcp"}
|
||||
ports := []container.PortRangeProto{"80/tcp", "81/tcp"}
|
||||
for _, port := range ports {
|
||||
if _, ok := config.ExposedPorts[port]; !ok {
|
||||
t.Fatalf("Expected %v, got %v", ports, config.ExposedPorts)
|
||||
|
||||
@ -11,8 +11,8 @@ import (
|
||||
"github.com/docker/cli/cli"
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/cli/cli/command/completion"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/fvbommel/sortorder"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@ -67,7 +67,7 @@ func runPort(ctx context.Context, dockerCli command.Cli, opts *portOptions) erro
|
||||
if _, err = strconv.ParseUint(port, 10, 16); err != nil {
|
||||
return errors.Wrapf(err, "Error: invalid port (%s)", port)
|
||||
}
|
||||
frontends, exists := c.NetworkSettings.Ports[nat.Port(port+"/"+proto)]
|
||||
frontends, exists := c.NetworkSettings.Ports[container.PortRangeProto(port+"/"+proto)]
|
||||
if !exists || len(frontends) == 0 {
|
||||
return errors.Errorf("Error: No public port '%s' published for %s", opts.port, opts.container)
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/cli/internal/test"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
"gotest.tools/v3/assert"
|
||||
"gotest.tools/v3/golden"
|
||||
@ -47,19 +46,19 @@ func TestNewPortCommandOutput(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
inspectFunc: func(string) (container.InspectResponse, error) {
|
||||
ci := container.InspectResponse{NetworkSettings: &container.NetworkSettings{}}
|
||||
ci.NetworkSettings.Ports = nat.PortMap{
|
||||
"80/tcp": make([]nat.PortBinding, len(tc.ips)),
|
||||
"443/tcp": make([]nat.PortBinding, len(tc.ips)),
|
||||
"443/udp": make([]nat.PortBinding, len(tc.ips)),
|
||||
ci.NetworkSettings.Ports = container.PortMap{
|
||||
"80/tcp": make([]container.PortBinding, len(tc.ips)),
|
||||
"443/tcp": make([]container.PortBinding, len(tc.ips)),
|
||||
"443/udp": make([]container.PortBinding, len(tc.ips)),
|
||||
}
|
||||
for i, ip := range tc.ips {
|
||||
ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{
|
||||
ci.NetworkSettings.Ports["80/tcp"][i] = container.PortBinding{
|
||||
HostIP: ip, HostPort: "3456",
|
||||
}
|
||||
ci.NetworkSettings.Ports["443/tcp"][i] = nat.PortBinding{
|
||||
ci.NetworkSettings.Ports["443/tcp"][i] = container.PortBinding{
|
||||
HostIP: ip, HostPort: "4567",
|
||||
}
|
||||
ci.NetworkSettings.Ports["443/udp"][i] = nat.PortBinding{
|
||||
ci.NetworkSettings.Ports["443/udp"][i] = container.PortBinding{
|
||||
HostIP: ip, HostPort: "5678",
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +24,7 @@ import (
|
||||
"github.com/docker/go-units"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/google/shlex"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
"github.com/moby/moby/api/types/versions"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -934,7 +935,7 @@ func toServicePortConfigs(value string) ([]any, error) {
|
||||
|
||||
for _, key := range keys {
|
||||
// Reuse ConvertPortToPortConfig so that it is consistent
|
||||
portConfig, err := swarmopts.ConvertPortToPortConfig(nat.Port(key), portBindings)
|
||||
portConfig, err := swarmopts.ConvertPortToPortConfig(container.PortRangeProto(key), portBindings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -150,28 +151,31 @@ func (p *PortOpt) Value() []swarm.PortConfig {
|
||||
|
||||
// ConvertPortToPortConfig converts ports to the swarm type
|
||||
func ConvertPortToPortConfig(
|
||||
port nat.Port,
|
||||
portBindings map[nat.Port][]nat.PortBinding,
|
||||
portRangeProto container.PortRangeProto,
|
||||
portBindings map[container.PortRangeProto][]container.PortBinding,
|
||||
) ([]swarm.PortConfig, error) {
|
||||
ports := []swarm.PortConfig{}
|
||||
proto, port := nat.SplitProtoPort(string(portRangeProto))
|
||||
portInt, _ := strconv.ParseUint(port, 10, 16)
|
||||
proto = strings.ToLower(proto)
|
||||
|
||||
for _, binding := range portBindings[port] {
|
||||
var ports []swarm.PortConfig
|
||||
for _, binding := range portBindings[portRangeProto] {
|
||||
if p := net.ParseIP(binding.HostIP); p != nil && !p.IsUnspecified() {
|
||||
// TODO(thaJeztah): use context-logger, so that this output can be suppressed (in tests).
|
||||
logrus.Warnf("ignoring IP-address (%s:%s) service will listen on '0.0.0.0'", net.JoinHostPort(binding.HostIP, binding.HostPort), port)
|
||||
logrus.Warnf("ignoring IP-address (%s:%s) service will listen on '0.0.0.0'", net.JoinHostPort(binding.HostIP, binding.HostPort), portRangeProto)
|
||||
}
|
||||
|
||||
startHostPort, endHostPort, err := nat.ParsePortRange(binding.HostPort)
|
||||
|
||||
if err != nil && binding.HostPort != "" {
|
||||
return nil, fmt.Errorf("invalid hostport binding (%s) for port (%s)", binding.HostPort, port.Port())
|
||||
return nil, fmt.Errorf("invalid hostport binding (%s) for port (%s)", binding.HostPort, port)
|
||||
}
|
||||
|
||||
for i := startHostPort; i <= endHostPort; i++ {
|
||||
ports = append(ports, swarm.PortConfig{
|
||||
// TODO Name: ?
|
||||
Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())),
|
||||
TargetPort: uint32(port.Int()),
|
||||
Protocol: swarm.PortConfigProtocol(proto),
|
||||
TargetPort: uint32(portInt),
|
||||
PublishedPort: uint32(i),
|
||||
PublishMode: swarm.PortConfigPublishModeIngress,
|
||||
})
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gotest.tools/v3/assert"
|
||||
@ -347,7 +347,7 @@ func TestConvertPortToPortConfigWithIP(t *testing.T) {
|
||||
logrus.SetOutput(&b)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.value, func(t *testing.T) {
|
||||
_, err := ConvertPortToPortConfig("80/tcp", map[nat.Port][]nat.PortBinding{
|
||||
_, err := ConvertPortToPortConfig("80/tcp", map[container.PortRangeProto][]container.PortBinding{
|
||||
"80/tcp": {{HostIP: tc.value, HostPort: "2345"}},
|
||||
})
|
||||
assert.NilError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user