List in NodesInterface takes label selector

This commit is contained in:
Masahiro Sano
2015-03-29 17:49:23 +09:00
parent 24b478dd0a
commit d04cc5ced4
14 changed files with 59 additions and 20 deletions

View File

@@ -32,7 +32,7 @@ type NodesInterface interface {
type NodeInterface interface {
Get(name string) (result *api.Node, err error)
Create(node *api.Node) (*api.Node, error)
List() (*api.NodeList, error)
List(selector labels.Selector) (*api.NodeList, error)
Delete(name string) error
Update(*api.Node) (*api.Node, error)
Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error)
@@ -64,10 +64,10 @@ func (c *nodes) Create(node *api.Node) (*api.Node, error) {
return result, err
}
// List lists all the nodes in the cluster.
func (c *nodes) List() (*api.NodeList, error) {
// List takes a selector, and returns the list of nodes that match that selector in the cluster.
func (c *nodes) List(selector labels.Selector) (*api.NodeList, error) {
result := &api.NodeList{}
err := c.r.Get().Resource(c.resourceName()).Do().Into(result)
err := c.r.Get().Resource(c.resourceName()).LabelsSelectorParam(selector).Do().Into(result)
return result, err
}

View File

@@ -17,11 +17,13 @@ limitations under the License.
package client
import (
"net/url"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
func getNodesResourceName() string {
@@ -30,6 +32,7 @@ func getNodesResourceName() string {
}
return "nodes"
}
func TestListMinions(t *testing.T) {
c := &testClient{
Request: testRequest{
@@ -38,10 +41,41 @@ func TestListMinions(t *testing.T) {
},
Response: Response{StatusCode: 200, Body: &api.NodeList{ListMeta: api.ListMeta{ResourceVersion: "1"}}},
}
response, err := c.Setup().Nodes().List()
response, err := c.Setup().Nodes().List(labels.Everything())
c.Validate(t, response, err)
}
func TestListMinionsLabels(t *testing.T) {
ns := api.NamespaceNone
labelSelectorQueryParamName := api.LabelSelectorQueryParam(testapi.Version())
c := &testClient{
Request: testRequest{
Method: "GET",
Path: testapi.ResourcePath(getNodesResourceName(), "", ""),
Query: buildQueryValues(ns, url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
Response: Response{
StatusCode: 200,
Body: &api.NodeList{
Items: []api.Node{
{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
},
}
c.Setup()
c.QueryValidator[labelSelectorQueryParamName] = validateLabels
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
receivedNodeList, err := c.Nodes().List(selector)
c.Validate(t, receivedNodeList, err)
}
func TestGetMinion(t *testing.T) {
c := &testClient{
Request: testRequest{
@@ -56,12 +90,12 @@ func TestGetMinion(t *testing.T) {
func TestGetMinionWithNoName(t *testing.T) {
c := &testClient{Error: true}
receivedPod, err := c.Setup().Nodes().Get("")
receivedNode, err := c.Setup().Nodes().Get("")
if (err != nil) && (err.Error() != nameRequiredError) {
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err)
}
c.Validate(t, receivedPod, err)
c.Validate(t, receivedNode, err)
}
func TestCreateMinion(t *testing.T) {

View File

@@ -34,7 +34,7 @@ func (c *FakeNodes) Get(name string) (*api.Node, error) {
return obj.(*api.Node), err
}
func (c *FakeNodes) List() (*api.NodeList, error) {
func (c *FakeNodes) List(selector labels.Selector) (*api.NodeList, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "list-nodes"}, &api.NodeList{})
return obj.(*api.NodeList), err
}