Fix selector handling in listers.go.

This commit is contained in:
Daniel Smith 2015-04-16 16:25:54 -07:00
parent 8a17ea4894
commit da5111891c
2 changed files with 70 additions and 3 deletions

View File

@ -39,10 +39,16 @@ type StoreToPodLister struct {
Store
}
// TODO Get rid of the selector because that is confusing because the user might not realize that there has already been
// some selection at the caching stage. Also, consistency will facilitate code generation. However, the pkg/client
// is inconsistent too.
// 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.
//
// TODO: converge on the interface in pkg/client.
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() {
pod := m.(*api.Pod)
if selector.Matches(labels.Set(pod.Labels)) {
@ -52,6 +58,32 @@ func (s *StoreToPodLister) List(selector labels.Selector) (pods []*api.Pod, err
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.
func (s *StoreToPodLister) Exists(pod *api.Pod) (bool, error) {
_, exists, err := s.Store.Get(pod)
@ -116,6 +148,10 @@ func (s *StoreToServiceLister) GetPodServices(pod *api.Pod) (services []api.Serv
if service.Namespace != pod.Namespace {
continue
}
if service.Spec.Selector == nil {
// services with nil selectors match nothing, not everything.
continue
}
selector = labels.Set(service.Spec.Selector).AsSelector()
if selector.Matches(labels.Set(pod.Labels)) {
services = append(services, service)

View File

@ -90,3 +90,34 @@ func TestStoreToPodLister(t *testing.T) {
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)
}
}