Skip to content

Commit

Permalink
support byovpc and clean up list filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
eljohnson92 committed Mar 6, 2024
1 parent f37e291 commit fd77bbe
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
11 changes: 2 additions & 9 deletions cloud/services/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package services

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -21,20 +20,14 @@ const (

// 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
var linodeNB *linodego.NodeBalancer

NBLabel := fmt.Sprintf("%s-api-server", clusterScope.LinodeCluster.Name)
clusterUID := string(clusterScope.LinodeCluster.UID)
tags := []string{string(clusterScope.LinodeCluster.UID)}
filter := map[string]string{
"label": NBLabel,
}

rawFilter, err := json.Marshal(filter)
linodeNBs, err := clusterScope.LinodeClient.ListNodeBalancers(ctx, linodego.NewListOptions(1, util.CreateLinodeAPIFilter(clusterScope.LinodeCluster.Spec.Network.NodeBalancerID, NBLabel, tags)))
if err != nil {
return nil, err
}
if linodeNBs, err = clusterScope.LinodeClient.ListNodeBalancers(ctx, linodego.NewListOptions(1, string(rawFilter))); err != nil {
logger.Info("Failed to list NodeBalancers", "error", err.Error())

return nil, err
Expand Down
4 changes: 3 additions & 1 deletion cloud/services/object_storage_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
)

func EnsureObjectStorageBucket(ctx context.Context, bScope *scope.ObjectStorageBucketScope) (*linodego.ObjectStorageBucket, error) {
// Buckets do not have IDs so hardcode it to 0
bucketID := 0
buckets, err := bScope.LinodeClient.ListObjectStorageBucketsInCluster(
ctx,
linodego.NewListOptions(1, util.CreateLinodeAPIFilter(*bScope.Bucket.Spec.Label, nil)),
linodego.NewListOptions(1, util.CreateLinodeAPIFilter(bucketID, *bScope.Bucket.Spec.Label, nil)),
bScope.Bucket.Spec.Cluster,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (r *LinodeMachineReconciler) reconcileCreate(

tags := []string{machineScope.LinodeCluster.Name}

linodeInstances, err := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, util.CreateLinodeAPIFilter(machineScope.LinodeMachine.Name, tags)))
linodeInstances, err := machineScope.LinodeClient.ListInstances(ctx, linodego.NewListOptions(1, util.CreateLinodeAPIFilter(*machineScope.LinodeMachine.Spec.InstanceID, machineScope.LinodeMachine.Name, tags)))
if err != nil {
logger.Error(err, "Failed to list Linode machine instances")

Expand Down
10 changes: 4 additions & 6 deletions controller/linodevpc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ type LinodeVPCReconciler struct {
ReconcileTimeout time.Duration
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs/finalizers,verbs=update
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodevpcs/finalizers,verbs=update

//+kubebuilder:rbac:groups="",resources=events,verbs=create;update;patch
// +kubebuilder:rbac:groups="",resources=events,verbs=create;update;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the VPC closer to the desired state.
Expand Down Expand Up @@ -221,8 +221,6 @@ func (r *LinodeVPCReconciler) reconcileDelete(ctx context.Context, logger logr.L
logger.Error(err, "Failed to fetch VPC")

return res, err
} else if vpc == nil {
return res, errors.New("failed to fetch VPC")
}

if vpc != nil {
Expand Down
9 changes: 6 additions & 3 deletions controller/linodevpc_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ func (r *LinodeVPCReconciler) reconcileVPC(ctx context.Context, vpcScope *scope.

return err
}

if createConfig.Label == "" {
createConfig.Label = vpcScope.LinodeVPC.Name
}

if vpcs, err := vpcScope.LinodeClient.ListVPCs(ctx, linodego.NewListOptions(1, util.CreateLinodeAPIFilter(createConfig.Label, nil))); err != nil {
vpcID := 0
if vpcScope.LinodeVPC.Spec.VPCID != nil {
vpcID = *vpcScope.LinodeVPC.Spec.VPCID
}
if vpcs, err := vpcScope.LinodeClient.ListVPCs(ctx, linodego.NewListOptions(1, util.CreateLinodeAPIFilter(vpcID, createConfig.Label, nil))); err != nil {
logger.Error(err, "Failed to list VPCs")

return err
} else if len(vpcs) != 0 {
// Labels are unique
vpcScope.LinodeVPC.Spec.VPCID = &vpcs[0].ID
vpcScope.LinodeVPC.Spec.Label = vpcs[0].Label

return nil
}
Expand Down
14 changes: 8 additions & 6 deletions util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"encoding/json"
"strconv"
"strings"

"github.com/linode/linodego"
Expand All @@ -13,14 +14,15 @@ func Pointer[T any](t T) *T {
}

// CreateLinodeAPIFilter converts variables to API filter string
func CreateLinodeAPIFilter(label string, tags []string) string {
// Prefer filter in the order: ID, Label and then tags
func CreateLinodeAPIFilter(id int, label string, tags []string) string {
filter := map[string]string{}

if label != "" {
switch {
case id != 0:
filter["id"] = strconv.Itoa(id)
case label != "":
filter["label"] = label
}

if len(tags) != 0 {
case len(tags) != 0:
filter["tags"] = strings.Join(tags, ",")
}

Expand Down

0 comments on commit fd77bbe

Please sign in to comment.