mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 14:14:39 +00:00
Adding fields selector
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
kubeerr "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||
etcderr "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors/etcd"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
@@ -72,7 +73,7 @@ type Etcd struct {
|
||||
TTLFunc func(obj runtime.Object, update bool) (uint64, error)
|
||||
|
||||
// Returns a matcher corresponding to the provided labels and fields.
|
||||
PredicateFunc func(label, field labels.Selector) generic.Matcher
|
||||
PredicateFunc func(label labels.Selector, field fields.Selector) generic.Matcher
|
||||
|
||||
// Called on all objects returned from the underlying store, after
|
||||
// the exit hooks are invoked. Decorators are intended for integrations
|
||||
@@ -134,7 +135,7 @@ func (e *Etcd) NewList() runtime.Object {
|
||||
}
|
||||
|
||||
// List returns a list of items matching labels and field
|
||||
func (e *Etcd) List(ctx api.Context, label, field labels.Selector) (runtime.Object, error) {
|
||||
func (e *Etcd) List(ctx api.Context, label labels.Selector, field fields.Selector) (runtime.Object, error) {
|
||||
return e.ListPredicate(ctx, e.PredicateFunc(label, field))
|
||||
}
|
||||
|
||||
@@ -358,7 +359,7 @@ func (e *Etcd) Delete(ctx api.Context, name string) (runtime.Object, error) {
|
||||
|
||||
// WatchPredicate starts a watch for the items that m matches.
|
||||
// TODO: Detect if m references a single object instead of a list.
|
||||
func (e *Etcd) Watch(ctx api.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
func (e *Etcd) Watch(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
return e.WatchPredicate(ctx, e.PredicateFunc(label, field), resourceVersion)
|
||||
}
|
||||
|
||||
|
@@ -18,19 +18,20 @@ package generic
|
||||
|
||||
import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
// AttrFunc returns label and field sets for List or Watch to compare against, or an error.
|
||||
type AttrFunc func(obj runtime.Object) (label, field labels.Set, err error)
|
||||
type AttrFunc func(obj runtime.Object) (label labels.Set, field fields.Set, err error)
|
||||
|
||||
// SelectionPredicate implements a generic predicate that can be passed to
|
||||
// GenericRegistry's List or Watch methods. Implements the Matcher interface.
|
||||
type SelectionPredicate struct {
|
||||
Label labels.Selector
|
||||
Field labels.Selector
|
||||
Field fields.Selector
|
||||
GetAttrs AttrFunc
|
||||
}
|
||||
|
||||
|
@@ -21,6 +21,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
)
|
||||
@@ -39,7 +40,8 @@ func (*IgnoredList) IsAnAPIObject() {}
|
||||
func TestSelectionPredicate(t *testing.T) {
|
||||
table := map[string]struct {
|
||||
labelSelector, fieldSelector string
|
||||
labels, fields labels.Set
|
||||
labels labels.Set
|
||||
fields fields.Set
|
||||
err error
|
||||
shouldMatch bool
|
||||
}{
|
||||
@@ -47,21 +49,21 @@ func TestSelectionPredicate(t *testing.T) {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
labels: labels.Set{"name": "foo"},
|
||||
fields: labels.Set{"uid": "12345"},
|
||||
fields: fields.Set{"uid": "12345"},
|
||||
shouldMatch: true,
|
||||
},
|
||||
"B": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
labels: labels.Set{"name": "foo"},
|
||||
fields: labels.Set{},
|
||||
fields: fields.Set{},
|
||||
shouldMatch: false,
|
||||
},
|
||||
"C": {
|
||||
labelSelector: "name=foo",
|
||||
fieldSelector: "uid=12345",
|
||||
labels: labels.Set{},
|
||||
fields: labels.Set{"uid": "12345"},
|
||||
fields: fields.Set{"uid": "12345"},
|
||||
shouldMatch: false,
|
||||
},
|
||||
"error": {
|
||||
@@ -77,14 +79,14 @@ func TestSelectionPredicate(t *testing.T) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
parsedField, err := labels.ParseSelector(item.fieldSelector)
|
||||
parsedField, err := fields.ParseSelector(item.fieldSelector)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sp := &SelectionPredicate{
|
||||
Label: parsedLabel,
|
||||
Field: parsedField,
|
||||
GetAttrs: func(runtime.Object) (label, field labels.Set, err error) {
|
||||
GetAttrs: func(runtime.Object) (label labels.Set, field fields.Set, err error) {
|
||||
return item.labels, item.fields, item.err
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user