From 1538ea5d73aee03d56e480421e6092a2a81614bf Mon Sep 17 00:00:00 2001 From: Francesco Ilario Date: Mon, 22 Jul 2024 19:35:45 +0200 Subject: [PATCH] proposal: simplify labels.Requirement creation We can skip validations performed by [labels.NewRequirement](https://pkg.go.dev/k8s.io/apimachinery/pkg/labels#NewRequirement) as operator is fixed, key is constant, and value is extracted from Custom Resources. [SelectorFromSet](https://pkg.go.dev/k8s.io/apimachinery/pkg/labels#SelectorFromSet) does not perform any validation, which means the server will reject the request if the Set contains invalid values. Signed-off-by: Francesco Ilario --- pkg/proxy/handlers/spacelister_get.go | 20 +++++--------------- pkg/proxy/handlers/spacelister_list.go | 9 ++------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/pkg/proxy/handlers/spacelister_get.go b/pkg/proxy/handlers/spacelister_get.go index 53b98930..0572f063 100644 --- a/pkg/proxy/handlers/spacelister_get.go +++ b/pkg/proxy/handlers/spacelister_get.go @@ -19,7 +19,6 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/selection" runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -56,15 +55,9 @@ func GetUserWorkspace(ctx echo.Context, spaceLister *SpaceLister, workspaceName // recursively get all the spacebindings for the current workspace listSpaceBindingsFunc := func(spaceName string) ([]toolchainv1alpha1.SpaceBinding, error) { - spaceSelector, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingSpaceLabelKey, selection.Equals, []string{spaceName}) - if err != nil { - return nil, err - } - murSelector, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey, selection.Equals, []string{userSignup.CompliantUsername}) - if err != nil { - return nil, err - } - return spaceLister.GetInformerServiceFunc().ListSpaceBindings(*spaceSelector, *murSelector) + spaceSelector, _ := labels.SelectorFromSet(labels.Set{toolchainv1alpha1.SpaceBindingSpaceLabelKey: spaceName}).Requirements() + murSelector, _ := labels.SelectorFromSet(labels.Set{toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey: userSignup.CompliantUsername}).Requirements() + return spaceLister.GetInformerServiceFunc().ListSpaceBindings(spaceSelector[0], murSelector[0]) } spaceBindingLister := spacebinding.NewLister(listSpaceBindingsFunc, spaceLister.GetInformerServiceFunc().GetSpace) userSpaceBindings, err := spaceBindingLister.ListForSpace(space, []toolchainv1alpha1.SpaceBinding{}) @@ -100,11 +93,8 @@ func GetUserWorkspaceWithBindings(ctx echo.Context, spaceLister *SpaceLister, wo // recursively get all the spacebindings for the current workspace listSpaceBindingsFunc := func(spaceName string) ([]toolchainv1alpha1.SpaceBinding, error) { - spaceSelector, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingSpaceLabelKey, selection.Equals, []string{spaceName}) - if err != nil { - return nil, err - } - return spaceLister.GetInformerServiceFunc().ListSpaceBindings(*spaceSelector) + ss, _ := labels.SelectorFromSet(labels.Set{toolchainv1alpha1.SpaceBindingSpaceLabelKey: spaceName}).Requirements() + return spaceLister.GetInformerServiceFunc().ListSpaceBindings(ss[0]) } spaceBindingLister := spacebinding.NewLister(listSpaceBindingsFunc, spaceLister.GetInformerServiceFunc().GetSpace) allSpaceBindings, err := spaceBindingLister.ListForSpace(space, []toolchainv1alpha1.SpaceBinding{}) diff --git a/pkg/proxy/handlers/spacelister_list.go b/pkg/proxy/handlers/spacelister_list.go index bc2af8fe..01eb9c90 100644 --- a/pkg/proxy/handlers/spacelister_list.go +++ b/pkg/proxy/handlers/spacelister_list.go @@ -14,7 +14,6 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/selection" ) func HandleSpaceListRequest(spaceLister *SpaceLister) echo.HandlerFunc { @@ -68,12 +67,8 @@ func listWorkspaceResponse(ctx echo.Context, workspaces []toolchainv1alpha1.Work } func listSpaceBindingsForUser(spaceLister *SpaceLister, murName string) ([]toolchainv1alpha1.SpaceBinding, error) { - murSelector, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey, selection.Equals, []string{murName}) - if err != nil { - return nil, err - } - requirements := []labels.Requirement{*murSelector} - return spaceLister.GetInformerServiceFunc().ListSpaceBindings(requirements...) + mrr, _ := labels.SelectorFromSet(labels.Set{toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey: murName}).Requirements() + return spaceLister.GetInformerServiceFunc().ListSpaceBindings(mrr[0]) } func workspacesFromSpaceBindings(ctx echo.Context, spaceLister *SpaceLister, signupName string, spaceBindings []toolchainv1alpha1.SpaceBinding) []toolchainv1alpha1.Workspace {