Skip to content

Commit 90c5447

Browse files
committed
Preserve canonical Linux subnet CIDR in default network
1 parent 623176a commit 90c5447

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/network/bridge_linux.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,13 +1094,25 @@ func (m *manager) queryNetworkState(bridgeName string) (*Network, error) {
10941094

10951095
// Bridge existence plus an IPv4 address is sufficient. OperState may be
10961096
// OperUp or OperUnknown; both are functional for this bridge.
1097+
subnet := canonicalSubnetCIDR(gatewayAddr.IPNet)
10971098
return &Network{
10981099
Bridge: bridgeName,
10991100
Gateway: gatewayAddr.IP.String(),
1100-
Subnet: gatewayAddr.IPNet.String(),
1101+
Subnet: subnet,
11011102
}, nil
11021103
}
11031104

1105+
func canonicalSubnetCIDR(ipNet *net.IPNet) string {
1106+
if ipNet == nil {
1107+
return ""
1108+
}
1109+
1110+
return (&net.IPNet{
1111+
IP: ipNet.IP.Mask(ipNet.Mask),
1112+
Mask: ipNet.Mask,
1113+
}).String()
1114+
}
1115+
11041116
func selectBridgeGatewayAddr(addrs []netlink.Addr, preferredGateway net.IP) netlink.Addr {
11051117
for _, addr := range addrs {
11061118
if addr.IP.Equal(preferredGateway) {

lib/network/default_network_linux_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ func TestSelectBridgeGatewayAddrPrefersConfiguredGateway(t *testing.T) {
5555
assert.Equal(t, "10.123.0.1", selected.IP.String())
5656
}
5757

58+
func TestCanonicalSubnetCIDRUsesNetworkAddress(t *testing.T) {
59+
subnet := canonicalSubnetCIDR(&net.IPNet{
60+
IP: net.ParseIP("10.123.0.1"),
61+
Mask: net.CIDRMask(16, 32),
62+
})
63+
64+
assert.Equal(t, "10.123.0.0/16", subnet)
65+
}
66+
5867
func TestGetDefaultNetworkPreservesLookupError(t *testing.T) {
5968
cfg := &config.Config{
6069
Network: config.NetworkConfig{BridgeName: "hypeman-no-such-bridge"},

0 commit comments

Comments
 (0)