Skip to content

Commit

Permalink
move NB functions into loadbalancer.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDumaine committed Feb 6, 2024
1 parent f19e9ad commit 0bc4a2a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 87 deletions.
88 changes: 88 additions & 0 deletions cloud/services/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
89 changes: 2 additions & 87 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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")

Expand All @@ -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).
Expand Down

0 comments on commit 0bc4a2a

Please sign in to comment.