Skip to content

Commit aa013da

Browse files
committed
vendor: github.com/moby/moby/api, github.com/moby/moby/client master
full diff: moby/moby@4ca8aed...694e30a Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 74e3520 commit aa013da

Some content is hidden

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

47 files changed

+884
-507
lines changed

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/container/create.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"net/netip"
1110
"os"
1211
"path"
1312
"strings"
@@ -25,7 +24,6 @@ import (
2524
"github.com/docker/cli/cli/trust"
2625
"github.com/docker/cli/internal/jsonstream"
2726
"github.com/docker/cli/opts"
28-
"github.com/moby/moby/api/types/container"
2927
"github.com/moby/moby/api/types/mount"
3028
"github.com/moby/moby/api/types/versions"
3129
"github.com/moby/moby/client"
@@ -361,10 +359,6 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
361359
}
362360
}
363361

364-
if warn := localhostDNSWarning(*hostConfig); warn != "" {
365-
response.Warnings = append(response.Warnings, warn)
366-
}
367-
368362
containerID = response.ID
369363
for _, w := range response.Warnings {
370364
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING:", w)
@@ -385,19 +379,6 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
385379
return containerID, err
386380
}
387381

388-
// check the DNS settings passed via --dns against localhost regexp to warn if
389-
// they are trying to set a DNS to a localhost address.
390-
//
391-
// TODO(thaJeztah): move this to the daemon, which can make a better call if it will work or not (depending on networking mode).
392-
func localhostDNSWarning(hostConfig container.HostConfig) string {
393-
for _, dnsIP := range hostConfig.DNS {
394-
if addr, err := netip.ParseAddr(dnsIP); err == nil && addr.IsLoopback() {
395-
return fmt.Sprintf("Localhost DNS (%s) may fail in containers.", addr)
396-
}
397-
}
398-
return ""
399-
}
400-
401382
func validatePullOpt(val string) error {
402383
switch val {
403384
case PullImageAlways, PullImageMissing, PullImageNever, "":

cli/command/container/opts.go

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"net/netip"
89
"os"
910
"path"
1011
"path/filepath"
@@ -425,22 +426,46 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
425426
entrypoint = []string{""}
426427
}
427428

428-
publishOpts := copts.publish.GetSlice()
429-
var (
430-
ports map[container.PortRangeProto]struct{}
431-
portBindings map[container.PortRangeProto][]container.PortBinding
432-
convertedOpts []string
433-
)
434-
435-
convertedOpts, err = convertToStandardNotation(publishOpts)
429+
// TODO(thaJeztah): remove uses of go-connections/nat here.
430+
convertedOpts, err := convertToStandardNotation(copts.publish.GetSlice())
436431
if err != nil {
437432
return nil, err
438433
}
439434

440-
ports, portBindings, err = nat.ParsePortSpecs(convertedOpts)
435+
ports, natPortBindings, err := nat.ParsePortSpecs(convertedOpts)
441436
if err != nil {
442437
return nil, err
443438
}
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+
exposedPorts := network.PortSet{}
462+
for port := range ports {
463+
p, err := network.ParsePort(string(port))
464+
if err != nil {
465+
return nil, err
466+
}
467+
exposedPorts[p] = struct{}{}
468+
}
444469

445470
// Merge in exposed ports to the map of published ports
446471
for _, e := range copts.expose.GetSlice() {
@@ -636,7 +661,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
636661
config := &container.Config{
637662
Hostname: copts.hostname,
638663
Domainname: copts.domainname,
639-
ExposedPorts: ports,
664+
ExposedPorts: exposedPorts,
640665
User: copts.user,
641666
Tty: copts.tty,
642667
OpenStdin: copts.stdin,
@@ -672,7 +697,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
672697
// but pre created containers can still have those nil values.
673698
// See https://github.com/docker/docker/pull/17779
674699
// for a more detailed explanation on why we don't want that.
675-
DNS: copts.dns.GetAllOrEmpty(),
700+
DNS: toNetipAddrSlice(copts.dns.GetAllOrEmpty()),
676701
DNSSearch: copts.dnsSearch.GetAllOrEmpty(),
677702
DNSOptions: copts.dnsOptions.GetAllOrEmpty(),
678703
ExtraHosts: copts.extraHosts.GetSlice(),
@@ -815,10 +840,10 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
815840
if len(n.Links) > 0 && copts.links.Len() > 0 {
816841
return invalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
817842
}
818-
if n.IPv4Address != "" && copts.ipv4Address != "" {
843+
if n.IPv4Address.IsValid() && copts.ipv4Address != "" {
819844
return invalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
820845
}
821-
if n.IPv6Address != "" && copts.ipv6Address != "" {
846+
if n.IPv6Address.IsValid() && copts.ipv6Address != "" {
822847
return invalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
823848
}
824849
if n.MacAddress != "" && copts.macAddress != "" {
@@ -838,17 +863,24 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
838863
copy(n.Links, copts.links.GetSlice())
839864
}
840865
if copts.ipv4Address != "" {
841-
n.IPv4Address = copts.ipv4Address
866+
var err error
867+
n.IPv4Address, err = netip.ParseAddr(copts.ipv4Address)
868+
if err != nil {
869+
return err
870+
}
842871
}
843872
if copts.ipv6Address != "" {
844-
n.IPv6Address = copts.ipv6Address
873+
var err error
874+
n.IPv6Address, err = netip.ParseAddr(copts.ipv6Address)
875+
if err != nil {
876+
return err
877+
}
845878
}
846879
if copts.macAddress != "" {
847880
n.MacAddress = copts.macAddress
848881
}
849882
if copts.linkLocalIPs.Len() > 0 {
850-
n.LinkLocalIPs = make([]string, copts.linkLocalIPs.Len())
851-
copy(n.LinkLocalIPs, copts.linkLocalIPs.GetSlice())
883+
n.LinkLocalIPs = toNetipAddrSlice(copts.linkLocalIPs.GetSlice())
852884
}
853885
return nil
854886
}
@@ -877,7 +909,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*network.Endpoint
877909
if len(ep.Links) > 0 {
878910
epConfig.Links = ep.Links
879911
}
880-
if ep.IPv4Address != "" || ep.IPv6Address != "" || len(ep.LinkLocalIPs) > 0 {
912+
if ep.IPv4Address.IsValid() || ep.IPv6Address.IsValid() || len(ep.LinkLocalIPs) > 0 {
881913
epConfig.IPAMConfig = &network.EndpointIPAMConfig{
882914
IPv4Address: ep.IPv4Address,
883915
IPv6Address: ep.IPv6Address,
@@ -1141,3 +1173,15 @@ func validateAttach(val string) (string, error) {
11411173
}
11421174
return val, errors.New("valid streams are STDIN, STDOUT and STDERR")
11431175
}
1176+
1177+
func toNetipAddrSlice(ips []string) []netip.Addr {
1178+
netips := make([]netip.Addr, 0, len(ips))
1179+
for _, ip := range ips {
1180+
addr, err := netip.ParseAddr(ip)
1181+
if err != nil {
1182+
continue
1183+
}
1184+
netips = append(netips, addr)
1185+
}
1186+
return netips
1187+
}

cli/command/container/port.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import (
55
"fmt"
66
"net"
77
"sort"
8-
"strconv"
98
"strings"
109

1110
"github.com/docker/cli/cli"
1211
"github.com/docker/cli/cli/command"
1312
"github.com/docker/cli/cli/command/completion"
1413
"github.com/fvbommel/sortorder"
15-
"github.com/moby/moby/api/types/container"
14+
"github.com/moby/moby/api/types/network"
1615
"github.com/spf13/cobra"
1716
)
1817

@@ -60,24 +59,21 @@ func runPort(ctx context.Context, dockerCli command.Cli, opts *portOptions) erro
6059

6160
var out []string
6261
if opts.port != "" {
63-
port, proto, _ := strings.Cut(opts.port, "/")
64-
if proto == "" {
65-
proto = "tcp"
62+
port, err := network.ParsePort(opts.port)
63+
if err != nil {
64+
return err
6665
}
67-
if _, err = strconv.ParseUint(port, 10, 16); err != nil {
68-
return fmt.Errorf("invalid port (%s): %w", port, err)
69-
}
70-
frontends, exists := c.NetworkSettings.Ports[container.PortRangeProto(port+"/"+proto)]
66+
frontends, exists := c.NetworkSettings.Ports[port]
7167
if !exists || len(frontends) == 0 {
7268
return fmt.Errorf("no public port '%s' published for %s", opts.port, opts.container)
7369
}
7470
for _, frontend := range frontends {
75-
out = append(out, net.JoinHostPort(frontend.HostIP, frontend.HostPort))
71+
out = append(out, net.JoinHostPort(frontend.HostIP.String(), frontend.HostPort))
7672
}
7773
} else {
7874
for from, frontends := range c.NetworkSettings.Ports {
7975
for _, frontend := range frontends {
80-
out = append(out, fmt.Sprintf("%s -> %s", from, net.JoinHostPort(frontend.HostIP, frontend.HostPort)))
76+
out = append(out, fmt.Sprintf("%s -> %s", from, net.JoinHostPort(frontend.HostIP.String(), frontend.HostPort)))
8177
}
8278
}
8379
}

cli/command/formatter/container.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ func DisplayablePorts(ports []container.PortSummary) string {
360360
for _, port := range ports {
361361
current := port.PrivatePort
362362
portKey := port.Type
363-
if port.IP != "" {
363+
if !port.IP.IsUnspecified() {
364364
if port.PublicPort != current {
365-
hAddrPort := net.JoinHostPort(port.IP, strconv.Itoa(int(port.PublicPort)))
365+
hAddrPort := net.JoinHostPort(port.IP.String(), strconv.Itoa(int(port.PublicPort)))
366366
hostMappings = append(hostMappings, fmt.Sprintf("%s->%d/%s", hAddrPort, port.PrivatePort, port.Type))
367367
continue
368368
}
369-
portKey = port.IP + "/" + port.Type
369+
portKey = port.IP.String() + "/" + port.Type
370370
}
371371
group := groupMap[portKey]
372372

@@ -416,7 +416,7 @@ func comparePorts(i, j container.PortSummary) bool {
416416
}
417417

418418
if i.IP != j.IP {
419-
return i.IP < j.IP
419+
return i.IP.String() < j.IP.String()
420420
}
421421

422422
if i.PublicPort != j.PublicPort {

cli/command/network/connect.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package network
33
import (
44
"context"
55
"errors"
6+
"net"
7+
"net/netip"
68
"strings"
79

810
"github.com/docker/cli/cli"
@@ -17,11 +19,11 @@ import (
1719
type connectOptions struct {
1820
network string
1921
container string
20-
ipaddress string
21-
ipv6address string
22+
ipaddress net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
23+
ipv6address net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
2224
links opts.ListOpts
2325
aliases []string
24-
linklocalips []string
26+
linklocalips []net.IP // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
2527
driverOpts []string
2628
gwPriority int
2729
}
@@ -51,11 +53,11 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
5153
}
5254

5355
flags := cmd.Flags()
54-
flags.StringVar(&options.ipaddress, "ip", "", `IPv4 address (e.g., "172.30.100.104")`)
55-
flags.StringVar(&options.ipv6address, "ip6", "", `IPv6 address (e.g., "2001:db8::33")`)
56+
flags.IPVar(&options.ipaddress, "ip", nil, `IPv4 address (e.g., "172.30.100.104")`)
57+
flags.IPVar(&options.ipv6address, "ip6", nil, `IPv6 address (e.g., "2001:db8::33")`)
5658
flags.Var(&options.links, "link", "Add link to another container")
5759
flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container")
58-
flags.StringSliceVar(&options.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
60+
flags.IPSliceVar(&options.linklocalips, "link-local-ip", nil, "Add a link-local address for the container")
5961
flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network")
6062
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Highest gw-priority provides the default gateway. Accepts positive and negative values.")
6163
return cmd
@@ -69,9 +71,9 @@ func runConnect(ctx context.Context, apiClient client.NetworkAPIClient, options
6971

7072
return apiClient.NetworkConnect(ctx, options.network, options.container, &network.EndpointSettings{
7173
IPAMConfig: &network.EndpointIPAMConfig{
72-
IPv4Address: options.ipaddress,
73-
IPv6Address: options.ipv6address,
74-
LinkLocalIPs: options.linklocalips,
74+
IPv4Address: toNetipAddr(options.ipaddress),
75+
IPv6Address: toNetipAddr(options.ipv6address),
76+
LinkLocalIPs: toNetipAddrSlice(options.linklocalips),
7577
},
7678
Links: options.links.GetSlice(),
7779
Aliases: options.aliases,
@@ -93,3 +95,48 @@ func convertDriverOpt(options []string) (map[string]string, error) {
9395
}
9496
return driverOpt, nil
9597
}
98+
99+
func toNetipAddrSlice(ips []net.IP) []netip.Addr {
100+
netips := make([]netip.Addr, 0, len(ips))
101+
for _, ip := range ips {
102+
netips = append(netips, toNetipAddr(ip))
103+
}
104+
return netips
105+
}
106+
107+
func toNetipAddr(ip net.IP) netip.Addr {
108+
if len(ip) == 0 {
109+
return netip.Addr{}
110+
}
111+
if ip4 := ip.To4(); ip4 != nil {
112+
a, _ := netip.AddrFromSlice(ip4)
113+
return a
114+
}
115+
if ip16 := ip.To16(); ip16 != nil {
116+
a, _ := netip.AddrFromSlice(ip16)
117+
return a
118+
}
119+
return netip.Addr{}
120+
}
121+
122+
func ipNetToPrefix(n net.IPNet) netip.Prefix {
123+
if n.IP == nil {
124+
return netip.Prefix{}
125+
}
126+
127+
ip := n.IP.To4()
128+
if ip == nil {
129+
ip = n.IP.To16()
130+
}
131+
if ip == nil {
132+
return netip.Prefix{}
133+
}
134+
135+
addr, ok := netip.AddrFromSlice(ip)
136+
if !ok {
137+
return netip.Prefix{}
138+
}
139+
140+
ones, _ := n.Mask.Size()
141+
return netip.PrefixFrom(addr, ones)
142+
}

0 commit comments

Comments
 (0)