Skip to content

Commit

Permalink
support get all nodes by ListPodNodes API
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 9, 2020
1 parent 600dc8b commit 16bf528
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
24 changes: 21 additions & 3 deletions store/etcdv3/node.go
Expand Up @@ -113,12 +113,30 @@ func (m *Mercury) GetNodes(ctx context.Context, nodenames []string) ([]*types.No
// GetNodesByPod get all nodes bound to pod
// here we use podname instead of pod instance
func (m *Mercury) GetNodesByPod(ctx context.Context, podname string, labels map[string]string, all bool) ([]*types.Node, error) {
key := fmt.Sprintf(nodePodKey, podname, "")
resp, err := m.Get(ctx, key, clientv3.WithPrefix())
do := func(podname string) ([]*types.Node, error) {
key := fmt.Sprintf(nodePodKey, podname, "")
resp, err := m.Get(ctx, key, clientv3.WithPrefix())
if err != nil {
return []*types.Node{}, err
}
return m.doGetNodes(ctx, resp.Kvs, labels, all)
}
if podname != "" {
return do(podname)
}
pods, err := m.GetAllPods(ctx)
if err != nil {
return []*types.Node{}, err
}
return m.doGetNodes(ctx, resp.Kvs, labels, all)
result := []*types.Node{}
for _, pod := range pods {
ns, err := do(pod.Name)
if err != nil {
return []*types.Node{}, err
}
result = append(result, ns...)
}
return result, nil
}

// UpdateNode update a node, save it to etcd
Expand Down
5 changes: 5 additions & 0 deletions store/etcdv3/node_test.go
Expand Up @@ -181,6 +181,11 @@ func TestGetNodesByPod(t *testing.T) {
ns, err = m.GetNodesByPod(ctx, "testpod", nil, false)
assert.NoError(t, err)
assert.NotEmpty(t, ns)
_, err = m.AddPod(ctx, "testpod", "")
assert.NoError(t, err)
ns, err = m.GetNodesByPod(ctx, "", nil, false)
assert.NoError(t, err)
assert.NotEmpty(t, ns)
}

func TestUpdateNode(t *testing.T) {
Expand Down

0 comments on commit 16bf528

Please sign in to comment.