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
da511189
Commit
da511189
authored
Apr 16, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix selector handling in listers.go.
parent
8a17ea48
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
3 deletions
+70
-3
listers.go
pkg/client/cache/listers.go
+39
-3
listers_test.go
pkg/client/cache/listers_test.go
+31
-0
No files found.
pkg/client/cache/listers.go
View file @
da511189
...
@@ -39,10 +39,16 @@ type StoreToPodLister struct {
...
@@ -39,10 +39,16 @@ type StoreToPodLister struct {
Store
Store
}
}
// TODO Get rid of the selector because that is confusing because the user might not realize that there has already been
// Please note that selector is filtering among the pods that have gotten into
// some selection at the caching stage. Also, consistency will facilitate code generation. However, the pkg/client
// the store; there may have been some filtering that already happened before
// is inconsistent too.
// that.
//
// TODO: converge on the interface in pkg/client.
func
(
s
*
StoreToPodLister
)
List
(
selector
labels
.
Selector
)
(
pods
[]
*
api
.
Pod
,
err
error
)
{
func
(
s
*
StoreToPodLister
)
List
(
selector
labels
.
Selector
)
(
pods
[]
*
api
.
Pod
,
err
error
)
{
// TODO: it'd be great to just call
// s.Pods(api.NamespaceAll).List(selector), however then we'd have to
// remake the list.Items as a []*api.Pod. So leave this separate for
// now.
for
_
,
m
:=
range
s
.
Store
.
List
()
{
for
_
,
m
:=
range
s
.
Store
.
List
()
{
pod
:=
m
.
(
*
api
.
Pod
)
pod
:=
m
.
(
*
api
.
Pod
)
if
selector
.
Matches
(
labels
.
Set
(
pod
.
Labels
))
{
if
selector
.
Matches
(
labels
.
Set
(
pod
.
Labels
))
{
...
@@ -52,6 +58,32 @@ func (s *StoreToPodLister) List(selector labels.Selector) (pods []*api.Pod, err
...
@@ -52,6 +58,32 @@ func (s *StoreToPodLister) List(selector labels.Selector) (pods []*api.Pod, err
return
pods
,
nil
return
pods
,
nil
}
}
// Pods is taking baby steps to be more like the api in pkg/client
func
(
s
*
StoreToPodLister
)
Pods
(
namespace
string
)
storePodsNamespacer
{
return
storePodsNamespacer
{
s
.
Store
,
namespace
}
}
type
storePodsNamespacer
struct
{
store
Store
namespace
string
}
// Please note that selector is filtering among the pods that have gotten into
// the store; there may have been some filtering that already happened before
// that.
func
(
s
storePodsNamespacer
)
List
(
selector
labels
.
Selector
)
(
pods
api
.
PodList
,
err
error
)
{
list
:=
api
.
PodList
{}
for
_
,
m
:=
range
s
.
store
.
List
()
{
pod
:=
m
.
(
*
api
.
Pod
)
if
s
.
namespace
==
api
.
NamespaceAll
||
s
.
namespace
==
pod
.
Namespace
{
if
selector
.
Matches
(
labels
.
Set
(
pod
.
Labels
))
{
list
.
Items
=
append
(
list
.
Items
,
*
pod
)
}
}
}
return
list
,
nil
}
// Exists returns true if a pod matching the namespace/name of the given pod exists in the store.
// Exists returns true if a pod matching the namespace/name of the given pod exists in the store.
func
(
s
*
StoreToPodLister
)
Exists
(
pod
*
api
.
Pod
)
(
bool
,
error
)
{
func
(
s
*
StoreToPodLister
)
Exists
(
pod
*
api
.
Pod
)
(
bool
,
error
)
{
_
,
exists
,
err
:=
s
.
Store
.
Get
(
pod
)
_
,
exists
,
err
:=
s
.
Store
.
Get
(
pod
)
...
@@ -116,6 +148,10 @@ func (s *StoreToServiceLister) GetPodServices(pod *api.Pod) (services []api.Serv
...
@@ -116,6 +148,10 @@ func (s *StoreToServiceLister) GetPodServices(pod *api.Pod) (services []api.Serv
if
service
.
Namespace
!=
pod
.
Namespace
{
if
service
.
Namespace
!=
pod
.
Namespace
{
continue
continue
}
}
if
service
.
Spec
.
Selector
==
nil
{
// services with nil selectors match nothing, not everything.
continue
}
selector
=
labels
.
Set
(
service
.
Spec
.
Selector
)
.
AsSelector
()
selector
=
labels
.
Set
(
service
.
Spec
.
Selector
)
.
AsSelector
()
if
selector
.
Matches
(
labels
.
Set
(
pod
.
Labels
))
{
if
selector
.
Matches
(
labels
.
Set
(
pod
.
Labels
))
{
services
=
append
(
services
,
service
)
services
=
append
(
services
,
service
)
...
...
pkg/client/cache/listers_test.go
View file @
da511189
...
@@ -90,3 +90,34 @@ func TestStoreToPodLister(t *testing.T) {
...
@@ -90,3 +90,34 @@ func TestStoreToPodLister(t *testing.T) {
t
.
Errorf
(
"Unexpected pod exists"
)
t
.
Errorf
(
"Unexpected pod exists"
)
}
}
}
}
func
TestStoreToServiceLister
(
t
*
testing
.
T
)
{
store
:=
NewStore
(
MetaNamespaceKeyFunc
)
store
.
Add
(
&
api
.
Service
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{},
},
})
store
.
Add
(
&
api
.
Service
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
}})
ssl
:=
StoreToServiceLister
{
store
}
pod
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foopod"
,
Labels
:
map
[
string
]
string
{
"role"
:
"foo"
},
},
}
services
,
err
:=
ssl
.
GetPodServices
(
pod
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
if
len
(
services
)
!=
1
{
t
.
Fatalf
(
"Expected 1 service, got %v"
,
len
(
services
))
}
if
e
,
a
:=
"foo"
,
services
[
0
]
.
Name
;
e
!=
a
{
t
.
Errorf
(
"Expected service %q, got %q"
,
e
,
a
)
}
}
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