diff --git a/pkg/framework/plugins/defaultevictor/defaultevictor_test.go b/pkg/framework/plugins/defaultevictor/defaultevictor_test.go index 61dd90e57..f88b75986 100644 --- a/pkg/framework/plugins/defaultevictor/defaultevictor_test.go +++ b/pkg/framework/plugins/defaultevictor/defaultevictor_test.go @@ -60,8 +60,28 @@ type testCase struct { pvcs []*v1.PersistentVolumeClaim } +func buildTestNode(name string, apply func(*v1.Node)) *v1.Node { + return test.BuildTestNode(name, 1000, 2000, 13, apply) +} + +func buildTestPod(name, nodeName string, apply func(*v1.Pod)) *v1.Pod { + return test.BuildTestPod(name, 400, 0, nodeName, apply) +} + +func newProtectedStorageClassesConfig(storageClassNames ...string) *PodProtectionsConfig { + protectedClasses := make([]ProtectedStorageClass, len(storageClassNames)) + for i, name := range storageClassNames { + protectedClasses[i] = ProtectedStorageClass{Name: name} + } + return &PodProtectionsConfig{ + PodsWithPVC: &PodsWithPVCConfig{ + ProtectedStorageClasses: protectedClasses, + }, + } +} + func TestDefaultEvictorPreEvictionFilter(t *testing.T) { - n1 := test.BuildTestNode("node1", 1000, 2000, 13, nil) + n1 := buildTestNode("node1", nil) nodeTaintKey := "hardware" nodeTaintValue := "gpu" @@ -69,40 +89,44 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { nodeLabelKey := "datacenter" nodeLabelValue := "east" + setNodeTaint := func(node *v1.Node) { + node.Spec.Taints = []v1.Taint{ + { + Key: nodeTaintKey, + Value: nodeTaintValue, + Effect: v1.TaintEffectNoSchedule, + }, + } + } + + setNodeLabel := func(node *v1.Node) { + node.ObjectMeta.Labels = map[string]string{ + nodeLabelKey: nodeLabelValue, + } + } + + setPodNodeSelector := func(pod *v1.Pod) { + pod.Spec.NodeSelector = map[string]string{ + nodeLabelKey: nodeLabelValue, + } + } + testCases := []testCase{ { description: "Pod with no tolerations running on normal node, all other nodes tainted", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - }), + buildTestPod("p1", n1.Name, test.SetNormalOwnerRef), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 1000, 2000, 13, func(node *v1.Node) { - node.Spec.Taints = []v1.Taint{ - { - Key: nodeTaintKey, - Value: nodeTaintValue, - Effect: v1.TaintEffectNoSchedule, - }, - } - }), - test.BuildTestNode("node3", 1000, 2000, 13, func(node *v1.Node) { - node.Spec.Taints = []v1.Taint{ - { - Key: nodeTaintKey, - Value: nodeTaintValue, - Effect: v1.TaintEffectNoSchedule, - }, - } - }), + buildTestNode("node2", setNodeTaint), + buildTestNode("node3", setNodeTaint), }, nodeFit: true, }, { description: "Pod with correct tolerations running on normal node, all other nodes tainted", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p1", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.Tolerations = []v1.Toleration{ { Key: nodeTaintKey, @@ -113,71 +137,37 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 1000, 2000, 13, func(node *v1.Node) { - node.Spec.Taints = []v1.Taint{ - { - Key: nodeTaintKey, - Value: nodeTaintValue, - Effect: v1.TaintEffectNoSchedule, - }, - } - }), - test.BuildTestNode("node3", 1000, 2000, 13, func(node *v1.Node) { - node.Spec.Taints = []v1.Taint{ - { - Key: nodeTaintKey, - Value: nodeTaintValue, - Effect: v1.TaintEffectNoSchedule, - }, - } - }), + buildTestNode("node2", setNodeTaint), + buildTestNode("node3", setNodeTaint), }, nodeFit: true, result: true, }, { description: "Pod with incorrect node selector", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p1", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.NodeSelector = map[string]string{ nodeLabelKey: "fail", } }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 1000, 2000, 13, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), - test.BuildTestNode("node3", 1000, 2000, 13, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), + buildTestNode("node2", setNodeLabel), + buildTestNode("node3", setNodeLabel), }, nodeFit: true, }, { description: "Pod with correct node selector", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.NodeSelector = map[string]string{ - nodeLabelKey: nodeLabelValue, - } + buildTestPod("p1", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodNodeSelector(pod) }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 1000, 2000, 13, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), - test.BuildTestNode("node3", 1000, 2000, 13, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), + buildTestNode("node2", setNodeLabel), + buildTestNode("node3", setNodeLabel), }, nodeFit: true, result: true, @@ -185,33 +175,21 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { description: "Pod with correct node selector, but only available node doesn't have enough CPU", pods: []*v1.Pod{ test.BuildTestPod("p1", 12, 8, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.NodeSelector = map[string]string{ - nodeLabelKey: nodeLabelValue, - } + test.SetNormalOwnerRef(pod) + setPodNodeSelector(pod) }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2-TEST", 10, 16, 10, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), - test.BuildTestNode("node3-TEST", 10, 16, 10, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), + test.BuildTestNode("node2-TEST", 10, 16, 10, setNodeLabel), + test.BuildTestNode("node3-TEST", 10, 16, 10, setNodeLabel), }, nodeFit: true, }, { description: "Pod with correct node selector, and one node has enough memory", pods: []*v1.Pod{ test.BuildTestPod("p1", 12, 8, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.NodeSelector = map[string]string{ - nodeLabelKey: nodeLabelValue, - } + test.SetNormalOwnerRef(pod) + setPodNodeSelector(pod) }), test.BuildTestPod("node2-pod-10GB-mem", 20, 10, "node2", func(pod *v1.Pod) { pod.ObjectMeta.Labels = map[string]string{ @@ -225,16 +203,8 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 100, 16, 10, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), - test.BuildTestNode("node3", 100, 20, 10, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), + test.BuildTestNode("node2", 100, 16, 10, setNodeLabel), + test.BuildTestNode("node3", 100, 20, 10, setNodeLabel), }, nodeFit: true, result: true, @@ -242,10 +212,8 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { description: "Pod with correct node selector, but both nodes don't have enough memory", pods: []*v1.Pod{ test.BuildTestPod("p1", 12, 8, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.NodeSelector = map[string]string{ - nodeLabelKey: nodeLabelValue, - } + test.SetNormalOwnerRef(pod) + setPodNodeSelector(pod) }), test.BuildTestPod("node2-pod-10GB-mem", 10, 10, "node2", func(pod *v1.Pod) { pod.ObjectMeta.Labels = map[string]string{ @@ -259,39 +227,23 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 100, 16, 10, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), - test.BuildTestNode("node3", 100, 16, 10, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), + test.BuildTestNode("node2", 100, 16, 10, setNodeLabel), + test.BuildTestNode("node3", 100, 16, 10, setNodeLabel), }, nodeFit: true, }, { description: "Pod with incorrect node selector, but nodefit false, should still be evicted", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p1", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.NodeSelector = map[string]string{ nodeLabelKey: "fail", } }), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 1000, 2000, 13, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), - test.BuildTestNode("node3", 1000, 2000, 13, func(node *v1.Node) { - node.ObjectMeta.Labels = map[string]string{ - nodeLabelKey: nodeLabelValue, - } - }), + buildTestNode("node2", setNodeLabel), + buildTestNode("node3", setNodeLabel), }, result: true, }, @@ -316,14 +268,50 @@ func TestDefaultEvictorPreEvictionFilter(t *testing.T) { } func TestDefaultEvictorFilter(t *testing.T) { - n1 := test.BuildTestNode("node1", 1000, 2000, 13, nil) + n1 := buildTestNode("node1", nil) lowPriority := int32(800) highPriority := int32(900) minPodAge := metav1.Duration{Duration: 50 * time.Minute} - nodeTaintKey := "hardware" - nodeTaintValue := "gpu" + setNodeTaint := func(node *v1.Node) { + node.Spec.Taints = []v1.Taint{ + { + Key: "hardware", + Value: "gpu", + Effect: v1.TaintEffectNoSchedule, + }, + } + } + + setPodEvictAnnotation := func(pod *v1.Pod) { + pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} + } + + setPodLocalStorage := func(pod *v1.Pod) { + pod.Spec.Volumes = []v1.Volume{ + { + Name: "sample", + VolumeSource: v1.VolumeSource{ + HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, + EmptyDir: &v1.EmptyDirVolumeSource{ + SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), + }, + }, + }, + } + } + + setPodPVCVolumeWithFooClaimName := func(pod *v1.Pod) { + pod.Spec.Volumes = []v1.Volume{ + { + Name: "pvc", + VolumeSource: v1.VolumeSource{ + PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: "foo"}, + }, + }, + } + } ownerRefUUID := uuid.NewUUID() @@ -331,20 +319,20 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Failed pod eviction with no ownerRefs", pods: []*v1.Pod{ - test.BuildTestPod("bare_pod_failed", 400, 0, n1.Name, func(pod *v1.Pod) { + buildTestPod("bare_pod_failed", n1.Name, func(pod *v1.Pod) { pod.Status.Phase = v1.PodFailed }), }, }, { description: "Normal pod eviction with no ownerRefs and evictFailedBarePods enabled", - pods: []*v1.Pod{test.BuildTestPod("bare_pod", 400, 0, n1.Name, nil)}, + pods: []*v1.Pod{buildTestPod("bare_pod", n1.Name, nil)}, evictFailedBarePods: true, }, { description: "Failed pod eviction with no ownerRefs", pods: []*v1.Pod{ - test.BuildTestPod("bare_pod_failed_but_can_be_evicted", 400, 0, n1.Name, func(pod *v1.Pod) { + buildTestPod("bare_pod_failed_but_can_be_evicted", n1.Name, func(pod *v1.Pod) { pod.Status.Phase = v1.PodFailed }), }, @@ -354,18 +342,16 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Normal pod eviction with normal ownerRefs", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - }), + buildTestPod("p1", n1.Name, test.SetNormalOwnerRef), }, result: true, }, { description: "Normal pod eviction with normal ownerRefs and " + evictPodAnnotationKey + " annotation", pods: []*v1.Pod{ - test.BuildTestPod("p2", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p2", n1.Name, func(pod *v1.Pod) { + setPodEvictAnnotation(pod) + test.SetNormalOwnerRef(pod) }), }, result: true, @@ -373,9 +359,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Normal pod eviction with normal ownerRefs and " + evictionutils.SoftNoEvictionAnnotationKey + " annotation (preference)", pods: []*v1.Pod{ - test.BuildTestPod("p2", 400, 0, n1.Name, func(pod *v1.Pod) { + buildTestPod("p2", n1.Name, func(pod *v1.Pod) { pod.Annotations = map[string]string{evictionutils.SoftNoEvictionAnnotationKey: ""} - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) }), }, evictLocalStoragePods: false, @@ -385,9 +371,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Normal pod eviction with normal ownerRefs and " + evictionutils.SoftNoEvictionAnnotationKey + " annotation (mandatory)", pods: []*v1.Pod{ - test.BuildTestPod("p2", 400, 0, n1.Name, func(pod *v1.Pod) { + buildTestPod("p2", n1.Name, func(pod *v1.Pod) { pod.Annotations = map[string]string{evictionutils.SoftNoEvictionAnnotationKey: ""} - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) }), }, evictLocalStoragePods: false, @@ -398,18 +384,16 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Normal pod eviction with replicaSet ownerRefs", pods: []*v1.Pod{ - test.BuildTestPod("p3", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - }), + buildTestPod("p3", n1.Name, test.SetNormalOwnerRef), }, result: true, }, { description: "Normal pod eviction with replicaSet ownerRefs and " + evictPodAnnotationKey + " annotation", pods: []*v1.Pod{ - test.BuildTestPod("p4", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} - pod.ObjectMeta.OwnerReferences = test.GetReplicaSetOwnerRefList() + buildTestPod("p4", n1.Name, func(pod *v1.Pod) { + setPodEvictAnnotation(pod) + test.SetRSOwnerRef(pod) }), }, result: true, @@ -417,18 +401,16 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Normal pod eviction with statefulSet ownerRefs", pods: []*v1.Pod{ - test.BuildTestPod("p18", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - }), + buildTestPod("p18", n1.Name, test.SetNormalOwnerRef), }, result: true, }, { description: "Normal pod eviction with statefulSet ownerRefs and " + evictPodAnnotationKey + " annotation", pods: []*v1.Pod{ - test.BuildTestPod("p19", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} - pod.ObjectMeta.OwnerReferences = test.GetStatefulSetOwnerRefList() + buildTestPod("p19", n1.Name, func(pod *v1.Pod) { + setPodEvictAnnotation(pod) + test.SetSSOwnerRef(pod) }), }, result: true, @@ -436,38 +418,18 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod not evicted because it is bound to a PV and evictLocalStoragePods = false", pods: []*v1.Pod{ - test.BuildTestPod("p5", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + buildTestPod("p5", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodLocalStorage(pod) }), }, }, { description: "Pod is evicted because it is bound to a PV and evictLocalStoragePods = true", pods: []*v1.Pod{ - test.BuildTestPod("p6", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + buildTestPod("p6", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodLocalStorage(pod) }), }, evictLocalStoragePods: true, @@ -476,20 +438,10 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it is bound to a PV and evictLocalStoragePods = false, but it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p7", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + buildTestPod("p7", n1.Name, func(pod *v1.Pod) { + setPodEvictAnnotation(pod) + test.SetNormalOwnerRef(pod) + setPodLocalStorage(pod) }), }, result: true, @@ -497,8 +449,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod not evicted because it is part of a daemonSet", pods: []*v1.Pod{ - test.BuildTestPod("p8", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p8", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.ObjectMeta.OwnerReferences = test.GetDaemonSetOwnerRefList() }), }, @@ -506,8 +458,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it is part of a daemonSet, but it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p9", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} + buildTestPod("p9", n1.Name, func(pod *v1.Pod) { + setPodEvictAnnotation(pod) pod.ObjectMeta.OwnerReferences = test.GetDaemonSetOwnerRefList() }), }, @@ -516,18 +468,18 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod not evicted because it is a mirror poddsa", pods: []*v1.Pod{ - test.BuildTestPod("p10", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Annotations = test.GetMirrorPodAnnotation() + buildTestPod("p10", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + test.SetMirrorPodAnnotation(pod) }), }, }, { description: "Pod is evicted because it is a mirror pod, but it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p11", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Annotations = test.GetMirrorPodAnnotation() + buildTestPod("p11", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + test.SetMirrorPodAnnotation(pod) pod.Annotations[evictPodAnnotationKey] = "true" }), }, @@ -536,20 +488,18 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod not evicted because it has system critical priority", pods: []*v1.Pod{ - test.BuildTestPod("p12", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + buildTestPod("p12", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), }, }, { description: "Pod is evicted because it has system critical priority, but it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p13", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + buildTestPod("p13", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + test.SetPodPriority(pod, utils.SystemCriticalPriority) pod.Annotations = map[string]string{ evictPodAnnotationKey: "true", } @@ -560,8 +510,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod not evicted because it has a priority higher than the configured priority threshold", pods: []*v1.Pod{ - test.BuildTestPod("p14", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p14", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.Priority = &highPriority }), }, @@ -570,9 +520,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it has a priority higher than the configured priority threshold, but it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p15", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} + buildTestPod("p15", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodEvictAnnotation(pod) pod.Spec.Priority = &highPriority }), }, @@ -582,10 +532,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it has system critical priority, but evictSystemCriticalPods = true", pods: []*v1.Pod{ - test.BuildTestPod("p16", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + buildTestPod("p16", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), }, evictSystemCriticalPods: true, @@ -594,11 +543,10 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it has system critical priority, but evictSystemCriticalPods = true and it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p16", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + buildTestPod("p16", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodEvictAnnotation(pod) + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), }, evictSystemCriticalPods: true, @@ -607,8 +555,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it has a priority higher than the configured priority threshold, but evictSystemCriticalPods = true", pods: []*v1.Pod{ - test.BuildTestPod("p17", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p17", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.Priority = &highPriority }), }, @@ -619,9 +567,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod is evicted because it has a priority higher than the configured priority threshold, but evictSystemCriticalPods = true and it has scheduler.alpha.kubernetes.io/evict annotation", pods: []*v1.Pod{ - test.BuildTestPod("p17", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Annotations = map[string]string{evictPodAnnotationKey: "true"} + buildTestPod("p17", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodEvictAnnotation(pod) pod.Spec.Priority = &highPriority }), }, @@ -632,29 +580,11 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod with no tolerations running on normal node, all other nodes tainted, no PreEvictionFilter, should ignore nodeFit", pods: []*v1.Pod{ - test.BuildTestPod("p1", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - }), + buildTestPod("p1", n1.Name, test.SetNormalOwnerRef), }, nodes: []*v1.Node{ - test.BuildTestNode("node2", 1000, 2000, 13, func(node *v1.Node) { - node.Spec.Taints = []v1.Taint{ - { - Key: nodeTaintKey, - Value: nodeTaintValue, - Effect: v1.TaintEffectNoSchedule, - }, - } - }), - test.BuildTestNode("node3", 1000, 2000, 13, func(node *v1.Node) { - node.Spec.Taints = []v1.Taint{ - { - Key: nodeTaintKey, - Value: nodeTaintValue, - Effect: v1.TaintEffectNoSchedule, - }, - } - }), + buildTestNode("node2", setNodeTaint), + buildTestNode("node3", setNodeTaint), }, nodeFit: true, result: true, @@ -663,11 +593,11 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "minReplicas of 2, owner with 2 replicas, evicts", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) pod.ObjectMeta.OwnerReferences[0].UID = ownerRefUUID }), test.BuildTestPod("p2", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) pod.ObjectMeta.OwnerReferences[0].UID = ownerRefUUID }), }, @@ -678,11 +608,11 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "minReplicas of 3, owner with 2 replicas, no eviction", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) pod.ObjectMeta.OwnerReferences[0].UID = ownerRefUUID }), test.BuildTestPod("p2", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) pod.ObjectMeta.OwnerReferences[0].UID = ownerRefUUID }), }, @@ -696,7 +626,7 @@ func TestDefaultEvictorFilter(t *testing.T) { pod.ObjectMeta.OwnerReferences[0].UID = ownerRefUUID }), test.BuildTestPod("p2", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) }), }, minReplicas: 2, @@ -706,7 +636,7 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "minPodAge of 50, pod created 10 minutes ago, no eviction", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) podStartTime := metav1.Now().Add(time.Minute * time.Duration(-10)) pod.Status.StartTime = &metav1.Time{Time: podStartTime} }), @@ -717,7 +647,7 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "minPodAge of 50, pod created 60 minutes ago, evicts", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) podStartTime := metav1.Now().Add(time.Minute * time.Duration(-60)) pod.Status.StartTime = &metav1.Time{Time: podStartTime} }), @@ -729,7 +659,7 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "nil minPodAge, pod created 60 minutes ago, evicts", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) podStartTime := metav1.Now().Add(time.Minute * time.Duration(-60)) pod.Status.StartTime = &metav1.Time{Time: podStartTime} }), @@ -740,7 +670,7 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "ignorePodsWithoutPDB, pod with no PDBs, no eviction", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) pod.Labels = map[string]string{ "app": "foo", } @@ -752,7 +682,7 @@ func TestDefaultEvictorFilter(t *testing.T) { description: "ignorePodsWithoutPDB, pod with PDBs, evicts", pods: []*v1.Pod{ test.BuildTestPod("p1", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) pod.Labels = map[string]string{ "app": "foo", } @@ -767,15 +697,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "ignorePvcPods is set, pod with PVC, not evicts", pods: []*v1.Pod{ - test.BuildTestPod("p15", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "pvc", VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: "foo"}, - }, - }, - } + buildTestPod("p15", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodPVCVolumeWithFooClaimName(pod) }), }, ignorePvcPods: true, @@ -783,15 +707,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "ignorePvcPods is not set, pod with PVC, evicts", pods: []*v1.Pod{ - test.BuildTestPod("p15", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "pvc", VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: "foo"}, - }, - }, - } + buildTestPod("p15", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodPVCVolumeWithFooClaimName(pod) }), }, result: true, @@ -799,8 +717,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod with local storage is evicted because 'PodsWithLocalStorage' is in DefaultDisabled", pods: []*v1.Pod{ - test.BuildTestPod("p18", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p18", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.Volumes = []v1.Volume{ { Name: "local-storage", VolumeSource: v1.VolumeSource{ @@ -818,7 +736,7 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "DaemonSet pod is evicted because 'DaemonSetPods' is in DefaultDisabled", pods: []*v1.Pod{ - test.BuildTestPod("p19", 400, 0, n1.Name, func(pod *v1.Pod) { + buildTestPod("p19", n1.Name, func(pod *v1.Pod) { pod.ObjectMeta.OwnerReferences = []metav1.OwnerReference{ { Kind: "DaemonSet", @@ -836,15 +754,9 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod with PVC is not evicted because 'PodsWithPVC' is in ExtraEnabled", pods: []*v1.Pod{ - test.BuildTestPod("p20", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "pvc", VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: "foo"}, - }, - }, - } + buildTestPod("p20", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodPVCVolumeWithFooClaimName(pod) }), }, podProtections: PodProtections{ @@ -855,9 +767,7 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod without PDB is not evicted because 'PodsWithoutPDB' is in ExtraEnabled", pods: []*v1.Pod{ - test.BuildTestPod("p21", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - }), + buildTestPod("p21", n1.Name, test.SetNormalOwnerRef), }, podProtections: PodProtections{ ExtraEnabled: []PodProtection{PodsWithoutPDB}, @@ -867,8 +777,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod with ResourceClaims is not evicted because 'PodsWithResourceClaims' is in ExtraEnabled", pods: []*v1.Pod{ - test.BuildTestPod("p20", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p20", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.ResourceClaims = []v1.PodResourceClaim{ { Name: "test-claim", @@ -885,30 +795,14 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod using StorageClass is not evicted because 'PodsWithPVC' is in ExtraEnabled", pods: []*v1.Pod{ - test.BuildTestPod("p23", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "pvc", VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ - ClaimName: "foo", - }, - }, - }, - } + buildTestPod("p23", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodPVCVolumeWithFooClaimName(pod) }), }, podProtections: PodProtections{ ExtraEnabled: []PodProtection{PodsWithPVC}, - Config: &PodProtectionsConfig{ - PodsWithPVC: &PodsWithPVCConfig{ - ProtectedStorageClasses: []ProtectedStorageClass{ - { - Name: "standard", - }, - }, - }, - }, + Config: newProtectedStorageClassesConfig("standard"), }, pvcs: []*v1.PersistentVolumeClaim{ test.BuildTestPVC("foo", "standard"), @@ -918,30 +812,14 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod using unprotected StorageClass is evicted even though 'PodsWithPVC' is in ExtraEnabled", pods: []*v1.Pod{ - test.BuildTestPod("p24", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "pvc", VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ - ClaimName: "foo", - }, - }, - }, - } + buildTestPod("p24", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodPVCVolumeWithFooClaimName(pod) }), }, podProtections: PodProtections{ ExtraEnabled: []PodProtection{PodsWithPVC}, - Config: &PodProtectionsConfig{ - PodsWithPVC: &PodsWithPVCConfig{ - ProtectedStorageClasses: []ProtectedStorageClass{ - { - Name: "protected", - }, - }, - }, - }, + Config: newProtectedStorageClassesConfig("protected"), }, pvcs: []*v1.PersistentVolumeClaim{ test.BuildTestPVC("foo", "unprotected"), @@ -951,30 +829,14 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod using unexisting PVC is not evicted because we cannot determine if storage class is protected or not", pods: []*v1.Pod{ - test.BuildTestPod("p25", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() - pod.Spec.Volumes = []v1.Volume{ - { - Name: "pvc", VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ - ClaimName: "foo", - }, - }, - }, - } + buildTestPod("p25", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) + setPodPVCVolumeWithFooClaimName(pod) }), }, podProtections: PodProtections{ ExtraEnabled: []PodProtection{PodsWithPVC}, - Config: &PodProtectionsConfig{ - PodsWithPVC: &PodsWithPVCConfig{ - ProtectedStorageClasses: []ProtectedStorageClass{ - { - Name: "protected", - }, - }, - }, - }, + Config: newProtectedStorageClassesConfig("protected"), }, pvcs: []*v1.PersistentVolumeClaim{}, result: false, @@ -982,8 +844,8 @@ func TestDefaultEvictorFilter(t *testing.T) { { description: "Pod using protected and unprotected StorageClasses is not evicted", pods: []*v1.Pod{ - test.BuildTestPod("p26", 400, 0, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + buildTestPod("p26", n1.Name, func(pod *v1.Pod) { + test.SetNormalOwnerRef(pod) pod.Spec.Volumes = []v1.Volume{ { Name: "protected-pvc", VolumeSource: v1.VolumeSource{ @@ -1004,15 +866,7 @@ func TestDefaultEvictorFilter(t *testing.T) { }, podProtections: PodProtections{ ExtraEnabled: []PodProtection{PodsWithPVC}, - Config: &PodProtectionsConfig{ - PodsWithPVC: &PodsWithPVCConfig{ - ProtectedStorageClasses: []ProtectedStorageClass{ - { - Name: "protected", - }, - }, - }, - }, + Config: newProtectedStorageClassesConfig("protected"), }, pvcs: []*v1.PersistentVolumeClaim{ test.BuildTestPVC("protected", "protected"), @@ -1041,7 +895,7 @@ func TestDefaultEvictorFilter(t *testing.T) { } func TestReinitialization(t *testing.T) { - n1 := test.BuildTestNode("node1", 1000, 2000, 13, nil) + n1 := buildTestNode("node1", nil) ownerRefUUID := uuid.NewUUID() testCases := []testCase{ @@ -1053,7 +907,7 @@ func TestReinitialization(t *testing.T) { pod.ObjectMeta.OwnerReferences[0].UID = ownerRefUUID }), test.BuildTestPod("p2", 1, 1, n1.Name, func(pod *v1.Pod) { - pod.ObjectMeta.OwnerReferences = test.GetNormalPodOwnerRefList() + test.SetNormalOwnerRef(pod) }), }, minReplicas: 2, @@ -1287,14 +1141,7 @@ func Test_protectedPVCStorageClasses(t *testing.T) { name: "storage classes specified", args: &DefaultEvictorArgs{ PodProtections: PodProtections{ - Config: &PodProtectionsConfig{ - PodsWithPVC: &PodsWithPVCConfig{ - ProtectedStorageClasses: []ProtectedStorageClass{ - {Name: "sc1"}, - {Name: "sc2"}, - }, - }, - }, + Config: newProtectedStorageClassesConfig("sc1", "sc2"), }, }, expected: []ProtectedStorageClass{