mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-17 15:27:42 +00:00
List in NodesInterface takes label selector
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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++ {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user