Skip to content

Commit

Permalink
fix(vmclass, vm): proper affinity and tolerations merging (#547)
Browse files Browse the repository at this point in the history
proper affinity and tolerations merging
---------
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat authored Dec 4, 2024
1 parent dc6d82a commit 62500cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
29 changes: 12 additions & 17 deletions images/virtualization-artifact/pkg/controller/kvbuilder/kvvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,8 @@ func (b *KVVM) SetNodeSelector(vmNodeSelector, classNodeSelector map[string]stri
}

func (b *KVVM) SetTolerations(vmTolerations, classTolerations []corev1.Toleration) {
tolerationsMap := make(map[string]corev1.Toleration)
for _, toleration := range classTolerations {
tolerationsMap[toleration.Key] = toleration
}
for _, toleration := range vmTolerations {
tolerationsMap[toleration.Key] = toleration
}
resultTolerations := make([]corev1.Toleration, 0, len(tolerationsMap))
for _, toleration := range tolerationsMap {
resultTolerations = append(resultTolerations, toleration)
}
b.Resource.Spec.Template.Spec.Tolerations = resultTolerations
b.Resource.Spec.Template.Spec.Tolerations = append(b.Resource.Spec.Template.Spec.Tolerations, classTolerations...)
b.Resource.Spec.Template.Spec.Tolerations = append(b.Resource.Spec.Template.Spec.Tolerations, vmTolerations...)
}

func (b *KVVM) SetPriorityClassName(priorityClassName string) {
Expand All @@ -193,11 +183,16 @@ func (b *KVVM) SetAffinity(vmAffinity *corev1.Affinity, classMatchExpressions []
if vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms == nil {
vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = []corev1.NodeSelectorTerm{}
}

vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = append(
vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms,
corev1.NodeSelectorTerm{MatchExpressions: classMatchExpressions},
)
if len(vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms) == 0 {
vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = append(
vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms,
corev1.NodeSelectorTerm{MatchExpressions: classMatchExpressions})
} else {
for i := range vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms {
vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[i].MatchExpressions = append(
vmAffinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[i].MatchExpressions, classMatchExpressions...)
}
}

b.Resource.Spec.Template.Spec.Affinity = vmAffinity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,11 @@ func TestSetAffinity(t *testing.T) {
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: getDefaultMatchExpressions(),
},
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "node-role.kubernetes.io/master",
Operator: corev1.NodeSelectorOpIn,
Values: []string{""},
},
},
MatchExpressions: append(getDefaultMatchExpressions(), corev1.NodeSelectorRequirement{
Key: "node-role.kubernetes.io/master",
Operator: corev1.NodeSelectorOpIn,
Values: []string{""},
}),
},
},
},
Expand Down

0 comments on commit 62500cb

Please sign in to comment.