Skip to content

Commit

Permalink
proposal: simplify labels.Requirement creation
Browse files Browse the repository at this point in the history
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 <filario@redhat.com>
  • Loading branch information
filariow committed Jul 22, 2024
1 parent bd75cb2 commit 1538ea5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
20 changes: 5 additions & 15 deletions pkg/proxy/handlers/spacelister_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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{})
Expand Down
9 changes: 2 additions & 7 deletions pkg/proxy/handlers/spacelister_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1538ea5

Please sign in to comment.