Skip to content

Commit

Permalink
Merge pull request kubernetes#12869 from vishh/daemon-registry-client
Browse files Browse the repository at this point in the history
Daemon registry client
  • Loading branch information
saad-ali committed Aug 21, 2015
2 parents cd798a4 + ec22c2d commit 264a658
Show file tree
Hide file tree
Showing 14 changed files with 1,441 additions and 3 deletions.
52 changes: 52 additions & 0 deletions pkg/client/unversioned/cache/listers.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,58 @@ func (s *StoreToReplicationControllerLister) GetPodControllers(pod *api.Pod) (co
return
}

// StoreToDaemonLister gives a store List and Exists methods. The store must contain only Daemons.
type StoreToDaemonLister struct {
Store
}

// Exists checks if the given dc exists in the store.
func (s *StoreToDaemonLister) Exists(daemon *api.Daemon) (bool, error) {
_, exists, err := s.Store.Get(daemon)
if err != nil {
return false, err
}
return exists, nil
}

// StoreToDaemonLister lists all daemons in the store.
// TODO: converge on the interface in pkg/client
func (s *StoreToDaemonLister) List() (daemons []api.Daemon, err error) {
for _, c := range s.Store.List() {
daemons = append(daemons, *(c.(*api.Daemon)))
}
return daemons, nil
}

// GetPodDaemons returns a list of daemons managing a pod. Returns an error iff no matching daemons are found.
func (s *StoreToDaemonLister) GetPodDaemons(pod *api.Pod) (daemons []api.Daemon, err error) {
var selector labels.Selector
var daemonController api.Daemon

if len(pod.Labels) == 0 {
err = fmt.Errorf("No daemons found for pod %v because it has no labels", pod.Name)
return
}

for _, m := range s.Store.List() {
daemonController = *m.(*api.Daemon)
if daemonController.Namespace != pod.Namespace {
continue
}
selector = labels.Set(daemonController.Spec.Selector).AsSelector()

// If a daemonController with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
continue
}
daemons = append(daemons, daemonController)
}
if len(daemons) == 0 {
err = fmt.Errorf("Could not find daemons for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
}
return
}

// StoreToServiceLister makes a Store that has the List method of the client.ServiceInterface
// The Store must contain (only) Services.
type StoreToServiceLister struct {
Expand Down
124 changes: 123 additions & 1 deletion pkg/client/unversioned/cache/listers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestStoreToReplicationControllerLister(t *testing.T) {
},
outRCNames: util.NewStringSet("basic"),
},
// No pod lables
// No pod labels
{
inRCs: []*api.ReplicationController{
{
Expand Down Expand Up @@ -155,6 +155,128 @@ func TestStoreToReplicationControllerLister(t *testing.T) {
}
}

func TestStoreToDaemonLister(t *testing.T) {
store := NewStore(MetaNamespaceKeyFunc)
lister := StoreToDaemonLister{store}
testCases := []struct {
inDCs []*api.Daemon
list func() ([]api.Daemon, error)
outDCNames util.StringSet
expectErr bool
}{
// Basic listing
{
inDCs: []*api.Daemon{
{ObjectMeta: api.ObjectMeta{Name: "basic"}},
},
list: func() ([]api.Daemon, error) {
return lister.List()
},
outDCNames: util.NewStringSet("basic"),
},
// Listing multiple controllers
{
inDCs: []*api.Daemon{
{ObjectMeta: api.ObjectMeta{Name: "basic"}},
{ObjectMeta: api.ObjectMeta{Name: "complex"}},
{ObjectMeta: api.ObjectMeta{Name: "complex2"}},
},
list: func() ([]api.Daemon, error) {
return lister.List()
},
outDCNames: util.NewStringSet("basic", "complex", "complex2"),
},
// No pod labels
{
inDCs: []*api.Daemon{
{
ObjectMeta: api.ObjectMeta{Name: "basic", Namespace: "ns"},
Spec: api.DaemonSpec{
Selector: map[string]string{"foo": "baz"},
},
},
},
list: func() ([]api.Daemon, error) {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{Name: "pod1", Namespace: "ns"},
}
return lister.GetPodDaemons(pod)
},
outDCNames: util.NewStringSet(),
expectErr: true,
},
// No RC selectors
{
inDCs: []*api.Daemon{
{
ObjectMeta: api.ObjectMeta{Name: "basic", Namespace: "ns"},
},
},
list: func() ([]api.Daemon, error) {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "pod1",
Namespace: "ns",
Labels: map[string]string{"foo": "bar"},
},
}
return lister.GetPodDaemons(pod)
},
outDCNames: util.NewStringSet(),
expectErr: true,
},
// Matching labels to selectors and namespace
{
inDCs: []*api.Daemon{
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: api.DaemonSpec{
Selector: map[string]string{"foo": "bar"},
},
},
{
ObjectMeta: api.ObjectMeta{Name: "bar", Namespace: "ns"},
Spec: api.DaemonSpec{
Selector: map[string]string{"foo": "bar"},
},
},
},
list: func() ([]api.Daemon, error) {
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "pod1",
Labels: map[string]string{"foo": "bar"},
Namespace: "ns",
},
}
return lister.GetPodDaemons(pod)
},
outDCNames: util.NewStringSet("bar"),
},
}
for _, c := range testCases {
for _, r := range c.inDCs {
store.Add(r)
}

gotControllers, err := c.list()
if err != nil && c.expectErr {
continue
} else if c.expectErr {
t.Fatalf("Expected error, got none")
} else if err != nil {
t.Fatalf("Unexpected error %#v", err)
}
gotNames := make([]string, len(gotControllers))
for ix := range gotControllers {
gotNames[ix] = gotControllers[ix].Name
}
if !c.outDCNames.HasAll(gotNames...) || len(gotNames) != len(c.outDCNames) {
t.Errorf("Unexpected got controllers %+v expected %+v", gotNames, c.outDCNames)
}
}
}

func TestStoreToPodLister(t *testing.T) {
store := NewStore(MetaNamespaceKeyFunc)
ids := []string{"foo", "bar", "baz"}
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/unversioned/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Interface interface {
PodsNamespacer
PodTemplatesNamespacer
ReplicationControllersNamespacer
DaemonsNamespacer
ServicesNamespacer
EndpointsNamespacer
VersionInterface
Expand All @@ -52,6 +53,10 @@ func (c *Client) ReplicationControllers(namespace string) ReplicationControllerI
return newReplicationControllers(c, namespace)
}

func (c *Client) Daemons(namespace string) DaemonInterface {
return newDaemons(c, namespace)
}

func (c *Client) Nodes() NodeInterface {
return newNodes(c)
}
Expand Down
95 changes: 95 additions & 0 deletions pkg/client/unversioned/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package unversioned

import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/watch"
)

// DaemonsNamespacer has methods to work with Daemon resources in a namespace
type DaemonsNamespacer interface {
Daemons(namespace string) DaemonInterface
}

type DaemonInterface interface {
List(selector labels.Selector) (*api.DaemonList, error)
Get(name string) (*api.Daemon, error)
Create(ctrl *api.Daemon) (*api.Daemon, error)
Update(ctrl *api.Daemon) (*api.Daemon, error)
Delete(name string) error
Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error)
}

// daemons implements DaemonsNamespacer interface
type daemons struct {
r *Client
ns string
}

func newDaemons(c *Client, namespace string) *daemons {
return &daemons{c, namespace}
}

// Ensure statically that daemons implements DaemonInterface.
var _ DaemonInterface = &daemons{}

func (c *daemons) List(selector labels.Selector) (result *api.DaemonList, err error) {
result = &api.DaemonList{}
err = c.r.Get().Namespace(c.ns).Resource("daemons").LabelsSelectorParam(selector).Do().Into(result)
return
}

// Get returns information about a particular daemon.
func (c *daemons) Get(name string) (result *api.Daemon, err error) {
result = &api.Daemon{}
err = c.r.Get().Namespace(c.ns).Resource("daemons").Name(name).Do().Into(result)
return
}

// Create creates a new daemon.
func (c *daemons) Create(daemon *api.Daemon) (result *api.Daemon, err error) {
result = &api.Daemon{}
err = c.r.Post().Namespace(c.ns).Resource("daemons").Body(daemon).Do().Into(result)
return
}

// Update updates an existing daemon.
func (c *daemons) Update(daemon *api.Daemon) (result *api.Daemon, err error) {
result = &api.Daemon{}
err = c.r.Put().Namespace(c.ns).Resource("daemons").Name(daemon.Name).Body(daemon).Do().Into(result)
return
}

// Delete deletes an existing daemon.
func (c *daemons) Delete(name string) error {
return c.r.Delete().Namespace(c.ns).Resource("daemons").Name(name).Do().Error()
}

// Watch returns a watch.Interface that watches the requested daemons.
func (c *daemons) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
return c.r.Get().
Prefix("watch").
Namespace(c.ns).
Resource("daemons").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}
Loading

0 comments on commit 264a658

Please sign in to comment.