-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
Implement Pod Affinity and AntiAffinity for DaemonSets #31136
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"fmt" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -382,7 +383,7 @@ func (dsc *DaemonSetsController) addNode(obj interface{}) { | |
node := obj.(*v1.Node) | ||
for i := range dsList.Items { | ||
ds := &dsList.Items[i] | ||
shouldEnqueue := dsc.nodeShouldRunDaemonPod(node, ds) | ||
shouldEnqueue := dsc.nodeShouldRunDaemonPod(node, ds, nil) | ||
if shouldEnqueue { | ||
dsc.enqueueDaemonSet(ds) | ||
} | ||
|
@@ -403,7 +404,8 @@ func (dsc *DaemonSetsController) updateNode(old, cur interface{}) { | |
} | ||
for i := range dsList.Items { | ||
ds := &dsList.Items[i] | ||
shouldEnqueue := (dsc.nodeShouldRunDaemonPod(oldNode, ds) != dsc.nodeShouldRunDaemonPod(curNode, ds)) | ||
// TBD: fix false positives (don't pass nil as nodeToDaemonPods) | ||
shouldEnqueue := (dsc.nodeShouldRunDaemonPod(oldNode, ds, nil) != dsc.nodeShouldRunDaemonPod(curNode, ds, nil)) | ||
if shouldEnqueue { | ||
dsc.enqueueDaemonSet(ds) | ||
} | ||
|
@@ -446,7 +448,7 @@ func (dsc *DaemonSetsController) manage(ds *extensions.DaemonSet) error { | |
} | ||
var nodesNeedingDaemonPods, podsToDelete []string | ||
for _, node := range nodeList.Items { | ||
shouldRun := dsc.nodeShouldRunDaemonPod(&node, ds) | ||
shouldRun := dsc.nodeShouldRunDaemonPod(&node, ds, nodeToDaemonPods) | ||
|
||
daemonPods, isRunning := nodeToDaemonPods[node.Name] | ||
|
||
|
@@ -574,7 +576,7 @@ func (dsc *DaemonSetsController) updateDaemonSetStatus(ds *extensions.DaemonSet) | |
|
||
var desiredNumberScheduled, currentNumberScheduled, numberMisscheduled, numberReady int | ||
for _, node := range nodeList.Items { | ||
shouldRun := dsc.nodeShouldRunDaemonPod(&node, ds) | ||
shouldRun := dsc.nodeShouldRunDaemonPod(&node, ds, nodeToDaemonPods) | ||
|
||
scheduled := len(nodeToDaemonPods[node.Name]) > 0 | ||
|
||
|
@@ -644,7 +646,7 @@ func (dsc *DaemonSetsController) syncDaemonSet(key string) error { | |
return dsc.updateDaemonSetStatus(ds) | ||
} | ||
|
||
func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *v1.Node, ds *extensions.DaemonSet) bool { | ||
func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *v1.Node, ds *extensions.DaemonSet, nodeToDaemonPods map[string][]*v1.Pod) bool { | ||
// If the daemon set specifies a node name, check that it matches with node.Name. | ||
if !(ds.Spec.Template.Spec.NodeName == "" || ds.Spec.Template.Spec.NodeName == node.Name) { | ||
return false | ||
|
@@ -696,6 +698,29 @@ func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *v1.Node, ds *exten | |
} | ||
} | ||
} | ||
if !fit { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return false | ||
} | ||
|
||
if nodeToDaemonPods != nil && len(nodeToDaemonPods[node.Name]) != 0 { | ||
// pod (anti)affinity is currently schedule-only, | ||
// so let's not delete the pod if it's already | ||
// being run | ||
return true | ||
} | ||
|
||
podAffinityChecker := predicates.NewPodAffinityPredicate( | ||
&predicates.CachedNodeInfo{StoreToNodeLister: dsc.nodeStore}, | ||
dsc.podStore, | ||
// problem: that should be obtained from --failure-domains flag | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, deprecated here #41195 |
||
strings.Split(v1.DefaultFailureDomains, ",")) | ||
fit, reasons, err = podAffinityChecker(newPod, nil, nodeInfo) | ||
if err != nil { | ||
glog.Warningf("Pod affinity checker failed on pod %s due to unexpected error: %v", newPod.Name, err) | ||
} | ||
for _, r := range reasons { | ||
glog.V(2).Infof("Pod affinity checker failed on pod %s for reason: %v", newPod.Name, r.GetReason()) | ||
} | ||
return fit | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nodeToDaemonPods
is not enough; it only includes the Pods matching ds's selector (or ownReference). If we want to enable PodAffinity/Anti-Affinity, we need to include all Pods on the node.