From d04cc5ced4d051077a2faa2f12c326656d340467 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Sun, 29 Mar 2015 17:49:23 +0900 Subject: [PATCH] List in NodesInterface takes label selector --- pkg/client/nodes.go | 8 ++-- pkg/client/nodes_test.go | 40 +++++++++++++++++-- pkg/client/testclient/fake_nodes.go | 2 +- .../controller/nodecontroller.go | 4 +- .../controller/nodecontroller_test.go | 2 +- pkg/kubelet/kubelet.go | 2 +- plugin/pkg/scheduler/factory/factory.go | 3 +- test/e2e/cadvisor.go | 3 +- test/e2e/density.go | 2 +- test/e2e/es_cluster_logging.go | 2 +- test/e2e/monitoring.go | 2 +- test/e2e/networking.go | 3 +- test/e2e/pd.go | 3 +- test/soak/serve_hostnames/serve_hostnames.go | 3 +- 14 files changed, 59 insertions(+), 20 deletions(-) diff --git a/pkg/client/nodes.go b/pkg/client/nodes.go index ac959cacbdf..8e36a91e350 100644 --- a/pkg/client/nodes.go +++ b/pkg/client/nodes.go @@ -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 } diff --git a/pkg/client/nodes_test.go b/pkg/client/nodes_test.go index 994879b7b95..8d94f1c8c0e 100644 --- a/pkg/client/nodes_test.go +++ b/pkg/client/nodes_test.go @@ -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) { diff --git a/pkg/client/testclient/fake_nodes.go b/pkg/client/testclient/fake_nodes.go index f36f30425b4..b58378fbb28 100644 --- a/pkg/client/testclient/fake_nodes.go +++ b/pkg/client/testclient/fake_nodes.go @@ -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 } diff --git a/pkg/cloudprovider/controller/nodecontroller.go b/pkg/cloudprovider/controller/nodecontroller.go index 9213c131094..1be036c275e 100644 --- a/pkg/cloudprovider/controller/nodecontroller.go +++ b/pkg/cloudprovider/controller/nodecontroller.go @@ -213,7 +213,7 @@ func (nc *NodeController) SyncCloudNodes() error { if err != nil { return err } - nodes, err := nc.kubeClient.Nodes().List() + nodes, err := nc.kubeClient.Nodes().List(labels.Everything()) if err != nil { return err } @@ -434,7 +434,7 @@ func (nc *NodeController) tryUpdateNodeStatus(node *api.Node) (time.Duration, ap // post "NodeReady==ConditionUnknown". It also evicts all pods if node is not ready or // not reachable for a long period of time. func (nc *NodeController) MonitorNodeStatus() error { - nodes, err := nc.kubeClient.Nodes().List() + nodes, err := nc.kubeClient.Nodes().List(labels.Everything()) if err != nil { return err } diff --git a/pkg/cloudprovider/controller/nodecontroller_test.go b/pkg/cloudprovider/controller/nodecontroller_test.go index 36e460a72be..42c73d65196 100644 --- a/pkg/cloudprovider/controller/nodecontroller_test.go +++ b/pkg/cloudprovider/controller/nodecontroller_test.go @@ -86,7 +86,7 @@ func (m *FakeNodeHandler) Get(name string) (*api.Node, error) { return nil, nil } -func (m *FakeNodeHandler) List() (*api.NodeList, error) { +func (m *FakeNodeHandler) List(selector labels.Selector) (*api.NodeList, error) { defer func() { m.RequestCount++ }() var nodes []*api.Node for i := 0; i < len(m.UpdatedNodes); i++ { diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index c7ebd9cc86f..62780ee07e1 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -172,7 +172,7 @@ func NewMainKubelet( listWatch := &cache.ListWatch{ ListFunc: func() (runtime.Object, error) { // TODO: Use List() with fieldSelector when it is supported. - return kubeClient.Nodes().List() + return kubeClient.Nodes().List(labels.Everything()) }, WatchFunc: func(resourceVersion string) (watch.Interface, error) { return kubeClient.Nodes().Watch(labels.Everything(), fieldSelector, resourceVersion) diff --git a/plugin/pkg/scheduler/factory/factory.go b/plugin/pkg/scheduler/factory/factory.go index f0a7aa131da..79ae170b865 100644 --- a/plugin/pkg/scheduler/factory/factory.go +++ b/plugin/pkg/scheduler/factory/factory.go @@ -27,6 +27,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache" "github.com/GoogleCloudPlatform/kubernetes/pkg/fields" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" algorithm "github.com/GoogleCloudPlatform/kubernetes/pkg/scheduler" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler" @@ -233,7 +234,7 @@ func (factory *ConfigFactory) createMinionLW() *cache.ListWatch { // Lists all minions and filter out unhealthy ones, then returns // an enumerator for cache.Poller. func (factory *ConfigFactory) pollMinions() (cache.Enumerator, error) { - allNodes, err := factory.Client.Nodes().List() + allNodes, err := factory.Client.Nodes().List(labels.Everything()) if err != nil { return nil, err } diff --git a/test/e2e/cadvisor.go b/test/e2e/cadvisor.go index d1e5fc92c32..ed38bfbafe2 100644 --- a/test/e2e/cadvisor.go +++ b/test/e2e/cadvisor.go @@ -21,6 +21,7 @@ import ( "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" . "github.com/onsi/ginkgo" ) @@ -47,7 +48,7 @@ var _ = Describe("Cadvisor", func() { func CheckCadvisorHealthOnAllNodes(c *client.Client, timeout time.Duration) { By("getting list of nodes") - nodeList, err := c.Nodes().List() + nodeList, err := c.Nodes().List(labels.Everything()) expectNoError(err) var errors []error retries := maxRetries diff --git a/test/e2e/density.go b/test/e2e/density.go index 4824a22ea60..79b2a2230ad 100644 --- a/test/e2e/density.go +++ b/test/e2e/density.go @@ -189,7 +189,7 @@ var _ = Describe("Density", func() { var err error c, err = loadClient() expectNoError(err) - minions, err := c.Nodes().List() + minions, err := c.Nodes().List(labels.Everything()) expectNoError(err) minionCount = len(minions.Items) Expect(minionCount).NotTo(BeZero()) diff --git a/test/e2e/es_cluster_logging.go b/test/e2e/es_cluster_logging.go index 43ff34b91d2..86536d0976e 100644 --- a/test/e2e/es_cluster_logging.go +++ b/test/e2e/es_cluster_logging.go @@ -163,7 +163,7 @@ func ClusterLevelLoggingWithElasticsearch(c *client.Client) { } // Obtain a list of nodes so we can place one synthetic logger on each node. - nodes, err := c.Nodes().List() + nodes, err := c.Nodes().List(labels.Everything()) if err != nil { Failf("Failed to list nodes: %v", err) } diff --git a/test/e2e/monitoring.go b/test/e2e/monitoring.go index 0d7cb376eca..e8fceb33688 100644 --- a/test/e2e/monitoring.go +++ b/test/e2e/monitoring.go @@ -121,7 +121,7 @@ func expectedServicesExist(c *client.Client) error { } func getAllNodesInCluster(c *client.Client) ([]string, error) { - nodeList, err := c.Nodes().List() + nodeList, err := c.Nodes().List(labels.Everything()) if err != nil { return nil, err } diff --git a/test/e2e/networking.go b/test/e2e/networking.go index fa40993b415..73b866889c4 100644 --- a/test/e2e/networking.go +++ b/test/e2e/networking.go @@ -23,6 +23,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" . "github.com/onsi/ginkgo" @@ -159,7 +160,7 @@ var _ = Describe("Networking", func() { By("Creating a webserver (pending) pod on each node") - nodes, err := c.Nodes().List() + nodes, err := c.Nodes().List(labels.Everything()) if err != nil { Failf("Failed to list nodes: %v", err) } diff --git a/test/e2e/pd.go b/test/e2e/pd.go index 016644e4f25..958380ca513 100644 --- a/test/e2e/pd.go +++ b/test/e2e/pd.go @@ -24,6 +24,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -45,7 +46,7 @@ var _ = Describe("PD", func() { podClient = c.Pods(api.NamespaceDefault) - nodes, err := c.Nodes().List() + nodes, err := c.Nodes().List(labels.Everything()) expectNoError(err, "Failed to list nodes for e2e cluster.") Expect(len(nodes.Items) >= 2).To(BeTrue()) diff --git a/test/soak/serve_hostnames/serve_hostnames.go b/test/soak/serve_hostnames/serve_hostnames.go index 946a9e71b36..f193c2afafa 100644 --- a/test/soak/serve_hostnames/serve_hostnames.go +++ b/test/soak/serve_hostnames/serve_hostnames.go @@ -35,6 +35,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/golang/glog" ) @@ -87,7 +88,7 @@ func main() { var nodes *api.NodeList for start := time.Now(); time.Since(start) < nodeListTimeout; time.Sleep(2 * time.Second) { - nodes, err = c.Nodes().List() + nodes, err = c.Nodes().List(labels.Everything()) if err == nil { break }