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

Implement Pod Affinity and AntiAffinity for DaemonSets #31136

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions pkg/controller/daemon/daemoncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"reflect"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Copy link
Member

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.


daemonPods, isRunning := nodeToDaemonPods[node.Name]

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -696,6 +698,29 @@ func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *v1.Node, ds *exten
}
}
}
if !fit {
Copy link
Contributor

Choose a reason for hiding this comment

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

fit was now removed in master branch, a rebase is needed.

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
Copy link
Member

Choose a reason for hiding this comment

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

--failure-domains flag is deprecated so don't worry about it

Copy link
Contributor

Choose a reason for hiding this comment

The 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
}

Expand Down
77 changes: 77 additions & 0 deletions pkg/controller/daemon/daemoncontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ var (
simpleNodeLabel = map[string]string{"color": "blue", "speed": "fast"}
simpleNodeLabel2 = map[string]string{"color": "red", "speed": "fast"}
alwaysReady = func() bool { return true }
podAffinity = map[string]string{
v1.AffinityAnnotationKey: `
{"podAffinity": { "requiredDuringSchedulingIgnoredDuringExecution": [{
"labelSelector": {
"matchLabels": {
"foobar": "foo"
}
},
"topologyKey": "kubernetes.io/hostname",
"namespaces": []
}]}}`,
}
podAntiAffinity = map[string]string{
// make the pod "antiaffine" with itself, too
v1.AffinityAnnotationKey: `
{"podAntiAffinity": { "requiredDuringSchedulingIgnoredDuringExecution": [{
"labelSelector": {
"matchLabels": {
"type": "production"
}
},
"topologyKey": "kubernetes.io/hostname",
"namespaces": []
}]}}`,
}
)

func getKey(ds *extensions.DaemonSet, t *testing.T) string {
Expand Down Expand Up @@ -595,3 +620,55 @@ func TestNumberReadyStatus(t *testing.T) {
t.Errorf("Wrong daemon %s status: %v", daemon.Name, daemon.Status)
}
}

func testAffinityUsingSinglePod(t *testing.T, annotations map[string]string, expectedCreates int) {
manager, podControl := newTestController()
addNodes(manager.nodeStore.Store, 0, 1, map[string]string{
"kubernetes.io/hostname": "node-0",
})
daemon := newDaemonSet("foo")
daemon.Spec.Template.ObjectMeta.Annotations = annotations
manager.dsStore.Add(daemon)
syncAndValidateDaemonSets(t, manager, daemon, podControl, expectedCreates, 0)
}

func testAffinityUsingTwoPods(t *testing.T, annotations map[string]string, expectedCreates int) {
manager, podControl := newTestController()
addNodes(manager.nodeStore.Store, 0, 1, map[string]string{
"kubernetes.io/hostname": "node-0",
})
manager.podStore.Indexer.Add(&v1.Pod{
TypeMeta: metav1.TypeMeta{APIVersion: registered.GroupOrDie(v1.GroupName).GroupVersion.String()},
ObjectMeta: v1.ObjectMeta{
Name: "samplepod",
Labels: map[string]string{
"type": "production",
"foobar": "foo",
},
Namespace: v1.NamespaceDefault,
},
Spec: v1.PodSpec{
NodeName: "default/node-0",
},
})
daemon := newDaemonSet("foo")
daemon.Spec.Template.ObjectMeta.Annotations = annotations
manager.dsStore.Add(daemon)
syncAndValidateDaemonSets(t, manager, daemon, podControl, expectedCreates, 0)
}

func TestPodAffinityDaemonDoesntLaunchNonMatchingPods(t *testing.T) {
testAffinityUsingSinglePod(t, podAffinity, 0)
}

func TestPodAffinityDaemonLaunchesMatchingPods(t *testing.T) {
testAffinityUsingTwoPods(t, podAffinity, 1)
}

func TestPodAntiAffinityDaemonLaunchesMatchingPods(t *testing.T) {
testAffinityUsingSinglePod(t, podAntiAffinity, 1)
}

func TestPodAntiAffinityDaemonDoesntLaunchNonMatchingPods(t *testing.T) {
testAffinityUsingTwoPods(t, podAntiAffinity, 0)
}