From 565c74adff31cb84d186956e5bc488a19c022ed2 Mon Sep 17 00:00:00 2001 From: Ashley Dumaine Date: Tue, 5 Nov 2024 10:14:17 -0500 Subject: [PATCH] respect vlan interface if defined in linodemachine --- controller/linodemachine_controller_helpers.go | 13 +++++++++++-- controller/linodemachine_controller_test.go | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/controller/linodemachine_controller_helpers.go b/controller/linodemachine_controller_helpers.go index 1665e8ec7..688cd2eb6 100644 --- a/controller/linodemachine_controller_helpers.go +++ b/controller/linodemachine_controller_helpers.go @@ -129,8 +129,9 @@ func newCreateConfig(ctx context.Context, machineScope *scope.MachineScope, logg // if vlan is enabled, attach additional interface as eth0 to linode if machineScope.LinodeCluster.Spec.Network.UseVlan { - iface, err := getVlanInterfaceConfig(ctx, machineScope, logger) + iface, err := getVlanInterfaceConfig(ctx, machineScope, createConfig.Interfaces, logger) if err != nil { + logger.Error(err, "Failed to get VLAN interface config") return nil, err } if iface != nil { @@ -363,7 +364,7 @@ func getFirewallID(ctx context.Context, machineScope *scope.MachineScope, logger return *linodeFirewall.Spec.FirewallID, nil } -func getVlanInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, logger logr.Logger) (*linodego.InstanceConfigInterfaceCreateOptions, error) { +func getVlanInterfaceConfig(ctx context.Context, machineScope *scope.MachineScope, interfaces []linodego.InstanceConfigInterfaceCreateOptions, logger logr.Logger) (*linodego.InstanceConfigInterfaceCreateOptions, error) { logger = logger.WithValues("vlanName", machineScope.Cluster.Name) // Try to obtain a IP for the machine using its name @@ -373,6 +374,14 @@ func getVlanInterfaceConfig(ctx context.Context, machineScope *scope.MachineScop } logger.Info("obtained IP for machine", "name", machineScope.LinodeMachine.Name, "ip", ip) + + for i, netInterface := range interfaces { + if netInterface.Purpose == linodego.InterfacePurposeVLAN { + interfaces[i].IPAMAddress = fmt.Sprintf(vlanIPFormat, ip) + return nil, nil //nolint:nilnil // it is important we don't return an interface if a VLAN interface already exists + } + } + return &linodego.InstanceConfigInterfaceCreateOptions{ Purpose: linodego.InterfacePurposeVLAN, Label: machineScope.Cluster.Name, diff --git a/controller/linodemachine_controller_test.go b/controller/linodemachine_controller_test.go index 05d98a895..7df1fbb73 100644 --- a/controller/linodemachine_controller_test.go +++ b/controller/linodemachine_controller_test.go @@ -2152,6 +2152,14 @@ var _ = Describe("machine in vlan", Label("machine", "vlan"), Ordered, func() { Type: "g6-nanode-1", Image: rutil.DefaultMachineControllerLinodeImage, DiskEncryption: string(linodego.InstanceDiskEncryptionEnabled), + Interfaces: []infrav1alpha2.InstanceConfigInterfaceCreateOptions{ + { + Purpose: linodego.InterfacePurposePublic, + }, + { + Purpose: linodego.InterfacePurposeVLAN, + }, + }, }, } @@ -2221,6 +2229,9 @@ var _ = Describe("machine in vlan", Label("machine", "vlan"), Ordered, func() { After(getAddrs). Return([]linodego.InstanceConfig{{ Interfaces: []linodego.InstanceConfigInterface{ + { + Purpose: linodego.InterfacePurposePublic, + }, { Purpose: linodego.InterfacePurposeVLAN, IPAMAddress: "10.0.0.2/11",