2014-09-24 16:32:36 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +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"
|
2014-11-26 02:10:25 +00:00
|
|
|
"math"
|
2016-01-06 01:10:59 +00:00
|
|
|
"reflect"
|
2014-09-24 16:32:36 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
2016-04-28 14:51:17 +00:00
|
|
|
"time"
|
2014-09-24 16:32:36 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-08-26 14:08:40 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-09-09 17:45:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
2016-04-28 14:51:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
|
2016-01-06 01:10:59 +00:00
|
|
|
algorithmpredicates "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
|
2016-08-26 14:08:40 +00:00
|
|
|
algorithmpriorities "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities"
|
|
|
|
priorityutil "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities/util"
|
2015-09-04 06:50:14 +00:00
|
|
|
schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api"
|
2016-01-28 20:14:45 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
|
2014-09-24 16:32:36 +00:00
|
|
|
)
|
|
|
|
|
2016-08-09 12:01:46 +00:00
|
|
|
func falsePredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
|
|
|
|
return false, []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate}, nil
|
2014-09-25 18:56:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:01:46 +00:00
|
|
|
func truePredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
|
|
|
|
return true, nil, nil
|
2014-09-25 18:56:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:01:46 +00:00
|
|
|
func matchesPredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
|
2016-04-28 14:51:17 +00:00
|
|
|
node := nodeInfo.Node()
|
|
|
|
if node == nil {
|
2016-08-09 12:01:46 +00:00
|
|
|
return false, nil, fmt.Errorf("node not found")
|
2016-04-28 14:51:17 +00:00
|
|
|
}
|
|
|
|
if pod.Name == node.Name {
|
2016-08-09 12:01:46 +00:00
|
|
|
return true, nil, nil
|
2016-01-06 01:10:59 +00:00
|
|
|
}
|
2016-08-09 12:01:46 +00:00
|
|
|
return false, []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate}, nil
|
2014-09-24 16:32:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:01:46 +00:00
|
|
|
func hasNoPodsPredicate(pod *api.Pod, meta interface{}, nodeInfo *schedulercache.NodeInfo) (bool, []algorithm.PredicateFailureReason, error) {
|
2016-01-06 01:10:59 +00:00
|
|
|
if len(nodeInfo.Pods()) == 0 {
|
2016-08-09 12:01:46 +00:00
|
|
|
return true, nil, nil
|
2016-01-06 01:10:59 +00:00
|
|
|
}
|
2016-08-09 12:01:46 +00:00
|
|
|
return false, []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate}, nil
|
2015-06-10 21:35:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 12:35:43 +00:00
|
|
|
func numericPriority(pod *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*api.Node) (schedulerapi.HostPriorityList, error) {
|
2015-09-04 06:50:14 +00:00
|
|
|
result := []schedulerapi.HostPriority{}
|
2016-07-11 14:55:10 +00:00
|
|
|
for _, node := range nodes {
|
2015-09-10 08:40:22 +00:00
|
|
|
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{
|
2015-09-10 08:40:22 +00:00
|
|
|
Host: node.Name,
|
2015-05-08 11:01:09 +00:00
|
|
|
Score: score,
|
2014-09-24 16:32:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-07-19 12:35:43 +00:00
|
|
|
func reverseNumericPriority(pod *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*api.Node) (schedulerapi.HostPriorityList, error) {
|
2014-11-26 02:10:25 +00:00
|
|
|
var maxScore float64
|
|
|
|
minScore := math.MaxFloat64
|
2015-09-04 06:50:14 +00:00
|
|
|
reverseResult := []schedulerapi.HostPriority{}
|
2016-07-19 12:35:43 +00:00
|
|
|
result, err := numericPriority(pod, nodeNameToInfo, nodes)
|
2014-11-26 02:10:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostPriority := range result {
|
2015-05-08 11:01:09 +00:00
|
|
|
maxScore = math.Max(maxScore, float64(hostPriority.Score))
|
|
|
|
minScore = math.Min(minScore, float64(hostPriority.Score))
|
2014-11-26 02:10:25 +00:00
|
|
|
}
|
|
|
|
for _, hostPriority := range result {
|
2015-09-04 06:50:14 +00:00
|
|
|
reverseResult = append(reverseResult, schedulerapi.HostPriority{
|
2015-05-08 11:01:09 +00:00
|
|
|
Host: hostPriority.Host,
|
|
|
|
Score: int(maxScore + minScore - float64(hostPriority.Score)),
|
2014-11-26 02:10:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return reverseResult, nil
|
|
|
|
}
|
|
|
|
|
2016-07-11 14:55:10 +00:00
|
|
|
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}})
|
2014-10-06 22:58:18 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-11-06 05:48:42 +00:00
|
|
|
func TestSelectHost(t *testing.T) {
|
2016-05-21 18:36:53 +00:00
|
|
|
scheduler := genericScheduler{}
|
2014-11-06 05:48:42 +00:00
|
|
|
tests := []struct {
|
2015-09-04 06:50:14 +00:00
|
|
|
list schedulerapi.HostPriorityList
|
2015-09-09 17:45:01 +00:00
|
|
|
possibleHosts sets.String
|
2014-11-07 05:38:41 +00:00
|
|
|
expectsErr bool
|
2014-11-06 05:48:42 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-09-04 06:50:14 +00:00
|
|
|
list: []schedulerapi.HostPriority{
|
2015-05-08 11:01:09 +00:00
|
|
|
{Host: "machine1.1", Score: 1},
|
|
|
|
{Host: "machine2.1", Score: 2},
|
2014-11-06 05:48:42 +00:00
|
|
|
},
|
2015-09-09 17:45:01 +00:00
|
|
|
possibleHosts: sets.NewString("machine2.1"),
|
2014-11-07 05:38:41 +00:00
|
|
|
expectsErr: false,
|
2014-11-06 05:48:42 +00:00
|
|
|
},
|
|
|
|
// equal scores
|
|
|
|
{
|
2015-09-04 06:50:14 +00:00
|
|
|
list: []schedulerapi.HostPriority{
|
2015-05-08 11:01:09 +00:00
|
|
|
{Host: "machine1.1", Score: 1},
|
|
|
|
{Host: "machine1.2", Score: 2},
|
|
|
|
{Host: "machine1.3", Score: 2},
|
|
|
|
{Host: "machine2.1", Score: 2},
|
2014-11-06 05:48:42 +00:00
|
|
|
},
|
2015-09-09 17:45:01 +00:00
|
|
|
possibleHosts: sets.NewString("machine1.2", "machine1.3", "machine2.1"),
|
2014-11-07 05:38:41 +00:00
|
|
|
expectsErr: false,
|
2014-11-06 05:48:42 +00:00
|
|
|
},
|
|
|
|
// out of order scores
|
|
|
|
{
|
2015-09-04 06:50:14 +00:00
|
|
|
list: []schedulerapi.HostPriority{
|
2015-05-08 11:01:09 +00:00
|
|
|
{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},
|
2014-11-06 05:48:42 +00:00
|
|
|
},
|
2015-09-09 17:45:01 +00:00
|
|
|
possibleHosts: sets.NewString("machine1.1", "machine1.2", "machine1.3"),
|
2014-11-07 05:38:41 +00:00
|
|
|
expectsErr: false,
|
|
|
|
},
|
|
|
|
// empty priorityList
|
|
|
|
{
|
2015-09-04 06:50:14 +00:00
|
|
|
list: []schedulerapi.HostPriority{},
|
2015-09-09 17:45:01 +00:00
|
|
|
possibleHosts: sets.NewString(),
|
2014-11-07 05:38:41 +00:00
|
|
|
expectsErr: true,
|
2014-11-06 05:48:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
// increase the randomness
|
|
|
|
for i := 0; i < 10; i++ {
|
2014-11-07 05:38:41 +00:00
|
|
|
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-11-06 05:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 16:32:36 +00:00
|
|
|
func TestGenericScheduler(t *testing.T) {
|
|
|
|
tests := []struct {
|
2016-01-21 02:39:59 +00:00
|
|
|
name string
|
|
|
|
predicates map[string]algorithm.FitPredicate
|
|
|
|
prioritizers []algorithm.PriorityConfig
|
|
|
|
nodes []string
|
|
|
|
pod *api.Pod
|
|
|
|
pods []*api.Pod
|
|
|
|
expectedHosts sets.String
|
|
|
|
expectsErr bool
|
2016-01-06 01:10:59 +00:00
|
|
|
wErr error
|
2014-09-24 16:32:36 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-05-08 11:01:09 +00:00
|
|
|
predicates: map[string]algorithm.FitPredicate{"false": falsePredicate},
|
2016-09-30 13:14:29 +00:00
|
|
|
prioritizers: []algorithm.PriorityConfig{{Map: EqualPriorityMap, Weight: 1}},
|
2014-11-20 22:42:31 +00:00
|
|
|
nodes: []string{"machine1", "machine2"},
|
|
|
|
expectsErr: true,
|
2016-07-20 12:46:50 +00:00
|
|
|
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
2014-11-26 02:10:25 +00:00
|
|
|
name: "test 1",
|
2016-07-20 12:46:50 +00:00
|
|
|
wErr: &FitError{
|
|
|
|
Pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
|
|
|
FailedPredicates: FailedPredicateMap{
|
2016-08-09 12:01:46 +00:00
|
|
|
"machine1": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
|
|
|
"machine2": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
2016-07-20 12:46:50 +00:00
|
|
|
}},
|
2014-09-24 16:32:36 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-21 02:39:59 +00:00
|
|
|
predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
|
2016-09-30 13:14:29 +00:00
|
|
|
prioritizers: []algorithm.PriorityConfig{{Map: EqualPriorityMap, Weight: 1}},
|
2016-01-21 02:39:59 +00:00
|
|
|
nodes: []string{"machine1", "machine2"},
|
|
|
|
expectedHosts: sets.NewString("machine1", "machine2"),
|
|
|
|
name: "test 2",
|
2016-01-06 01:10:59 +00:00
|
|
|
wErr: nil,
|
2014-09-24 16:32:36 +00:00
|
|
|
},
|
|
|
|
{
|
2014-09-24 21:18:31 +00:00
|
|
|
// Fits on a machine where the pod ID matches the machine name
|
2016-01-21 02:39:59 +00:00
|
|
|
predicates: map[string]algorithm.FitPredicate{"matches": matchesPredicate},
|
2016-09-30 13:14:29 +00:00
|
|
|
prioritizers: []algorithm.PriorityConfig{{Map: EqualPriorityMap, Weight: 1}},
|
2016-01-21 02:39:59 +00:00
|
|
|
nodes: []string{"machine1", "machine2"},
|
|
|
|
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "machine2"}},
|
|
|
|
expectedHosts: sets.NewString("machine2"),
|
|
|
|
name: "test 3",
|
2016-01-06 01:10:59 +00:00
|
|
|
wErr: nil,
|
2014-09-24 16:32:36 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-21 02:39:59 +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",
|
2016-01-06 01:10:59 +00:00
|
|
|
wErr: nil,
|
2014-09-24 16:32:36 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-21 02:39:59 +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",
|
2016-01-06 01:10:59 +00:00
|
|
|
wErr: nil,
|
2014-11-26 02:10:25 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-21 02:39:59 +00:00
|
|
|
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",
|
2016-01-06 01:10:59 +00:00
|
|
|
wErr: nil,
|
2014-09-24 16:32:36 +00:00
|
|
|
},
|
|
|
|
{
|
2015-05-08 11:01:09 +00:00
|
|
|
predicates: map[string]algorithm.FitPredicate{"true": truePredicate, "false": falsePredicate},
|
|
|
|
prioritizers: []algorithm.PriorityConfig{{Function: numericPriority, Weight: 1}},
|
2014-11-20 22:42:31 +00:00
|
|
|
nodes: []string{"3", "2", "1"},
|
2016-07-20 12:46:50 +00:00
|
|
|
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
2014-11-20 22:42:31 +00:00
|
|
|
expectsErr: true,
|
2014-11-26 02:10:25 +00:00
|
|
|
name: "test 7",
|
2016-07-20 12:46:50 +00:00
|
|
|
wErr: &FitError{
|
|
|
|
Pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
|
|
|
FailedPredicates: FailedPredicateMap{
|
2016-08-09 12:01:46 +00:00
|
|
|
"3": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
|
|
|
"2": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
|
|
|
"1": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
2016-07-20 12:46:50 +00:00
|
|
|
},
|
|
|
|
},
|
2014-09-24 16:32:36 +00:00
|
|
|
},
|
2015-06-10 21:35:59 +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",
|
2016-07-20 12:46:50 +00:00
|
|
|
wErr: &FitError{
|
|
|
|
Pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "2"}},
|
|
|
|
FailedPredicates: FailedPredicateMap{
|
2016-08-09 12:01:46 +00:00
|
|
|
"1": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
|
|
|
"2": []algorithm.PredicateFailureReason{algorithmpredicates.ErrFakePredicate},
|
2016-07-20 12:46:50 +00:00
|
|
|
},
|
|
|
|
},
|
2015-06-10 21:35:59 +00:00
|
|
|
},
|
2014-09-24 16:32:36 +00:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2016-04-28 14:51:17 +00:00
|
|
|
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}})
|
|
|
|
}
|
2016-09-13 09:54:13 +00:00
|
|
|
scheduler := NewGenericScheduler(
|
|
|
|
cache, test.predicates, algorithm.EmptyMetadataProducer,
|
|
|
|
test.prioritizers, []algorithm.SchedulerExtender{})
|
2015-09-10 08:40:22 +00:00
|
|
|
machine, err := scheduler.Schedule(test.pod, algorithm.FakeNodeLister(makeNodeList(test.nodes)))
|
2016-07-20 12:46:50 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(err, test.wErr) {
|
|
|
|
t.Errorf("Failed : %s, Unexpected error: %v, expected: %v", test.name, err, test.wErr)
|
|
|
|
}
|
|
|
|
if test.expectedHosts != nil && !test.expectedHosts.Has(machine) {
|
|
|
|
t.Errorf("Failed : %s, Expected: %s, got: %s", test.name, test.expectedHosts, machine)
|
2014-09-24 16:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 04:36:22 +00:00
|
|
|
|
|
|
|
func TestFindFitAllError(t *testing.T) {
|
|
|
|
nodes := []string{"3", "2", "1"}
|
2015-05-08 11:01:09 +00:00
|
|
|
predicates := map[string]algorithm.FitPredicate{"true": truePredicate, "false": falsePredicate}
|
2016-01-28 20:14:45 +00:00
|
|
|
nodeNameToInfo := map[string]*schedulercache.NodeInfo{
|
|
|
|
"3": schedulercache.NewNodeInfo(),
|
|
|
|
"2": schedulercache.NewNodeInfo(),
|
|
|
|
"1": schedulercache.NewNodeInfo(),
|
2015-12-10 19:30:30 +00:00
|
|
|
}
|
2016-07-19 12:35:43 +00:00
|
|
|
_, predicateMap, err := findNodesThatFit(&api.Pod{}, nodeNameToInfo, makeNodeList(nodes), predicates, nil)
|
2015-02-24 04:36:22 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-03-31 22:32:02 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
2015-02-24 04:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(predicateMap) != len(nodes) {
|
|
|
|
t.Errorf("unexpected failed predicate map: %v", predicateMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
2016-08-09 12:01:46 +00:00
|
|
|
failures, found := predicateMap[node]
|
2015-02-24 04:36:22 +00:00
|
|
|
if !found {
|
|
|
|
t.Errorf("failed to find node: %s in %v", node, predicateMap)
|
|
|
|
}
|
2016-08-09 12:01:46 +00:00
|
|
|
if len(failures) != 1 || failures[0] != algorithmpredicates.ErrFakePredicate {
|
|
|
|
t.Errorf("unexpected failures: %v", failures)
|
2015-02-24 04:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindFitSomeError(t *testing.T) {
|
|
|
|
nodes := []string{"3", "2", "1"}
|
2015-05-08 11:01:09 +00:00
|
|
|
predicates := map[string]algorithm.FitPredicate{"true": truePredicate, "match": matchesPredicate}
|
2015-04-03 22:51:50 +00:00
|
|
|
pod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}}
|
2016-01-28 20:14:45 +00:00
|
|
|
nodeNameToInfo := map[string]*schedulercache.NodeInfo{
|
|
|
|
"3": schedulercache.NewNodeInfo(),
|
|
|
|
"2": schedulercache.NewNodeInfo(),
|
|
|
|
"1": schedulercache.NewNodeInfo(pod),
|
2015-12-10 19:30:30 +00:00
|
|
|
}
|
2016-04-28 14:51:17 +00:00
|
|
|
for name := range nodeNameToInfo {
|
|
|
|
nodeNameToInfo[name].SetNode(&api.Node{ObjectMeta: api.ObjectMeta{Name: name}})
|
|
|
|
}
|
2015-02-24 04:36:22 +00:00
|
|
|
|
2016-07-19 12:35:43 +00:00
|
|
|
_, predicateMap, err := findNodesThatFit(pod, nodeNameToInfo, makeNodeList(nodes), predicates, nil)
|
2016-08-09 12:01:46 +00:00
|
|
|
if err != nil {
|
2015-03-31 22:32:02 +00:00
|
|
|
t.Errorf("unexpected error: %v", err)
|
2015-02-24 04:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(predicateMap) != (len(nodes) - 1) {
|
|
|
|
t.Errorf("unexpected failed predicate map: %v", predicateMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node == pod.Name {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-09 12:01:46 +00:00
|
|
|
failures, found := predicateMap[node]
|
2015-02-24 04:36:22 +00:00
|
|
|
if !found {
|
|
|
|
t.Errorf("failed to find node: %s in %v", node, predicateMap)
|
|
|
|
}
|
2016-08-09 12:01:46 +00:00
|
|
|
if len(failures) != 1 || failures[0] != algorithmpredicates.ErrFakePredicate {
|
|
|
|
t.Errorf("unexpected failures: %v", failures)
|
2015-02-24 04:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 14:08:40 +00:00
|
|
|
|
|
|
|
func makeNode(node string, milliCPU, memory int64) *api.Node {
|
|
|
|
return &api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: node},
|
|
|
|
Status: api.NodeStatus{
|
|
|
|
Capacity: api.ResourceList{
|
|
|
|
"cpu": *resource.NewMilliQuantity(milliCPU, resource.DecimalSI),
|
|
|
|
"memory": *resource.NewQuantity(memory, resource.BinarySI),
|
|
|
|
},
|
|
|
|
Allocatable: api.ResourceList{
|
|
|
|
"cpu": *resource.NewMilliQuantity(milliCPU, resource.DecimalSI),
|
|
|
|
"memory": *resource.NewQuantity(memory, resource.BinarySI),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The point of this test is to show that you:
|
|
|
|
// - get the same priority for a zero-request pod as for a pod with the defaults requests,
|
|
|
|
// both when the zero-request pod is already on the machine and when the zero-request pod
|
|
|
|
// is the one being scheduled.
|
|
|
|
// - don't get the same score no matter what we schedule.
|
|
|
|
func TestZeroRequest(t *testing.T) {
|
|
|
|
// A pod with no resources. We expect spreading to count it as having the default resources.
|
|
|
|
noResources := api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
noResources1 := noResources
|
|
|
|
noResources1.NodeName = "machine1"
|
|
|
|
// A pod with the same resources as a 0-request pod gets by default as its resources (for spreading).
|
|
|
|
small := api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Resources: api.ResourceRequirements{
|
|
|
|
Requests: api.ResourceList{
|
|
|
|
"cpu": resource.MustParse(
|
|
|
|
strconv.FormatInt(priorityutil.DefaultMilliCpuRequest, 10) + "m"),
|
|
|
|
"memory": resource.MustParse(
|
|
|
|
strconv.FormatInt(priorityutil.DefaultMemoryRequest, 10)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
small2 := small
|
|
|
|
small2.NodeName = "machine2"
|
|
|
|
// A larger pod.
|
|
|
|
large := api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Resources: api.ResourceRequirements{
|
|
|
|
Requests: api.ResourceList{
|
|
|
|
"cpu": resource.MustParse(
|
|
|
|
strconv.FormatInt(priorityutil.DefaultMilliCpuRequest*3, 10) + "m"),
|
|
|
|
"memory": resource.MustParse(
|
|
|
|
strconv.FormatInt(priorityutil.DefaultMemoryRequest*3, 10)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
large1 := large
|
|
|
|
large1.NodeName = "machine1"
|
|
|
|
large2 := large
|
|
|
|
large2.NodeName = "machine2"
|
|
|
|
tests := []struct {
|
|
|
|
pod *api.Pod
|
|
|
|
pods []*api.Pod
|
|
|
|
nodes []*api.Node
|
|
|
|
test string
|
|
|
|
}{
|
|
|
|
// The point of these next two tests is to show you get the same priority for a zero-request pod
|
|
|
|
// as for a pod with the defaults requests, both when the zero-request pod is already on the machine
|
|
|
|
// and when the zero-request pod is the one being scheduled.
|
|
|
|
{
|
|
|
|
pod: &api.Pod{Spec: noResources},
|
|
|
|
nodes: []*api.Node{makeNode("machine1", 1000, priorityutil.DefaultMemoryRequest*10), makeNode("machine2", 1000, priorityutil.DefaultMemoryRequest*10)},
|
|
|
|
test: "test priority of zero-request pod with machine with zero-request pod",
|
|
|
|
pods: []*api.Pod{
|
|
|
|
{Spec: large1}, {Spec: noResources1},
|
|
|
|
{Spec: large2}, {Spec: small2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pod: &api.Pod{Spec: small},
|
|
|
|
nodes: []*api.Node{makeNode("machine1", 1000, priorityutil.DefaultMemoryRequest*10), makeNode("machine2", 1000, priorityutil.DefaultMemoryRequest*10)},
|
|
|
|
test: "test priority of nonzero-request pod with machine with zero-request pod",
|
|
|
|
pods: []*api.Pod{
|
|
|
|
{Spec: large1}, {Spec: noResources1},
|
|
|
|
{Spec: large2}, {Spec: small2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// The point of this test is to verify that we're not just getting the same score no matter what we schedule.
|
|
|
|
{
|
|
|
|
pod: &api.Pod{Spec: large},
|
|
|
|
nodes: []*api.Node{makeNode("machine1", 1000, priorityutil.DefaultMemoryRequest*10), makeNode("machine2", 1000, priorityutil.DefaultMemoryRequest*10)},
|
|
|
|
test: "test priority of larger pod with machine with zero-request pod",
|
|
|
|
pods: []*api.Pod{
|
|
|
|
{Spec: large1}, {Spec: noResources1},
|
|
|
|
{Spec: large2}, {Spec: small2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectedPriority int = 25
|
|
|
|
for _, test := range tests {
|
|
|
|
// This should match the configuration in defaultPriorities() in
|
|
|
|
// plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go if you want
|
|
|
|
// to test what's actually in production.
|
|
|
|
priorityConfigs := []algorithm.PriorityConfig{
|
|
|
|
{Map: algorithmpriorities.LeastRequestedPriorityMap, Weight: 1},
|
2016-09-09 12:42:10 +00:00
|
|
|
{Map: algorithmpriorities.BalancedResourceAllocationMap, Weight: 1},
|
2016-08-26 14:08:40 +00:00
|
|
|
{
|
|
|
|
Function: algorithmpriorities.NewSelectorSpreadPriority(
|
2016-09-16 17:38:50 +00:00
|
|
|
algorithm.FakeServiceLister([]*api.Service{}),
|
2016-09-16 17:19:58 +00:00
|
|
|
algorithm.FakeControllerLister([]*api.ReplicationController{}),
|
2016-10-04 17:23:27 +00:00
|
|
|
algorithm.FakeReplicaSetLister([]*extensions.ReplicaSet{})),
|
2016-08-26 14:08:40 +00:00
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes)
|
|
|
|
list, err := PrioritizeNodes(
|
2016-09-13 09:54:13 +00:00
|
|
|
test.pod, nodeNameToInfo, algorithm.EmptyMetadataProducer, priorityConfigs,
|
2016-08-26 14:08:40 +00:00
|
|
|
algorithm.FakeNodeLister(test.nodes), []algorithm.SchedulerExtender{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
for _, hp := range list {
|
|
|
|
if test.test == "test priority of larger pod with machine with zero-request pod" {
|
|
|
|
if hp.Score == expectedPriority {
|
|
|
|
t.Errorf("%s: expected non-%d for all priorities, got list %#v", test.test, expectedPriority, list)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hp.Score != expectedPriority {
|
|
|
|
t.Errorf("%s: expected %d for all priorities, got list %#v", test.test, expectedPriority, list)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|