mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Support List() single-matchers
This commit is contained in:
parent
0ea87e4864
commit
6feaf8ee4f
@ -145,9 +145,20 @@ func (e *Etcd) List(ctx api.Context, label labels.Selector, field fields.Selecto
|
|||||||
// ListPredicate returns a list of all the items matching m.
|
// ListPredicate returns a list of all the items matching m.
|
||||||
func (e *Etcd) ListPredicate(ctx api.Context, m generic.Matcher) (runtime.Object, error) {
|
func (e *Etcd) ListPredicate(ctx api.Context, m generic.Matcher) (runtime.Object, error) {
|
||||||
list := e.NewListFunc()
|
list := e.NewListFunc()
|
||||||
err := e.Helper.ExtractToList(e.KeyRootFunc(ctx), list)
|
if name, ok := m.MatchesSingle(); ok {
|
||||||
if err != nil {
|
key, err := e.KeyFunc(ctx, name)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = e.Helper.ExtractObjToList(key, list)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := e.Helper.ExtractToList(e.KeyRootFunc(ctx), list)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return generic.FilterList(list, m, generic.DecoratorFunc(e.Decorator))
|
return generic.FilterList(list, m, generic.DecoratorFunc(e.Decorator))
|
||||||
}
|
}
|
||||||
|
@ -126,6 +126,11 @@ func TestEtcdList(t *testing.T) {
|
|||||||
Spec: api.PodSpec{Host: "machine"},
|
Spec: api.PodSpec{Host: "machine"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
singleElemListResp := &etcd.Response{
|
||||||
|
Node: &etcd.Node{
|
||||||
|
Value: runtime.EncodeOrDie(testapi.Codec(), podA),
|
||||||
|
},
|
||||||
|
}
|
||||||
normalListResp := &etcd.Response{
|
normalListResp := &etcd.Response{
|
||||||
Node: &etcd.Node{
|
Node: &etcd.Node{
|
||||||
Nodes: []*etcd.Node{
|
Nodes: []*etcd.Node{
|
||||||
@ -174,7 +179,7 @@ func TestEtcdList(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"normalFiltered": {
|
"normalFiltered": {
|
||||||
in: tools.EtcdResponseWithError{
|
in: tools.EtcdResponseWithError{
|
||||||
R: normalListResp,
|
R: singleElemListResp,
|
||||||
E: nil,
|
E: nil,
|
||||||
},
|
},
|
||||||
m: setMatcher{util.NewStringSet("foo")},
|
m: setMatcher{util.NewStringSet("foo")},
|
||||||
@ -194,7 +199,16 @@ func TestEtcdList(t *testing.T) {
|
|||||||
|
|
||||||
for name, item := range table {
|
for name, item := range table {
|
||||||
fakeClient, registry := NewTestGenericEtcdRegistry(t)
|
fakeClient, registry := NewTestGenericEtcdRegistry(t)
|
||||||
fakeClient.Data[registry.KeyRootFunc(api.NewContext())] = item.in
|
if name, ok := item.m.MatchesSingle(); ok {
|
||||||
|
key, err := registry.KeyFunc(api.NewContext(), name)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Couldn't create key for %v", name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fakeClient.Data[key] = item.in
|
||||||
|
} else {
|
||||||
|
fakeClient.Data[registry.KeyRootFunc(api.NewContext())] = item.in
|
||||||
|
}
|
||||||
list, err := registry.ListPredicate(api.NewContext(), item.m)
|
list, err := registry.ListPredicate(api.NewContext(), item.m)
|
||||||
if e, a := item.succeed, err == nil; e != a {
|
if e, a := item.succeed, err == nil; e != a {
|
||||||
t.Errorf("%v: expected %v, got %v", name, e, a)
|
t.Errorf("%v: expected %v, got %v", name, e, a)
|
||||||
|
@ -280,6 +280,15 @@ func TestListPodListSelection(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fakeEtcdClient.Data["/registry/pods/default/zot"] = tools.EtcdResponseWithError{
|
||||||
|
R: &etcd.Response{
|
||||||
|
Node: &etcd.Node{
|
||||||
|
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{Name: "zot"},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
storage := NewStorage(helper, nil).Pod
|
storage := NewStorage(helper, nil).Pod
|
||||||
|
|
||||||
ctx := api.NewDefaultContext()
|
ctx := api.NewDefaultContext()
|
||||||
|
@ -151,6 +151,33 @@ func (h *EtcdHelper) ExtractToList(key string, listObj runtime.Object) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExtractObjToList unmarshals json found at key and opaques it into a *List api object
|
||||||
|
// (an object that satisfies the runtime.IsList definition).
|
||||||
|
func (h *EtcdHelper) ExtractObjToList(key string, listObj runtime.Object) error {
|
||||||
|
listPtr, err := runtime.GetItemsPtr(listObj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := h.Client.Get(key, false, false)
|
||||||
|
if err != nil && !IsEtcdNotFound(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes := make([]*etcd.Node, 0)
|
||||||
|
nodes = append(nodes, response.Node)
|
||||||
|
|
||||||
|
if err := h.decodeNodeList(nodes, listPtr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if h.Versioner != nil {
|
||||||
|
if err := h.Versioner.UpdateList(listObj, response.EtcdIndex); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ExtractObj unmarshals json found at key into objPtr. On a not found error, will either return
|
// ExtractObj unmarshals json found at key into objPtr. On a not found error, will either return
|
||||||
// a zero object of the requested type, or an error, depending on ignoreNotFound. Treats
|
// a zero object of the requested type, or an error, depending on ignoreNotFound. Treats
|
||||||
// empty responses and nil response nodes exactly like a not found error.
|
// empty responses and nil response nodes exactly like a not found error.
|
||||||
|
Loading…
Reference in New Issue
Block a user