From fd77bbe898975df9217199962602e38c27ee503f Mon Sep 17 00:00:00 2001 From: Evan Johnson Date: Wed, 6 Mar 2024 09:44:54 -0500 Subject: [PATCH] support byovpc and clean up list filtering --- cloud/services/loadbalancers.go | 11 ++--------- cloud/services/object_storage_buckets.go | 4 +++- controller/linodemachine_controller.go | 2 +- controller/linodevpc_controller.go | 10 ++++------ controller/linodevpc_controller_helpers.go | 9 ++++++--- util/helpers.go | 14 ++++++++------ 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/cloud/services/loadbalancers.go b/cloud/services/loadbalancers.go index 4a5cdb012..224bcaa60 100644 --- a/cloud/services/loadbalancers.go +++ b/cloud/services/loadbalancers.go @@ -2,7 +2,6 @@ package services import ( "context" - "encoding/json" "errors" "fmt" "net/http" @@ -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 diff --git a/cloud/services/object_storage_buckets.go b/cloud/services/object_storage_buckets.go index ca94a5fc7..3eebb6617 100644 --- a/cloud/services/object_storage_buckets.go +++ b/cloud/services/object_storage_buckets.go @@ -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 { diff --git a/controller/linodemachine_controller.go b/controller/linodemachine_controller.go index cdbb97b7f..16afa8c15 100644 --- a/controller/linodemachine_controller.go +++ b/controller/linodemachine_controller.go @@ -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") diff --git a/controller/linodevpc_controller.go b/controller/linodevpc_controller.go index 771c95ae5..df77123c1 100644 --- a/controller/linodevpc_controller.go +++ b/controller/linodevpc_controller.go @@ -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. @@ -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 { diff --git a/controller/linodevpc_controller_helpers.go b/controller/linodevpc_controller_helpers.go index 2c168bb85..caa358547 100644 --- a/controller/linodevpc_controller_helpers.go +++ b/controller/linodevpc_controller_helpers.go @@ -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 } diff --git a/util/helpers.go b/util/helpers.go index 86f472dd2..908070903 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -2,6 +2,7 @@ package util import ( "encoding/json" + "strconv" "strings" "github.com/linode/linodego" @@ -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, ",") }