@@ -55,6 +55,7 @@ import (
5555"k8s.io/kubernetes/test/utils/ktesting"
5656testingclock "k8s.io/utils/clock/testing"
5757"k8s.io/utils/pointer"
58+ "k8s.io/utils/ptr"
5859
5960"github.com/google/go-cmp/cmp"
6061"github.com/stretchr/testify/assert"
@@ -433,6 +434,128 @@ func TestCountTerminatingPods(t *testing.T) {
433434assert .Len (t , terminatingList , int (2 ))
434435}
435436
437+ func TestClaimedPodFiltering (t * testing.T ) {
438+ rsUUID := uuid .NewUUID ()
439+
440+ type podData struct {
441+ podName string
442+ ownerReferences []metav1.OwnerReference
443+ labels map [string ]string
444+ }
445+
446+ type test struct {
447+ name string
448+ pods []podData
449+ wantPodNames []string
450+ }
451+
452+ tests := []test {
453+ {
454+ name : "Filters claimed pods" ,
455+ pods : []podData {
456+ // single owner reference
457+ {podName : "claimed-1" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
458+ {UID : rsUUID , Controller : ptr .To (true )},
459+ }},
460+ {podName : "wrong-selector-1" , labels : map [string ]string {"foo" : "baz" }, ownerReferences : []metav1.OwnerReference {
461+ {UID : rsUUID , Controller : ptr .To (true )},
462+ }},
463+ {podName : "non-controller-1" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
464+ {UID : rsUUID , Controller : nil },
465+ }},
466+ {podName : "other-controller-1" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
467+ {UID : uuid .NewUUID (), Controller : ptr .To (true )},
468+ }},
469+ {podName : "other-workload-1" , labels : map [string ]string {"foo" : "bee" }, ownerReferences : []metav1.OwnerReference {
470+ {UID : uuid .NewUUID (), Controller : ptr .To (true )},
471+ }},
472+ {podName : "standalone-pod-1" , labels : map [string ]string {"foo" : "beetle" }, ownerReferences : []metav1.OwnerReference {}},
473+ // additional controller owner reference set to controller=false
474+ {podName : "claimed-2" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
475+ {UID : uuid .NewUUID (), Controller : ptr .To (false )},
476+ {UID : rsUUID , Controller : ptr .To (true )},
477+ }},
478+ {podName : "wrong-selector-2" , labels : map [string ]string {"foo" : "baz" }, ownerReferences : []metav1.OwnerReference {
479+ {UID : uuid .NewUUID (), Controller : ptr .To (false )},
480+ {UID : rsUUID , Controller : ptr .To (true )},
481+ }},
482+ {podName : "non-controller-2" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
483+ {UID : uuid .NewUUID (), Controller : ptr .To (false )},
484+ {UID : rsUUID , Controller : ptr .To (false )},
485+ }},
486+ {podName : "other-controller-2" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
487+ {UID : uuid .NewUUID (), Controller : ptr .To (false )},
488+ {UID : uuid .NewUUID (), Controller : ptr .To (true )},
489+ }},
490+ {podName : "other-workload-1" , labels : map [string ]string {"foo" : "bee" }, ownerReferences : []metav1.OwnerReference {
491+ {UID : uuid .NewUUID (), Controller : ptr .To (false )},
492+ {UID : uuid .NewUUID (), Controller : ptr .To (true )},
493+ }},
494+ {podName : "standalone-pod-1" , labels : map [string ]string {"foo" : "beetle" }, ownerReferences : []metav1.OwnerReference {
495+ {UID : uuid .NewUUID (), Controller : ptr .To (false )},
496+ }},
497+ // additional controller owner reference set to controller=nil
498+ {podName : "claimed-3" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
499+ {UID : uuid .NewUUID ()},
500+ {UID : rsUUID , Controller : ptr .To (true )},
501+ }},
502+ {podName : "wrong-selector-3" , labels : nil , ownerReferences : []metav1.OwnerReference {
503+ {UID : uuid .NewUUID ()},
504+ {UID : rsUUID , Controller : ptr .To (true )},
505+ }},
506+ {podName : "non-controller-3" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
507+ {UID : uuid .NewUUID ()},
508+ {UID : rsUUID , Controller : nil },
509+ }},
510+ {podName : "other-controller-3" , labels : map [string ]string {"foo" : "bar" }, ownerReferences : []metav1.OwnerReference {
511+ {UID : uuid .NewUUID ()},
512+ {UID : uuid .NewUUID (), Controller : ptr .To (true )},
513+ }},
514+ {podName : "other-workload-1" , labels : map [string ]string {"foo" : "bee" }, ownerReferences : []metav1.OwnerReference {
515+ {UID : uuid .NewUUID ()},
516+ }},
517+ {podName : "standalone-pod-1" , labels : map [string ]string {"foo" : "beetle" }, ownerReferences : []metav1.OwnerReference {
518+ {UID : uuid .NewUUID ()},
519+ }},
520+ },
521+ wantPodNames : []string {"claimed-1" , "claimed-2" , "claimed-3" },
522+ },
523+ }
524+
525+ for _ , test := range tests {
526+ t .Run (test .name , func (t * testing.T ) {
527+ // This rc is not needed by the test, only the newPodList to give the pods labels/a namespace.
528+ rs := newReplicaSet ("test-claim" , 3 , rsUUID )
529+ var pods []* v1.Pod
530+ for _ , p := range test .pods {
531+ pods = append (pods , & v1.Pod {
532+ ObjectMeta : metav1.ObjectMeta {
533+ Name : p .podName ,
534+ Namespace : rs .Namespace ,
535+ Labels : p .labels ,
536+ OwnerReferences : p .ownerReferences ,
537+ },
538+ Status : v1.PodStatus {Phase : v1 .PodRunning },
539+ })
540+ }
541+
542+ selector , err := metav1 .LabelSelectorAsSelector (rs .Spec .Selector )
543+ if err != nil {
544+ t .Fatalf ("Couldn't get selector for object %#v: %v" , rs , err )
545+ }
546+ got := FilterClaimedPods (rs , selector , pods )
547+ gotNames := sets .NewString ()
548+ for _ , pod := range got {
549+ gotNames .Insert (pod .Name )
550+ }
551+
552+ if diff := cmp .Diff (test .wantPodNames , gotNames .List ()); diff != "" {
553+ t .Errorf ("Active pod names (-want,+got):\n %s" , diff )
554+ }
555+ })
556+ }
557+ }
558+
436559func TestActivePodFiltering (t * testing.T ) {
437560logger , _ := ktesting .NewTestContext (t )
438561type podData struct {
0 commit comments