From 6abe490f4d4af68972e809105958685ddd190196 Mon Sep 17 00:00:00 2001 From: Ashley Dumaine <5779804+AshleyDumaine@users.noreply.github.com> Date: Wed, 7 Feb 2024 09:19:23 -0500 Subject: [PATCH] store NB config ID in LinodeCluster network spec (#85) --- api/v1alpha1/linodecluster_types.go | 3 + api/v1alpha1/zz_generated.deepcopy.go | 7 +- cloud/services/loadbalancers.go | 66 ++++--------------- ...cture.cluster.x-k8s.io_linodeclusters.yaml | 4 ++ ...uster.x-k8s.io_linodeclustertemplates.yaml | 4 ++ controller/linodecluster_controller.go | 3 + controller/linodemachine_controller.go | 9 ++- 7 files changed, 36 insertions(+), 60 deletions(-) diff --git a/api/v1alpha1/linodecluster_types.go b/api/v1alpha1/linodecluster_types.go index 31ae6d277..44aeb7c79 100644 --- a/api/v1alpha1/linodecluster_types.go +++ b/api/v1alpha1/linodecluster_types.go @@ -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 diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 84feb9fdd..c65da8d0d 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -136,7 +136,7 @@ func (in *LinodeClusterList) DeepCopyObject() runtime.Object { func (in *LinodeClusterSpec) DeepCopyInto(out *LinodeClusterSpec) { *out = *in out.ControlPlaneEndpoint = in.ControlPlaneEndpoint - out.Network = in.Network + in.Network.DeepCopyInto(&out.Network) if in.VPCRef != nil { in, out := &in.VPCRef, &out.VPCRef *out = new(v1.ObjectReference) @@ -645,6 +645,11 @@ func (in *LinodeVPCStatus) DeepCopy() *LinodeVPCStatus { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NetworkSpec) DeepCopyInto(out *NetworkSpec) { *out = *in + if in.NodeBalancerConfigID != nil { + in, out := &in.NodeBalancerConfigID, &out.NodeBalancerConfigID + *out = new(int) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkSpec. diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index dd1a20575..98d161c9d 100644 --- a/cloud/services/loadbalancers.go +++ b/cloud/services/loadbalancers.go @@ -7,7 +7,6 @@ import ( "fmt" "net/http" "slices" - "strconv" "github.com/go-logr/logr" "github.com/linode/cluster-api-provider-linode/cloud/scope" @@ -16,7 +15,7 @@ import ( kutil "sigs.k8s.io/cluster-api/util" ) -var ( +const ( defaultLBPort = 6443 ) @@ -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 @@ -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) { @@ -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, }, ) @@ -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) { @@ -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 { diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclusters.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclusters.yaml index 6fdc5281c..389314846 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclusters.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclusters.yaml @@ -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 diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclustertemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclustertemplates.yaml index 70e93db67..e19d79388 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclustertemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_linodeclustertemplates.yaml @@ -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 diff --git a/controller/linodecluster_controller.go b/controller/linodecluster_controller.go index fe5bf2323..eebfb2d36 100644 --- a/controller/linodecluster_controller.go +++ b/controller/linodecluster_controller.go @@ -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), @@ -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 diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index 91577ed77..dbd5c83d9 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -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 } @@ -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 @@ -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 @@ -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") @@ -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")