Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support excluding nodes when creating workloads #365

Merged
merged 5 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ func (c *Calcium) CreateWorkload(ctx context.Context, opts *types.DeployOptions)
return c.doCreateWorkloads(ctx, opts), nil
}

// getDeployNodenames checks nodenames and excludedNodenames
// if excludedNodenames is not set or nodenames is set, then return nodenames
// for other cases, then node within excludedNodename list will not be returned
func (c *Calcium) getDeployNodenames(ctx context.Context, podname string, nodenames, excludedNodenames []string) ([]string, error) {
if len(excludedNodenames) == 0 || len(nodenames) != 0 {
return nodenames, nil
}

allNodes, err := c.ListPodNodes(ctx, podname, map[string]string{}, false)
if err != nil {
return nil, err
}

excludes := map[string]struct{}{}
for _, n := range excludedNodenames {
excludes[n] = struct{}{}
}

rv := []string{}
for _, n := range allNodes {
if _, ok := excludes[n.Name]; ok {
continue
}
rv = append(rv, n.Name)
}
return rv, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得你这样不如直接改 withNodeLocks 了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我其实甚至觉得 withNodeLocks 都不应该有 podname ... 为了保持一致就改这里了

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那你不如抽出整个 node 筛选的函数,包括 pod,all,黑名单白名单,withNodeLocks 就当纯粹的锁……现在就很分裂

}

// transaction: resource metadata consistency
func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptions) chan *types.CreateWorkloadMessage {
logger := log.WithField("Calcium", "doCreateWorkloads").WithField("opts", opts)
Expand All @@ -62,12 +90,18 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio
close(ch)
}()

nodenames, err := c.getDeployNodenames(ctx, opts.Podname, opts.Nodenames, opts.ExcludedNodenames)
if err != nil {
ch <- &types.CreateWorkloadMessage{Error: logger.Err(err)}
return
}

_ = utils.Txn(
ctx,

// if: alloc resources
func(ctx context.Context) error {
return c.withNodesLocked(ctx, opts.Podname, opts.Nodenames, opts.NodeLabels, false, func(ctx context.Context, nodeMap map[string]*types.Node) (err error) {
return c.withNodesLocked(ctx, opts.Podname, nodenames, opts.NodeLabels, false, func(ctx context.Context, nodeMap map[string]*types.Node) (err error) {
defer func() {
if err != nil {
ch <- &types.CreateWorkloadMessage{Error: logger.Err(err)}
Expand Down
49 changes: 49 additions & 0 deletions cluster/calcium/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,52 @@ func newCreateWorkloadCluster(t *testing.T) (*Calcium, []*types.Node) {

return c, nodes
}

func TestGetDeployNodenames(t *testing.T) {
assert := assert.New(t)
c := NewTestCluster()
store := c.store.(*storemocks.Store)
nodes := []*types.Node{
{
NodeMeta: types.NodeMeta{Name: "A"},
},
{
NodeMeta: types.NodeMeta{Name: "B"},
},
{
NodeMeta: types.NodeMeta{Name: "C"},
},
{
NodeMeta: types.NodeMeta{Name: "D"},
},
}

// error
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, errors.New("fail to list pod nodes")).Once()
_, err := c.getDeployNodenames(context.Background(), "pod", []string{}, []string{"A", "X"})
assert.Error(err)

// empty nodenames, non-empty excludeNodenames
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil).Once()
nodenames1, err := c.getDeployNodenames(context.Background(), "pod", []string{}, []string{"A", "B"})
assert.NoError(err)
assert.Equal([]string{"C", "D"}, nodenames1)

// empty nodenames, empty excludeNodenames
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil).Once()
nodenames2, err := c.getDeployNodenames(context.Background(), "pod", []string{}, []string{})
assert.NoError(err)
assert.Equal([]string{}, nodenames2)

// non-empty nodenames, empty excludeNodenames
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil).Once()
nodenames3, err := c.getDeployNodenames(context.Background(), "pod", []string{"O", "P"}, []string{})
assert.NoError(err)
assert.Equal([]string{"O", "P"}, nodenames3)

// non-empty nodenames
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil).Once()
nodenames4, err := c.getDeployNodenames(context.Background(), "pod", []string{"X", "Y"}, []string{"A", "B"})
assert.NoError(err)
assert.Equal([]string{"X", "Y"}, nodenames4)
}
Loading