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

Check topology key for pod antiaffinity #1415

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
export CONTAINER_ENGINE ?= docker

# VERSION is based on a date stamp plus the last commit
VERSION?=v$(shell date +%Y%m%d)-$(shell git describe --tags)
VERSION?=$(shell git rev-parse --short HEAD)
BRANCH?=$(shell git branch --show-current)
SHA1?=$(shell git rev-parse HEAD)
BUILD=$(shell date +%FT%T%z)
Expand Down
3 changes: 2 additions & 1 deletion pkg/descheduler/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NodeFit(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1.Pod, node *v
if match, err := podMatchesInterPodAntiAffinity(nodeIndexer, pod, node); err != nil {
errors = append(errors, err)
} else if match {
errors = append(errors, fmt.Errorf("pod matches inter-pod anti-affinity rule of other pod on node"))
errors = append(errors, fmt.Errorf("pod matches interpod antiaffinity rule of other pod on node"))
}

return errors
Expand Down Expand Up @@ -336,6 +336,7 @@ func PodMatchNodeSelector(pod *v1.Pod, node *v1.Node) bool {
// If a match is found, it returns true.
func podMatchesInterPodAntiAffinity(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1.Pod, node *v1.Node) (bool, error) {
if pod.Spec.Affinity == nil || pod.Spec.Affinity.PodAntiAffinity == nil {
klog.V(4).InfoS("no Pod antiaffinity rule found", "pod", klog.KObj(pod))
return false, nil
}

Expand Down
37 changes: 21 additions & 16 deletions pkg/descheduler/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,12 @@ func TestPodFitsAnyOtherNode(t *testing.T) {
}

func TestNodeFit(t *testing.T) {
node := test.BuildTestNode("node", 64000, 128*1000*1000*1000, 2, func(node *v1.Node) {
currentNode := test.BuildTestNode("current-node", 64000, 128*1000*1000*1000, 2, func(node *v1.Node) {
node.ObjectMeta.Labels = map[string]string{
"region": "main-region",
}
})
otherNode := test.BuildTestNode("other-node", 64000, 128*1000*1000*1000, 2, func(node *v1.Node) {
node.ObjectMeta.Labels = map[string]string{
"region": "main-region",
}
Expand All @@ -768,36 +773,36 @@ func TestNodeFit(t *testing.T) {
}{
{
description: "insufficient cpu",
pod: test.BuildTestPod("p1", 10000, 2*1000*1000*1000, "", nil),
node: node,
pod: test.BuildTestPod("p1", 10000, 2*1000*1000*1000, currentNode.GetName(), nil),
node: otherNode,
podsOnNode: []*v1.Pod{
test.BuildTestPod("p2", 60000, 60*1000*1000*1000, "node", nil),
test.BuildTestPod("p2", 60000, 60*1000*1000*1000, otherNode.GetName(), nil),
},
err: errors.New("insufficient cpu"),
},
{
description: "insufficient pod num",
pod: test.BuildTestPod("p1", 1000, 2*1000*1000*1000, "", nil),
node: node,
pod: test.BuildTestPod("p1", 1000, 2*1000*1000*1000, currentNode.GetName(), nil),
node: otherNode,
podsOnNode: []*v1.Pod{
test.BuildTestPod("p2", 1000, 2*1000*1000*1000, "node", nil),
test.BuildTestPod("p3", 1000, 2*1000*1000*1000, "node", nil),
test.BuildTestPod("p2", 1000, 2*1000*1000*1000, otherNode.GetName(), nil),
test.BuildTestPod("p3", 1000, 2*1000*1000*1000, otherNode.GetName(), nil),
},
err: errors.New("insufficient pods"),
},
{
description: "matches inter-pod anti-affinity rule of pod on node",
pod: test.PodWithPodAntiAffinity(test.BuildTestPod("p1", 1000, 1000, node.Name, nil), "foo", "bar"),
node: node,
description: "matches inter-pod anti-affinity rule of pod on other node",
pod: test.PodWithPodAntiAffinity(test.BuildTestPod("p1", 1000, 1000, currentNode.GetName(), nil), "foo", "bar"),
node: otherNode,
podsOnNode: []*v1.Pod{
test.PodWithPodAntiAffinity(test.BuildTestPod("p2", 1000, 1000, node.Name, nil), "foo", "bar"),
test.PodWithPodAntiAffinity(test.BuildTestPod("p2", 1000, 1000, otherNode.GetName(), nil), "foo", "bar"),
},
err: errors.New("pod matches inter-pod anti-affinity rule of other pod on node"),
err: errors.New("pod matches interpod antiaffinity rule of other pod on node"),
},
{
description: "pod fits on node",
pod: test.BuildTestPod("p1", 1000, 1000, "", func(pod *v1.Pod) {}),
node: node,
description: "pod fits on other node",
pod: test.BuildTestPod("p1", 1000, 1000, currentNode.GetName(), func(pod *v1.Pod) {}),
node: otherNode,
podsOnNode: []*v1.Pod{},
err: nil,
},
Expand Down
45 changes: 34 additions & 11 deletions pkg/utils/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,30 @@ func CheckPodsWithAntiAffinityExist(pod *v1.Pod, pods map[string][]*v1.Pod, node
}
for namespace := range namespaces {
for _, existingPod := range pods[namespace] {
if existingPod.Name != pod.Name && podMatchesTermsNamespaceAndSelector(existingPod, namespaces, selector) {
node, ok := nodeMap[pod.Spec.NodeName]
if !ok {
if existingPod.Name != pod.Name && podMatchesTermsNamespaceAndSelector(existingPod, pod, namespaces, selector) {
nodeCount := len(nodeMap)
if nodeCount == 1 {
if hasSameTopologyKey() {
klog.V(1).InfoS("Found Pod with matching antiaffinity", "evaluated pod", klog.KObj(pod), "existing pod", klog.KObj(existingPod))
return true
}
continue
}
nodeHavingExistingPod, ok := nodeMap[existingPod.Spec.NodeName]
if !ok {
continue
}
if hasSameLabelValue(node, nodeHavingExistingPod, term.TopologyKey) {
klog.V(1).InfoS("Found Pods matching PodAntiAffinity", "pod with anti-affinity", klog.KObj(pod))
return true
} else if nodeCount == 2 {
node, ok := nodeMap[pod.Spec.NodeName]
if !ok {
klog.V(4).InfoS("antiaffinity check: pod being evaluated not in node", "pod", klog.KObj(pod), "node", klog.KObjs(node))
continue
}
nodeHavingExistingPod, ok := nodeMap[existingPod.Spec.NodeName]
if !ok {
klog.V(4).InfoS("antiaffinity check: existing pod not in existing node", "existingPod", klog.KObj(pod), "existing node", klog.KObjs(nodeHavingExistingPod))
continue
}
if hasSameLabelValue(node, nodeHavingExistingPod, term.TopologyKey) {
klog.V(1).InfoS("Found Pods with matching antiaffinity", "evaluated pod", klog.KObj(pod), "existing pod", klog.KObj(existingPod))
return true
}
klog.V(4).InfoS("did not find matching Pod AntiAfffinity on node", "node", klog.KObj(nodeHavingExistingPod))
}
}
}
Expand Down Expand Up @@ -381,3 +393,14 @@ func hasSameLabelValue(node1, node2 *v1.Node, key string) bool {

return value1 == value2
}

func hasSameTopologyKey(currentPod, existingPod *v1.Pod) bool {
if currentPod.Spec.Affinity != nil {
if currentPod.Spec.Affinity.PodAntiAffinity != nil {
return true
}
}

// next: add unit tests
return false
}
22 changes: 22 additions & 0 deletions pkg/utils/predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,28 @@ func TestCheckPodsWithAntiAffinityExist(t *testing.T) {
nodeMap: map[string]*v1.Node{},
expMatch: false,
},
{
name: "found pod with matching anti-affinity on different node",
pod: test.PodWithPodAntiAffinity(test.BuildTestPod("current-pod", 1000, 1000, "current-node", nil), "foo", "bar"),
podsInNamespace: map[string][]*v1.Pod{
"default": {
test.PodWithPodAntiAffinity(test.BuildTestPod("other-pod", 1000, 1000, "other-node", nil), "foo", "bar"),
},
},
nodeMap: map[string]*v1.Node{
"current-node": test.BuildTestNode("current-node", 64000, 128*1000*1000*1000, 2, func(node *v1.Node) {
node.ObjectMeta.Labels = map[string]string{
"region": "main-region",
}
}),
"other-node": test.BuildTestNode("other-node", 64000, 128*1000*1000*1000, 2, func(node *v1.Node) {
node.ObjectMeta.Labels = map[string]string{
"region": "main-region",
}
}),
},
expMatch: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions pkg/utils/priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"sigs.k8s.io/descheduler/pkg/api"
)

Expand All @@ -29,14 +30,18 @@ func getNamespacesFromPodAffinityTerm(pod *v1.Pod, podAffinityTerm *v1.PodAffini

// podMatchesTermsNamespaceAndSelector returns true if the given <pod>
// matches the namespace and selector defined by <affinityPod>`s <term>.
func podMatchesTermsNamespaceAndSelector(pod *v1.Pod, namespaces sets.Set[string], selector labels.Selector) bool {
if !namespaces.Has(pod.Namespace) {
func podMatchesTermsNamespaceAndSelector(existingPod, evaluatedPod *v1.Pod, namespaces sets.Set[string], selector labels.Selector) bool {
if !namespaces.Has(existingPod.Namespace) {
klog.V(4).InfoS("antiaffinity check: pod does not match namespace", "pod", klog.KObj(existingPod), "given namespaces", klog.KObjs(namespaces))
return false
}

if !selector.Matches(labels.Set(pod.Labels)) {
if !selector.Matches(labels.Set(existingPod.Labels)) {
klog.V(4).InfoS("antiaffinity check: selector of Pod being evaluated does not match existing Pod's labels", "evaluated pod", klog.KObj(evaluatedPod), "existing pod", klog.KObj(existingPod))
return false
}

klog.V(4).InfoS("antiaffinity check: selector of Pod being evaluated matches labels of existing Pod", "evaluated pod", klog.KObj(evaluatedPod), "existing pod", klog.KObj(existingPod))
return true
}

Expand Down