Skip to content

Commit

Permalink
only use labelSelector if not nil
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshu committed May 22, 2024
1 parent b89d976 commit a04fd40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/api/v1/clients/memory/resource_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,18 @@ func (rc *ResourceClient) List(namespace string, opts clients.ListOpts) (resourc
opts = opts.WithDefaults()
cachedResources := rc.cache.List(rc.Prefix(namespace))

labelSelector, err := kubeutils.ToLabelSelector(opts)
if err != nil {
return nil, errors.Wrapf(err, "parsing label selector")
var labelSelector labels.Selector
var err error
if kubeutils.HasSelector(opts) {
labelSelector, err = kubeutils.ToLabelSelector(opts)
if err != nil {
return nil, errors.Wrapf(err, "parsing label selector")
}
}

var resourceList resources.ResourceList
for _, resource := range cachedResources {
if labelSelector.Matches(labels.Set(resource.GetMetadata().Labels)) {
if labelSelector == nil || labelSelector.Matches(labels.Set(resource.GetMetadata().Labels)) {
clone := resources.Clone(resource)
resourceList = append(resourceList, clone)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/kubeutils/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
kubetypes "k8s.io/apimachinery/pkg/types"
)

// HasSelector returns true if the ListOpts contains a selector (which may be either an equality-based selector
// or set-based expression selector).
func HasSelector(listOpts clients.ListOpts) bool {
return listOpts.ExpressionSelector != "" || len(listOpts.Selector) > 0
}

// ToLabelSelector converts the selector specified by the ListOpts into an apimachinery label selector.
// If both ExpressionSelector and Selector are specified in the opts, only ExpressionSelector is used.
func ToLabelSelector(listOpts clients.ListOpts) (labels.Selector, error) {
Expand Down

0 comments on commit a04fd40

Please sign in to comment.