Skip to content

Commit 9fdaa84

Browse files
committed
Revert "revert flag-type changes"
This reverts commit 4303d6b. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 3a6129c commit 9fdaa84

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

cli/command/network/connect.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
type connectOptions struct {
2020
network string
2121
container string
22-
ipaddress string // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
23-
ipv6address string // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
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
2424
links opts.ListOpts
2525
aliases []string
26-
linklocalips []string // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
26+
linklocalips []net.IP // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
2727
driverOpts []string
2828
gwPriority int
2929
}
@@ -53,11 +53,11 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
5353
}
5454

5555
flags := cmd.Flags()
56-
flags.StringVar(&options.ipaddress, "ip", "", `IPv4 address (e.g., "172.30.100.104")`)
57-
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")`)
5858
flags.Var(&options.links, "link", "Add link to another container")
5959
flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container")
60-
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")
6161
flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network")
6262
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Highest gw-priority provides the default gateway. Accepts positive and negative values.")
6363
return cmd
@@ -71,8 +71,8 @@ func runConnect(ctx context.Context, apiClient client.NetworkAPIClient, options
7171

7272
return apiClient.NetworkConnect(ctx, options.network, options.container, &network.EndpointSettings{
7373
IPAMConfig: &network.EndpointIPAMConfig{
74-
IPv4Address: tryParseAddr(options.ipaddress),
75-
IPv6Address: tryParseAddr(options.ipv6address),
74+
IPv4Address: toNetipAddr(options.ipaddress),
75+
IPv6Address: toNetipAddr(options.ipv6address),
7676
LinkLocalIPs: toNetipAddrSlice(options.linklocalips),
7777
},
7878
Links: options.links.GetSlice(),
@@ -96,17 +96,27 @@ func convertDriverOpt(options []string) (map[string]string, error) {
9696
return driverOpt, nil
9797
}
9898

99-
func toNetipAddrSlice(ips []string) []netip.Addr {
100-
netIPs := make([]netip.Addr, 0, len(ips))
99+
func toNetipAddrSlice(ips []net.IP) []netip.Addr {
100+
netips := make([]netip.Addr, 0, len(ips))
101101
for _, ip := range ips {
102-
netIPs = append(netIPs, tryParseAddr(ip))
102+
netips = append(netips, toNetipAddr(ip))
103103
}
104-
return netIPs
104+
return netips
105105
}
106106

107-
func tryParseAddr(ip string) netip.Addr {
108-
addr, _ := netip.ParseAddr(ip)
109-
return addr
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{}
110120
}
111121

112122
func ipNetToPrefix(n net.IPNet) netip.Prefix {

cli/command/network/connect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ func TestNetworkConnectErrors(t *testing.T) {
4848
func TestNetworkConnectWithFlags(t *testing.T) {
4949
expectedConfig := &network.EndpointSettings{
5050
IPAMConfig: &network.EndpointIPAMConfig{
51-
IPv4Address: netip.MustParseAddr("192.168.4.1").Unmap(),
51+
IPv4Address: netip.MustParseAddr("192.168.4.1"),
5252
IPv6Address: netip.MustParseAddr("fdef:f401:8da0:1234::5678"),
53-
LinkLocalIPs: []netip.Addr{netip.MustParseAddr("169.254.42.42").Unmap()},
53+
LinkLocalIPs: []netip.Addr{netip.MustParseAddr("169.254.42.42")},
5454
},
5555
Links: []string{"otherctr"},
5656
Aliases: []string{"poor-yorick"},

cli/command/network/create.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type ipamOptions struct {
3737
driver string
3838
subnets []string // TODO(thaJeztah): change to []net.IPNet? This won't accept a bare address (without "/xxx"); we need a flag-type to handle []netip.Prefix directly
3939
ipRanges []net.IPNet // TODO(thaJeztah): we need a flag-type to handle []netip.Prefix directly
40-
gateways []string // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
40+
gateways []net.IP // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
4141
auxAddresses opts.MapOpts
4242
driverOpts opts.MapOpts
4343
}
@@ -94,7 +94,7 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
9494
flags.StringVar(&options.ipam.driver, "ipam-driver", "default", "IP Address Management Driver")
9595
flags.StringSliceVar(&options.ipam.subnets, "subnet", []string{}, "Subnet in CIDR format that represents a network segment")
9696
flags.IPNetSliceVar(&options.ipam.ipRanges, "ip-range", nil, "Allocate container ip from a sub-range")
97-
flags.StringSliceVar(&options.ipam.gateways, "gateway", []string{}, "IPv4 or IPv6 Gateway for the master subnet")
97+
flags.IPSliceVar(&options.ipam.gateways, "gateway", nil, "IPv4 or IPv6 Gateway for the master subnet")
9898

9999
flags.Var(&options.ipam.auxAddresses, "aux-address", "Auxiliary IPv4 or IPv6 addresses used by Network driver")
100100
flags.Var(&options.ipam.driverOpts, "ipam-opt", "Set IPAM driver specific options")
@@ -202,7 +202,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
202202
match := false
203203
for _, s := range options.subnets {
204204
// TODO(thaJeztah): is all this validation needed on the CLI-side?
205-
ok, err := subnetMatches(s, g)
205+
ok, err := subnetMatches(s, g.String())
206206
if err != nil {
207207
return nil, err
208208
}
@@ -213,7 +213,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
213213
return nil, fmt.Errorf("cannot configure multiple gateways (%s, %s) for the same subnet (%s)", g, iData[s].Gateway, s)
214214
}
215215
d := iData[s]
216-
d.Gateway = tryParseAddr(g)
216+
d.Gateway = toNetipAddr(g)
217217
match = true
218218
}
219219
if !match {

0 commit comments

Comments
 (0)