k3s/plugin/pkg/scheduler/generic_scheduler_test.go

365 lines
11 KiB
Go
Raw Normal View History

2014-09-24 16:32:36 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2014-09-24 16:32:36 +00:00
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 scheduler
import (
"fmt"
"math"
"reflect"
2014-09-24 16:32:36 +00:00
"strconv"
"testing"
"time"
2014-09-24 16:32:36 +00:00
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/wait"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
algorithmpredicates "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
2015-09-04 06:50:14 +00:00
schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
2014-09-24 16:32:36 +00:00
)
2016-07-08 06:12:44 +00:00
func falsePredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, error) {
2016-04-13 03:07:34 +00:00
return false, algorithmpredicates.ErrFakePredicate
}
2016-07-08 06:12:44 +00:00
func truePredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, error) {
return true, nil
}
2016-07-08 06:12:44 +00:00
func matchesPredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, error) {
node := nodeInfo.Node()
if node == nil {
return false, fmt.Errorf("node not found")
}
if pod.Name == node.Name {
return true, nil
}
2016-04-13 03:07:34 +00:00
return false, algorithmpredicates.ErrFakePredicate
2014-09-24 16:32:36 +00:00
}
2016-07-08 06:12:44 +00:00
func hasNoPodsPredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, error) {
if len(nodeInfo.Pods()) == 0 {
return true, nil
}
2016-04-13 03:07:34 +00:00
return false, algorithmpredicates.ErrFakePredicate
}
func numericPriority(pod *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodeLister algorithm.NodeLister) (schedulerapi.HostPriorityList, error) {
nodes, err := nodeLister.List()
2015-09-04 06:50:14 +00:00
result := []schedulerapi.HostPriority{}
2014-09-24 16:32:36 +00:00
if err != nil {
2015-05-10 04:17:00 +00:00
return nil, fmt.Errorf("failed to list nodes: %v", err)
2014-09-24 16:32:36 +00:00
}
for _, node := range nodes {
score, err := strconv.Atoi(node.Name)
2014-09-24 16:32:36 +00:00
if err != nil {
return nil, err
}
2015-09-04 06:50:14 +00:00
result = append(result, schedulerapi.HostPriority{
Host: node.Name,
Score: score,
2014-09-24 16:32:36 +00:00
})
}
return result, nil
}
func reverseNumericPriority(pod *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodeLister algorithm.NodeLister) (schedulerapi.HostPriorityList, error) {
var maxScore float64
minScore := math.MaxFloat64
2015-09-04 06:50:14 +00:00
reverseResult := []schedulerapi.HostPriority{}
result, err := numericPriority(pod, nodeNameToInfo, nodeLister)
if err != nil {
return nil, err
}
for _, hostPriority := range result {
maxScore = math.Max(maxScore, float64(hostPriority.Score))
minScore = math.Min(minScore, float64(hostPriority.Score))
}
for _, hostPriority := range result {
2015-09-04 06:50:14 +00:00
reverseResult = append(reverseResult, schedulerapi.HostPriority{
Host: hostPriority.Host,
Score: int(maxScore + minScore - float64(hostPriority.Score)),
})
}
return reverseResult, nil
}
func makeNodeList(nodeNames []string) []*api.Node {
result := make([]*api.Node, 0, len(nodeNames))
for _, nodeName := range nodeNames {
result = append(result, &api.Node{ObjectMeta: api.ObjectMeta{Name: nodeName}})
}
return result
}
func TestSelectHost(t *testing.T) {
scheduler := genericScheduler{}
tests := []struct {
2015-09-04 06:50:14 +00:00
list schedulerapi.HostPriorityList
possibleHosts sets.String
expectsErr bool
}{
{
2015-09-04 06:50:14 +00:00
list: []schedulerapi.HostPriority{
{Host: "machine1.1", Score: 1},
{Host: "machine2.1", Score: 2},
},
possibleHosts: sets.NewString("machine2.1"),
expectsErr: false,
},
// equal scores
{
2015-09-04 06:50:14 +00:00
list: []schedulerapi.HostPriority{
{Host: "machine1.1", Score: 1},
{Host: "machine1.2", Score: 2},
{Host: "machine1.3", Score: 2},
{Host: "machine2.1", Score: 2},
},
possibleHosts: sets.NewString("machine1.2", "machine1.3", "machine2.1"),
expectsErr: false,
},
// out of order scores
{
2015-09-04 06:50:14 +00:00
list: []schedulerapi.HostPriority{
{Host: "machine1.1", Score: 3},
{Host: "machine1.2", Score: 3},
{Host: "machine2.1", Score: 2},
{Host: "machine3.1", Score: 1},
{Host: "machine1.3", Score: 3},
},
possibleHosts: sets.NewString("machine1.1", "machine1.2", "machine1.3"),
expectsErr: false,
},
// empty priorityList
{
2015-09-04 06:50:14 +00:00
list: []schedulerapi.HostPriority{},
possibleHosts: sets.NewString(),
expectsErr: true,
},
}
for _, test := range tests {
// increase the randomness
for i := 0; i < 10; i++ {
got, err := scheduler.selectHost(test.list)
if test.expectsErr {
if err == nil {
t.Error("Unexpected non-error")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !test.possibleHosts.Has(got) {
t.Errorf("got %s is not in the possible map %v", got, test.possibleHosts)
}
}
}
}
}
2014-09-24 16:32:36 +00:00
func TestGenericScheduler(t *testing.T) {
tests := []struct {
name string
predicates map[string]algorithm.FitPredicate
prioritizers []algorithm.PriorityConfig
nodes []string
pod *api.Pod
pods []*api.Pod
expectedHosts sets.String
expectsErr bool
wErr error
2014-09-24 16:32:36 +00:00
}{
{
predicates: map[string]algorithm.FitPredicate{"false": falsePredicate},
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
nodes: []string{"machine1", "machine2"},
expectsErr: true,
name: "test 1",
2016-04-13 03:07:34 +00:00
wErr: algorithmpredicates.ErrFakePredicate,
2014-09-24 16:32:36 +00:00
},
{
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
nodes: []string{"machine1", "machine2"},
expectedHosts: sets.NewString("machine1", "machine2"),
name: "test 2",
wErr: nil,
2014-09-24 16:32:36 +00:00
},
{
// Fits on a machine where the pod ID matches the machine name
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
nodes: []string{"machine1", "machine2"},
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "machine2"}},
expectedHosts: sets.NewString("machine2"),
name: "test 3",
wErr: nil,
2014-09-24 16:32:36 +00:00
},
{
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
nodes: []string{"3", "2", "1"},
expectedHosts: sets.NewString("3"),
name: "test 4",
wErr: nil,
2014-09-24 16:32:36 +00:00
},
{
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
nodes: []string{"3", "2", "1"},
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
expectedHosts: sets.NewString("2"),
name: "test 5",
wErr: nil,
},
{
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}, {Function: reverseNumericPriority, Weight: 2}},
nodes: []string{"3", "2", "1"},
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
expectedHosts: sets.NewString("1"),
name: "test 6",
wErr: nil,
2014-09-24 16:32:36 +00:00
},
{
predicates: map[string]algorithm.FitPredicate{"true": truePredicate, "false": falsePredicate},
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
nodes: []string{"3", "2", "1"},
expectsErr: true,
name: "test 7",
wErr: nil,
2014-09-24 16:32:36 +00:00
},
{
predicates: map[string]algorithm.FitPredicate{
"nopods": hasNoPodsPredicate,
"matches": matchesPredicate,
},
pods: []*api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "2"},
Spec: api.PodSpec{
NodeName: "2",
},
Status: api.PodStatus{
Phase: api.PodRunning,
},
},
},
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
nodes: []string{"1", "2"},
expectsErr: true,
name: "test 8",
wErr: nil,
},
2014-09-24 16:32:36 +00:00
}
for _, test := range tests {
cache := schedulercache.New(time.Duration(0), wait.NeverStop)
for _, pod := range test.pods {
cache.AddPod(pod)
}
for _, name := range test.nodes {
cache.AddNode(&api.Node{ObjectMeta: api.ObjectMeta{Name: name}})
}
scheduler := NewGenericScheduler(cache, test.predicates, test.prioritizers, []algorithm.SchedulerExtender{})
machine, err := scheduler.Schedule(test.pod, algorithm.FakeNodeLister(makeNodeList(test.nodes)))
2014-09-24 16:32:36 +00:00
if test.expectsErr {
if err == nil {
t.Errorf("Unexpected non-error at %s", test.name)
2014-09-24 16:32:36 +00:00
}
} else {
if !reflect.DeepEqual(err, test.wErr) {
t.Errorf("Failed : %s, Unexpected error: %v, expected: %v", test.name, err, test.wErr)
2014-09-24 16:32:36 +00:00
}
if !test.expectedHosts.Has(machine) {
t.Errorf("Failed : %s, Expected: %s, Saw: %s", test.name, test.expectedHosts, machine)
2014-09-24 16:32:36 +00:00
}
}
}
}
func TestFindFitAllError(t *testing.T) {
nodes := []string{"3", "2", "1"}
predicates := map[string]algorithm.FitPredicate{"true": truePredicate, "false": falsePredicate}
nodeNameToInfo := map[string]*schedulercache.NodeInfo{
"3": schedulercache.NewNodeInfo(),
"2": schedulercache.NewNodeInfo(),
"1": schedulercache.NewNodeInfo(),
2015-12-10 19:30:30 +00:00
}
_, predicateMap, err := findNodesThatFit(&api.Pod{}, nodeNameToInfo, predicates, makeNodeList(nodes), nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(predicateMap) != len(nodes) {
t.Errorf("unexpected failed predicate map: %v", predicateMap)
}
for _, node := range nodes {
2016-04-14 08:58:35 +00:00
failure, found := predicateMap[node]
if !found {
t.Errorf("failed to find node: %s in %v", node, predicateMap)
}
2016-04-13 03:07:34 +00:00
if failure != "FakePredicateError" {
2016-04-14 08:58:35 +00:00
t.Errorf("unexpected failures: %v", failure)
}
}
}
func TestFindFitSomeError(t *testing.T) {
nodes := []string{"3", "2", "1"}
predicates := map[string]algorithm.FitPredicate{"true": truePredicate, "match": matchesPredicate}
pod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}}
nodeNameToInfo := map[string]*schedulercache.NodeInfo{
"3": schedulercache.NewNodeInfo(),
"2": schedulercache.NewNodeInfo(),
"1": schedulercache.NewNodeInfo(pod),
2015-12-10 19:30:30 +00:00
}
for name := range nodeNameToInfo {
nodeNameToInfo[name].SetNode(&api.Node{ObjectMeta: api.ObjectMeta{Name: name}})
}
_, predicateMap, err := findNodesThatFit(pod, nodeNameToInfo, predicates, makeNodeList(nodes), nil)
2016-04-13 03:07:34 +00:00
if err != nil && !reflect.DeepEqual(err, algorithmpredicates.ErrFakePredicate) {
t.Errorf("unexpected error: %v", err)
}
if len(predicateMap) != (len(nodes) - 1) {
t.Errorf("unexpected failed predicate map: %v", predicateMap)
}
for _, node := range nodes {
if node == pod.Name {
continue
}
2016-04-14 08:58:35 +00:00
failure, found := predicateMap[node]
if !found {
t.Errorf("failed to find node: %s in %v", node, predicateMap)
}
2016-04-13 03:07:34 +00:00
if failure != "FakePredicateError" {
2016-04-14 08:58:35 +00:00
t.Errorf("unexpected failures: %v", failure)
}
}
}