Browse code

Fix network inspect IPv6 gateway address format

When an IPv6 network is first created with no specific IPAM config,
network inspect adds a CIDR range to the gateway address. After the
daemon has been restarted, it's just a plain address.

Once the daaemon's been restated, "info" becomes "config", and the
address is reported correctly from "config".

Make the IPv6 code to report the gateway from "info" use net.IPNet.IP
instead of the whole net.IPNet - like the IPv4 code.

Signed-off-by: Rob Murray <rob.murray@docker.com>

Rob Murray authored on 2025/02/22 20:10:49
Showing 3 changed files
... ...
@@ -751,9 +751,13 @@ func buildIPAMResources(nw *libnetwork.Network) networktypes.IPAM {
751 751
 				if info.IPAMData.Pool == nil {
752 752
 					continue
753 753
 				}
754
+				var gw string
755
+				if info.IPAMData.Gateway != nil {
756
+					gw = info.IPAMData.Gateway.IP.String()
757
+				}
754 758
 				ipamConfig = append(ipamConfig, networktypes.IPAMConfig{
755 759
 					Subnet:  info.IPAMData.Pool.String(),
756
-					Gateway: info.IPAMData.Gateway.String(),
760
+					Gateway: gw,
757 761
 				})
758 762
 			}
759 763
 		}
... ...
@@ -57,7 +57,7 @@ func TestDaemonDefaultBridgeIPAM_Docker0(t *testing.T) {
57 57
 			},
58 58
 			expIPAMConfig: []network.IPAMConfig{
59 59
 				{Subnet: "192.168.176.0/24", Gateway: "192.168.176.1"},
60
-				{Subnet: "fdd1:8161:2d2c::/64", Gateway: "fdd1:8161:2d2c::1/64"},
60
+				{Subnet: "fdd1:8161:2d2c::/64", Gateway: "fdd1:8161:2d2c::1"},
61 61
 			},
62 62
 		},
63 63
 		{
... ...
@@ -1542,3 +1542,21 @@ func TestAdvertiseAddresses(t *testing.T) {
1542 1542
 		})
1543 1543
 	}
1544 1544
 }
1545
+
1546
+// TestNetworkInspectGateway checks that gateways reported in inspect output are parseable as addresses.
1547
+func TestNetworkInspectGateway(t *testing.T) {
1548
+	ctx := setupTest(t)
1549
+	c := testEnv.APIClient()
1550
+
1551
+	const netName = "test-inspgw"
1552
+	nid, err := network.Create(ctx, c, netName, network.WithIPv6())
1553
+	assert.NilError(t, err)
1554
+	defer network.RemoveNoError(ctx, t, c, netName)
1555
+
1556
+	insp, err := c.NetworkInspect(ctx, nid, networktypes.InspectOptions{})
1557
+	assert.NilError(t, err)
1558
+	for _, ipamCfg := range insp.IPAM.Config {
1559
+		_, err := netip.ParseAddr(ipamCfg.Gateway)
1560
+		assert.Check(t, err)
1561
+	}
1562
+}