Skip to content

Commit cfe34c8

Browse files
committed
addressing review comments
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent e707d9c commit cfe34c8

File tree

15 files changed

+60
-99
lines changed

15 files changed

+60
-99
lines changed

cli/command/completion/functions_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sort"
77
"testing"
88

9-
"github.com/google/go-cmp/cmp/cmpopts"
109
"github.com/moby/moby/api/types/container"
1110
"github.com/moby/moby/api/types/image"
1211
"github.com/moby/moby/api/types/network"
@@ -155,7 +154,7 @@ func TestCompleteContainerNames(t *testing.T) {
155154
}
156155
comp := ContainerNames(fakeCLI{&fakeClient{
157156
containerListFunc: func(opts client.ContainerListOptions) ([]container.Summary, error) {
158-
assert.Check(t, is.DeepEqual(opts, tc.expOpts, cmpopts.IgnoreUnexported(client.ContainerListOptions{})))
157+
assert.Check(t, is.DeepEqual(opts, tc.expOpts))
159158
if tc.expDirective == cobra.ShellCompDirectiveError {
160159
return nil, errors.New("some error occurred")
161160
}

cli/command/container/opts.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,13 +1158,16 @@ func validateAttach(val string) (string, error) {
11581158
}
11591159

11601160
func toNetipAddrSlice(ips []string) []netip.Addr {
1161-
netips := make([]netip.Addr, 0, len(ips))
1161+
if len(ips) == 0 {
1162+
return nil
1163+
}
1164+
netIPs := make([]netip.Addr, 0, len(ips))
11621165
for _, ip := range ips {
11631166
addr, err := netip.ParseAddr(ip)
11641167
if err != nil {
11651168
continue
11661169
}
1167-
netips = append(netips, addr)
1170+
netIPs = append(netIPs, addr)
11681171
}
1169-
return netips
1172+
return netIPs
11701173
}

cli/command/container/opts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ func TestParseNetworkConfig(t *testing.T) {
755755
assert.NilError(t, err)
756756
assert.DeepEqual(t, config.MacAddress, tc.expectedCfg.MacAddress) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
757757
assert.DeepEqual(t, hConfig.NetworkMode, tc.expectedHostCfg.NetworkMode)
758-
assert.DeepEqual(t, nwConfig.EndpointsConfig, tc.expected, cmpopts.IgnoreUnexported(netip.Addr{}))
758+
assert.DeepEqual(t, nwConfig.EndpointsConfig, tc.expected, cmpopts.EquateComparable(netip.Addr{}))
759759
})
760760
}
761761
}

cli/command/network/connect.go

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -97,46 +97,39 @@ func convertDriverOpt(options []string) (map[string]string, error) {
9797
}
9898

9999
func toNetipAddrSlice(ips []net.IP) []netip.Addr {
100-
netips := make([]netip.Addr, 0, len(ips))
100+
if len(ips) == 0 {
101+
return nil
102+
}
103+
netIPs := make([]netip.Addr, 0, len(ips))
101104
for _, ip := range ips {
102-
netips = append(netips, toNetipAddr(ip))
105+
netIPs = append(netIPs, toNetipAddr(ip))
103106
}
104-
return netips
107+
return netIPs
105108
}
106109

107110
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{}
111+
a, _ := netip.AddrFromSlice(ip)
112+
return a.Unmap()
120113
}
121114

122-
func ipNetToPrefix(n net.IPNet) netip.Prefix {
123-
if n.IP == nil {
124-
return netip.Prefix{}
115+
// toPrefix converts n into a netip.Prefix. If n is not a valid IPv4 or IPV6
116+
// address, ToPrefix returns netip.Prefix{}, false.
117+
//
118+
// TODO(thaJeztah): create internal package similar to https://github.com/moby/moby/blob/0769fe708773892d6ac399ee137e71a777b35de7/daemon/internal/netiputil/netiputil.go#L21-L42
119+
func toPrefix(n net.IPNet) (netip.Prefix, bool) {
120+
if ll := len(n.Mask); ll != net.IPv4len && ll != net.IPv6len {
121+
return netip.Prefix{}, false
125122
}
126123

127-
ip := n.IP.To4()
128-
if ip == nil {
129-
ip = n.IP.To16()
130-
}
131-
if ip == nil {
132-
return netip.Prefix{}
124+
addr, ok := netip.AddrFromSlice(n.IP)
125+
if !ok {
126+
return netip.Prefix{}, false
133127
}
134128

135-
addr, ok := netip.AddrFromSlice(ip)
136-
if !ok {
137-
return netip.Prefix{}
129+
ones, bits := n.Mask.Size()
130+
if ones == 0 && bits == 0 {
131+
return netip.Prefix{}, false
138132
}
139133

140-
ones, _ := n.Mask.Size()
141-
return netip.PrefixFrom(addr, ones)
134+
return netip.PrefixFrom(addr.Unmap(), ones), true
142135
}

cli/command/network/connect_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestNetworkConnectWithFlags(t *testing.T) {
6262
}
6363
cli := test.NewFakeCli(&fakeClient{
6464
networkConnectFunc: func(ctx context.Context, networkID, container string, config *network.EndpointSettings) error {
65-
assert.Check(t, is.DeepEqual(expectedConfig, config, cmpopts.IgnoreUnexported(netip.Addr{})))
65+
assert.Check(t, is.DeepEqual(expectedConfig, config, cmpopts.EquateComparable(netip.Addr{})))
6666
return nil
6767
},
6868
})

cli/command/network/create.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
150150

151151
// Populate non-overlapping subnets into consolidation map
152152
for _, s := range options.subnets {
153-
// TODO(thaJeztah): is all this validation needed on the CLI-side?
154153
for k := range iData {
155154
ok1, err := subnetMatches(s, k)
156155
if err != nil {
@@ -164,7 +163,7 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
164163
return nil, errors.New("multiple overlapping subnet configuration is not supported")
165164
}
166165
}
167-
sn, err := parsePrefixOrAddr(s)
166+
sn, err := netip.ParsePrefix(s)
168167
if err != nil {
169168
return nil, err
170169
}
@@ -173,7 +172,6 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
173172

174173
// Validate and add valid ip ranges
175174
for _, r := range options.ipRanges {
176-
// TODO(thaJeztah): is all this validation needed on the CLI-side?
177175
match := false
178176
for _, s := range options.subnets {
179177
ok, err := subnetMatches(s, r.String())
@@ -188,9 +186,10 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
188186
if iData[s].IPRange.IsValid() {
189187
return nil, fmt.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r.String(), iData[s].IPRange.String(), s)
190188
}
191-
d := iData[s]
192-
d.IPRange = ipNetToPrefix(r)
193-
match = true
189+
if ipRange, ok := toPrefix(r); ok {
190+
iData[s].IPRange = ipRange
191+
match = true
192+
}
194193
}
195194
if !match {
196195
return nil, fmt.Errorf("no matching subnet for range %s", r.String())
@@ -201,7 +200,6 @@ func createIPAMConfig(options ipamOptions) (*network.IPAM, error) {
201200
for _, g := range options.gateways {
202201
match := false
203202
for _, s := range options.subnets {
204-
// TODO(thaJeztah): is all this validation needed on the CLI-side?
205203
ok, err := subnetMatches(s, g.String())
206204
if err != nil {
207205
return nil, err
@@ -278,20 +276,3 @@ func subnetMatches(subnet, data string) (bool, error) {
278276

279277
return s.Contains(ip), nil
280278
}
281-
282-
// parsePrefixOrAddr parses s as a subnet in CIDR notation (e.g. "10.0.0.0/24").
283-
// If s does not include a prefix length, it is interpreted as a single-address
284-
// subnet using the full address width (/32 for IPv4 or /128 for IPv6).
285-
//
286-
// It returns the resulting netip.Prefix or an error if the input is invalid.
287-
func parsePrefixOrAddr(s string) (netip.Prefix, error) {
288-
pfx, err := netip.ParsePrefix(s)
289-
if err != nil {
290-
addr, err := netip.ParseAddr(s)
291-
if err != nil {
292-
return netip.Prefix{}, fmt.Errorf("invalid address: %w", err)
293-
}
294-
pfx = netip.PrefixFrom(addr, addr.BitLen())
295-
}
296-
return pfx, nil
297-
}

cli/command/network/create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestNetworkCreateErrors(t *testing.T) {
4040
"gateway": "255.0.255.0", // FIXME(thaJeztah): this used to accept a CIDR ("255.0.255.0/24")
4141
"subnet": "10.1.2.0.30.50",
4242
},
43-
expectedError: `invalid address: ParseAddr("10.1.2.0.30.50")`,
43+
expectedError: `netip.ParsePrefix("10.1.2.0.30.50"): no '/'`,
4444
},
4545
{
4646
args: []string{"toto"},
@@ -168,7 +168,7 @@ func TestNetworkCreateWithFlags(t *testing.T) {
168168
cli := test.NewFakeCli(&fakeClient{
169169
networkCreateFunc: func(ctx context.Context, name string, options client.NetworkCreateOptions) (network.CreateResponse, error) {
170170
assert.Check(t, is.Equal(expectedDriver, options.Driver), "not expected driver error")
171-
assert.Check(t, is.DeepEqual(expectedOpts, options.IPAM.Config, cmpopts.IgnoreUnexported(netip.Addr{}, netip.Prefix{})), "not expected driver error")
171+
assert.Check(t, is.DeepEqual(expectedOpts, options.IPAM.Config, cmpopts.EquateComparable(netip.Addr{}, netip.Prefix{})), "not expected driver error")
172172
return network.CreateResponse{
173173
ID: name,
174174
}, nil

cli/command/service/opts.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,13 +1076,16 @@ const (
10761076
)
10771077

10781078
func toNetipAddrSlice(ips []string) []netip.Addr {
1079-
netips := make([]netip.Addr, 0, len(ips))
1079+
if len(ips) == 0 {
1080+
return nil
1081+
}
1082+
netIPs := make([]netip.Addr, 0, len(ips))
10801083
for _, ip := range ips {
10811084
addr, err := netip.ParseAddr(ip)
10821085
if err != nil {
10831086
continue
10841087
}
1085-
netips = append(netips, addr)
1088+
netIPs = append(netIPs, addr)
10861089
}
1087-
return netips
1090+
return netIPs
10881091
}

cli/command/swarm/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func newInitCommand(dockerCLI command.Cli) *cobra.Command {
6868
func runInit(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts initOptions) error {
6969
apiClient := dockerCLI.Client()
7070

71-
// TODO(thaJeztah): should we change opts.defaultAddrPools to be the right type? What formats does it accept?
71+
// TODO(thaJeztah): change opts.defaultAddrPools a []netip.Prefix; see https://github.com/docker/cli/pull/6545#discussion_r2420361609
7272
defaultAddrPool := make([]netip.Prefix, 0, len(opts.defaultAddrPools))
7373
for _, p := range opts.defaultAddrPools {
7474
if len(p.IP) == 0 {

cli/compose/convert/compose.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package convert
22

33
import (
4-
"fmt"
54
"net/netip"
65
"os"
76
"strings"
@@ -86,7 +85,7 @@ func Networks(namespace Namespace, networks networkMap, servicesNetworks map[str
8685
Driver: nw.Ipam.Driver,
8786
}
8887
for _, ipamConfig := range nw.Ipam.Config {
89-
sn, _ := parsePrefixOrAddr(ipamConfig.Subnet) // TODO(thaJeztah): change Subnet field to netip.Prefix (but this would break "address only" formats.
88+
sn, _ := netip.ParsePrefix(ipamConfig.Subnet)
9089
createOpts.IPAM.Config = append(createOpts.IPAM.Config, network.IPAMConfig{
9190
Subnet: sn,
9291
})
@@ -202,20 +201,3 @@ func fileObjectConfig(namespace Namespace, name string, obj composetypes.FileObj
202201
Data: data,
203202
}, nil
204203
}
205-
206-
// parsePrefixOrAddr parses s as a subnet in CIDR notation (e.g. "10.0.0.0/24").
207-
// If s does not include a prefix length, it is interpreted as a single-address
208-
// subnet using the full address width (/32 for IPv4 or /128 for IPv6).
209-
//
210-
// It returns the resulting netip.Prefix or an error if the input is invalid.
211-
func parsePrefixOrAddr(s string) (netip.Prefix, error) {
212-
pfx, err := netip.ParsePrefix(s)
213-
if err != nil {
214-
addr, err := netip.ParseAddr(s)
215-
if err != nil {
216-
return netip.Prefix{}, fmt.Errorf("invalid address: %w", err)
217-
}
218-
pfx = netip.PrefixFrom(addr, addr.BitLen())
219-
}
220-
return pfx, nil
221-
}

0 commit comments

Comments
 (0)