Skip to content

Commit

Permalink
fix: nocloud network link matching on MAC addresses
Browse files Browse the repository at this point in the history
Fixes #9811

Fall back to non-permanent hardware addresses, ignore non-physical links
(e.g. bonds).

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Nov 27, 2024
1 parent 2a9130a commit e10e90b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,15 @@ func (n *Nocloud) applyNetworkConfigV1(config *NetworkConfig, st state.State, ne
macAddressMatched := false

for hostInterface := range hostInterfaces.All() {
if !hostInterface.TypedSpec().Physical() {
continue
}

macAddress := hostInterface.TypedSpec().PermanentAddr.String()
if macAddress == "" {
macAddress = hostInterface.TypedSpec().HardwareAddr.String()
}

if macAddress == ntwrk.Mac {
name = hostInterface.Metadata().ID()
macAddressMatched = true
Expand Down Expand Up @@ -626,7 +634,15 @@ func (n *Nocloud) applyNetworkConfigV2(config *NetworkConfig, st state.State, ne
macAddressMatched := false

for hostInterface := range hostInterfaces.All() {
if !hostInterface.TypedSpec().Physical() {
continue
}

macAddress := hostInterface.TypedSpec().PermanentAddr.String()
if macAddress == "" {
macAddress = hostInterface.TypedSpec().HardwareAddr.String()
}

if macAddress == eth.Match.HWAddr {
name = hostInterface.Metadata().ID()
macAddressMatched = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,27 @@ func TestParseMetadata(t *testing.T) {

st := state.WrapCore(namespaced.NewState(inmem.Build))

bond0 := network.NewLinkStatus(network.NamespaceName, "bond0")
bond0.TypedSpec().PermanentAddr = nethelpers.HardwareAddr{0x68, 0x05, 0xca, 0xb8, 0xf1, 0xf7} // this link is not a physical one, so it should be ignored
bond0.TypedSpec().Type = nethelpers.LinkEther
bond0.TypedSpec().Kind = "bond"
require.NoError(t, st.Create(context.TODO(), bond0))

eth0 := network.NewLinkStatus(network.NamespaceName, "eth0")
eth0.TypedSpec().PermanentAddr = nethelpers.HardwareAddr{0x68, 0x05, 0xca, 0xb8, 0xf1, 0xf7}
eth0.TypedSpec().Type = nethelpers.LinkEther
eth0.TypedSpec().Kind = ""
require.NoError(t, st.Create(context.TODO(), eth0))

eth1 := network.NewLinkStatus(network.NamespaceName, "eth1")
eth1.TypedSpec().HardwareAddr = nethelpers.HardwareAddr{0x68, 0x05, 0xca, 0xb8, 0xf1, 0xf9} // this link has a permanent address, so hardware addr should be ignored
eth1.TypedSpec().PermanentAddr = nethelpers.HardwareAddr{0x68, 0x05, 0xca, 0xb8, 0xf1, 0xf8}
eth1.TypedSpec().Type = nethelpers.LinkEther
eth1.TypedSpec().Kind = ""
require.NoError(t, st.Create(context.TODO(), eth1))

eth2 := network.NewLinkStatus(network.NamespaceName, "eth2")
eth2.TypedSpec().PermanentAddr = nethelpers.HardwareAddr{0x68, 0x05, 0xca, 0xb8, 0xf1, 0xf9}
eth2.TypedSpec().HardwareAddr = nethelpers.HardwareAddr{0x68, 0x05, 0xca, 0xb8, 0xf1, 0xf9} // this link doesn't have a permanent address, but only a hardware address
eth2.TypedSpec().Type = nethelpers.LinkEther
eth2.TypedSpec().Kind = ""
require.NoError(t, st.Create(context.TODO(), eth2))
Expand Down

0 comments on commit e10e90b

Please sign in to comment.