Skip to content

Commit c042f1f

Browse files
authored
fix(webhook): preserve required affinity branch semantics (#5757)
* test(requirenodewithfuse): add coverage for Mutate node-selector injection branch Signed-off-by: Harsh <harshmastic@gmail.com> * test(requirenodewithfuse): prove multi-term affinity injection appends only into term[0] Signed-off-by: Harsh <harshmastic@gmail.com> * test(requirenodewithfuse): strengthen multi-term affinity assertions Signed-off-by: Harsh <harshmastic@gmail.com> * fix(webhook): preserve required affinity branch semantics Signed-off-by: Harsh <harshmastic@gmail.com> * fix webhook empty affinity injection Signed-off-by: Harsh <harshmastic@gmail.com> * test avoid new testify assertions Signed-off-by: Harsh <harshmastic@gmail.com> * fix webhook empty required affinity injection Signed-off-by: Harsh <harshmastic@gmail.com> * fix webhook empty injected affinity terms Signed-off-by: Harsh <harshmastic@gmail.com> * test clean up require node with fuse assertions Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent a106621 commit c042f1f

4 files changed

Lines changed: 784 additions & 24 deletions

File tree

pkg/utils/webhook.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,56 @@ func InjectNodeSelectorTerms(requiredSchedulingTerms []corev1.NodeSelectorTerm,
6262
if len(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms) == 0 {
6363
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = requiredSchedulingTerms
6464
} else {
65+
hasRequiredConstraints := false
6566
for i := 0; i < len(requiredSchedulingTerms); i++ {
66-
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions =
67-
append(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions, requiredSchedulingTerms[i].MatchExpressions...)
67+
if len(requiredSchedulingTerms[i].MatchExpressions) != 0 || len(requiredSchedulingTerms[i].MatchFields) != 0 {
68+
hasRequiredConstraints = true
69+
break
70+
}
71+
}
72+
if !hasRequiredConstraints {
73+
return
74+
}
75+
76+
existingTerms := pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
77+
combinedTerms := make([]corev1.NodeSelectorTerm, 0, len(existingTerms)*len(requiredSchedulingTerms))
78+
hasExistingConstraints := false
79+
for i := 0; i < len(existingTerms); i++ {
80+
if len(existingTerms[i].MatchExpressions) != 0 || len(existingTerms[i].MatchFields) != 0 {
81+
hasExistingConstraints = true
82+
break
83+
}
84+
}
85+
if !hasExistingConstraints {
86+
filteredTerms := make([]corev1.NodeSelectorTerm, 0, len(requiredSchedulingTerms))
87+
for i := 0; i < len(requiredSchedulingTerms); i++ {
88+
if len(requiredSchedulingTerms[i].MatchExpressions) == 0 && len(requiredSchedulingTerms[i].MatchFields) == 0 {
89+
continue
90+
}
91+
filteredTerms = append(filteredTerms, requiredSchedulingTerms[i])
92+
}
93+
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = filteredTerms
94+
return
95+
}
96+
for i := 0; i < len(existingTerms); i++ {
97+
if len(existingTerms[i].MatchExpressions) == 0 && len(existingTerms[i].MatchFields) == 0 {
98+
continue
99+
}
100+
for j := 0; j < len(requiredSchedulingTerms); j++ {
101+
if len(requiredSchedulingTerms[j].MatchExpressions) == 0 && len(requiredSchedulingTerms[j].MatchFields) == 0 {
102+
continue
103+
}
104+
combinedTerm := corev1.NodeSelectorTerm{
105+
MatchExpressions: append(append([]corev1.NodeSelectorRequirement{}, existingTerms[i].MatchExpressions...), requiredSchedulingTerms[j].MatchExpressions...),
106+
MatchFields: append(append([]corev1.NodeSelectorRequirement{}, existingTerms[i].MatchFields...), requiredSchedulingTerms[j].MatchFields...),
107+
}
108+
combinedTerms = append(combinedTerms, combinedTerm)
109+
}
110+
}
111+
if len(combinedTerms) == 0 {
112+
return
68113
}
114+
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = combinedTerms
69115
}
70116

71117
}

0 commit comments

Comments
 (0)