Skip to content
Draft
Show file tree
Hide file tree
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
20 changes: 17 additions & 3 deletions pkg/utils/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
}

// InjectRequiredSchedulingTerms inject the NodeSelectorTerms into a pod
func InjectNodeSelectorTerms(requiredSchedulingTerms []corev1.NodeSelectorTerm, pod *corev1.Pod) {

Check failure on line 45 in pkg/utils/webhook.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=fluid-cloudnative_fluid&issues=AZ1Nq6ro7R0-edR1iPBQ&open=AZ1Nq6ro7R0-edR1iPBQ&pullRequest=5757
if len(requiredSchedulingTerms) == 0 {
return
}
Expand All @@ -62,10 +62,24 @@
if len(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms) == 0 {
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = requiredSchedulingTerms
} else {
for i := 0; i < len(requiredSchedulingTerms); i++ {
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions =
append(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions, requiredSchedulingTerms[i].MatchExpressions...)
existingTerms := pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms
combinedTerms := make([]corev1.NodeSelectorTerm, 0, len(existingTerms)*len(requiredSchedulingTerms))
for i := 0; i < len(existingTerms); i++ {
if len(existingTerms[i].MatchExpressions) == 0 && len(existingTerms[i].MatchFields) == 0 {
continue
}
for j := 0; j < len(requiredSchedulingTerms); j++ {
if len(requiredSchedulingTerms[j].MatchExpressions) == 0 && len(requiredSchedulingTerms[j].MatchFields) == 0 {
continue
}
combinedTerm := corev1.NodeSelectorTerm{
MatchExpressions: append(append([]corev1.NodeSelectorRequirement{}, existingTerms[i].MatchExpressions...), requiredSchedulingTerms[j].MatchExpressions...),
MatchFields: append(append([]corev1.NodeSelectorRequirement{}, existingTerms[i].MatchFields...), requiredSchedulingTerms[j].MatchFields...),
}
combinedTerms = append(combinedTerms, combinedTerm)
}
}
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms = combinedTerms
Comment on lines +65 to +82
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

InjectNodeSelectorTerms can set RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms to an empty slice when all existing terms are empty (or all injected terms are empty and get skipped). This effectively wipes required node affinity and can make a pod unschedulable/invalid. Consider filtering out empty injected terms up front, and if no non-empty existing terms remain then fall back to setting NodeSelectorTerms to the (filtered) injected terms instead of overwriting with an empty combinedTerms (or treat empty existing term as a match-all branch and still combine).

Copilot uses AI. Check for mistakes.
}

}
Expand Down
Loading
Loading