Skip to content

Commit 90f6262

Browse files
authored
Add filtering before DeleteOrEvictPods (#1106)
* Add filtering before DeleteOrEvictPods
1 parent 07c0756 commit 90f6262

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

pkg/node/node.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const (
6161
RebalanceRecommendationTaint = "aws-node-termination-handler/rebalance-recommendation"
6262

6363
maxTaintValueLength = 63
64+
daemonSet = "DaemonSet"
6465
)
6566

6667
const (
@@ -144,6 +145,7 @@ func (n Node) CordonAndDrain(nodeName string, reason string, recorder recorderIn
144145
}
145146
if n.nthConfig.UseAPIServerCacheToListPods {
146147
if pods != nil {
148+
pods = n.FilterOutDaemonSetPods(pods)
147149
err = n.drainHelper.DeleteOrEvictPods(pods.Items)
148150
}
149151
} else {
@@ -647,6 +649,23 @@ func (n Node) fetchAllPods(nodeName string) (*corev1.PodList, error) {
647649
return n.drainHelper.Client.CoreV1().Pods("").List(context.TODO(), listOptions)
648650
}
649651

652+
// FilterOutDaemonSetPods filters a list of pods to exclude DaemonSet pods when IgnoreDaemonSets is enabled
653+
func (n *Node) FilterOutDaemonSetPods(pods *corev1.PodList) *corev1.PodList {
654+
if !n.nthConfig.IgnoreDaemonSets {
655+
return pods
656+
}
657+
658+
var nonDaemonSetPods []corev1.Pod
659+
for _, pod := range pods.Items {
660+
if !isDaemonSetPod(pod) {
661+
nonDaemonSetPods = append(nonDaemonSetPods, pod)
662+
}
663+
}
664+
665+
pods.Items = nonDaemonSetPods
666+
return pods
667+
}
668+
650669
func getDrainHelper(nthConfig config.Config, clientset *kubernetes.Clientset) (*drain.Helper, error) {
651670
drainHelper := &drain.Helper{
652671
Ctx: context.TODO(),
@@ -838,6 +857,15 @@ func filterPodForDeletion(podName, podNamespace string) func(pod corev1.Pod) dra
838857
}
839858
}
840859

860+
func isDaemonSetPod(pod corev1.Pod) bool {
861+
for _, owner := range pod.OwnerReferences {
862+
if owner.Kind == daemonSet {
863+
return true
864+
}
865+
}
866+
return false
867+
}
868+
841869
type recorderInterface interface {
842870
AnnotatedEventf(object runtime.Object, annotations map[string]string, eventType, reason, messageFmt string, args ...interface{})
843871
}

pkg/node/node_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/aws/aws-node-termination-handler/pkg/node"
2727
h "github.com/aws/aws-node-termination-handler/pkg/test"
2828
"github.com/aws/aws-node-termination-handler/pkg/uptime"
29+
corev1 "k8s.io/api/core/v1"
2930
v1 "k8s.io/api/core/v1"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/client-go/kubernetes/fake"
@@ -377,3 +378,43 @@ func TestUncordonIfRebootedTimeParseFailure(t *testing.T) {
377378
err = tNode.UncordonIfRebooted(nodeName)
378379
h.Assert(t, err != nil, "Failed to return error on UncordonIfReboted failure to parse time")
379380
}
381+
382+
func TestFilterOutDaemonSetPods(t *testing.T) {
383+
tNode, err := newNode(config.Config{IgnoreDaemonSets: true}, fake.NewSimpleClientset())
384+
h.Ok(t, err)
385+
386+
mockPodList := &corev1.PodList{
387+
Items: []corev1.Pod{
388+
{
389+
ObjectMeta: metav1.ObjectMeta{
390+
Name: "mock-daemon-pod",
391+
OwnerReferences: []metav1.OwnerReference{
392+
{
393+
Kind: "DaemonSet",
394+
Name: "daemon-1",
395+
},
396+
},
397+
},
398+
},
399+
{
400+
ObjectMeta: metav1.ObjectMeta{
401+
Name: "mock-replica-pod",
402+
OwnerReferences: []metav1.OwnerReference{
403+
{
404+
Kind: "ReplicaSet",
405+
Name: "replica-1",
406+
},
407+
},
408+
},
409+
},
410+
{
411+
ObjectMeta: metav1.ObjectMeta{
412+
Name: "mock-regular-pod",
413+
},
414+
},
415+
},
416+
}
417+
418+
filteredMockPodList := tNode.FilterOutDaemonSetPods(mockPodList)
419+
h.Equals(t, 2, len(filteredMockPodList.Items))
420+
}

0 commit comments

Comments
 (0)