Skip to content

Commit 483a220

Browse files
committed
vendor: github.com/moby/moby/api, github.com/moby/moby/client master
full diff: moby/moby@4ca8aed...ad72822 Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 85ac71a commit 483a220

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1050
-726
lines changed

cli/command/builder/client_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ package builder
33
import (
44
"context"
55

6-
"github.com/moby/moby/api/types/build"
76
"github.com/moby/moby/client"
87
)
98

109
type fakeClient struct {
1110
client.Client
12-
builderPruneFunc func(ctx context.Context, opts client.BuildCachePruneOptions) (*build.CachePruneReport, error)
11+
builderPruneFunc func(ctx context.Context, opts client.BuildCachePruneOptions) (client.BuildCachePruneResult, error)
1312
}
1413

15-
func (c *fakeClient) BuildCachePrune(ctx context.Context, opts client.BuildCachePruneOptions) (*build.CachePruneReport, error) {
14+
func (c *fakeClient) BuildCachePrune(ctx context.Context, opts client.BuildCachePruneOptions) (client.BuildCachePruneResult, error) {
1615
if c.builderPruneFunc != nil {
1716
return c.builderPruneFunc(ctx, opts)
1817
}
19-
return nil, nil
18+
return client.BuildCachePruneResult{}, nil
2019
}

cli/command/builder/prune.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
8787
}
8888
}
8989

90-
report, err := dockerCli.Client().BuildCachePrune(ctx, client.BuildCachePruneOptions{
90+
resp, err := dockerCli.Client().BuildCachePrune(ctx, client.BuildCachePruneOptions{
9191
All: options.all,
9292
ReservedSpace: options.reservedSpace.Value(),
9393
Filters: pruneFilters,
9494
})
9595
if err != nil {
9696
return 0, "", err
9797
}
98-
98+
report := resp.Report
9999
if len(report.CachesDeleted) > 0 {
100100
var sb strings.Builder
101101
sb.WriteString("Deleted build cache objects:\n")

cli/command/builder/prune_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
"github.com/docker/cli/internal/test"
10-
"github.com/moby/moby/api/types/build"
1110
"github.com/moby/moby/client"
1211
)
1312

@@ -16,8 +15,8 @@ func TestBuilderPromptTermination(t *testing.T) {
1615
t.Cleanup(cancel)
1716

1817
cli := test.NewFakeCli(&fakeClient{
19-
builderPruneFunc: func(ctx context.Context, opts client.BuildCachePruneOptions) (*build.CachePruneReport, error) {
20-
return nil, errors.New("fakeClient builderPruneFunc should not be called")
18+
builderPruneFunc: func(ctx context.Context, opts client.BuildCachePruneOptions) (client.BuildCachePruneResult, error) {
19+
return client.BuildCachePruneResult{}, errors.New("fakeClient builderPruneFunc should not be called")
2120
},
2221
})
2322
cmd := newPruneCommand(cli)

cli/command/container/opts.go

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"errors"
77
"fmt"
88
"net"
9+
"net/netip"
910
"os"
1011
"path"
1112
"path/filepath"
1213
"reflect"
13-
"strconv"
1414
"strings"
1515
"time"
1616

@@ -426,38 +426,60 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
426426
entrypoint = []string{""}
427427
}
428428

429+
// TODO(thaJeztah): remove uses of go-connections/nat here.
429430
convertedOpts, err := convertToStandardNotation(copts.publish.GetSlice())
430431
if err != nil {
431432
return nil, err
432433
}
433434

434-
ports, portBindings, err := nat.ParsePortSpecs(convertedOpts)
435+
ports, natPortBindings, err := nat.ParsePortSpecs(convertedOpts)
435436
if err != nil {
436437
return nil, err
437438
}
439+
portBindings := network.PortMap{}
440+
for port, bindings := range natPortBindings {
441+
p, err := network.ParsePort(string(port))
442+
if err != nil {
443+
return nil, err
444+
}
445+
portBindings[p] = []network.PortBinding{}
446+
for _, b := range bindings {
447+
var hostIP netip.Addr
448+
if b.HostIP != "" {
449+
hostIP, err = netip.ParseAddr(b.HostIP)
450+
if err != nil {
451+
return nil, err
452+
}
453+
}
454+
portBindings[p] = append(portBindings[p], network.PortBinding{
455+
HostIP: hostIP,
456+
HostPort: b.HostPort,
457+
})
458+
}
459+
}
460+
461+
// Add published ports as exposed ports.
462+
exposedPorts := network.PortSet{}
463+
for port := range ports {
464+
p, err := network.ParsePort(string(port))
465+
if err != nil {
466+
return nil, err
467+
}
468+
exposedPorts[p] = struct{}{}
469+
}
438470

439471
// Merge in exposed ports to the map of published ports
440472
for _, e := range copts.expose.GetSlice() {
441-
if strings.Contains(e, ":") {
442-
return nil, fmt.Errorf("invalid port format for --expose: %s", e)
443-
}
444473
// support two formats for expose, original format <portnum>/[<proto>]
445474
// or <startport-endport>/[<proto>]
446-
proto, port := nat.SplitProtoPort(e)
447-
// parse the start and end port and create a sequence of ports to expose
448-
// if expose a port, the start and end port are the same
449-
start, end, err := nat.ParsePortRange(port)
475+
pr, err := network.ParsePortRange(e)
450476
if err != nil {
451-
return nil, fmt.Errorf("invalid range format for --expose: %s, error: %w", e, err)
477+
return nil, fmt.Errorf("invalid port format for --expose: %w", err)
452478
}
453-
for i := start; i <= end; i++ {
454-
p, err := nat.NewPort(proto, strconv.FormatUint(i, 10))
455-
if err != nil {
456-
return nil, err
457-
}
458-
if _, exists := ports[p]; !exists {
459-
ports[p] = struct{}{}
460-
}
479+
// parse the start and end port and create a sequence of ports to expose
480+
// if expose a port, the start and end port are the same
481+
for p := range pr.All() {
482+
exposedPorts[p] = struct{}{}
461483
}
462484
}
463485

@@ -626,7 +648,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
626648
config := &container.Config{
627649
Hostname: copts.hostname,
628650
Domainname: copts.domainname,
629-
ExposedPorts: ports,
651+
ExposedPorts: exposedPorts,
630652
User: copts.user,
631653
Tty: copts.tty,
632654
OpenStdin: copts.stdin,
@@ -662,7 +684,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
662684
// but pre created containers can still have those nil values.
663685
// See https://github.com/docker/docker/pull/17779
664686
// for a more detailed explanation on why we don't want that.
665-
DNS: copts.dns.GetAllOrEmpty(),
687+
DNS: toNetipAddrSlice(copts.dns.GetAllOrEmpty()),
666688
DNSSearch: copts.dnsSearch.GetAllOrEmpty(),
667689
DNSOptions: copts.dnsOptions.GetAllOrEmpty(),
668690
ExtraHosts: copts.extraHosts.GetSlice(),
@@ -805,10 +827,10 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
805827
if len(n.Links) > 0 && copts.links.Len() > 0 {
806828
return invalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
807829
}
808-
if n.IPv4Address != "" && copts.ipv4Address != "" {
830+
if n.IPv4Address.IsValid() && copts.ipv4Address != "" {
809831
return invalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
810832
}
811-
if n.IPv6Address != "" && copts.ipv6Address != "" {
833+
if n.IPv6Address.IsValid() && copts.ipv6Address != "" {
812834
return invalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
813835
}
814836
if n.MacAddress != "" && copts.macAddress != "" {
@@ -828,17 +850,24 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
828850
copy(n.Links, copts.links.GetSlice())
829851
}
830852
if copts.ipv4Address != "" {
831-
n.IPv4Address = copts.ipv4Address
853+
var err error
854+
n.IPv4Address, err = netip.ParseAddr(copts.ipv4Address)
855+
if err != nil {
856+
return err
857+
}
832858
}
833859
if copts.ipv6Address != "" {
834-
n.IPv6Address = copts.ipv6Address
860+
var err error
861+
n.IPv6Address, err = netip.ParseAddr(copts.ipv6Address)
862+
if err != nil {
863+
return err
864+
}
835865
}
836866
if copts.macAddress != "" {
837867
n.MacAddress = copts.macAddress
838868
}
839869
if copts.linkLocalIPs.Len() > 0 {
840-
n.LinkLocalIPs = make([]string, copts.linkLocalIPs.Len())
841-
copy(n.LinkLocalIPs, copts.linkLocalIPs.GetSlice())
870+
n.LinkLocalIPs = toNetipAddrSlice(copts.linkLocalIPs.GetSlice())
842871
}
843872
return nil
844873
}
@@ -867,7 +896,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*network.Endpoint
867896
if len(ep.Links) > 0 {
868897
epConfig.Links = ep.Links
869898
}
870-
if ep.IPv4Address != "" || ep.IPv6Address != "" || len(ep.LinkLocalIPs) > 0 {
899+
if ep.IPv4Address.IsValid() || ep.IPv6Address.IsValid() || len(ep.LinkLocalIPs) > 0 {
871900
epConfig.IPAMConfig = &network.EndpointIPAMConfig{
872901
IPv4Address: ep.IPv4Address,
873902
IPv6Address: ep.IPv6Address,
@@ -1131,3 +1160,15 @@ func validateAttach(val string) (string, error) {
11311160
}
11321161
return val, errors.New("valid streams are STDIN, STDOUT and STDERR")
11331162
}
1163+
1164+
func toNetipAddrSlice(ips []string) []netip.Addr {
1165+
netips := make([]netip.Addr, 0, len(ips))
1166+
for _, ip := range ips {
1167+
addr, err := netip.ParseAddr(ip)
1168+
if err != nil {
1169+
continue
1170+
}
1171+
netips = append(netips, addr)
1172+
}
1173+
return netips
1174+
}

cli/command/container/opts_test.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"net/netip"
78
"os"
89
"runtime"
910
"strings"
1011
"testing"
1112
"time"
1213

14+
"github.com/google/go-cmp/cmp/cmpopts"
1315
"github.com/moby/moby/api/types/container"
1416
networktypes "github.com/moby/moby/api/types/network"
1517
"github.com/spf13/pflag"
@@ -430,14 +432,14 @@ func TestParseHostnameDomainname(t *testing.T) {
430432
func TestParseWithExpose(t *testing.T) {
431433
t.Run("invalid", func(t *testing.T) {
432434
tests := map[string]string{
433-
":": "invalid port format for --expose: :",
434-
"8080:9090": "invalid port format for --expose: 8080:9090",
435-
"/tcp": "invalid range format for --expose: /tcp, error: empty string specified for ports",
436-
"/udp": "invalid range format for --expose: /udp, error: empty string specified for ports",
437-
"NaN/tcp": `invalid range format for --expose: NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
438-
"NaN-NaN/tcp": `invalid range format for --expose: NaN-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
439-
"8080-NaN/tcp": `invalid range format for --expose: 8080-NaN/tcp, error: strconv.ParseUint: parsing "NaN": invalid syntax`,
440-
"1234567890-8080/tcp": `invalid range format for --expose: 1234567890-8080/tcp, error: strconv.ParseUint: parsing "1234567890": value out of range`,
435+
":": `invalid port format for --expose: invalid start port ':': invalid syntax`,
436+
"8080:9090": `invalid port format for --expose: invalid start port '8080:9090': invalid syntax`,
437+
"/tcp": `invalid port format for --expose: invalid start port '': value is empty`,
438+
"/udp": `invalid port format for --expose: invalid start port '': value is empty`,
439+
"NaN/tcp": `invalid port format for --expose: invalid start port 'NaN': invalid syntax`,
440+
"NaN-NaN/tcp": `invalid port format for --expose: invalid start port 'NaN': invalid syntax`,
441+
"8080-NaN/tcp": `invalid port format for --expose: invalid end port 'NaN': invalid syntax`,
442+
"1234567890-8080/tcp": `invalid port format for --expose: invalid start port '1234567890': value out of range`,
441443
}
442444
for expose, expectedError := range tests {
443445
t.Run(expose, func(t *testing.T) {
@@ -447,20 +449,20 @@ func TestParseWithExpose(t *testing.T) {
447449
}
448450
})
449451
t.Run("valid", func(t *testing.T) {
450-
tests := map[string][]container.PortRangeProto{
451-
"8080/tcp": {"8080/tcp"},
452-
"8080/udp": {"8080/udp"},
453-
"8080/ncp": {"8080/ncp"},
454-
"8080-8080/udp": {"8080/udp"},
455-
"8080-8082/tcp": {"8080/tcp", "8081/tcp", "8082/tcp"},
452+
tests := map[string][]networktypes.Port{
453+
"8080/tcp": {networktypes.MustParsePort("8080/tcp")},
454+
"8080/udp": {networktypes.MustParsePort("8080/udp")},
455+
"8080/ncp": {networktypes.MustParsePort("8080/ncp")},
456+
"8080-8080/udp": {networktypes.MustParsePort("8080/udp")},
457+
"8080-8082/tcp": {networktypes.MustParsePort("8080/tcp"), networktypes.MustParsePort("8081/tcp"), networktypes.MustParsePort("8082/tcp")},
456458
}
457459
for expose, exposedPorts := range tests {
458460
t.Run(expose, func(t *testing.T) {
459461
config, _, _, err := parseRun([]string{fmt.Sprintf("--expose=%v", expose), "img", "cmd"})
460462
assert.NilError(t, err)
461463
for _, port := range exposedPorts {
462464
_, ok := config.ExposedPorts[port]
463-
assert.Check(t, ok, "missing port %q in exposed ports", port)
465+
assert.Check(t, ok, "missing port %q in exposed ports: %#+v", port, config.ExposedPorts[port])
464466
}
465467
})
466468
}
@@ -471,10 +473,10 @@ func TestParseWithExpose(t *testing.T) {
471473
config, _, _, err := parseRun([]string{"--publish=80", "--expose=80-81/tcp", "img", "cmd"})
472474
assert.NilError(t, err)
473475
assert.Check(t, is.Len(config.ExposedPorts, 2))
474-
ports := []container.PortRangeProto{"80/tcp", "81/tcp"}
476+
ports := []networktypes.Port{networktypes.MustParsePort("80/tcp"), networktypes.MustParsePort("81/tcp")}
475477
for _, port := range ports {
476478
_, ok := config.ExposedPorts[port]
477-
assert.Check(t, ok, "missing port %q in exposed ports", port)
479+
assert.Check(t, ok, "missing port %q in exposed ports: %#+v", port, config.ExposedPorts[port])
478480
}
479481
})
480482
}
@@ -606,9 +608,9 @@ func TestParseNetworkConfig(t *testing.T) {
606608
expected: map[string]*networktypes.EndpointSettings{
607609
"net1": {
608610
IPAMConfig: &networktypes.EndpointIPAMConfig{
609-
IPv4Address: "172.20.88.22",
610-
IPv6Address: "2001:db8::8822",
611-
LinkLocalIPs: []string{"169.254.2.2", "fe80::169:254:2:2"},
611+
IPv4Address: netip.MustParseAddr("172.20.88.22"),
612+
IPv6Address: netip.MustParseAddr("2001:db8::8822"),
613+
LinkLocalIPs: []netip.Addr{netip.MustParseAddr("169.254.2.2"), netip.MustParseAddr("fe80::169:254:2:2")},
612614
},
613615
Links: []string{"foo:bar", "bar:baz"},
614616
Aliases: []string{"web1", "web2"},
@@ -636,9 +638,9 @@ func TestParseNetworkConfig(t *testing.T) {
636638
"net1": {
637639
DriverOpts: map[string]string{"field1": "value1"},
638640
IPAMConfig: &networktypes.EndpointIPAMConfig{
639-
IPv4Address: "172.20.88.22",
640-
IPv6Address: "2001:db8::8822",
641-
LinkLocalIPs: []string{"169.254.2.2", "fe80::169:254:2:2"},
641+
IPv4Address: netip.MustParseAddr("172.20.88.22"),
642+
IPv6Address: netip.MustParseAddr("2001:db8::8822"),
643+
LinkLocalIPs: []netip.Addr{netip.MustParseAddr("169.254.2.2"), netip.MustParseAddr("fe80::169:254:2:2")},
642644
},
643645
Links: []string{"foo:bar", "bar:baz"},
644646
Aliases: []string{"web1", "web2"},
@@ -647,15 +649,15 @@ func TestParseNetworkConfig(t *testing.T) {
647649
"net3": {
648650
DriverOpts: map[string]string{"field3": "value3"},
649651
IPAMConfig: &networktypes.EndpointIPAMConfig{
650-
IPv4Address: "172.20.88.22",
651-
IPv6Address: "2001:db8::8822",
652+
IPv4Address: netip.MustParseAddr("172.20.88.22"),
653+
IPv6Address: netip.MustParseAddr("2001:db8::8822"),
652654
},
653655
Aliases: []string{"web3"},
654656
},
655657
"net4": {
656658
MacAddress: "02:32:1c:23:00:04",
657659
IPAMConfig: &networktypes.EndpointIPAMConfig{
658-
LinkLocalIPs: []string{"169.254.169.254"},
660+
LinkLocalIPs: []netip.Addr{netip.MustParseAddr("169.254.169.254")},
659661
},
660662
},
661663
},
@@ -671,8 +673,8 @@ func TestParseNetworkConfig(t *testing.T) {
671673
"field2": "value2",
672674
},
673675
IPAMConfig: &networktypes.EndpointIPAMConfig{
674-
IPv4Address: "172.20.88.22",
675-
IPv6Address: "2001:db8::8822",
676+
IPv4Address: netip.MustParseAddr("172.20.88.22"),
677+
IPv6Address: netip.MustParseAddr("2001:db8::8822"),
676678
},
677679
Aliases: []string{"web1", "web2"},
678680
MacAddress: "02:32:1c:23:00:04",
@@ -753,7 +755,7 @@ func TestParseNetworkConfig(t *testing.T) {
753755
assert.NilError(t, err)
754756
assert.DeepEqual(t, config.MacAddress, tc.expectedCfg.MacAddress) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
755757
assert.DeepEqual(t, hConfig.NetworkMode, tc.expectedHostCfg.NetworkMode)
756-
assert.DeepEqual(t, nwConfig.EndpointsConfig, tc.expected)
758+
assert.DeepEqual(t, nwConfig.EndpointsConfig, tc.expected, cmpopts.IgnoreUnexported(netip.Addr{}))
757759
})
758760
}
759761
}

0 commit comments

Comments
 (0)