Add client code

pull/6/head
Ananya Kumar 2015-08-06 23:29:31 -07:00 committed by Vishnu Kannan
parent b0679f18bc
commit 4a148f99d6
11 changed files with 515 additions and 3 deletions

92
pkg/client/daemon.go Normal file
View File

@ -0,0 +1,92 @@
/*
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 client
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}
}
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()
}

159
pkg/client/daemon_test.go Normal file
View File

@ -0,0 +1,159 @@
/*
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 client
import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/labels"
)
func getDCResourceName() string {
return "daemons"
}
func TestListDaemons(t *testing.T) {
ns := api.NamespaceAll
c := &testClient{
Request: testRequest{
Method: "GET",
Path: testapi.ResourcePath(getDCResourceName(), ns, ""),
},
Response: Response{StatusCode: 200,
Body: &api.DaemonList{
Items: []api.Daemon{
{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.DaemonSpec{
Template: &api.PodTemplateSpec{},
},
},
},
},
},
}
receivedControllerList, err := c.Setup().Daemons(ns).List(labels.Everything())
c.Validate(t, receivedControllerList, err)
}
func TestGetDaemon(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "GET", Path: testapi.ResourcePath(getDCResourceName(), ns, "foo"), Query: buildQueryValues(nil)},
Response: Response{
StatusCode: 200,
Body: &api.Daemon{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.DaemonSpec{
Template: &api.PodTemplateSpec{},
},
},
},
}
receivedController, err := c.Setup().Daemons(ns).Get("foo")
c.Validate(t, receivedController, err)
}
func TestGetDaemonWithNoName(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{Error: true}
receivedPod, err := c.Setup().Daemons(ns).Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
}
func TestUpdateDaemon(t *testing.T) {
ns := api.NamespaceDefault
requestController := &api.Daemon{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"},
}
c := &testClient{
Request: testRequest{Method: "PUT", Path: testapi.ResourcePath(getDCResourceName(), ns, "foo"), Query: buildQueryValues(nil)},
Response: Response{
StatusCode: 200,
Body: &api.Daemon{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.DaemonSpec{
Template: &api.PodTemplateSpec{},
},
},
},
}
receivedController, err := c.Setup().Daemons(ns).Update(requestController)
c.Validate(t, receivedController, err)
}
func TestDeleteDaemon(t *testing.T) {
ns := api.NamespaceDefault
c := &testClient{
Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getDCResourceName(), ns, "foo"), Query: buildQueryValues(nil)},
Response: Response{StatusCode: 200},
}
err := c.Setup().Daemons(ns).Delete("foo")
c.Validate(t, nil, err)
}
func TestCreateDaemon(t *testing.T) {
ns := api.NamespaceDefault
requestController := &api.Daemon{
ObjectMeta: api.ObjectMeta{Name: "foo"},
}
c := &testClient{
Request: testRequest{Method: "POST", Path: testapi.ResourcePath(getDCResourceName(), ns, ""), Body: requestController, Query: buildQueryValues(nil)},
Response: Response{
StatusCode: 200,
Body: &api.Daemon{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
Spec: api.DaemonSpec{
Template: &api.PodTemplateSpec{},
},
},
},
}
receivedController, err := c.Setup().Daemons(ns).Create(requestController)
c.Validate(t, receivedController, err)
}

View File

@ -0,0 +1,70 @@
/*
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 testclient
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/watch"
)
// FakeDaemons implements DaemonInterface. Meant to be embedded into a struct to get a default
// implementation. This makes faking out just the method you want to test easier.
type FakeDaemons struct {
Fake *Fake
Namespace string
}
const (
GetDaemonAction = "get-daemon"
UpdateDaemonAction = "update-daemon"
WatchDaemonAction = "watch-daemon"
DeleteDaemonAction = "delete-daemon"
ListDaemonAction = "list-daemons"
CreateDaemonAction = "create-daemon"
)
func (c *FakeDaemons) Get(name string) (*api.Daemon, error) {
obj, err := c.Fake.Invokes(NewGetAction("daemons", c.Namespace, name), &api.Daemon{})
return obj.(*api.Daemon), err
}
func (c *FakeDaemons) List(label labels.Selector) (*api.DaemonList, error) {
obj, err := c.Fake.Invokes(NewListAction("daemons", c.Namespace, label, nil), &api.DaemonList{})
return obj.(*api.DaemonList), err
}
func (c *FakeDaemons) Create(daemon *api.Daemon) (*api.Daemon, error) {
obj, err := c.Fake.Invokes(NewCreateAction("daemons", c.Namespace, daemon), &api.Daemon{})
return obj.(*api.Daemon), err
}
func (c *FakeDaemons) Update(daemon *api.Daemon) (*api.Daemon, error) {
obj, err := c.Fake.Invokes(NewUpdateAction("daemons", c.Namespace, daemon), &api.Daemon{})
return obj.(*api.Daemon), err
}
func (c *FakeDaemons) Delete(name string) error {
_, err := c.Fake.Invokes(NewDeleteAction("daemons", c.Namespace, name), &api.Daemon{})
return err
}
func (c *FakeDaemons) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
c.Fake.Invokes(NewWatchAction("daemons", c.Namespace, label, field, resourceVersion), nil)
return c.Fake.Watch, nil
}

View File

@ -225,6 +225,59 @@ 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
}
// GetPodDaemon returns a list of daemon daemons managing a pod. Returns an error iff no matching daemons are found.
func (s *StoreToDaemonLister) GetPodDaemon(pod *api.Pod) (daemons []api.Daemon, err error) {
var selector labels.Selector
var dc 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() {
dc = *m.(*api.Daemon)
if dc.Namespace != pod.Namespace {
continue
}
labelSet := labels.Set(dc.Spec.Selector)
selector = labels.Set(dc.Spec.Selector).AsSelector()
// If an dc with a nil or empty selector creeps in, it should match nothing, not everything.
if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) {
continue
}
daemons = append(daemons, dc)
}
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 {

View File

@ -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 lables
{
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.GetPodDaemon(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.GetPodDaemon(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.GetPodDaemon(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"}

View File

@ -33,6 +33,7 @@ type Interface interface {
PodsNamespacer
PodTemplatesNamespacer
ReplicationControllersNamespacer
DaemonsNamespacer
ServicesNamespacer
EndpointsNamespacer
VersionInterface
@ -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)
}

View File

@ -17,7 +17,7 @@ limitations under the License.
/*
Package client contains the implementation of the client side communication with the
Kubernetes master. The Client class provides methods for reading, creating, updating,
and deleting pods, replication controllers, services, and minions.
and deleting pods, replication controllers, daemons, services, and minions.
Most consumers should use the Config object to create a Client:

View File

@ -127,7 +127,7 @@ type TLSClientConfig struct {
}
// New creates a Kubernetes client for the given config. This client works with pods,
// replication controllers and services. It allows operations such as list, get, update
// replication controllers, daemons, and services. It allows operations such as list, get, update
// and delete on these objects. An error is returned if the provided configuration
// is not valid.
func New(c *Config) (*Client, error) {

View File

@ -45,6 +45,7 @@ func TestResourceQuotaCreate(t *testing.T) {
api.ResourcePods: resource.MustParse("10"),
api.ResourceServices: resource.MustParse("10"),
api.ResourceReplicationControllers: resource.MustParse("10"),
api.ResourceDaemon: resource.MustParse("10"),
api.ResourceQuotas: resource.MustParse("10"),
},
},
@ -77,6 +78,7 @@ func TestResourceQuotaGet(t *testing.T) {
api.ResourcePods: resource.MustParse("10"),
api.ResourceServices: resource.MustParse("10"),
api.ResourceReplicationControllers: resource.MustParse("10"),
api.ResourceDaemon: resource.MustParse("10"),
api.ResourceQuotas: resource.MustParse("10"),
},
},
@ -133,6 +135,7 @@ func TestResourceQuotaUpdate(t *testing.T) {
api.ResourcePods: resource.MustParse("10"),
api.ResourceServices: resource.MustParse("10"),
api.ResourceReplicationControllers: resource.MustParse("10"),
api.ResourceDaemon: resource.MustParse("10"),
api.ResourceQuotas: resource.MustParse("10"),
},
},
@ -160,6 +163,7 @@ func TestResourceQuotaStatusUpdate(t *testing.T) {
api.ResourcePods: resource.MustParse("10"),
api.ResourceServices: resource.MustParse("10"),
api.ResourceReplicationControllers: resource.MustParse("10"),
api.ResourceDaemon: resource.MustParse("10"),
api.ResourceQuotas: resource.MustParse("10"),
},
},

View File

@ -114,6 +114,10 @@ func (c *Fake) ReplicationControllers(namespace string) client.ReplicationContro
return &FakeReplicationControllers{Fake: c, Namespace: namespace}
}
func (c *Fake) Daemons(namespace string) client.DaemonInterface {
return &FakeDaemons{Fake: c, Namespace: namespace}
}
func (c *Fake) Nodes() client.NodeInterface {
return &FakeNodes{Fake: c}
}

View File

@ -1,9 +1,12 @@
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
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.