mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +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.
|
||||
func (e *Etcd) ListPredicate(ctx api.Context, m generic.Matcher) (runtime.Object, error) {
|
||||
list := e.NewListFunc()
|
||||
err := e.Helper.ExtractToList(e.KeyRootFunc(ctx), list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if name, ok := m.MatchesSingle(); ok {
|
||||
key, err := e.KeyFunc(ctx, name)
|
||||
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))
|
||||
}
|
||||
|
@ -126,6 +126,11 @@ func TestEtcdList(t *testing.T) {
|
||||
Spec: api.PodSpec{Host: "machine"},
|
||||
}
|
||||
|
||||
singleElemListResp := &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), podA),
|
||||
},
|
||||
}
|
||||
normalListResp := &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Nodes: []*etcd.Node{
|
||||
@ -174,7 +179,7 @@ func TestEtcdList(t *testing.T) {
|
||||
},
|
||||
"normalFiltered": {
|
||||
in: tools.EtcdResponseWithError{
|
||||
R: normalListResp,
|
||||
R: singleElemListResp,
|
||||
E: nil,
|
||||
},
|
||||
m: setMatcher{util.NewStringSet("foo")},
|
||||
@ -194,7 +199,16 @@ func TestEtcdList(t *testing.T) {
|
||||
|
||||
for name, item := range table {
|
||||
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)
|
||||
if e, a := item.succeed, err == nil; 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
|
||||
|
||||
ctx := api.NewDefaultContext()
|
||||
|
@ -151,6 +151,33 @@ func (h *EtcdHelper) ExtractToList(key string, listObj runtime.Object) error {
|
||||
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
|
||||
// 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.
|
||||
|
Loading…
Reference in New Issue
Block a user