Skip to content

Commit 6cfedd5

Browse files
committed
KUBE-1815: pods with wildcard tolerations are ignored during drain
1 parent 19ccc8e commit 6cfedd5

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

internal/k8s/kubernetes.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func PartitionPodsForEviction(pods []*v1.Pod, castNamespace string, skipDeletedT
457457
continue
458458
}
459459

460-
if IsDaemonSetPod(p) || IsStaticPod(p) {
460+
if IsDaemonSetPod(p) || IsStaticPod(p) || HasWildcardToleration(p) {
461461
nonEvictable = append(nonEvictable, p)
462462
continue
463463
}
@@ -493,7 +493,19 @@ func IsControlledBy(p *v1.Pod, kind string) bool {
493493
}
494494

495495
func IsNonEvictible(p *v1.Pod) bool {
496-
return IsDaemonSetPod(p) || IsStaticPod(p)
496+
return IsDaemonSetPod(p) || IsStaticPod(p) || HasWildcardToleration(p)
497+
}
498+
499+
// HasWildcardToleration returns true if the pod has a toleration that matches all taints
500+
// (i.e. key is empty and operator is Exists). Such pods would be rescheduled back onto
501+
// a cordoned node since they tolerate the node.kubernetes.io/unschedulable taint.
502+
func HasWildcardToleration(p *v1.Pod) bool {
503+
for _, t := range p.Spec.Tolerations {
504+
if t.Key == "" && t.Operator == v1.TolerationOpExists {
505+
return true
506+
}
507+
}
508+
return false
497509
}
498510

499511
// PatchNode patches a node with the given change function.

internal/k8s/kubernetes_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,40 @@ func TestPartitionPodsForEviction(t *testing.T) {
10871087
skipDeletedTimeoutSecs: 60,
10881088
wantEvictableLen: 0,
10891089
},
1090+
{
1091+
name: "pod with wildcard toleration is non-evictable",
1092+
pods: []v1.Pod{
1093+
{
1094+
ObjectMeta: metav1.ObjectMeta{Name: "wildcard-pod", Namespace: "default"},
1095+
Spec: v1.PodSpec{
1096+
Tolerations: []v1.Toleration{
1097+
{Operator: v1.TolerationOpExists},
1098+
},
1099+
},
1100+
Status: v1.PodStatus{Phase: v1.PodRunning},
1101+
},
1102+
},
1103+
castNamespace: testCastNamespace,
1104+
wantNonEvictableLen: 1,
1105+
wantNonEvictablePodNames: []string{"wildcard-pod"},
1106+
},
1107+
{
1108+
name: "pod with specific key toleration is evictable",
1109+
pods: []v1.Pod{
1110+
{
1111+
ObjectMeta: metav1.ObjectMeta{Name: "specific-toleration-pod", Namespace: "default"},
1112+
Spec: v1.PodSpec{
1113+
Tolerations: []v1.Toleration{
1114+
{Key: "node.kubernetes.io/unschedulable", Operator: v1.TolerationOpExists},
1115+
},
1116+
},
1117+
Status: v1.PodStatus{Phase: v1.PodRunning},
1118+
},
1119+
},
1120+
castNamespace: testCastNamespace,
1121+
wantEvictableLen: 1,
1122+
wantEvictablePodNames: []string{"specific-toleration-pod"},
1123+
},
10901124
{
10911125
name: "mixed pods are partitioned correctly",
10921126
pods: []v1.Pod{

0 commit comments

Comments
 (0)