From ebbcd4a3ba71e9a8135fb73e16beb7fd74986b36 Mon Sep 17 00:00:00 2001 From: dinghaiyang Date: Fri, 7 Aug 2015 09:56:54 +0800 Subject: [PATCH] Move prioritizer function EqualPriority to package priorities --- .../algorithm/priorities/priorities.go | 18 ++++++++++++++++ .../algorithmprovider/defaults/defaults.go | 3 +-- plugin/pkg/scheduler/generic_scheduler.go | 21 ++----------------- .../pkg/scheduler/generic_scheduler_test.go | 7 ++++--- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/plugin/pkg/scheduler/algorithm/priorities/priorities.go b/plugin/pkg/scheduler/algorithm/priorities/priorities.go index 58c6105514..68a61f7cd7 100644 --- a/plugin/pkg/scheduler/algorithm/priorities/priorities.go +++ b/plugin/pkg/scheduler/algorithm/priorities/priorities.go @@ -245,3 +245,21 @@ func fractionOfCapacity(requested, capacity int64) float64 { } return float64(requested) / float64(capacity) } + +// EqualPriority is a prioritizer function that gives an equal score of one to all nodes +func EqualPriority(_ *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) { + nodes, err := minionLister.List() + if err != nil { + glog.Errorf("failed to list nodes: %v", err) + return []algorithm.HostPriority{}, err + } + + result := []algorithm.HostPriority{} + for _, minion := range nodes.Items { + result = append(result, algorithm.HostPriority{ + Host: minion.Name, + Score: 1, + }) + } + return result, nil +} diff --git a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go index 30b3300576..50d475e478 100644 --- a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go +++ b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go @@ -19,7 +19,6 @@ package defaults import ( "k8s.io/kubernetes/pkg/util" - "k8s.io/kubernetes/plugin/pkg/scheduler" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities" @@ -31,7 +30,7 @@ func init() { // EqualPriority is a prioritizer function that gives an equal weight of one to all minions // Register the priority function so that its available // but do not include it as part of the default priorities - factory.RegisterPriorityFunction("EqualPriority", scheduler.EqualPriority, 1) + factory.RegisterPriorityFunction("EqualPriority", priorities.EqualPriority, 1) } func defaultPredicates() util.StringSet { diff --git a/plugin/pkg/scheduler/generic_scheduler.go b/plugin/pkg/scheduler/generic_scheduler.go index cf0eb213d2..9b26f65bfe 100644 --- a/plugin/pkg/scheduler/generic_scheduler.go +++ b/plugin/pkg/scheduler/generic_scheduler.go @@ -28,6 +28,7 @@ import ( "k8s.io/kubernetes/pkg/util" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates" + "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities" ) type FailedPredicateMap map[string]util.StringSet @@ -148,7 +149,7 @@ func PrioritizeNodes(pod *api.Pod, podLister algorithm.PodLister, priorityConfig // If no priority configs are provided, then the EqualPriority function is applied // This is required to generate the priority list in the required format if len(priorityConfigs) == 0 { - return EqualPriority(pod, podLister, minionLister) + return priorities.EqualPriority(pod, podLister, minionLister) } combinedScores := map[string]int{} @@ -186,24 +187,6 @@ func getBestHosts(list algorithm.HostPriorityList) []string { return result } -// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes -func EqualPriority(_ *api.Pod, podLister algorithm.PodLister, minionLister algorithm.MinionLister) (algorithm.HostPriorityList, error) { - nodes, err := minionLister.List() - if err != nil { - glog.Errorf("failed to list nodes: %v", err) - return []algorithm.HostPriority{}, err - } - - result := []algorithm.HostPriority{} - for _, minion := range nodes.Items { - result = append(result, algorithm.HostPriority{ - Host: minion.Name, - Score: 1, - }) - } - return result, nil -} - func NewGenericScheduler(predicates map[string]algorithm.FitPredicate, prioritizers []algorithm.PriorityConfig, pods algorithm.PodLister, random *rand.Rand) algorithm.ScheduleAlgorithm { return &genericScheduler{ predicates: predicates, diff --git a/plugin/pkg/scheduler/generic_scheduler_test.go b/plugin/pkg/scheduler/generic_scheduler_test.go index 37b9548345..9044e9fa30 100644 --- a/plugin/pkg/scheduler/generic_scheduler_test.go +++ b/plugin/pkg/scheduler/generic_scheduler_test.go @@ -26,6 +26,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/util" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm" + "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities" ) func falsePredicate(pod *api.Pod, existingPods []*api.Pod, node string) (bool, error) { @@ -176,14 +177,14 @@ func TestGenericScheduler(t *testing.T) { }{ { predicates: map[string]algorithm.FitPredicate{"false": falsePredicate}, - prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}}, + prioritizers: []algorithm.PriorityConfig{{Function: priorities.EqualPriority, Weight: 1}}, nodes: []string{"machine1", "machine2"}, expectsErr: true, name: "test 1", }, { predicates: map[string]algorithm.FitPredicate{"true": truePredicate}, - prioritizers: []algorithm.PriorityConfig{{Function: EqualPriority, Weight: 1}}, + prioritizers: []algorithm.PriorityConfig{{Function: priorities.EqualPriority, Weight: 1}}, nodes: []string{"machine1", "machine2"}, // Random choice between both, the rand seeded above with zero, chooses "machine1" expectedHost: "machine1", @@ -192,7 +193,7 @@ func TestGenericScheduler(t *testing.T) { { // 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}}, + prioritizers: []algorithm.PriorityConfig{{Function: priorities.EqualPriority, Weight: 1}}, nodes: []string{"machine1", "machine2"}, pod: &api.Pod{ObjectMeta: api.ObjectMeta{Name: "machine2"}}, expectedHost: "machine2",