From 92ff04cdbb46c01b8e5927f5d914b603dfeb8778 Mon Sep 17 00:00:00 2001 From: Ashley Dumaine Date: Tue, 20 Aug 2024 15:59:03 -0400 Subject: [PATCH] pass instanceID instead of checking status --- cloud/services/loadbalancers.go | 6 +----- cloud/services/loadbalancers_test.go | 4 ++-- controller/linodemachine_controller.go | 21 ++++++++++++++----- .../linodemachine_controller_helpers.go | 3 ++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index 44c12cd0..b23933b8 100644 --- a/cloud/services/loadbalancers.go +++ b/cloud/services/loadbalancers.go @@ -179,17 +179,13 @@ func AddNodeToNB( ctx context.Context, logger logr.Logger, machineScope *scope.MachineScope, + instanceID int, ) error { // Update the NB backend with the new instance if it's a control plane node if !kutil.IsControlPlaneMachine(machineScope.Machine) { return nil } - instanceID, err := util.GetInstanceID(machineScope.LinodeMachine.Spec.ProviderID) - if err != nil { - logger.Error(err, "Failed to parse instance ID from provider ID") - return err - } // Get the private IP that was assigned addresses, err := machineScope.LinodeClient.GetInstanceIPAddresses(ctx, instanceID) if err != nil { diff --git a/cloud/services/loadbalancers_test.go b/cloud/services/loadbalancers_test.go index 9ed9ccf4..8676e9c8 100644 --- a/cloud/services/loadbalancers_test.go +++ b/cloud/services/loadbalancers_test.go @@ -550,7 +550,7 @@ func TestAddNodeToNBConditions(t *testing.T) { testcase.machineScope.Client = MockK8sClient testcase.expectK8sClient(MockK8sClient) - err := AddNodeToNB(context.Background(), logr.Discard(), testcase.machineScope) + err := AddNodeToNB(context.Background(), logr.Discard(), testcase.machineScope, 123) if testcase.expectedError != nil { assert.ErrorContains(t, err, testcase.expectedError.Error()) } @@ -724,7 +724,7 @@ func TestAddNodeToNBFullWorkflow(t *testing.T) { testcase.machineScope.Client = MockK8sClient testcase.expectK8sClient(MockK8sClient) - err := AddNodeToNB(context.Background(), logr.Discard(), testcase.machineScope) + err := AddNodeToNB(context.Background(), logr.Discard(), testcase.machineScope, 123) if testcase.expectedError != nil { assert.ErrorContains(t, err, testcase.expectedError.Error()) } diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index 3fe1e947..abb055d4 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -41,7 +41,6 @@ import ( crcontroller "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" - "sigs.k8s.io/controller-runtime/pkg/reconcile" infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1" infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2" @@ -140,10 +139,22 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques cluster, err := kutil.GetClusterFromMetadata(ctx, r.TracedClient(), machine.ObjectMeta) if err != nil { log.Info("Failed to fetch cluster by label") - return reconcile.Result{}, nil + return ctrl.Result{}, nil + } + + // Fetch linode cluster + linodeClusterKey := client.ObjectKey{ + Namespace: linodeMachine.Namespace, + Name: cluster.Spec.InfrastructureRef.Name, + } + linodeCluster := &infrav1alpha2.LinodeCluster{} + if err := r.Client.Get(ctx, linodeClusterKey, linodeCluster); err != nil { + if err = client.IgnoreNotFound(err); err != nil { + return ctrl.Result{}, fmt.Errorf("get linodecluster %q: %w", linodeClusterKey, err) + } } - log = log.WithValues("cluster", cluster.Name) + log = log.WithValues("LinodeCluster", linodeCluster.Name) machineScope, err := scope.NewMachineScope( ctx, @@ -153,7 +164,7 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques Client: r.TracedClient(), Cluster: cluster, Machine: machine, - LinodeCluster: &infrav1alpha2.LinodeCluster{}, + LinodeCluster: linodeCluster, LinodeMachine: linodeMachine, }, ) @@ -366,7 +377,7 @@ func (r *LinodeMachineReconciler) reconcileInstanceCreate( } if !reconciler.ConditionTrue(machineScope.LinodeMachine, ConditionPreflightNetworking) { - if err := r.addMachineToLB(ctx, machineScope); err != nil { + if err := r.addMachineToLB(ctx, machineScope, linodeInstance.ID); err != nil { logger.Error(err, "Failed to add machine to LB") if reconciler.RecordDecayingCondition(machineScope.LinodeMachine, diff --git a/controller/linodemachine_controller_helpers.go b/controller/linodemachine_controller_helpers.go index ae25bff8..bfac58c4 100644 --- a/controller/linodemachine_controller_helpers.go +++ b/controller/linodemachine_controller_helpers.go @@ -481,10 +481,11 @@ func createInstanceConfigDeviceMap(instanceDisks map[string]*infrav1alpha2.Insta func (r *LinodeMachineReconciler) addMachineToLB( ctx context.Context, machineScope *scope.MachineScope, + linodeInstanceID int, ) error { logger := logr.FromContextOrDiscard(ctx) if machineScope.LinodeCluster.Spec.Network.LoadBalancerType != "dns" { - if err := services.AddNodeToNB(ctx, logger, machineScope); err != nil { + if err := services.AddNodeToNB(ctx, logger, machineScope, linodeInstanceID); err != nil { return err } } else {