123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875 |
- /*
- Copyright 2019 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package nodeaffinity
- import (
- "context"
- "reflect"
- "testing"
- v1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- api "k8s.io/kubernetes/pkg/apis/core"
- framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
- "k8s.io/kubernetes/pkg/scheduler/internal/cache"
- schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
- )
- // TODO: Add test case for RequiredDuringSchedulingRequiredDuringExecution after it's implemented.
- func TestNodeAffinity(t *testing.T) {
- tests := []struct {
- pod *v1.Pod
- labels map[string]string
- nodeName string
- name string
- wantStatus *framework.Status
- }{
- {
- pod: &v1.Pod{},
- name: "no selector",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- NodeSelector: map[string]string{
- "foo": "bar",
- },
- },
- },
- name: "missing labels",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- NodeSelector: map[string]string{
- "foo": "bar",
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "same labels",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- NodeSelector: map[string]string{
- "foo": "bar",
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- "baz": "blah",
- },
- name: "node labels are superset",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- NodeSelector: map[string]string{
- "foo": "bar",
- "baz": "blah",
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "node labels are subset",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar", "value2"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with matchExpressions using In operator that matches the existing node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "kernel-version",
- Operator: v1.NodeSelectorOpGt,
- Values: []string{"0204"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- // We use two digit to denote major version and two digit for minor version.
- "kernel-version": "0206",
- },
- name: "Pod with matchExpressions using Gt operator that matches the existing node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "mem-type",
- Operator: v1.NodeSelectorOpNotIn,
- Values: []string{"DDR", "DDR2"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "mem-type": "DDR3",
- },
- name: "Pod with matchExpressions using NotIn operator that matches the existing node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "GPU",
- Operator: v1.NodeSelectorOpExists,
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "GPU": "NVIDIA-GRID-K1",
- },
- name: "Pod with matchExpressions using Exists operator that matches the existing node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"value1", "value2"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with affinity that don't match node's labels won't schedule onto the node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: nil,
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with a nil []NodeSelectorTerm in affinity, can't match the node's labels and won't schedule onto the node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{},
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with an empty []NodeSelectorTerm in affinity, can't match the node's labels and won't schedule onto the node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{},
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with empty MatchExpressions is not a valid value will match no objects and won't schedule onto the node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{},
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with no Affinity will schedule onto a node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: nil,
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with Affinity but nil NodeSelector will schedule onto a node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "GPU",
- Operator: v1.NodeSelectorOpExists,
- }, {
- Key: "GPU",
- Operator: v1.NodeSelectorOpNotIn,
- Values: []string{"AMD", "INTER"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "GPU": "NVIDIA-GRID-K1",
- },
- name: "Pod with multiple matchExpressions ANDed that matches the existing node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "GPU",
- Operator: v1.NodeSelectorOpExists,
- }, {
- Key: "GPU",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"AMD", "INTER"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "GPU": "NVIDIA-GRID-K1",
- },
- name: "Pod with multiple matchExpressions ANDed that doesn't match the existing node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar", "value2"},
- },
- },
- },
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "diffkey",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"wrong", "value2"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with multiple NodeSelectorTerms ORed in affinity, matches the node's labels and will schedule onto the node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- NodeSelector: map[string]string{
- "foo": "bar",
- },
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpExists,
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with an Affinity and a PodSpec.NodeSelector(the old thing that we are deprecating) " +
- "both are satisfied, will schedule onto the node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- NodeSelector: map[string]string{
- "foo": "bar",
- },
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpExists,
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "barrrrrr",
- },
- name: "Pod with an Affinity matches node's labels but the PodSpec.NodeSelector(the old thing that we are deprecating) " +
- "is not satisfied, won't schedule onto the node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpNotIn,
- Values: []string{"invalid value: ___@#$%^"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- labels: map[string]string{
- "foo": "bar",
- },
- name: "Pod with an invalid value in Affinity term won't be scheduled onto the node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchFields: []v1.NodeSelectorRequirement{
- {
- Key: api.ObjectNameField,
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"node_1"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- nodeName: "node_1",
- name: "Pod with matchFields using In operator that matches the existing node",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchFields: []v1.NodeSelectorRequirement{
- {
- Key: api.ObjectNameField,
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"node_1"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- nodeName: "node_2",
- name: "Pod with matchFields using In operator that does not match the existing node",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchFields: []v1.NodeSelectorRequirement{
- {
- Key: api.ObjectNameField,
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"node_1"},
- },
- },
- },
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- nodeName: "node_2",
- labels: map[string]string{"foo": "bar"},
- name: "Pod with two terms: matchFields does not match, but matchExpressions matches",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchFields: []v1.NodeSelectorRequirement{
- {
- Key: api.ObjectNameField,
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"node_1"},
- },
- },
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- nodeName: "node_2",
- labels: map[string]string{"foo": "bar"},
- name: "Pod with one term: matchFields does not match, but matchExpressions matches",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchFields: []v1.NodeSelectorRequirement{
- {
- Key: api.ObjectNameField,
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"node_1"},
- },
- },
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- nodeName: "node_1",
- labels: map[string]string{"foo": "bar"},
- name: "Pod with one term: both matchFields and matchExpressions match",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
- NodeSelectorTerms: []v1.NodeSelectorTerm{
- {
- MatchFields: []v1.NodeSelectorRequirement{
- {
- Key: api.ObjectNameField,
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"node_1"},
- },
- },
- },
- {
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"not-match-to-bar"},
- },
- },
- },
- },
- },
- },
- },
- },
- },
- nodeName: "node_2",
- labels: map[string]string{"foo": "bar"},
- name: "Pod with two terms: both matchFields and matchExpressions do not match",
- wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- node := v1.Node{ObjectMeta: metav1.ObjectMeta{
- Name: test.nodeName,
- Labels: test.labels,
- }}
- nodeInfo := schedulernodeinfo.NewNodeInfo()
- nodeInfo.SetNode(&node)
- p, _ := New(nil, nil)
- gotStatus := p.(framework.FilterPlugin).Filter(context.Background(), nil, test.pod, nodeInfo)
- if !reflect.DeepEqual(gotStatus, test.wantStatus) {
- t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
- }
- })
- }
- }
- func TestNodeAffinityPriority(t *testing.T) {
- label1 := map[string]string{"foo": "bar"}
- label2 := map[string]string{"key": "value"}
- label3 := map[string]string{"az": "az1"}
- label4 := map[string]string{"abc": "az11", "def": "az22"}
- label5 := map[string]string{"foo": "bar", "key": "value", "az": "az1"}
- affinity1 := &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{{
- Weight: 2,
- Preference: v1.NodeSelectorTerm{
- MatchExpressions: []v1.NodeSelectorRequirement{{
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar"},
- }},
- },
- }},
- },
- }
- affinity2 := &v1.Affinity{
- NodeAffinity: &v1.NodeAffinity{
- PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
- {
- Weight: 2,
- Preference: v1.NodeSelectorTerm{
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar"},
- },
- },
- },
- },
- {
- Weight: 4,
- Preference: v1.NodeSelectorTerm{
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "key",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"value"},
- },
- },
- },
- },
- {
- Weight: 5,
- Preference: v1.NodeSelectorTerm{
- MatchExpressions: []v1.NodeSelectorRequirement{
- {
- Key: "foo",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"bar"},
- },
- {
- Key: "key",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"value"},
- },
- {
- Key: "az",
- Operator: v1.NodeSelectorOpIn,
- Values: []string{"az1"},
- },
- },
- },
- },
- },
- },
- }
- tests := []struct {
- pod *v1.Pod
- nodes []*v1.Node
- expectedList framework.NodeScoreList
- name string
- }{
- {
- pod: &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Annotations: map[string]string{},
- },
- },
- nodes: []*v1.Node{
- {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
- },
- expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}, {Name: "machine3", Score: 0}},
- name: "all machines are same priority as NodeAffinity is nil",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: affinity1,
- },
- },
- nodes: []*v1.Node{
- {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label4}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
- },
- expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}, {Name: "machine3", Score: 0}},
- name: "no machine macthes preferred scheduling requirements in NodeAffinity of pod so all machines' priority is zero",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: affinity1,
- },
- },
- nodes: []*v1.Node{
- {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
- },
- expectedList: []framework.NodeScore{{Name: "machine1", Score: framework.MaxNodeScore}, {Name: "machine2", Score: 0}, {Name: "machine3", Score: 0}},
- name: "only machine1 matches the preferred scheduling requirements of pod",
- },
- {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Affinity: affinity2,
- },
- },
- nodes: []*v1.Node{
- {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine5", Labels: label5}},
- {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
- },
- expectedList: []framework.NodeScore{{Name: "machine1", Score: 18}, {Name: "machine5", Score: framework.MaxNodeScore}, {Name: "machine2", Score: 36}},
- name: "all machines matches the preferred scheduling requirements of pod but with different priorities ",
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- state := framework.NewCycleState()
- fh, _ := framework.NewFramework(nil, nil, nil, framework.WithSnapshotSharedLister(cache.NewSnapshot(nil, test.nodes)))
- p, _ := New(nil, fh)
- var gotList framework.NodeScoreList
- for _, n := range test.nodes {
- nodeName := n.ObjectMeta.Name
- score, status := p.(framework.ScorePlugin).Score(context.Background(), state, test.pod, nodeName)
- if !status.IsSuccess() {
- t.Errorf("unexpected error: %v", status)
- }
- gotList = append(gotList, framework.NodeScore{Name: nodeName, Score: score})
- }
- status := p.(framework.ScorePlugin).ScoreExtensions().NormalizeScore(context.Background(), state, test.pod, gotList)
- if !status.IsSuccess() {
- t.Errorf("unexpected error: %v", status)
- }
- if !reflect.DeepEqual(test.expectedList, gotList) {
- t.Errorf("expected:\n\t%+v,\ngot:\n\t%+v", test.expectedList, gotList)
- }
- })
- }
- }
|