From 30edb81529b84153a185bf380d3109f6d85e2afb Mon Sep 17 00:00:00 2001 From: AxeZhan Date: Sat, 8 Jun 2024 18:58:23 +0800 Subject: [PATCH] tests for nodes with different nodeName and name --- pkg/scheduler/schedule_one_test.go | 250 ++++++++++++++++++++++------- 1 file changed, 195 insertions(+), 55 deletions(-) diff --git a/pkg/scheduler/schedule_one_test.go b/pkg/scheduler/schedule_one_test.go index fdb4519a46c83..f1e18417183c2 100644 --- a/pkg/scheduler/schedule_one_test.go +++ b/pkg/scheduler/schedule_one_test.go @@ -1805,8 +1805,9 @@ func TestSchedulerSchedulePod(t *testing.T) { name string registerPlugins []tf.RegisterPluginFunc extenders []tf.FakeExtender - nodes []string + nodes []*v1.Node pvcs []v1.PersistentVolumeClaim + pvs []v1.PersistentVolume pod *v1.Pod pods []*v1.Pod wantNodes sets.Set[string] @@ -1819,9 +1820,12 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterFilterPlugin("FalseFilter", tf.NewFalseFilterPlugin), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, - pod: st.MakePod().Name("2").UID("2").Obj(), - name: "test 1", + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, + pod: st.MakePod().Name("2").UID("2").Obj(), + name: "test 1", wErr: &framework.FitError{ Pod: st.MakePod().Name("2").UID("2").Obj(), NumAllNodes: 2, @@ -1841,7 +1845,10 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("EqualPrioritizerPlugin", tf.NewEqualPrioritizerPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, pod: st.MakePod().Name("ignore").UID("ignore").Obj(), wantNodes: sets.New("node1", "node2"), name: "test 2", @@ -1854,7 +1861,10 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterFilterPlugin("MatchFilter", tf.NewMatchFilterPlugin), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, pod: st.MakePod().Name("node2").UID("node2").Obj(), wantNodes: sets.New("node2"), name: "test 3", @@ -1867,7 +1877,11 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"3", "2", "1"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + }, pod: st.MakePod().Name("ignore").UID("ignore").Obj(), wantNodes: sets.New("3"), name: "test 4", @@ -1880,7 +1894,11 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"3", "2", "1"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + }, pod: st.MakePod().Name("2").UID("2").Obj(), wantNodes: sets.New("2"), name: "test 5", @@ -1894,7 +1912,11 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("ReverseNumericMap", newReverseNumericMapPlugin(), 2), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"3", "2", "1"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + }, pod: st.MakePod().Name("2").UID("2").Obj(), wantNodes: sets.New("1"), name: "test 6", @@ -1908,9 +1930,13 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"3", "2", "1"}, - pod: st.MakePod().Name("2").UID("2").Obj(), - name: "test 7", + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + }, + pod: st.MakePod().Name("2").UID("2").Obj(), + name: "test 7", wErr: &framework.FitError{ Pod: st.MakePod().Name("2").UID("2").Obj(), NumAllNodes: 3, @@ -1935,9 +1961,12 @@ func TestSchedulerSchedulePod(t *testing.T) { pods: []*v1.Pod{ st.MakePod().Name("2").UID("2").Node("2").Phase(v1.PodRunning).Obj(), }, - pod: st.MakePod().Name("2").UID("2").Obj(), - nodes: []string{"1", "2"}, - name: "test 8", + pod: st.MakePod().Name("2").UID("2").Obj(), + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + }, + name: "test 8", wErr: &framework.FitError{ Pod: st.MakePod().Name("2").UID("2").Obj(), NumAllNodes: 2, @@ -1959,13 +1988,19 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("EqualPrioritizerPlugin", tf.NewEqualPrioritizerPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, pvcs: []v1.PersistentVolumeClaim{ { ObjectMeta: metav1.ObjectMeta{Name: "existingPVC", UID: types.UID("existingPVC"), Namespace: v1.NamespaceDefault}, Spec: v1.PersistentVolumeClaimSpec{VolumeName: "existingPV"}, }, }, + pvs: []v1.PersistentVolume{ + {ObjectMeta: metav1.ObjectMeta{Name: "existingPV"}}, + }, pod: st.MakePod().Name("ignore").UID("ignore").Namespace(v1.NamespaceDefault).PVC("existingPVC").Obj(), wantNodes: sets.New("node1", "node2"), name: "existing PVC", @@ -1979,9 +2014,12 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterFilterPlugin("TrueFilter", tf.NewTrueFilterPlugin), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, - pod: st.MakePod().Name("ignore").UID("ignore").PVC("unknownPVC").Obj(), - name: "unknown PVC", + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, + pod: st.MakePod().Name("ignore").UID("ignore").PVC("unknownPVC").Obj(), + name: "unknown PVC", wErr: &framework.FitError{ Pod: st.MakePod().Name("ignore").UID("ignore").PVC("unknownPVC").Obj(), NumAllNodes: 2, @@ -2003,10 +2041,13 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterFilterPlugin("TrueFilter", tf.NewTrueFilterPlugin), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, - pvcs: []v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "existingPVC", UID: types.UID("existingPVC"), Namespace: v1.NamespaceDefault, DeletionTimestamp: &metav1.Time{}}}}, - pod: st.MakePod().Name("ignore").UID("ignore").Namespace(v1.NamespaceDefault).PVC("existingPVC").Obj(), - name: "deleted PVC", + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, + pvcs: []v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "existingPVC", UID: types.UID("existingPVC"), Namespace: v1.NamespaceDefault, DeletionTimestamp: &metav1.Time{}}}}, + pod: st.MakePod().Name("ignore").UID("ignore").Namespace(v1.NamespaceDefault).PVC("existingPVC").Obj(), + name: "deleted PVC", wErr: &framework.FitError{ Pod: st.MakePod().Name("ignore").UID("ignore").Namespace(v1.NamespaceDefault).PVC("existingPVC").Obj(), NumAllNodes: 2, @@ -2028,10 +2069,13 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("TrueMap", newTrueMapPlugin(), 2), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"2", "1"}, - pod: st.MakePod().Name("2").Obj(), - name: "test error with priority map", - wErr: fmt.Errorf("running Score plugins: %w", fmt.Errorf(`plugin "FalseMap" failed with: %w`, errPrioritize)), + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + }, + pod: st.MakePod().Name("2").Obj(), + name: "test error with priority map", + wErr: fmt.Errorf("running Score plugins: %w", fmt.Errorf(`plugin "FalseMap" failed with: %w`, errPrioritize)), }, { name: "test podtopologyspread plugin - 2 nodes with maxskew=1", @@ -2045,8 +2089,11 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, - pod: st.MakePod().Name("p").UID("p").Label("foo", "").SpreadConstraint(1, "hostname", v1.DoNotSchedule, &metav1.LabelSelector{ + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, + pod: st.MakePod().Name("p").UID("p").Label("foo", "").SpreadConstraint(1, "kubernetes.io/hostname", v1.DoNotSchedule, &metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ { Key: "foo", @@ -2073,8 +2120,12 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("EqualPrioritizerPlugin", tf.NewEqualPrioritizerPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2", "node3"}, - pod: st.MakePod().Name("p").UID("p").Label("foo", "").SpreadConstraint(2, "hostname", v1.DoNotSchedule, &metav1.LabelSelector{ + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node3", Labels: map[string]string{"kubernetes.io/hostname": "node3"}}}, + }, + pod: st.MakePod().Name("p").UID("p").Label("foo", "").SpreadConstraint(2, "kubernetes.io/hostname", v1.DoNotSchedule, &metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ { Key: "foo", @@ -2101,7 +2152,9 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + }, pod: st.MakePod().Name("test-filter").UID("test-filter").Obj(), wantNodes: nil, wErr: &framework.FitError{ @@ -2132,7 +2185,11 @@ func TestSchedulerSchedulePod(t *testing.T) { Predicates: []tf.FitPredicate{tf.FalsePredicateExtender}, }, }, - nodes: []string{"1", "2", "3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + }, pod: st.MakePod().Name("test-filter").UID("test-filter").Obj(), wantNodes: nil, wErr: &framework.FitError{ @@ -2159,7 +2216,9 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "3", Labels: map[string]string{"kubernetes.io/hostname": "3"}}}, + }, pod: st.MakePod().Name("test-filter").UID("test-filter").Obj(), wantNodes: nil, wErr: &framework.FitError{ @@ -2184,7 +2243,10 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"1", "2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + }, pod: st.MakePod().Name("test-filter").UID("test-filter").Obj(), wantNodes: nil, wErr: nil, @@ -2199,7 +2261,10 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"1", "2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + }, pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wantNodes: nil, wErr: &framework.FitError{ @@ -2225,7 +2290,10 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"1", "2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + }, pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wantNodes: nil, wErr: fmt.Errorf(`running PreFilter plugin "FakePreFilter": %w`, errors.New("injected error status")), @@ -2248,7 +2316,11 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2", "node3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node3", Labels: map[string]string{"kubernetes.io/hostname": "node3"}}}, + }, pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wantNodes: sets.New("node2"), // since this case has no score plugin, we'll only try to find one node in Filter stage @@ -2272,8 +2344,12 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2", "node3"}, - pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node3", Labels: map[string]string{"kubernetes.io/hostname": "node3"}}}, + }, + pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wErr: &framework.FitError{ Pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), NumAllNodes: 3, @@ -2302,8 +2378,10 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1"}, - pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + }, + pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wErr: &framework.FitError{ Pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), NumAllNodes: 1, @@ -2330,8 +2408,11 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2"}, - pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, + pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wErr: &framework.FitError{ Pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), NumAllNodes: 2, @@ -2376,7 +2457,11 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterScorePlugin("EqualPrioritizerPlugin", tf.NewEqualPrioritizerPlugin(), 1), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2", "node3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node3", Labels: map[string]string{"kubernetes.io/hostname": "node3"}}}, + }, pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wantNodes: sets.New("node2", "node3"), wantEvaluatedNodes: ptr.To[int32](3), @@ -2392,7 +2477,10 @@ func TestSchedulerSchedulePod(t *testing.T) { framework.NewStatus(framework.Error, "this score function shouldn't be executed because this plugin returned Skip in the PreScore"), ), "PreScore", "Score"), }, - nodes: []string{"node1", "node2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + }, pod: st.MakePod().Name("ignore").UID("ignore").Obj(), wantNodes: sets.New("node1", "node2"), }, @@ -2403,7 +2491,11 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterFilterPlugin("TrueFilter", tf.NewTrueFilterPlugin), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2", "node3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node3", Labels: map[string]string{"kubernetes.io/hostname": "node3"}}}, + }, pod: st.MakePod().Name("pod1").UID("pod1").Obj(), wantNodes: sets.New("node1", "node2", "node3"), wantEvaluatedNodes: ptr.To[int32](1), @@ -2418,7 +2510,11 @@ func TestSchedulerSchedulePod(t *testing.T) { ), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"node1", "node2", "node3"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "node1"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node2", Labels: map[string]string{"kubernetes.io/hostname": "node2"}}}, + {ObjectMeta: metav1.ObjectMeta{Name: "node3", Labels: map[string]string{"kubernetes.io/hostname": "node3"}}}, + }, pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wantNodes: sets.New("node1", "node2"), // since this case has no score plugin, we'll only try to find one node in Filter stage @@ -2437,7 +2533,9 @@ func TestSchedulerSchedulePod(t *testing.T) { tf.RegisterFilterPlugin("TrueFilter", tf.NewTrueFilterPlugin), tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), }, - nodes: []string{"1", "2"}, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "1", Labels: map[string]string{"kubernetes.io/hostname": "1"}}}, {ObjectMeta: metav1.ObjectMeta{Name: "2", Labels: map[string]string{"kubernetes.io/hostname": "2"}}}, + }, pod: st.MakePod().Name("test-prefilter").UID("test-prefilter").Obj(), wantNodes: nil, wErr: &framework.FitError{ @@ -2448,6 +2546,50 @@ func TestSchedulerSchedulePod(t *testing.T) { }, }, }, + { + registerPlugins: []tf.RegisterPluginFunc{ + tf.RegisterQueueSortPlugin(queuesort.Name, queuesort.New), + tf.RegisterPreFilterPlugin(volumebinding.Name, frameworkruntime.FactoryAdapter(fts, volumebinding.New)), + tf.RegisterFilterPlugin("TrueFilter", tf.NewTrueFilterPlugin), + tf.RegisterScorePlugin("EqualPrioritizerPlugin", tf.NewEqualPrioritizerPlugin(), 1), + tf.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New), + }, + nodes: []*v1.Node{ + {ObjectMeta: metav1.ObjectMeta{Name: "node1", Labels: map[string]string{"kubernetes.io/hostname": "host1"}}}, + }, + pvcs: []v1.PersistentVolumeClaim{ + { + ObjectMeta: metav1.ObjectMeta{Name: "PVC1", UID: types.UID("PVC1"), Namespace: v1.NamespaceDefault}, + Spec: v1.PersistentVolumeClaimSpec{VolumeName: "PV1"}, + }, + }, + pvs: []v1.PersistentVolume{ + { + ObjectMeta: metav1.ObjectMeta{Name: "PV1", UID: types.UID("PV1")}, + Spec: v1.PersistentVolumeSpec{ + NodeAffinity: &v1.VolumeNodeAffinity{ + Required: &v1.NodeSelector{ + NodeSelectorTerms: []v1.NodeSelectorTerm{ + { + MatchExpressions: []v1.NodeSelectorRequirement{ + { + Key: "kubernetes.io/hostname", + Operator: v1.NodeSelectorOpIn, + Values: []string{"host1"}, + }, + }, + }, + }, + }, + }, + }, + }, + }, + pod: st.MakePod().Name("pod1").UID("pod1").Namespace(v1.NamespaceDefault).PVC("PVC1").Obj(), + wantNodes: sets.New("node1"), + name: "hostname and nodename of the node do not match", + wErr: nil, + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { @@ -2460,8 +2602,7 @@ func TestSchedulerSchedulePod(t *testing.T) { cache.AddPod(logger, pod) } var nodes []*v1.Node - for _, name := range test.nodes { - node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{"hostname": name}}} + for _, node := range test.nodes { nodes = append(nodes, node) cache.AddNode(logger, node) } @@ -2471,10 +2612,9 @@ func TestSchedulerSchedulePod(t *testing.T) { for _, pvc := range test.pvcs { metav1.SetMetaDataAnnotation(&pvc.ObjectMeta, volume.AnnBindCompleted, "true") cs.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(ctx, &pvc, metav1.CreateOptions{}) - if pvName := pvc.Spec.VolumeName; pvName != "" { - pv := v1.PersistentVolume{ObjectMeta: metav1.ObjectMeta{Name: pvName}} - cs.CoreV1().PersistentVolumes().Create(ctx, &pv, metav1.CreateOptions{}) - } + } + for _, pv := range test.pvs { + cs.CoreV1().PersistentVolumes().Create(ctx, &pv, metav1.CreateOptions{}) } snapshot := internalcache.NewSnapshot(test.pods, nodes) fwk, err := tf.NewFramework(