Skip to content

Commit

Permalink
Ensures that we generate a Netplan that matches devices based on MAC
Browse files Browse the repository at this point in the history
address, when we create LXD VMs on-machine. This mimics the behaviour
for the KVM provider.

The option setting is simplified to remove multiple negative clauses,
and the KVM broker made explicit in it's use of the option.
  • Loading branch information
manadart committed May 23, 2024
1 parent e583cc8 commit 729c2b0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
8 changes: 4 additions & 4 deletions cloudconfig/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ type cloudConfig struct {
// update_hostname
attrs map[string]interface{}

// omitNetplanHWAddrMatch if true, causes Netplan to be rendered without
// useNetplanHWAddrMatch if false, causes Netplan to be rendered without
// a stanza that matches by MAC address in order to apply configuration to
// a device.
// This will be recruited for LXD, where we have observed 22.04 containers
// being assigned a different MAC to the one configured.
// This will be disabled for LXD containers, where we have observed 22.04
// containers being assigned a different MAC to the one configured.
// For these cases we fall back to the default match by ID (name).
// MAC address matching is still required by KVM where devices are assigned
// different names by the kernel to those we configured.
omitNetplanHWAddrMatch bool
useNetplanHWAddrMatch bool
}

// getPackagingConfigurer is defined on the AdvancedPackagingConfig interface.
Expand Down
9 changes: 7 additions & 2 deletions cloudconfig/cloudinit/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,13 @@ type NetworkingConfig interface {
AddNetworkConfig(interfaces corenetwork.InterfaceInfos) error
}

func WithDisableNetplanMACMatch(cfg *cloudConfig) {
cfg.omitNetplanHWAddrMatch = true
// WithNetplanMACMatch returns a cloud config option for telling Netplan
// whether to match by MAC address (true) or the interface name (false) when
// configuring NICs.
func WithNetplanMACMatch(enabled bool) func(*cloudConfig) {
return func(cfg *cloudConfig) {
cfg.useNetplanHWAddrMatch = enabled
}
}

// New returns a new Config with no options set.
Expand Down
4 changes: 2 additions & 2 deletions cloudconfig/cloudinit/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type InterfaceSuite struct{}
var _ = gc.Suite(InterfaceSuite{})

func (HelperSuite) TestNewCloudConfigWithoutMACMatch(c *gc.C) {
cfg, err := New("ubuntu", WithDisableNetplanMACMatch)
cfg, err := New("ubuntu", WithNetplanMACMatch(true))
c.Assert(err, jc.ErrorIsNil)
c.Check(cfg.(*ubuntuCloudConfig).omitNetplanHWAddrMatch, jc.IsTrue)
c.Check(cfg.(*ubuntuCloudConfig).useNetplanHWAddrMatch, jc.IsTrue)
}
3 changes: 2 additions & 1 deletion cloudconfig/cloudinit/network_ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func GenerateNetplan(interfaces corenetwork.InterfaceInfos, matchHWAddr bool) (s
if err != nil {
return "", errors.Trace(err)
}

return string(out), nil
}

Expand Down Expand Up @@ -320,7 +321,7 @@ func (cfg *ubuntuCloudConfig) AddNetworkConfig(interfaces corenetwork.InterfaceI
if err != nil {
return errors.Trace(err)
}
netPlan, err := GenerateNetplan(interfaces, !cfg.omitNetplanHWAddrMatch)
netPlan, err := GenerateNetplan(interfaces, cfg.useNetplanHWAddrMatch)
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cloudconfig/cloudinit/network_ubuntu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ network:

func (s *NetworkUbuntuSuite) TestAddNetworkConfigSampleConfig(c *gc.C) {
netConfig := container.BridgeNetworkConfig(0, s.fakeInterfaces)
cloudConf, err := cloudinit.New("ubuntu")
cloudConf, err := cloudinit.New("ubuntu", cloudinit.WithNetplanMACMatch(true))
c.Assert(err, jc.ErrorIsNil)
c.Assert(cloudConf, gc.NotNil)
err = cloudConf.AddNetworkConfig(netConfig.Interfaces)
Expand Down
3 changes: 2 additions & 1 deletion container/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func (manager *containerManager) CreateContainer(
kvmContainer := KVMObjectFactory.New(name)

// Create the cloud-init.
cloudConfig, err := cloudinit.New(instanceConfig.Base.OS)
// Netplan configures NICs by matching MAC addresses.
cloudConfig, err := cloudinit.New(instanceConfig.Base.OS, cloudinit.WithNetplanMACMatch(true))
if err != nil {
return nil, nil, errors.Trace(err)
}
Expand Down
4 changes: 3 additions & 1 deletion container/lxd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ func (m *containerManager) getContainerSpec(
networkConfig.Interfaces = interfaces
}

cloudConfig, err := cloudinit.New(instanceConfig.Base.OS, cloudinit.WithDisableNetplanMACMatch)
// We tell Netplan to match by interface name for containers; by MAC for VMs.
cloudConfig, err := cloudinit.New(
instanceConfig.Base.OS, cloudinit.WithNetplanMACMatch(virtType == api.InstanceTypeVM))
if err != nil {
return ContainerSpec{}, errors.Trace(err)
}
Expand Down

0 comments on commit 729c2b0

Please sign in to comment.