Skip to content

Commit

Permalink
update(scheduler): compose node anti affinity (#2327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Muzry authored Oct 12, 2021
1 parent 1497df9 commit 9e4f431
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 8 additions & 6 deletions modules/scheduler/executor/plugins/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,23 +1130,25 @@ func (k *Kubernetes) Scale(ctx context.Context, spec interface{}) (interface{},
}

func (k *Kubernetes) composeNodeAntiAffinityPreferredWithWorkspace(workspace string) []apiv1.PreferredSchedulingTerm {
var workspaces = map[string]int32{"dev": 60, "test": 60, "staging": 80, "prod": 100}
var workspaceKeys = []string{"dev", "test", "staging", "prod"}
var weightMap = map[string]int32{"dev": 60, "test": 60, "staging": 80, "prod": 100}

preferredSchedulerTerms := []apiv1.PreferredSchedulingTerm{}

for key := range workspaces {
for index, key := range workspaceKeys {
if strings.ToLower(workspace) == key {
delete(workspaces, key)
workspaceKeys = append(workspaceKeys[:index], workspaceKeys[index+1:]...)
break
}
}

for ws, weight := range workspaces {
for _, key := range workspaceKeys {
preferredSchedulerTerms = append(preferredSchedulerTerms, apiv1.PreferredSchedulingTerm{
Weight: weight,
Weight: weightMap[key],
Preference: apiv1.NodeSelectorTerm{
MatchExpressions: []apiv1.NodeSelectorRequirement{
{
Key: fmt.Sprintf("dice/workspace-%s", ws),
Key: fmt.Sprintf("dice/workspace-%s", key),
Operator: apiv1.NodeSelectorOpDoesNotExist,
},
},
Expand Down
13 changes: 11 additions & 2 deletions modules/scheduler/executor/plugins/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func TestComposeDeploymentNodeAffinityPreferredWithServiceWorkspace(t *testing.T
}

resPreferred := k.composeDeploymentNodeAntiAffinityPreferred(workspace)
assert.DeepEqual(t, deploymentPreferred, resPreferred)
for index, preferred := range deploymentPreferred {
assert.DeepEqual(t, preferred.Preference.MatchExpressions[0].Key, resPreferred[index].Preference.MatchExpressions[0].Key)
assert.DeepEqual(t, preferred.Preference.MatchExpressions[0].Operator, resPreferred[index].Preference.MatchExpressions[0].Operator)
assert.DeepEqual(t, preferred.Weight, resPreferred[index].Weight)
}

}

func TestComposeStatefulSetNodeAffinityPreferredWithServiceWorkspace(t *testing.T) {
Expand Down Expand Up @@ -127,5 +132,9 @@ func TestComposeStatefulSetNodeAffinityPreferredWithServiceWorkspace(t *testing.
},
}
resPreferred := k.composeStatefulSetNodeAntiAffinityPreferred(workspace)
assert.DeepEqual(t, statefulSetPreferred, resPreferred)
for index, preferred := range statefulSetPreferred {
assert.DeepEqual(t, preferred.Preference.MatchExpressions[0].Key, resPreferred[index].Preference.MatchExpressions[0].Key)
assert.DeepEqual(t, preferred.Preference.MatchExpressions[0].Operator, resPreferred[index].Preference.MatchExpressions[0].Operator)
assert.DeepEqual(t, preferred.Weight, resPreferred[index].Weight)
}
}

0 comments on commit 9e4f431

Please sign in to comment.