Skip to content

Commit

Permalink
store NB config ID in LinodeCluster network spec (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyDumaine authored Feb 7, 2024
1 parent f1bc15e commit 6abe490
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 60 deletions.
3 changes: 3 additions & 0 deletions api/v1alpha1/linodecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type NetworkSpec struct {
// NodeBalancerID is the id of api server NodeBalancer.
// +optional
NodeBalancerID int `json:"nodeBalancerID,omitempty"`
// NodeBalancerConfigID is the config ID of api server NodeBalancer.
// +optional
NodeBalancerConfigID *int `json:"nodeBalancerConfigID,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
7 changes: 6 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 12 additions & 54 deletions cloud/services/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"slices"
"strconv"

"github.com/go-logr/logr"
"github.com/linode/cluster-api-provider-linode/cloud/scope"
Expand All @@ -16,7 +15,7 @@ import (
kutil "sigs.k8s.io/cluster-api/util"
)

var (
const (
defaultLBPort = 6443
)

Expand Down Expand Up @@ -85,17 +84,6 @@ func CreateNodeBalancerConfig(
var linodeNBConfig *linodego.NodeBalancerConfig
var err error

if linodeNBConfig, err = GetNodeBalancerConfig(ctx, clusterScope, logger); err != nil {
logger.Info("Failed to list NodeBalancer Configs", "error", err.Error())

return nil, err
}
if linodeNBConfig != nil {
logger.Info("NodeBalancer ", strconv.Itoa(linodeNBConfig.ID), " already exists")

return linodeNBConfig, err
}

lbPort := defaultLBPort
if clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort != 0 {
lbPort = clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort
Expand All @@ -120,38 +108,12 @@ func CreateNodeBalancerConfig(
return linodeNBConfig, nil
}

// GetNodeBalancerConfig gets the first NodeBalancer config
func GetNodeBalancerConfig(
ctx context.Context,
clusterScope *scope.ClusterScope,
logger logr.Logger,
) (*linodego.NodeBalancerConfig, error) {
var linodeNBConfigs []linodego.NodeBalancerConfig
var err error

if linodeNBConfigs, err = clusterScope.LinodeClient.ListNodeBalancerConfigs(
ctx,
clusterScope.LinodeCluster.Spec.Network.NodeBalancerID,
linodego.NewListOptions(1, ""),
); err != nil {
logger.Info("Failed to list NodeBalancer Configs", "error", err.Error())

return nil, err
}
if len(linodeNBConfigs) == 0 {
return nil, err
}

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) {
Expand All @@ -171,19 +133,23 @@ func AddNodeToNB(
return err
}

linodeNBConfig, err := GetNodeBalancerConfig(ctx, clusterScope, logger)
if err != nil {
logger.Error(err, "Failed to get Node Balancer config")
lbPort := defaultLBPort
if clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort != 0 {
lbPort = clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort
}
if machineScope.LinodeCluster.Spec.Network.NodeBalancerConfigID == nil {
err := errors.New("nil NodeBalancer Config ID")
logger.Error(err, "config ID for NodeBalancer is nil")

return err
}
_, err = machineScope.LinodeClient.CreateNodeBalancerNode(
ctx,
machineScope.LinodeCluster.Spec.Network.NodeBalancerID,
linodeNBConfig.ID,
*machineScope.LinodeCluster.Spec.Network.NodeBalancerConfigID,
linodego.NodeBalancerNodeCreateOptions{
Label: machineScope.Cluster.Name,
Address: fmt.Sprintf("%s:%d", addresses.IPv4.Private[0].Address, linodeNBConfig.Port),
Address: fmt.Sprintf("%s:%d", addresses.IPv4.Private[0].Address, lbPort),
Mode: linodego.ModeAccept,
},
)
Expand All @@ -201,7 +167,6 @@ 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) {
Expand All @@ -212,17 +177,10 @@ func DeleteNodeFromNB(
return errors.New("no InstanceID")
}

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
}

err = machineScope.LinodeClient.DeleteNodeBalancerNode(
err := machineScope.LinodeClient.DeleteNodeBalancerNode(
ctx,
machineScope.LinodeCluster.Spec.Network.NodeBalancerID,
linodeNBConfig.ID,
*machineScope.LinodeCluster.Spec.Network.NodeBalancerConfigID,
*machineScope.LinodeMachine.Spec.InstanceID,
)
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ spec:
enum:
- NodeBalancer
type: string
nodeBalancerConfigID:
description: NodeBalancerConfigID is the config ID of api server
NodeBalancer.
type: integer
nodeBalancerID:
description: NodeBalancerID is the id of api server NodeBalancer.
type: integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ spec:
enum:
- NodeBalancer
type: string
nodeBalancerConfigID:
description: NodeBalancerConfigID is the config ID of
api server NodeBalancer.
type: integer
nodeBalancerID:
description: NodeBalancerID is the id of api server NodeBalancer.
type: integer
Expand Down
3 changes: 3 additions & 0 deletions controller/linodecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ func (r *LinodeClusterReconciler) reconcileCreate(ctx context.Context, logger lo
return err
}

clusterScope.LinodeCluster.Spec.Network.NodeBalancerConfigID = util.Pointer(linodeNBConfig.ID)

clusterScope.LinodeCluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
Host: *linodeNB.IPv4,
Port: int32(linodeNBConfig.Port),
Expand Down Expand Up @@ -210,6 +212,7 @@ func (r *LinodeClusterReconciler) reconcileDelete(ctx context.Context, logger lo
conditions.MarkFalse(clusterScope.LinodeCluster, clusterv1.ReadyCondition, clusterv1.DeletedReason, clusterv1.ConditionSeverityInfo, "Load balancer deleted")

clusterScope.LinodeCluster.Spec.Network.NodeBalancerID = 0
clusterScope.LinodeCluster.Spec.Network.NodeBalancerConfigID = nil
controllerutil.RemoveFinalizer(clusterScope.LinodeCluster, infrav1alpha1.GroupVersion.String())

return nil
Expand Down
9 changes: 4 additions & 5 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (r *LinodeMachineReconciler) reconcile(
if !machineScope.LinodeMachine.ObjectMeta.DeletionTimestamp.IsZero() {
failureReason = cerrs.DeleteMachineError

err = r.reconcileDelete(ctx, logger, machineScope, clusterScope)
err = r.reconcileDelete(ctx, logger, machineScope)

return
}
Expand Down Expand Up @@ -363,7 +363,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(
})
}

if err = services.AddNodeToNB(ctx, logger, machineScope, clusterScope, linodeInstance); err != nil {
if err = services.AddNodeToNB(ctx, logger, machineScope, clusterScope); err != nil {
logger.Error(err, "Failed to add instance to Node Balancer backend")

return err
Expand Down Expand Up @@ -397,7 +397,7 @@ func (r *LinodeMachineReconciler) reconcileUpdate(
machineScope.LinodeMachine.Spec.ProviderID = nil
machineScope.LinodeMachine.Spec.InstanceID = nil

conditions.MarkFalse(machineScope.LinodeMachine, clusterv1.ReadyCondition, string("missing"), clusterv1.ConditionSeverityWarning, "instance not found")
conditions.MarkFalse(machineScope.LinodeMachine, clusterv1.ReadyCondition, "missing", clusterv1.ConditionSeverityWarning, "instance not found")
}

return res, err
Expand Down Expand Up @@ -436,7 +436,6 @@ func (r *LinodeMachineReconciler) reconcileDelete(
ctx context.Context,
logger logr.Logger,
machineScope *scope.MachineScope,
clusterScope *scope.ClusterScope,
) error {
logger.Info("deleting machine")

Expand All @@ -447,7 +446,7 @@ func (r *LinodeMachineReconciler) reconcileDelete(
return nil
}

err := services.DeleteNodeFromNB(ctx, logger, machineScope, clusterScope)
err := services.DeleteNodeFromNB(ctx, logger, machineScope)
if err != nil {
logger.Error(err, "Failed to remove node from Node Balancer backend")

Expand Down

0 comments on commit 6abe490

Please sign in to comment.