From 3cf4ff971d1b3bfde4d3cd7ac18193ef7bf775bd Mon Sep 17 00:00:00 2001 From: Rob Murray Date: Sat, 22 Feb 2025 11:10:49 +0000 Subject: [PATCH] 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 --- daemon/network.go | 6 +++++- integration/daemon/daemon_linux_test.go | 2 +- integration/networking/bridge_linux_test.go | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/daemon/network.go b/daemon/network.go index 8e4687e121..d9eea4e314 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -751,9 +751,13 @@ func buildIPAMResources(nw *libnetwork.Network) networktypes.IPAM { if info.IPAMData.Pool == nil { continue } + var gw string + if info.IPAMData.Gateway != nil { + gw = info.IPAMData.Gateway.IP.String() + } ipamConfig = append(ipamConfig, networktypes.IPAMConfig{ Subnet: info.IPAMData.Pool.String(), - Gateway: info.IPAMData.Gateway.String(), + Gateway: gw, }) } } diff --git a/integration/daemon/daemon_linux_test.go b/integration/daemon/daemon_linux_test.go index d3c79d37f5..da2c3d2412 100644 --- a/integration/daemon/daemon_linux_test.go +++ b/integration/daemon/daemon_linux_test.go @@ -57,7 +57,7 @@ func TestDaemonDefaultBridgeIPAM_Docker0(t *testing.T) { }, expIPAMConfig: []network.IPAMConfig{ {Subnet: "192.168.176.0/24", Gateway: "192.168.176.1"}, - {Subnet: "fdd1:8161:2d2c::/64", Gateway: "fdd1:8161:2d2c::1/64"}, + {Subnet: "fdd1:8161:2d2c::/64", Gateway: "fdd1:8161:2d2c::1"}, }, }, { diff --git a/integration/networking/bridge_linux_test.go b/integration/networking/bridge_linux_test.go index 1e084b92ec..bcf2750f3f 100644 --- a/integration/networking/bridge_linux_test.go +++ b/integration/networking/bridge_linux_test.go @@ -1542,3 +1542,21 @@ func TestAdvertiseAddresses(t *testing.T) { }) } } + +// TestNetworkInspectGateway checks that gateways reported in inspect output are parseable as addresses. +func TestNetworkInspectGateway(t *testing.T) { + ctx := setupTest(t) + c := testEnv.APIClient() + + const netName = "test-inspgw" + nid, err := network.Create(ctx, c, netName, network.WithIPv6()) + assert.NilError(t, err) + defer network.RemoveNoError(ctx, t, c, netName) + + insp, err := c.NetworkInspect(ctx, nid, networktypes.InspectOptions{}) + assert.NilError(t, err) + for _, ipamCfg := range insp.IPAM.Config { + _, err := netip.ParseAddr(ipamCfg.Gateway) + assert.Check(t, err) + } +}