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>
This commit is contained in:
Rob Murray
2025-02-22 11:10:49 +00:00
parent 459686bbea
commit 3cf4ff971d
3 changed files with 24 additions and 2 deletions

View File

@@ -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,
})
}
}

View File

@@ -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"},
},
},
{

View File

@@ -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)
}
}