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

change nodeAffinity to And #1637

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Changes from all 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
45 changes: 44 additions & 1 deletion api/tenant-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ func parseTenantPoolRequest(poolParams *models.Pool) (*miniov2.Pool, error) {
if len(nodeSelectorTerms) > 0 || len(preferredSchedulingTerm) > 0 {
nodeAffinity = &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: nodeSelectorTerms,
NodeSelectorTerms: mergeNodeSelectorTerms(nodeSelectorTerms),
},
PreferredDuringSchedulingIgnoredDuringExecution: preferredSchedulingTerm,
}
Expand Down Expand Up @@ -1028,6 +1028,49 @@ func parseTenantPoolRequest(poolParams *models.Pool) (*miniov2.Pool, error) {
return pool, nil
}

// mergeNodeSelectorTerms is for matchExpressions merge, when matchExpressions/matchFields have the same Key+Operator , we merge values.
// When matchExpressions/matchFields have the same Key+Operator, Set it 'And'
func mergeNodeSelectorTerms(terms []corev1.NodeSelectorTerm) []corev1.NodeSelectorTerm {
mergedTerms := []corev1.NodeSelectorTerm{}
for _, term := range terms {
nodeSelectorTerm := corev1.NodeSelectorTerm{}
nodeSelectorMatchExpressionsMap := map[string]*corev1.NodeSelectorRequirement{}
for _, exp := range term.MatchExpressions {
if selector, ok := nodeSelectorMatchExpressionsMap[fmt.Sprintf("%s-%s", exp.Key, exp.Operator)]; ok {
selector.Values = append(selector.Values, exp.Values...)
} else {
nodeSelectorMatchExpressionsMap[fmt.Sprintf("%s-%s", exp.Key, exp.Operator)] = &corev1.NodeSelectorRequirement{
Key: exp.Key,
Operator: exp.Operator,
Values: exp.Values,
}
}
}
nodeSelectorMatchMatchFieldsMap := map[string]*corev1.NodeSelectorRequirement{}
for _, field := range term.MatchFields {
if selector, ok := nodeSelectorMatchMatchFieldsMap[fmt.Sprintf("%s-%s", field.Key, field.Operator)]; ok {
selector.Values = append(selector.Values, field.Values...)
} else {
nodeSelectorMatchMatchFieldsMap[fmt.Sprintf("%s-%s", field.Key, field.Operator)] = &corev1.NodeSelectorRequirement{
Key: field.Key,
Operator: field.Operator,
Values: field.Values,
}
}
}
for _, exp := range nodeSelectorMatchExpressionsMap {
nodeSelectorTerm.MatchExpressions = append(nodeSelectorTerm.MatchExpressions, *exp)
}
for _, field := range nodeSelectorMatchMatchFieldsMap {
nodeSelectorTerm.MatchExpressions = append(nodeSelectorTerm.MatchFields, *field)
}
if len(nodeSelectorMatchExpressionsMap) != 0 || len(nodeSelectorMatchMatchFieldsMap) != 0 {
mergedTerms = append(mergedTerms, nodeSelectorTerm)
}
}
return mergedTerms
}

func parseModelPodAffinityTerm(term *models.PodAffinityTerm) corev1.PodAffinityTerm {
labelMatchExpressions := []metav1.LabelSelectorRequirement{}
for _, exp := range term.LabelSelector.MatchExpressions {
Expand Down