Skip to content

Commit

Permalink
use kubebuilder default for NB port, store NB config ID in LinodeClus…
Browse files Browse the repository at this point in the history
…ter status
  • Loading branch information
AshleyDumaine committed Feb 6, 2024
1 parent 26dfbc8 commit b6c4e89
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 60 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/linodecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type LinodeClusterStatus struct {
// +optional
Ready bool `json:"ready"`

// NodeBalancerConfigID contains the config ID for the NodeBalancer associated with
// the LinodeCluster
// +optional
NodeBalancerConfigID *int `json:"nodeBalancerConfigID,omitempty"`

// FailureReason will be set in the event that there is a terminal problem
// reconciling the LinodeCluster and will contain a succinct value suitable
// for machine interpretation.
Expand Down Expand Up @@ -98,6 +103,7 @@ type NetworkSpec struct {
// LoadBalancerPort used by the api server. It must be valid ports range (1-65535). If omitted, default value is 6443.
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=65535
// +kubebuilder:default:=6443
// +optional
LoadBalancerPort int `json:"loadBalancerPort,omitempty"`
// NodeBalancerID is the id of api server NodeBalancer.
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

79 changes: 21 additions & 58 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,10 +15,6 @@ import (
kutil "sigs.k8s.io/cluster-api/util"
)

var (
defaultLBPort = 6443
)

// 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 @@ -81,32 +76,21 @@ func CreateNodeBalancerConfig(
ctx context.Context,
clusterScope *scope.ClusterScope,
logger logr.Logger,
) (*linodego.NodeBalancerConfig, error) {
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")
) (configID *int, err error) {
if clusterScope.LinodeCluster.Status.NodeBalancerConfigID != nil {
logger.Info("NodeBalancer config already exists")

return linodeNBConfig, err
return clusterScope.LinodeCluster.Status.NodeBalancerConfigID, nil
}

lbPort := defaultLBPort
if clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort != 0 {
lbPort = clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort
}
createConfig := linodego.NodeBalancerConfigCreateOptions{
Port: lbPort,
Port: clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort,
Protocol: linodego.ProtocolTCP,
Algorithm: linodego.AlgorithmRoundRobin,
Check: linodego.CheckConnection,
}

var linodeNBConfig *linodego.NodeBalancerConfig
if linodeNBConfig, err = clusterScope.LinodeClient.CreateNodeBalancerConfig(
ctx,
clusterScope.LinodeCluster.Spec.Network.NodeBalancerID,
Expand All @@ -117,32 +101,7 @@ func CreateNodeBalancerConfig(
return nil, err
}

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
return util.Pointer(linodeNBConfig.ID), nil
}

// AddNodeToNB adds a backend Node on the Node Balancer configuration
Expand Down Expand Up @@ -171,20 +130,24 @@ func AddNodeToNB(
return err
}

linodeNBConfig, err := GetNodeBalancerConfig(ctx, clusterScope, logger)
if err != nil {
if machineScope.LinodeCluster.Status.NodeBalancerConfigID == nil {
err := errors.New("nil NodeBalancer config ID")
logger.Error(err, "Failed to get Node Balancer config")

return err
}
_, err = machineScope.LinodeClient.CreateNodeBalancerNode(
ctx,
machineScope.LinodeCluster.Spec.Network.NodeBalancerID,
linodeNBConfig.ID,
*machineScope.LinodeCluster.Status.NodeBalancerConfigID,
linodego.NodeBalancerNodeCreateOptions{
Label: machineScope.Cluster.Name,
Address: fmt.Sprintf("%s:%d", addresses.IPv4.Private[0].Address, linodeNBConfig.Port),
Mode: linodego.ModeAccept,
Label: machineScope.Cluster.Name,
Address: fmt.Sprintf(
"%s:%d",
addresses.IPv4.Private[0].Address,
machineScope.LinodeCluster.Spec.Network.LoadBalancerPort,
),
Mode: linodego.ModeAccept,
},
)
if err != nil {
Expand Down Expand Up @@ -212,17 +175,17 @@ func DeleteNodeFromNB(
return errors.New("no InstanceID")
}

linodeNBConfig, err := GetNodeBalancerConfig(ctx, clusterScope, logger)
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil || linodeNBConfig == nil {
if machineScope.LinodeCluster.Status.NodeBalancerConfigID == nil {
err := errors.New("nil NodeBalancer config ID")
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.Status.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 @@ -73,6 +73,7 @@ spec:
network.
properties:
loadBalancerPort:
default: 6443
description: LoadBalancerPort used by the api server. It must
be valid ports range (1-65535). If omitted, default value is
6443.
Expand Down Expand Up @@ -216,6 +217,10 @@ spec:
a terminal problem reconciling the LinodeCluster and will contain
a succinct value suitable for machine interpretation.
type: string
nodeBalancerConfigID:
description: NodeBalancerConfigID contains the config ID for the NodeBalancer
associated with the LinodeCluster
type: integer
ready:
description: Ready denotes that the cluster (infrastructure) is ready.
type: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ spec:
Linode network.
properties:
loadBalancerPort:
default: 6443
description: LoadBalancerPort used by the api server.
It must be valid ports range (1-65535). If omitted,
default value is 6443.
Expand Down
11 changes: 9 additions & 2 deletions controller/linodecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (r *LinodeClusterReconciler) reconcileCreate(ctx context.Context, logger lo

clusterScope.LinodeCluster.Spec.Network.NodeBalancerID = linodeNB.ID

linodeNBConfig, err := services.CreateNodeBalancerConfig(ctx, clusterScope, logger)
configID, err := services.CreateNodeBalancerConfig(ctx, clusterScope, logger)
if err != nil {
setFailureReason(clusterScope, cerrs.CreateClusterError, err, r)

Expand All @@ -180,7 +180,14 @@ func (r *LinodeClusterReconciler) reconcileCreate(ctx context.Context, logger lo

clusterScope.LinodeCluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
Host: *linodeNB.IPv4,
Port: int32(linodeNBConfig.Port),
Port: int32(clusterScope.LinodeCluster.Spec.Network.LoadBalancerPort),
}

clusterScope.LinodeCluster.Status.NodeBalancerConfigID = configID
if err = r.Status().Update(ctx, clusterScope.LinodeCluster); err != nil {
setFailureReason(clusterScope, cerrs.CreateClusterError, err, r)

return err
}

return nil
Expand Down

0 comments on commit b6c4e89

Please sign in to comment.