@@ -29,6 +29,7 @@ import (
2929 "github.com/go-logr/logr"
3030 "github.com/google/uuid"
3131 "github.com/linode/linodego"
32+ corev1 "k8s.io/api/core/v1"
3233 apierrors "k8s.io/apimachinery/pkg/api/errors"
3334 "k8s.io/apimachinery/pkg/api/resource"
3435 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -113,12 +114,11 @@ func newCreateConfig(ctx context.Context, machineScope *scope.MachineScope, logg
113114
114115 fillCreateConfig (createConfig , machineScope )
115116
116- // if vpc is enabled, attach additional interface as eth0 to linode
117- if machineScope . LinodeCluster . Spec . VPCRef != nil {
118- iface , err := getVPCInterfaceConfig (ctx , machineScope , createConfig .Interfaces , logger )
117+ // Check for VPC configuration - first machine level, then cluster level
118+ if vpcRef := getVPCRefFromScope ( machineScope ); vpcRef != nil {
119+ iface , err := getVPCInterfaceConfig (ctx , machineScope , createConfig .Interfaces , logger , vpcRef )
119120 if err != nil {
120121 logger .Error (err , "Failed to get VPC interface config" )
121-
122122 return nil , err
123123 }
124124 if iface != nil {
@@ -380,13 +380,15 @@ func getVlanInterfaceConfig(ctx context.Context, machineScope *scope.MachineScop
380380 }, nil
381381}
382382
383- func getVPCInterfaceConfig (ctx context.Context , machineScope * scope.MachineScope , interfaces []linodego.InstanceConfigInterfaceCreateOptions , logger logr.Logger ) (* linodego.InstanceConfigInterfaceCreateOptions , error ) {
384- name := machineScope . LinodeCluster . Spec . VPCRef . Name
385- namespace := machineScope . LinodeCluster . Spec . VPCRef .Namespace
383+ func getVPCInterfaceConfig (ctx context.Context , machineScope * scope.MachineScope , interfaces []linodego.InstanceConfigInterfaceCreateOptions , logger logr.Logger , vpcRef * corev1. ObjectReference ) (* linodego.InstanceConfigInterfaceCreateOptions , error ) {
384+ // Get namespace from VPC ref or default to machine namespace
385+ namespace := vpcRef .Namespace
386386 if namespace == "" {
387- namespace = machineScope .LinodeCluster .Namespace
387+ namespace = machineScope .LinodeMachine .Namespace
388388 }
389389
390+ name := vpcRef .Name
391+
390392 logger = logger .WithValues ("vpcName" , name , "vpcNamespace" , namespace )
391393
392394 linodeVPC := infrav1alpha2.LinodeVPC {
@@ -712,3 +714,13 @@ func createInstance(ctx context.Context, logger logr.Logger, machineScope *scope
712714 inst , err := machineScope .LinodeClient .CreateInstance (ctx , * createOpts )
713715 return inst , ctr .RetryAfter (), err
714716}
717+
718+ // getVPCRefFromScope returns the appropriate VPC reference based on priority:
719+ // 1. Machine-level VPC reference
720+ // 2. Cluster-level VPC reference
721+ func getVPCRefFromScope (machineScope * scope.MachineScope ) * corev1.ObjectReference {
722+ if machineScope .LinodeMachine .Spec .VPCRef != nil {
723+ return machineScope .LinodeMachine .Spec .VPCRef
724+ }
725+ return machineScope .LinodeCluster .Spec .VPCRef
726+ }
0 commit comments