Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
1ea2c3ba
Commit
1ea2c3ba
authored
Apr 15, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6754 from wojtek-t/list_with_single_element
Support List() with single-matchers
parents
6cfce2c7
6feaf8ee
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
5 deletions
+66
-5
etcd.go
pkg/registry/generic/etcd/etcd.go
+14
-3
etcd_test.go
pkg/registry/generic/etcd/etcd_test.go
+16
-2
etcd_test.go
pkg/registry/pod/etcd/etcd_test.go
+9
-0
etcd_helper.go
pkg/tools/etcd_helper.go
+27
-0
No files found.
pkg/registry/generic/etcd/etcd.go
View file @
1ea2c3ba
...
@@ -145,9 +145,20 @@ func (e *Etcd) List(ctx api.Context, label labels.Selector, field fields.Selecto
...
@@ -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
))
}
}
...
...
pkg/registry/generic/etcd/etcd_test.go
View file @
1ea2c3ba
...
@@ -126,6 +126,11 @@ func TestEtcdList(t *testing.T) {
...
@@ -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) {
...
@@ -174,7 +179,7 @@ func TestEtcdList(t *testing.T) {
},
},
"normalFiltered"
:
{
"normalFiltered"
:
{
in
:
tools
.
EtcdResponseWithError
{
in
:
tools
.
EtcdResponseWithError
{
R
:
normal
ListResp
,
R
:
singleElem
ListResp
,
E
:
nil
,
E
:
nil
,
},
},
m
:
setMatcher
{
util
.
NewStringSet
(
"foo"
)},
m
:
setMatcher
{
util
.
NewStringSet
(
"foo"
)},
...
@@ -194,7 +199,16 @@ func TestEtcdList(t *testing.T) {
...
@@ -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
)
...
...
pkg/registry/pod/etcd/etcd_test.go
View file @
1ea2c3ba
...
@@ -280,6 +280,15 @@ func TestListPodListSelection(t *testing.T) {
...
@@ -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
()
...
...
pkg/tools/etcd_helper.go
View file @
1ea2c3ba
...
@@ -151,6 +151,33 @@ func (h *EtcdHelper) ExtractToList(key string, listObj runtime.Object) error {
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment