123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- /*
- Copyright 2015 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 scheduler
- import (
- "fmt"
- "testing"
- "k8s.io/kubernetes/pkg/api"
- "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
- schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api"
- "k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
- schedulertesting "k8s.io/kubernetes/plugin/pkg/scheduler/testing"
- )
- type fitPredicate func(pod *api.Pod, node *api.Node) (bool, error)
- type priorityFunc func(pod *api.Pod, nodes []*api.Node) (*schedulerapi.HostPriorityList, error)
- type priorityConfig struct {
- function priorityFunc
- weight int
- }
- func errorPredicateExtender(pod *api.Pod, node *api.Node) (bool, error) {
- return false, fmt.Errorf("Some error")
- }
- func falsePredicateExtender(pod *api.Pod, node *api.Node) (bool, error) {
- return false, nil
- }
- func truePredicateExtender(pod *api.Pod, node *api.Node) (bool, error) {
- return true, nil
- }
- func machine1PredicateExtender(pod *api.Pod, node *api.Node) (bool, error) {
- if node.Name == "machine1" {
- return true, nil
- }
- return false, nil
- }
- func machine2PredicateExtender(pod *api.Pod, node *api.Node) (bool, error) {
- if node.Name == "machine2" {
- return true, nil
- }
- return false, nil
- }
- func errorPrioritizerExtender(pod *api.Pod, nodes []*api.Node) (*schedulerapi.HostPriorityList, error) {
- return &schedulerapi.HostPriorityList{}, fmt.Errorf("Some error")
- }
- func machine1PrioritizerExtender(pod *api.Pod, nodes []*api.Node) (*schedulerapi.HostPriorityList, error) {
- result := schedulerapi.HostPriorityList{}
- for _, node := range nodes {
- score := 1
- if node.Name == "machine1" {
- score = 10
- }
- result = append(result, schedulerapi.HostPriority{Host: node.Name, Score: score})
- }
- return &result, nil
- }
- func machine2PrioritizerExtender(pod *api.Pod, nodes []*api.Node) (*schedulerapi.HostPriorityList, error) {
- result := schedulerapi.HostPriorityList{}
- for _, node := range nodes {
- score := 1
- if node.Name == "machine2" {
- score = 10
- }
- result = append(result, schedulerapi.HostPriority{Host: node.Name, Score: score})
- }
- return &result, nil
- }
- func machine2Prioritizer(_ *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodes []*api.Node) (schedulerapi.HostPriorityList, error) {
- result := []schedulerapi.HostPriority{}
- for _, node := range nodes {
- score := 1
- if node.Name == "machine2" {
- score = 10
- }
- result = append(result, schedulerapi.HostPriority{Host: node.Name, Score: score})
- }
- return result, nil
- }
- type FakeExtender struct {
- predicates []fitPredicate
- prioritizers []priorityConfig
- weight int
- }
- func (f *FakeExtender) Filter(pod *api.Pod, nodes []*api.Node) ([]*api.Node, schedulerapi.FailedNodesMap, error) {
- filtered := []*api.Node{}
- failedNodesMap := schedulerapi.FailedNodesMap{}
- for _, node := range nodes {
- fits := true
- for _, predicate := range f.predicates {
- fit, err := predicate(pod, node)
- if err != nil {
- return []*api.Node{}, schedulerapi.FailedNodesMap{}, err
- }
- if !fit {
- fits = false
- break
- }
- }
- if fits {
- filtered = append(filtered, node)
- } else {
- failedNodesMap[node.Name] = "FakeExtender failed"
- }
- }
- return filtered, failedNodesMap, nil
- }
- func (f *FakeExtender) Prioritize(pod *api.Pod, nodes []*api.Node) (*schedulerapi.HostPriorityList, int, error) {
- result := schedulerapi.HostPriorityList{}
- combinedScores := map[string]int{}
- for _, prioritizer := range f.prioritizers {
- weight := prioritizer.weight
- if weight == 0 {
- continue
- }
- priorityFunc := prioritizer.function
- prioritizedList, err := priorityFunc(pod, nodes)
- if err != nil {
- return &schedulerapi.HostPriorityList{}, 0, err
- }
- for _, hostEntry := range *prioritizedList {
- combinedScores[hostEntry.Host] += hostEntry.Score * weight
- }
- }
- for host, score := range combinedScores {
- result = append(result, schedulerapi.HostPriority{Host: host, Score: score})
- }
- return &result, f.weight, nil
- }
- func TestGenericSchedulerWithExtenders(t *testing.T) {
- tests := []struct {
- name string
- predicates map[string]algorithm.FitPredicate
- prioritizers []algorithm.PriorityConfig
- extenders []FakeExtender
- extenderPredicates []fitPredicate
- extenderPrioritizers []priorityConfig
- nodes []string
- pod *api.Pod
- pods []*api.Pod
- expectedHost string
- expectsErr bool
- }{
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{truePredicateExtender},
- },
- {
- predicates: []fitPredicate{errorPredicateExtender},
- },
- },
- nodes: []string{"machine1", "machine2"},
- expectsErr: true,
- name: "test 1",
- },
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{truePredicateExtender},
- },
- {
- predicates: []fitPredicate{falsePredicateExtender},
- },
- },
- nodes: []string{"machine1", "machine2"},
- expectsErr: true,
- name: "test 2",
- },
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{truePredicateExtender},
- },
- {
- predicates: []fitPredicate{machine1PredicateExtender},
- },
- },
- nodes: []string{"machine1", "machine2"},
- expectedHost: "machine1",
- name: "test 3",
- },
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{machine2PredicateExtender},
- },
- {
- predicates: []fitPredicate{machine1PredicateExtender},
- },
- },
- nodes: []string{"machine1", "machine2"},
- expectsErr: true,
- name: "test 4",
- },
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{truePredicateExtender},
- prioritizers: []priorityConfig{{errorPrioritizerExtender, 10}},
- weight: 1,
- },
- },
- nodes: []string{"machine1"},
- expectedHost: "machine1",
- name: "test 5",
- },
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{truePredicateExtender},
- prioritizers: []priorityConfig{{machine1PrioritizerExtender, 10}},
- weight: 1,
- },
- {
- predicates: []fitPredicate{truePredicateExtender},
- prioritizers: []priorityConfig{{machine2PrioritizerExtender, 10}},
- weight: 5,
- },
- },
- nodes: []string{"machine1", "machine2"},
- expectedHost: "machine2",
- name: "test 6",
- },
- {
- predicates: map[string]algorithm.FitPredicate{"true": truePredicate},
- prioritizers: []algorithm.PriorityConfig{{Function: machine2Prioritizer, Weight: 20}},
- extenders: []FakeExtender{
- {
- predicates: []fitPredicate{truePredicateExtender},
- prioritizers: []priorityConfig{{machine1PrioritizerExtender, 10}},
- weight: 1,
- },
- },
- nodes: []string{"machine1", "machine2"},
- expectedHost: "machine2", // machine2 has higher score
- name: "test 7",
- },
- }
- for _, test := range tests {
- extenders := []algorithm.SchedulerExtender{}
- for ii := range test.extenders {
- extenders = append(extenders, &test.extenders[ii])
- }
- scheduler := NewGenericScheduler(schedulertesting.PodsToCache(test.pods), test.predicates, test.prioritizers, extenders)
- machine, err := scheduler.Schedule(test.pod, algorithm.FakeNodeLister(makeNodeList(test.nodes)))
- if test.expectsErr {
- if err == nil {
- t.Errorf("Unexpected non-error for %s, machine %s", test.name, machine)
- }
- } else {
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- if test.expectedHost != machine {
- t.Errorf("Failed : %s, Expected: %s, Saw: %s", test.name, test.expectedHost, machine)
- }
- }
- }
- }
|