From 0bc4a2a01844d950974c5c601fee596a58dfde01 Mon Sep 17 00:00:00 2001 From: Ashley Dumaine Date: Mon, 5 Feb 2024 20:39:20 -0500 Subject: [PATCH] move NB functions into loadbalancer.go --- cloud/services/loadbalancers.go | 88 +++++++++++++++++++++++++ controller/linodemachine_controller.go | 89 +------------------------- 2 files changed, 90 insertions(+), 87 deletions(-) diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index f58dbea03..c5b98ce4f 100644 --- a/cloud/services/loadbalancers.go +++ b/cloud/services/loadbalancers.go @@ -13,12 +13,15 @@ import ( "github.com/linode/cluster-api-provider-linode/cloud/scope" "github.com/linode/cluster-api-provider-linode/util" "github.com/linode/linodego" + kutil "sigs.k8s.io/cluster-api/util" ) var ( defaultLBPort = 6443 ) +const expectedIPV4Count = 2 + // CreateNodeBalancer creates a new NodeBalancer if one doesn't exist func CreateNodeBalancer(ctx context.Context, clusterScope *scope.ClusterScope, logger logr.Logger) (*linodego.NodeBalancer, error) { var linodeNBs []linodego.NodeBalancer @@ -143,3 +146,88 @@ func GetNodeBalancerConfig( return &linodeNBConfigs[0], err } + +// AddNodeToNB adds a backend Node on the Node Balancer configuration +func AddNodeToNB( + ctx context.Context, + logger logr.Logger, + machineScope *scope.MachineScope, + clusterScope *scope.ClusterScope, + linodeInstance *linodego.Instance, +) error { + // Update the NB backend with the new instance if it's a control plane node + if !kutil.IsControlPlaneMachine(machineScope.Machine) { + return nil + } + // Get the private IP that was assigned + ips := linodeInstance.IPv4 + if len(ips) < expectedIPV4Count { + err := errors.New("no private IP address") + logger.Error(err, "Fewer IPV4 addresses than expected") + + return err + } + linodeNBConfig, err := GetNodeBalancerConfig(ctx, clusterScope, logger) + if err != nil { + logger.Error(err, "Failed to get Node Balancer config") + + return err + } + _, err = machineScope.LinodeClient.CreateNodeBalancerNode( + ctx, + machineScope.LinodeCluster.Spec.Network.NodeBalancerID, + linodeNBConfig.ID, + linodego.NodeBalancerNodeCreateOptions{ + Label: machineScope.Cluster.Name, + Address: fmt.Sprintf("%s:%d", linodeInstance.IPv4[1].String(), linodeNBConfig.Port), + Mode: linodego.ModeAccept, + }, + ) + if err != nil { + logger.Error(err, "Failed to update Node Balancer") + + return err + } + + return nil +} + +// DeleteNodeFromNB removes a backend Node from the Node Balancer configuration +func DeleteNodeFromNB( + ctx context.Context, + logger logr.Logger, + machineScope *scope.MachineScope, + clusterScope *scope.ClusterScope, +) error { + // Update the NB to remove the node if it's a control plane node + if !kutil.IsControlPlaneMachine(machineScope.Machine) { + return nil + } + + linodeNBConfig, err := GetNodeBalancerConfig(ctx, clusterScope, logger) + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil || linodeNBConfig == nil { + logger.Error(err, "Failed to get Node Balancer config") + + return err + } + + if machineScope.LinodeMachine.Spec.InstanceID == nil { + err = errors.New("no InstanceID") + + return err + } + + err = machineScope.LinodeClient.DeleteNodeBalancerNode( + ctx, + machineScope.LinodeCluster.Spec.Network.NodeBalancerID, + linodeNBConfig.ID, + *machineScope.LinodeMachine.Spec.InstanceID, + ) + if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { + logger.Error(err, "Failed to update Node Balancer") + + return err + } + + return nil +} diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index 2f940a6db..91577ed77 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -66,8 +66,6 @@ var requeueInstanceStatuses = map[linodego.InstanceStatus]bool{ linodego.InstanceResizing: true, } -const expectedIPV4Count = 2 - // LinodeMachineReconciler reconciles a LinodeMachine object type LinodeMachineReconciler struct { client.Client @@ -365,7 +363,7 @@ func (r *LinodeMachineReconciler) reconcileCreate( }) } - if err = addNodeToNB(ctx, logger, machineScope, clusterScope, linodeInstance); err != nil { + if err = services.AddNodeToNB(ctx, logger, machineScope, clusterScope, linodeInstance); err != nil { logger.Error(err, "Failed to add instance to Node Balancer backend") return err @@ -374,50 +372,6 @@ func (r *LinodeMachineReconciler) reconcileCreate( return nil } -func addNodeToNB( - ctx context.Context, - logger logr.Logger, - machineScope *scope.MachineScope, - clusterScope *scope.ClusterScope, - linodeInstance *linodego.Instance, -) error { - // Update the NB backend with the new instance if it's a control plane node - if !kutil.IsControlPlaneMachine(machineScope.Machine) { - return nil - } - // Get the private IP that was assigned - ips := linodeInstance.IPv4 - if len(ips) < expectedIPV4Count { - err := errors.New("no private IP address") - logger.Error(err, "Fewer IPV4 addresses than expected") - - return err - } - linodeNBConfig, err := services.GetNodeBalancerConfig(ctx, clusterScope, logger) - if err != nil { - logger.Error(err, "Failed to get Node Balancer config") - - return err - } - _, err = machineScope.LinodeClient.CreateNodeBalancerNode( - ctx, - machineScope.LinodeCluster.Spec.Network.NodeBalancerID, - linodeNBConfig.ID, - linodego.NodeBalancerNodeCreateOptions{ - Label: machineScope.Cluster.Name, - Address: fmt.Sprintf("%s:%d", linodeInstance.IPv4[1].String(), linodeNBConfig.Port), - Mode: linodego.ModeAccept, - }, - ) - if err != nil { - logger.Error(err, "Failed to update Node Balancer") - - return err - } - - return nil -} - func (r *LinodeMachineReconciler) reconcileUpdate( ctx context.Context, logger logr.Logger, @@ -493,7 +447,7 @@ func (r *LinodeMachineReconciler) reconcileDelete( return nil } - err := deleteNodeFromNB(ctx, logger, machineScope, clusterScope) + err := services.DeleteNodeFromNB(ctx, logger, machineScope, clusterScope) if err != nil { logger.Error(err, "Failed to remove node from Node Balancer backend") @@ -519,45 +473,6 @@ func (r *LinodeMachineReconciler) reconcileDelete( return nil } -func deleteNodeFromNB( - ctx context.Context, - logger logr.Logger, - machineScope *scope.MachineScope, - clusterScope *scope.ClusterScope, -) error { - // Update the NB to remove the node if it's a control plane node - if !kutil.IsControlPlaneMachine(machineScope.Machine) { - return nil - } - - linodeNBConfig, err := services.GetNodeBalancerConfig(ctx, clusterScope, logger) - if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil || linodeNBConfig == nil { - logger.Error(err, "Failed to get Node Balancer config") - - return err - } - - if machineScope.LinodeMachine.Spec.InstanceID == nil { - err = errors.New("no InstanceID") - - return err - } - - err = machineScope.LinodeClient.DeleteNodeBalancerNode( - ctx, - machineScope.LinodeCluster.Spec.Network.NodeBalancerID, - linodeNBConfig.ID, - *machineScope.LinodeMachine.Spec.InstanceID, - ) - if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil { - logger.Error(err, "Failed to update Node Balancer") - - return err - } - - return nil -} - // SetupWithManager sets up the controller with the Manager. func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { controller, err := ctrl.NewControllerManagedBy(mgr).