Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
9236e4a0
Commit
9236e4a0
authored
Jan 28, 2016
by
Hongchao Deng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
scheduler: change fit predicates and priority func as needed for optimization
parent
9942f6bf
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
254 additions
and
154 deletions
+254
-154
predicates.go
plugin/pkg/scheduler/algorithm/predicates/predicates.go
+35
-60
predicates_test.go
plugin/pkg/scheduler/algorithm/predicates/predicates_test.go
+0
-0
node_affinity.go
plugin/pkg/scheduler/algorithm/priorities/node_affinity.go
+2
-1
node_affinity_test.go
.../pkg/scheduler/algorithm/priorities/node_affinity_test.go
+2
-1
priorities.go
plugin/pkg/scheduler/algorithm/priorities/priorities.go
+7
-6
priorities_test.go
plugin/pkg/scheduler/algorithm/priorities/priorities_test.go
+11
-23
selector_spreading.go
.../pkg/scheduler/algorithm/priorities/selector_spreading.go
+11
-6
selector_spreading_test.go
...scheduler/algorithm/priorities/selector_spreading_test.go
+10
-19
types.go
plugin/pkg/scheduler/algorithm/types.go
+3
-2
defaults.go
plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go
+2
-2
extender_test.go
plugin/pkg/scheduler/extender_test.go
+2
-1
factory_test.go
plugin/pkg/scheduler/factory/factory_test.go
+5
-4
plugins.go
plugin/pkg/scheduler/factory/plugins.go
+1
-0
generic_scheduler.go
plugin/pkg/scheduler/generic_scheduler.go
+12
-10
generic_scheduler_test.go
plugin/pkg/scheduler/generic_scheduler_test.go
+20
-19
node_info.go
plugin/pkg/scheduler/schedulercache/node_info.go
+96
-0
util.go
plugin/pkg/scheduler/schedulercache/util.go
+35
-0
No files found.
plugin/pkg/scheduler/algorithm/predicates/predicates.go
View file @
9236e4a0
This diff is collapsed.
Click to expand it.
plugin/pkg/scheduler/algorithm/predicates/predicates_test.go
View file @
9236e4a0
This diff is collapsed.
Click to expand it.
plugin/pkg/scheduler/algorithm/priorities/node_affinity.go
View file @
9236e4a0
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
type
NodeAffinity
struct
{
type
NodeAffinity
struct
{
...
@@ -40,7 +41,7 @@ func NewNodeAffinityPriority(nodeLister algorithm.NodeLister) algorithm.Priority
...
@@ -40,7 +41,7 @@ func NewNodeAffinityPriority(nodeLister algorithm.NodeLister) algorithm.Priority
// it will a get an add of preferredSchedulingTerm.Weight. Thus, the more preferredSchedulingTerms
// it will a get an add of preferredSchedulingTerm.Weight. Thus, the more preferredSchedulingTerms
// the node satisfies and the more the preferredSchedulingTerm that is satisfied weights, the higher
// the node satisfies and the more the preferredSchedulingTerm that is satisfied weights, the higher
// score the node gets.
// score the node gets.
func
(
s
*
NodeAffinity
)
CalculateNodeAffinityPriority
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
(
s
*
NodeAffinity
)
CalculateNodeAffinityPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
var
maxCount
int
var
maxCount
int
counts
:=
map
[
string
]
int
{}
counts
:=
map
[
string
]
int
{}
...
...
plugin/pkg/scheduler/algorithm/priorities/node_affinity_test.go
View file @
9236e4a0
...
@@ -23,6 +23,7 @@ import (
...
@@ -23,6 +23,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
func
TestNodeAffinityPriority
(
t
*
testing
.
T
)
{
func
TestNodeAffinityPriority
(
t
*
testing
.
T
)
{
...
@@ -156,7 +157,7 @@ func TestNodeAffinityPriority(t *testing.T) {
...
@@ -156,7 +157,7 @@ func TestNodeAffinityPriority(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
nodeAffinity
:=
NodeAffinity
{
nodeLister
:
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
})}
nodeAffinity
:=
NodeAffinity
{
nodeLister
:
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
})}
list
,
err
:=
nodeAffinity
.
CalculateNodeAffinityPriority
(
test
.
pod
,
nil
,
nil
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
list
,
err
:=
nodeAffinity
.
CalculateNodeAffinityPriority
(
test
.
pod
,
schedulercache
.
CreateNodeNameToInfoMap
(
nil
)
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
...
plugin/pkg/scheduler/algorithm/priorities/priorities.go
View file @
9236e4a0
...
@@ -25,6 +25,7 @@ import (
...
@@ -25,6 +25,7 @@ import (
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
// the unused capacity is calculated on a scale of 0-10
// the unused capacity is calculated on a scale of 0-10
...
@@ -114,7 +115,7 @@ func calculateResourceOccupancy(pod *api.Pod, node api.Node, pods []*api.Pod) sc
...
@@ -114,7 +115,7 @@ func calculateResourceOccupancy(pod *api.Pod, node api.Node, pods []*api.Pod) sc
// It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
// It calculates the percentage of memory and CPU requested by pods scheduled on the node, and prioritizes
// based on the minimum of the average of the fraction of requested to capacity.
// based on the minimum of the average of the fraction of requested to capacity.
// Details: cpu((capacity - sum(requested)) * 10 / capacity) + memory((capacity - sum(requested)) * 10 / capacity) / 2
// Details: cpu((capacity - sum(requested)) * 10 / capacity) + memory((capacity - sum(requested)) * 10 / capacity) / 2
func
LeastRequestedPriority
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
LeastRequestedPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
if
err
!=
nil
{
if
err
!=
nil
{
return
schedulerapi
.
HostPriorityList
{},
err
return
schedulerapi
.
HostPriorityList
{},
err
...
@@ -122,7 +123,7 @@ func LeastRequestedPriority(pod *api.Pod, machinesToPods map[string][]*api.Pod,
...
@@ -122,7 +123,7 @@ func LeastRequestedPriority(pod *api.Pod, machinesToPods map[string][]*api.Pod,
list
:=
schedulerapi
.
HostPriorityList
{}
list
:=
schedulerapi
.
HostPriorityList
{}
for
_
,
node
:=
range
nodes
.
Items
{
for
_
,
node
:=
range
nodes
.
Items
{
list
=
append
(
list
,
calculateResourceOccupancy
(
pod
,
node
,
machinesToPods
[
node
.
Name
]
))
list
=
append
(
list
,
calculateResourceOccupancy
(
pod
,
node
,
nodeNameToInfo
[
node
.
Name
]
.
Pods
()
))
}
}
return
list
,
nil
return
list
,
nil
}
}
...
@@ -143,7 +144,7 @@ func NewNodeLabelPriority(label string, presence bool) algorithm.PriorityFunctio
...
@@ -143,7 +144,7 @@ func NewNodeLabelPriority(label string, presence bool) algorithm.PriorityFunctio
// CalculateNodeLabelPriority checks whether a particular label exists on a node or not, regardless of its value.
// CalculateNodeLabelPriority checks whether a particular label exists on a node or not, regardless of its value.
// If presence is true, prioritizes nodes that have the specified label, regardless of value.
// If presence is true, prioritizes nodes that have the specified label, regardless of value.
// If presence is false, prioritizes nodes that do not have the specified label.
// If presence is false, prioritizes nodes that do not have the specified label.
func
(
n
*
NodeLabelPrioritizer
)
CalculateNodeLabelPriority
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
(
n
*
NodeLabelPrioritizer
)
CalculateNodeLabelPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
var
score
int
var
score
int
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -182,7 +183,7 @@ const (
...
@@ -182,7 +183,7 @@ const (
// based on the total size of those images.
// based on the total size of those images.
// - If none of the images are present, this node will be given the lowest priority.
// - If none of the images are present, this node will be given the lowest priority.
// - If some of the images are present on a node, the larger their sizes' sum, the higher the node's priority.
// - If some of the images are present on a node, the larger their sizes' sum, the higher the node's priority.
func
ImageLocalityPriority
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
ImageLocalityPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
sumSizeMap
:=
make
(
map
[
string
]
int64
)
sumSizeMap
:=
make
(
map
[
string
]
int64
)
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
...
@@ -248,7 +249,7 @@ func calculateScoreFromSize(sumSize int64) int {
...
@@ -248,7 +249,7 @@ func calculateScoreFromSize(sumSize int64) int {
// close the two metrics are to each other.
// close the two metrics are to each other.
// Detail: score = 10 - abs(cpuFraction-memoryFraction)*10. The algorithm is partly inspired by:
// Detail: score = 10 - abs(cpuFraction-memoryFraction)*10. The algorithm is partly inspired by:
// "Wei Huang et al. An Energy Efficient Virtual Machine Placement Algorithm with Balanced Resource Utilization"
// "Wei Huang et al. An Energy Efficient Virtual Machine Placement Algorithm with Balanced Resource Utilization"
func
BalancedResourceAllocation
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
BalancedResourceAllocation
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
if
err
!=
nil
{
if
err
!=
nil
{
return
schedulerapi
.
HostPriorityList
{},
err
return
schedulerapi
.
HostPriorityList
{},
err
...
@@ -256,7 +257,7 @@ func BalancedResourceAllocation(pod *api.Pod, machinesToPods map[string][]*api.P
...
@@ -256,7 +257,7 @@ func BalancedResourceAllocation(pod *api.Pod, machinesToPods map[string][]*api.P
list
:=
schedulerapi
.
HostPriorityList
{}
list
:=
schedulerapi
.
HostPriorityList
{}
for
_
,
node
:=
range
nodes
.
Items
{
for
_
,
node
:=
range
nodes
.
Items
{
list
=
append
(
list
,
calculateBalancedResourceAllocation
(
pod
,
node
,
machinesToPods
[
node
.
Name
]
))
list
=
append
(
list
,
calculateBalancedResourceAllocation
(
pod
,
node
,
nodeNameToInfo
[
node
.
Name
]
.
Pods
()
))
}
}
return
list
,
nil
return
list
,
nil
}
}
...
...
plugin/pkg/scheduler/algorithm/priorities/priorities_test.go
View file @
9236e4a0
...
@@ -26,8 +26,8 @@ import (
...
@@ -26,8 +26,8 @@ import (
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/plugin/pkg/scheduler"
"k8s.io/kubernetes/plugin/pkg/scheduler"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
func
makeNode
(
node
string
,
milliCPU
,
memory
int64
)
api
.
Node
{
func
makeNode
(
node
string
,
milliCPU
,
memory
int64
)
api
.
Node
{
...
@@ -132,18 +132,15 @@ func TestZeroRequest(t *testing.T) {
...
@@ -132,18 +132,15 @@ func TestZeroRequest(t *testing.T) {
const
expectedPriority
int
=
25
const
expectedPriority
int
=
25
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
list
,
err
:=
scheduler
.
PrioritizeNodes
(
list
,
err
:=
scheduler
.
PrioritizeNodes
(
test
.
pod
,
test
.
pod
,
m2p
,
nodeNameToInfo
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakePodLister
(
test
.
pods
),
// This should match the configuration in defaultPriorities() in
// This should match the configuration in defaultPriorities() in
// plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go if you want
// plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go if you want
// to test what's actually in production.
// to test what's actually in production.
[]
algorithm
.
PriorityConfig
{{
Function
:
LeastRequestedPriority
,
Weight
:
1
},
{
Function
:
BalancedResourceAllocation
,
Weight
:
1
},
{
Function
:
NewSelectorSpreadPriority
(
algorithm
.
FakeServiceLister
([]
api
.
Service
{}),
algorithm
.
FakeControllerLister
([]
api
.
ReplicationController
{})),
Weight
:
1
}},
[]
algorithm
.
PriorityConfig
{{
Function
:
LeastRequestedPriority
,
Weight
:
1
},
{
Function
:
BalancedResourceAllocation
,
Weight
:
1
},
{
Function
:
NewSelectorSpreadPriority
(
algorithm
.
Fake
PodLister
(
test
.
pods
),
algorithm
.
Fake
ServiceLister
([]
api
.
Service
{}),
algorithm
.
FakeControllerLister
([]
api
.
ReplicationController
{})),
Weight
:
1
}},
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}),
[]
algorithm
.
SchedulerExtender
{})
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}),
[]
algorithm
.
SchedulerExtender
{})
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
...
@@ -387,11 +384,8 @@ func TestLeastRequested(t *testing.T) {
...
@@ -387,11 +384,8 @@ func TestLeastRequested(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
if
err
!=
nil
{
list
,
err
:=
LeastRequestedPriority
(
test
.
pod
,
nodeNameToInfo
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
list
,
err
:=
LeastRequestedPriority
(
test
.
pod
,
m2p
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -485,7 +479,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
...
@@ -485,7 +479,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
label
:
test
.
label
,
label
:
test
.
label
,
presence
:
test
.
presence
,
presence
:
test
.
presence
,
}
}
list
,
err
:=
prioritizer
.
CalculateNodeLabelPriority
(
nil
,
map
[
string
]
[]
*
api
.
Pod
{},
nil
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
list
,
err
:=
prioritizer
.
CalculateNodeLabelPriority
(
nil
,
map
[
string
]
*
schedulercache
.
NodeInfo
{}
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -723,11 +717,8 @@ func TestBalancedResourceAllocation(t *testing.T) {
...
@@ -723,11 +717,8 @@ func TestBalancedResourceAllocation(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
if
err
!=
nil
{
list
,
err
:=
BalancedResourceAllocation
(
test
.
pod
,
nodeNameToInfo
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
list
,
err
:=
BalancedResourceAllocation
(
test
.
pod
,
m2p
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -870,11 +861,8 @@ func TestImageLocalityPriority(t *testing.T) {
...
@@ -870,11 +861,8 @@ func TestImageLocalityPriority(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
if
err
!=
nil
{
list
,
err
:=
ImageLocalityPriority
(
test
.
pod
,
nodeNameToInfo
,
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
list
,
err
:=
ImageLocalityPriority
(
test
.
pod
,
m2p
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakeNodeLister
(
api
.
NodeList
{
Items
:
test
.
nodes
}))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
...
plugin/pkg/scheduler/algorithm/priorities/selector_spreading.go
View file @
9236e4a0
...
@@ -23,6 +23,7 @@ import (
...
@@ -23,6 +23,7 @@ import (
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
// The maximum priority value to give to a node
// The maximum priority value to give to a node
...
@@ -34,12 +35,14 @@ const maxPriority = 10
...
@@ -34,12 +35,14 @@ const maxPriority = 10
const
zoneWeighting
=
2.0
/
3.0
const
zoneWeighting
=
2.0
/
3.0
type
SelectorSpread
struct
{
type
SelectorSpread
struct
{
podLister
algorithm
.
PodLister
serviceLister
algorithm
.
ServiceLister
serviceLister
algorithm
.
ServiceLister
controllerLister
algorithm
.
ControllerLister
controllerLister
algorithm
.
ControllerLister
}
}
func
NewSelectorSpreadPriority
(
serviceLister
algorithm
.
ServiceLister
,
controllerLister
algorithm
.
ControllerLister
)
algorithm
.
PriorityFunction
{
func
NewSelectorSpreadPriority
(
podLister
algorithm
.
PodLister
,
serviceLister
algorithm
.
ServiceLister
,
controllerLister
algorithm
.
ControllerLister
)
algorithm
.
PriorityFunction
{
selectorSpread
:=
&
SelectorSpread
{
selectorSpread
:=
&
SelectorSpread
{
podLister
:
podLister
,
serviceLister
:
serviceLister
,
serviceLister
:
serviceLister
,
controllerLister
:
controllerLister
,
controllerLister
:
controllerLister
,
}
}
...
@@ -73,7 +76,7 @@ func getZoneKey(node *api.Node) string {
...
@@ -73,7 +76,7 @@ func getZoneKey(node *api.Node) string {
// i.e. it pushes the scheduler towards a node where there's the smallest number of
// i.e. it pushes the scheduler towards a node where there's the smallest number of
// pods which match the same service selectors or RC selectors as the pod being scheduled.
// pods which match the same service selectors or RC selectors as the pod being scheduled.
// Where zone information is included on the nodes, it favors nodes in zones with fewer existing matching pods.
// Where zone information is included on the nodes, it favors nodes in zones with fewer existing matching pods.
func
(
s
*
SelectorSpread
)
CalculateSpreadPriority
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
(
s
*
SelectorSpread
)
CalculateSpreadPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
var
nsPods
[]
*
api
.
Pod
var
nsPods
[]
*
api
.
Pod
selectors
:=
make
([]
labels
.
Selector
,
0
)
selectors
:=
make
([]
labels
.
Selector
,
0
)
...
@@ -91,7 +94,7 @@ func (s *SelectorSpread) CalculateSpreadPriority(pod *api.Pod, machinesToPods ma
...
@@ -91,7 +94,7 @@ func (s *SelectorSpread) CalculateSpreadPriority(pod *api.Pod, machinesToPods ma
}
}
if
len
(
selectors
)
>
0
{
if
len
(
selectors
)
>
0
{
pods
,
err
:=
podLister
.
List
(
labels
.
Everything
())
pods
,
err
:=
s
.
podLister
.
List
(
labels
.
Everything
())
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -198,12 +201,14 @@ func (s *SelectorSpread) CalculateSpreadPriority(pod *api.Pod, machinesToPods ma
...
@@ -198,12 +201,14 @@ func (s *SelectorSpread) CalculateSpreadPriority(pod *api.Pod, machinesToPods ma
}
}
type
ServiceAntiAffinity
struct
{
type
ServiceAntiAffinity
struct
{
podLister
algorithm
.
PodLister
serviceLister
algorithm
.
ServiceLister
serviceLister
algorithm
.
ServiceLister
label
string
label
string
}
}
func
NewServiceAntiAffinityPriority
(
serviceLister
algorithm
.
ServiceLister
,
label
string
)
algorithm
.
PriorityFunction
{
func
NewServiceAntiAffinityPriority
(
podLister
algorithm
.
PodLister
,
serviceLister
algorithm
.
ServiceLister
,
label
string
)
algorithm
.
PriorityFunction
{
antiAffinity
:=
&
ServiceAntiAffinity
{
antiAffinity
:=
&
ServiceAntiAffinity
{
podLister
:
podLister
,
serviceLister
:
serviceLister
,
serviceLister
:
serviceLister
,
label
:
label
,
label
:
label
,
}
}
...
@@ -213,7 +218,7 @@ func NewServiceAntiAffinityPriority(serviceLister algorithm.ServiceLister, label
...
@@ -213,7 +218,7 @@ func NewServiceAntiAffinityPriority(serviceLister algorithm.ServiceLister, label
// CalculateAntiAffinityPriority spreads pods by minimizing the number of pods belonging to the same service
// CalculateAntiAffinityPriority spreads pods by minimizing the number of pods belonging to the same service
// on machines with the same value for a particular label.
// on machines with the same value for a particular label.
// The label to be considered is provided to the struct (ServiceAntiAffinity).
// The label to be considered is provided to the struct (ServiceAntiAffinity).
func
(
s
*
ServiceAntiAffinity
)
CalculateAntiAffinityPriority
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
(
s
*
ServiceAntiAffinity
)
CalculateAntiAffinityPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
var
nsServicePods
[]
*
api
.
Pod
var
nsServicePods
[]
*
api
.
Pod
services
,
err
:=
s
.
serviceLister
.
GetPodServices
(
pod
)
services
,
err
:=
s
.
serviceLister
.
GetPodServices
(
pod
)
...
@@ -221,7 +226,7 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *api.Pod, machin
...
@@ -221,7 +226,7 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *api.Pod, machin
// just use the first service and get the other pods within the service
// just use the first service and get the other pods within the service
// TODO: a separate predicate can be created that tries to handle all services for the pod
// TODO: a separate predicate can be created that tries to handle all services for the pod
selector
:=
labels
.
SelectorFromSet
(
services
[
0
]
.
Spec
.
Selector
)
selector
:=
labels
.
SelectorFromSet
(
services
[
0
]
.
Spec
.
Selector
)
pods
,
err
:=
podLister
.
List
(
selector
)
pods
,
err
:=
s
.
podLister
.
List
(
selector
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
plugin/pkg/scheduler/algorithm/priorities/selector_spreading_test.go
View file @
9236e4a0
...
@@ -24,8 +24,8 @@ import (
...
@@ -24,8 +24,8 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
wellknownlabels
"k8s.io/kubernetes/pkg/api/unversioned"
wellknownlabels
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
func
TestSelectorSpreadPriority
(
t
*
testing
.
T
)
{
func
TestSelectorSpreadPriority
(
t
*
testing
.
T
)
{
...
@@ -219,12 +219,9 @@ func TestSelectorSpreadPriority(t *testing.T) {
...
@@ -219,12 +219,9 @@ func TestSelectorSpreadPriority(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
if
err
!=
nil
{
selectorSpread
:=
SelectorSpread
{
podLister
:
algorithm
.
FakePodLister
(
test
.
pods
),
serviceLister
:
algorithm
.
FakeServiceLister
(
test
.
services
),
controllerLister
:
algorithm
.
FakeControllerLister
(
test
.
rcs
)}
t
.
Errorf
(
"unexpected error: %v"
,
err
)
list
,
err
:=
selectorSpread
.
CalculateSpreadPriority
(
test
.
pod
,
nodeNameToInfo
,
algorithm
.
FakeNodeLister
(
makeNodeList
(
test
.
nodes
)))
}
selectorSpread
:=
SelectorSpread
{
serviceLister
:
algorithm
.
FakeServiceLister
(
test
.
services
),
controllerLister
:
algorithm
.
FakeControllerLister
(
test
.
rcs
)}
list
,
err
:=
selectorSpread
.
CalculateSpreadPriority
(
test
.
pod
,
m2p
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakeNodeLister
(
makeNodeList
(
test
.
nodes
)))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -422,12 +419,9 @@ func TestZoneSelectorSpreadPriority(t *testing.T) {
...
@@ -422,12 +419,9 @@ func TestZoneSelectorSpreadPriority(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
selectorSpread
:=
SelectorSpread
{
serviceLister
:
algorithm
.
FakeServiceLister
(
test
.
services
),
controllerLister
:
algorithm
.
FakeControllerLister
(
test
.
rcs
)}
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
selectorSpread
:=
SelectorSpread
{
podLister
:
algorithm
.
FakePodLister
(
test
.
pods
),
serviceLister
:
algorithm
.
FakeServiceLister
(
test
.
services
),
controllerLister
:
algorithm
.
FakeControllerLister
(
test
.
rcs
)}
if
err
!=
nil
{
list
,
err
:=
selectorSpread
.
CalculateSpreadPriority
(
test
.
pod
,
nodeNameToInfo
,
algorithm
.
FakeNodeLister
(
makeLabeledNodeList
(
labeledNodes
)))
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
list
,
err
:=
selectorSpread
.
CalculateSpreadPriority
(
test
.
pod
,
m2p
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakeNodeLister
(
makeLabeledNodeList
(
labeledNodes
)))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
@@ -597,12 +591,9 @@ func TestZoneSpreadPriority(t *testing.T) {
...
@@ -597,12 +591,9 @@ func TestZoneSpreadPriority(t *testing.T) {
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
m2p
,
err
:=
predicates
.
MapPodsToMachines
(
algorithm
.
FakePodLister
(
test
.
pods
))
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
test
.
pods
)
if
err
!=
nil
{
zoneSpread
:=
ServiceAntiAffinity
{
podLister
:
algorithm
.
FakePodLister
(
test
.
pods
),
serviceLister
:
algorithm
.
FakeServiceLister
(
test
.
services
),
label
:
"zone"
}
t
.
Errorf
(
"unexpected error: %v"
,
err
)
list
,
err
:=
zoneSpread
.
CalculateAntiAffinityPriority
(
test
.
pod
,
nodeNameToInfo
,
algorithm
.
FakeNodeLister
(
makeLabeledNodeList
(
test
.
nodes
)))
}
zoneSpread
:=
ServiceAntiAffinity
{
serviceLister
:
algorithm
.
FakeServiceLister
(
test
.
services
),
label
:
"zone"
}
list
,
err
:=
zoneSpread
.
CalculateAntiAffinityPriority
(
test
.
pod
,
m2p
,
algorithm
.
FakePodLister
(
test
.
pods
),
algorithm
.
FakeNodeLister
(
makeLabeledNodeList
(
test
.
nodes
)))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
}
}
...
...
plugin/pkg/scheduler/algorithm/types.go
View file @
9236e4a0
...
@@ -19,12 +19,13 @@ package algorithm
...
@@ -19,12 +19,13 @@ package algorithm
import
(
import
(
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
// FitPredicate is a function that indicates if a pod fits into an existing node.
// FitPredicate is a function that indicates if a pod fits into an existing node.
type
FitPredicate
func
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
type
FitPredicate
func
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
type
PriorityFunction
func
(
pod
*
api
.
Pod
,
machineToPods
map
[
string
][]
*
api
.
Pod
,
podLister
PodLister
,
nodeLister
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
type
PriorityFunction
func
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
type
PriorityConfig
struct
{
type
PriorityConfig
struct
{
Function
PriorityFunction
Function
PriorityFunction
...
...
plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go
View file @
9236e4a0
...
@@ -68,7 +68,7 @@ func init() {
...
@@ -68,7 +68,7 @@ func init() {
"ServiceSpreadingPriority"
,
"ServiceSpreadingPriority"
,
factory
.
PriorityConfigFactory
{
factory
.
PriorityConfigFactory
{
Function
:
func
(
args
factory
.
PluginFactoryArgs
)
algorithm
.
PriorityFunction
{
Function
:
func
(
args
factory
.
PluginFactoryArgs
)
algorithm
.
PriorityFunction
{
return
priorities
.
NewSelectorSpreadPriority
(
args
.
ServiceLister
,
algorithm
.
EmptyControllerLister
{})
return
priorities
.
NewSelectorSpreadPriority
(
args
.
PodLister
,
args
.
ServiceLister
,
algorithm
.
EmptyControllerLister
{})
},
},
Weight
:
1
,
Weight
:
1
,
},
},
...
@@ -141,7 +141,7 @@ func defaultPriorities() sets.String {
...
@@ -141,7 +141,7 @@ func defaultPriorities() sets.String {
"SelectorSpreadPriority"
,
"SelectorSpreadPriority"
,
factory
.
PriorityConfigFactory
{
factory
.
PriorityConfigFactory
{
Function
:
func
(
args
factory
.
PluginFactoryArgs
)
algorithm
.
PriorityFunction
{
Function
:
func
(
args
factory
.
PluginFactoryArgs
)
algorithm
.
PriorityFunction
{
return
priorities
.
NewSelectorSpreadPriority
(
args
.
ServiceLister
,
args
.
ControllerLister
)
return
priorities
.
NewSelectorSpreadPriority
(
args
.
PodLister
,
args
.
ServiceLister
,
args
.
ControllerLister
)
},
},
Weight
:
1
,
Weight
:
1
,
},
},
...
...
plugin/pkg/scheduler/extender_test.go
View file @
9236e4a0
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
type
fitPredicate
func
(
pod
*
api
.
Pod
,
node
*
api
.
Node
)
(
bool
,
error
)
type
fitPredicate
func
(
pod
*
api
.
Pod
,
node
*
api
.
Node
)
(
bool
,
error
)
...
@@ -88,7 +89,7 @@ func machine2PrioritizerExtender(pod *api.Pod, nodes *api.NodeList) (*schedulera
...
@@ -88,7 +89,7 @@ func machine2PrioritizerExtender(pod *api.Pod, nodes *api.NodeList) (*schedulera
return
&
result
,
nil
return
&
result
,
nil
}
}
func
machine2Prioritizer
(
_
*
api
.
Pod
,
machineToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
machine2Prioritizer
(
_
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
if
err
!=
nil
{
if
err
!=
nil
{
return
[]
schedulerapi
.
HostPriority
{},
err
return
[]
schedulerapi
.
HostPriority
{},
err
...
...
plugin/pkg/scheduler/factory/factory_test.go
View file @
9236e4a0
...
@@ -34,6 +34,7 @@ import (
...
@@ -34,6 +34,7 @@ import (
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
latestschedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api/latest"
latestschedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api/latest"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
func
TestCreate
(
t
*
testing
.
T
)
{
func
TestCreate
(
t
*
testing
.
T
)
{
...
@@ -117,19 +118,19 @@ func TestCreateFromEmptyConfig(t *testing.T) {
...
@@ -117,19 +118,19 @@ func TestCreateFromEmptyConfig(t *testing.T) {
factory
.
CreateFromConfig
(
policy
)
factory
.
CreateFromConfig
(
policy
)
}
}
func
PredicateOne
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
{
func
PredicateOne
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
{
return
true
,
nil
return
true
,
nil
}
}
func
PredicateTwo
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
{
func
PredicateTwo
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
{
return
true
,
nil
return
true
,
nil
}
}
func
PriorityOne
(
pod
*
api
.
Pod
,
m2p
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
PriorityOne
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
return
[]
schedulerapi
.
HostPriority
{},
nil
return
[]
schedulerapi
.
HostPriority
{},
nil
}
}
func
PriorityTwo
(
pod
*
api
.
Pod
,
m2p
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
PriorityTwo
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
return
[]
schedulerapi
.
HostPriority
{},
nil
return
[]
schedulerapi
.
HostPriority
{},
nil
}
}
...
...
plugin/pkg/scheduler/factory/plugins.go
View file @
9236e4a0
...
@@ -168,6 +168,7 @@ func RegisterCustomPriorityFunction(policy schedulerapi.PriorityPolicy) string {
...
@@ -168,6 +168,7 @@ func RegisterCustomPriorityFunction(policy schedulerapi.PriorityPolicy) string {
pcf
=
&
PriorityConfigFactory
{
pcf
=
&
PriorityConfigFactory
{
Function
:
func
(
args
PluginFactoryArgs
)
algorithm
.
PriorityFunction
{
Function
:
func
(
args
PluginFactoryArgs
)
algorithm
.
PriorityFunction
{
return
priorities
.
NewServiceAntiAffinityPriority
(
return
priorities
.
NewServiceAntiAffinityPriority
(
args
.
PodLister
,
args
.
ServiceLister
,
args
.
ServiceLister
,
policy
.
Argument
.
ServiceAntiAffinity
.
Label
,
policy
.
Argument
.
ServiceAntiAffinity
.
Label
,
)
)
...
...
plugin/pkg/scheduler/generic_scheduler.go
View file @
9236e4a0
...
@@ -25,11 +25,13 @@ import (
...
@@ -25,11 +25,13 @@ import (
"github.com/golang/glog"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
type
FailedPredicateMap
map
[
string
]
sets
.
String
type
FailedPredicateMap
map
[
string
]
sets
.
String
...
@@ -75,12 +77,12 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
...
@@ -75,12 +77,12 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
// TODO: we should compute this once and dynamically update it using Watch, not constantly re-compute.
// TODO: we should compute this once and dynamically update it using Watch, not constantly re-compute.
// But at least we're now only doing it in one place
// But at least we're now only doing it in one place
machinesToPods
,
err
:=
predicates
.
MapPodsToMachines
(
g
.
pods
)
pods
,
err
:=
g
.
pods
.
List
(
labels
.
Everything
()
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
nodeNameToInfo
:=
schedulercache
.
CreateNodeNameToInfoMap
(
pods
)
filteredNodes
,
failedPredicateMap
,
err
:=
findNodesThatFit
(
pod
,
machinesToPods
,
g
.
predicates
,
nodes
,
g
.
extenders
)
filteredNodes
,
failedPredicateMap
,
err
:=
findNodesThatFit
(
pod
,
nodeNameToInfo
,
g
.
predicates
,
nodes
,
g
.
extenders
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
...
@@ -92,7 +94,7 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
...
@@ -92,7 +94,7 @@ func (g *genericScheduler) Schedule(pod *api.Pod, nodeLister algorithm.NodeListe
}
}
}
}
priorityList
,
err
:=
PrioritizeNodes
(
pod
,
machinesToPods
,
g
.
pods
,
g
.
prioritizers
,
algorithm
.
FakeNodeLister
(
filteredNodes
),
g
.
extenders
)
priorityList
,
err
:=
PrioritizeNodes
(
pod
,
nodeNameToInfo
,
g
.
pods
,
g
.
prioritizers
,
algorithm
.
FakeNodeLister
(
filteredNodes
),
g
.
extenders
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
...
@@ -129,14 +131,14 @@ func (g *genericScheduler) selectHost(priorityList schedulerapi.HostPriorityList
...
@@ -129,14 +131,14 @@ func (g *genericScheduler) selectHost(priorityList schedulerapi.HostPriorityList
// Filters the nodes to find the ones that fit based on the given predicate functions
// Filters the nodes to find the ones that fit based on the given predicate functions
// Each node is passed through the predicate functions to determine if it is a fit
// Each node is passed through the predicate functions to determine if it is a fit
func
findNodesThatFit
(
pod
*
api
.
Pod
,
machineToPods
map
[
string
][]
*
api
.
Pod
,
predicateFuncs
map
[
string
]
algorithm
.
FitPredicate
,
nodes
api
.
NodeList
,
extenders
[]
algorithm
.
SchedulerExtender
)
(
api
.
NodeList
,
FailedPredicateMap
,
error
)
{
func
findNodesThatFit
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
predicateFuncs
map
[
string
]
algorithm
.
FitPredicate
,
nodes
api
.
NodeList
,
extenders
[]
algorithm
.
SchedulerExtender
)
(
api
.
NodeList
,
FailedPredicateMap
,
error
)
{
filtered
:=
[]
api
.
Node
{}
filtered
:=
[]
api
.
Node
{}
failedPredicateMap
:=
FailedPredicateMap
{}
failedPredicateMap
:=
FailedPredicateMap
{}
for
_
,
node
:=
range
nodes
.
Items
{
for
_
,
node
:=
range
nodes
.
Items
{
fits
:=
true
fits
:=
true
for
name
,
predicate
:=
range
predicateFuncs
{
for
name
,
predicate
:=
range
predicateFuncs
{
fit
,
err
:=
predicate
(
pod
,
machineToPods
[
node
.
Name
],
node
.
Name
)
fit
,
err
:=
predicate
(
pod
,
node
.
Name
,
nodeNameToInfo
[
node
.
Name
]
)
if
err
!=
nil
{
if
err
!=
nil
{
switch
e
:=
err
.
(
type
)
{
switch
e
:=
err
.
(
type
)
{
case
*
predicates
.
InsufficientResourceError
:
case
*
predicates
.
InsufficientResourceError
:
...
@@ -186,13 +188,13 @@ func findNodesThatFit(pod *api.Pod, machineToPods map[string][]*api.Pod, predica
...
@@ -186,13 +188,13 @@ func findNodesThatFit(pod *api.Pod, machineToPods map[string][]*api.Pod, predica
// Each priority function can also have its own weight
// Each priority function can also have its own weight
// The node scores returned by the priority function are multiplied by the weights to get weighted scores
// The node scores returned by the priority function are multiplied by the weights to get weighted scores
// All scores are finally combined (added) to get the total weighted scores of all nodes
// All scores are finally combined (added) to get the total weighted scores of all nodes
func
PrioritizeNodes
(
pod
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
priorityConfigs
[]
algorithm
.
PriorityConfig
,
nodeLister
algorithm
.
NodeLister
,
extenders
[]
algorithm
.
SchedulerExtender
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
PrioritizeNodes
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
podLister
algorithm
.
PodLister
,
priorityConfigs
[]
algorithm
.
PriorityConfig
,
nodeLister
algorithm
.
NodeLister
,
extenders
[]
algorithm
.
SchedulerExtender
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
result
:=
schedulerapi
.
HostPriorityList
{}
result
:=
schedulerapi
.
HostPriorityList
{}
// If no priority configs are provided, then the EqualPriority function is applied
// If no priority configs are provided, then the EqualPriority function is applied
// This is required to generate the priority list in the required format
// This is required to generate the priority list in the required format
if
len
(
priorityConfigs
)
==
0
&&
len
(
extenders
)
==
0
{
if
len
(
priorityConfigs
)
==
0
&&
len
(
extenders
)
==
0
{
return
EqualPriority
(
pod
,
machinesToPods
,
podLister
,
nodeLister
)
return
EqualPriority
(
pod
,
nodeNameToInfo
,
nodeLister
)
}
}
var
(
var
(
...
@@ -213,7 +215,7 @@ func PrioritizeNodes(pod *api.Pod, machinesToPods map[string][]*api.Pod, podList
...
@@ -213,7 +215,7 @@ func PrioritizeNodes(pod *api.Pod, machinesToPods map[string][]*api.Pod, podList
defer
wg
.
Done
()
defer
wg
.
Done
()
weight
:=
config
.
Weight
weight
:=
config
.
Weight
priorityFunc
:=
config
.
Function
priorityFunc
:=
config
.
Function
prioritizedList
,
err
:=
priorityFunc
(
pod
,
machinesToPods
,
podLister
,
nodeLister
)
prioritizedList
,
err
:=
priorityFunc
(
pod
,
nodeNameToInfo
,
nodeLister
)
if
err
!=
nil
{
if
err
!=
nil
{
mu
.
Lock
()
mu
.
Lock
()
errs
=
append
(
errs
,
err
)
errs
=
append
(
errs
,
err
)
...
@@ -269,7 +271,7 @@ func PrioritizeNodes(pod *api.Pod, machinesToPods map[string][]*api.Pod, podList
...
@@ -269,7 +271,7 @@ func PrioritizeNodes(pod *api.Pod, machinesToPods map[string][]*api.Pod, podList
}
}
// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
func
EqualPriority
(
_
*
api
.
Pod
,
machinesToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
EqualPriority
(
_
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
Errorf
(
"Failed to list nodes: %v"
,
err
)
glog
.
Errorf
(
"Failed to list nodes: %v"
,
err
)
...
...
plugin/pkg/scheduler/generic_scheduler_test.go
View file @
9236e4a0
...
@@ -27,25 +27,26 @@ import (
...
@@ -27,25 +27,26 @@ import (
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
"k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
schedulerapi
"k8s.io/kubernetes/plugin/pkg/scheduler/api"
"k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
)
)
func
falsePredicate
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
{
func
falsePredicate
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
{
return
false
,
nil
return
false
,
nil
}
}
func
truePredicate
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
{
func
truePredicate
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
{
return
true
,
nil
return
true
,
nil
}
}
func
matchesPredicate
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
{
func
matchesPredicate
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
{
return
pod
.
Name
==
node
,
nil
return
pod
.
Name
==
node
Name
,
nil
}
}
func
hasNoPodsPredicate
(
pod
*
api
.
Pod
,
existingPods
[]
*
api
.
Pod
,
node
string
)
(
bool
,
error
)
{
func
hasNoPodsPredicate
(
pod
*
api
.
Pod
,
nodeName
string
,
nodeInfo
*
schedulercache
.
NodeInfo
)
(
bool
,
error
)
{
return
len
(
existingPods
)
==
0
,
nil
return
len
(
nodeInfo
.
Pods
()
)
==
0
,
nil
}
}
func
numericPriority
(
pod
*
api
.
Pod
,
machineToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
numericPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
nodes
,
err
:=
nodeLister
.
List
()
nodes
,
err
:=
nodeLister
.
List
()
result
:=
[]
schedulerapi
.
HostPriority
{}
result
:=
[]
schedulerapi
.
HostPriority
{}
...
@@ -65,11 +66,11 @@ func numericPriority(pod *api.Pod, machineToPods map[string][]*api.Pod, podListe
...
@@ -65,11 +66,11 @@ func numericPriority(pod *api.Pod, machineToPods map[string][]*api.Pod, podListe
return
result
,
nil
return
result
,
nil
}
}
func
reverseNumericPriority
(
pod
*
api
.
Pod
,
machineToPods
map
[
string
][]
*
api
.
Pod
,
podLister
algorithm
.
PodLister
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
func
reverseNumericPriority
(
pod
*
api
.
Pod
,
nodeNameToInfo
map
[
string
]
*
schedulercache
.
NodeInfo
,
nodeLister
algorithm
.
NodeLister
)
(
schedulerapi
.
HostPriorityList
,
error
)
{
var
maxScore
float64
var
maxScore
float64
minScore
:=
math
.
MaxFloat64
minScore
:=
math
.
MaxFloat64
reverseResult
:=
[]
schedulerapi
.
HostPriority
{}
reverseResult
:=
[]
schedulerapi
.
HostPriority
{}
result
,
err
:=
numericPriority
(
pod
,
machineToPods
,
podLister
,
nodeLister
)
result
,
err
:=
numericPriority
(
pod
,
nodeNameToInfo
,
nodeLister
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -275,12 +276,12 @@ func TestGenericScheduler(t *testing.T) {
...
@@ -275,12 +276,12 @@ func TestGenericScheduler(t *testing.T) {
func
TestFindFitAllError
(
t
*
testing
.
T
)
{
func
TestFindFitAllError
(
t
*
testing
.
T
)
{
nodes
:=
[]
string
{
"3"
,
"2"
,
"1"
}
nodes
:=
[]
string
{
"3"
,
"2"
,
"1"
}
predicates
:=
map
[
string
]
algorithm
.
FitPredicate
{
"true"
:
truePredicate
,
"false"
:
falsePredicate
}
predicates
:=
map
[
string
]
algorithm
.
FitPredicate
{
"true"
:
truePredicate
,
"false"
:
falsePredicate
}
machineToPods
:=
map
[
string
][]
*
api
.
Pod
{
nodeNameToInfo
:=
map
[
string
]
*
schedulercache
.
NodeInfo
{
"3"
:
{}
,
"3"
:
schedulercache
.
NewNodeInfo
()
,
"2"
:
{}
,
"2"
:
schedulercache
.
NewNodeInfo
()
,
"1"
:
{}
,
"1"
:
schedulercache
.
NewNodeInfo
()
,
}
}
_
,
predicateMap
,
err
:=
findNodesThatFit
(
&
api
.
Pod
{},
machineToPods
,
predicates
,
makeNodeList
(
nodes
),
nil
)
_
,
predicateMap
,
err
:=
findNodesThatFit
(
&
api
.
Pod
{},
nodeNameToInfo
,
predicates
,
makeNodeList
(
nodes
),
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
...
@@ -305,12 +306,12 @@ func TestFindFitSomeError(t *testing.T) {
...
@@ -305,12 +306,12 @@ func TestFindFitSomeError(t *testing.T) {
nodes
:=
[]
string
{
"3"
,
"2"
,
"1"
}
nodes
:=
[]
string
{
"3"
,
"2"
,
"1"
}
predicates
:=
map
[
string
]
algorithm
.
FitPredicate
{
"true"
:
truePredicate
,
"match"
:
matchesPredicate
}
predicates
:=
map
[
string
]
algorithm
.
FitPredicate
{
"true"
:
truePredicate
,
"match"
:
matchesPredicate
}
pod
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"1"
}}
pod
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"1"
}}
machineToPods
:=
map
[
string
][]
*
api
.
Pod
{
nodeNameToInfo
:=
map
[
string
]
*
schedulercache
.
NodeInfo
{
"3"
:
{}
,
"3"
:
schedulercache
.
NewNodeInfo
()
,
"2"
:
{}
,
"2"
:
schedulercache
.
NewNodeInfo
()
,
"1"
:
{
pod
}
,
"1"
:
schedulercache
.
NewNodeInfo
(
pod
)
,
}
}
_
,
predicateMap
,
err
:=
findNodesThatFit
(
pod
,
machineToPods
,
predicates
,
makeNodeList
(
nodes
),
nil
)
_
,
predicateMap
,
err
:=
findNodesThatFit
(
pod
,
nodeNameToInfo
,
predicates
,
makeNodeList
(
nodes
),
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error: %v"
,
err
)
t
.
Errorf
(
"unexpected error: %v"
,
err
)
...
...
plugin/pkg/scheduler/schedulercache/node_info.go
0 → 100644
View file @
9236e4a0
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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
schedulercache
import
(
"fmt"
"k8s.io/kubernetes/pkg/api"
)
var
emptyResource
=
Resource
{}
// NodeInfo is node level aggregated information.
type
NodeInfo
struct
{
// Total requested resource of all pods on this node.
// It includes assumed pods which scheduler sends binding to apiserver but
// didn't get it as scheduled yet.
requestedResource
*
Resource
pods
[]
*
api
.
Pod
}
// Resource is a collection of compute resource.
type
Resource
struct
{
MilliCPU
int64
Memory
int64
}
// NewNodeInfo returns a ready to use empty NodeInfo object.
// If any pods are given in arguments, their information will be aggregated in
// the returned object.
func
NewNodeInfo
(
pods
...*
api
.
Pod
)
*
NodeInfo
{
ni
:=
&
NodeInfo
{
requestedResource
:
&
Resource
{},
}
for
_
,
pod
:=
range
pods
{
ni
.
addPod
(
pod
)
}
return
ni
}
// Pods return all pods scheduled (including assumed to be) on this node.
func
(
n
*
NodeInfo
)
Pods
()
[]
*
api
.
Pod
{
if
n
==
nil
{
return
nil
}
return
n
.
pods
}
// RequestedResource returns aggregated resource request of pods on this node.
func
(
n
*
NodeInfo
)
RequestedResource
()
Resource
{
if
n
==
nil
{
return
emptyResource
}
return
*
n
.
requestedResource
}
// String returns representation of human readable format of this NodeInfo.
func
(
n
*
NodeInfo
)
String
()
string
{
podKeys
:=
make
([]
string
,
len
(
n
.
pods
))
for
i
,
pod
:=
range
n
.
pods
{
podKeys
[
i
]
=
pod
.
Name
}
return
fmt
.
Sprintf
(
"&NodeInfo{Pods:%v, RequestedResource:%#v}"
,
podKeys
,
n
.
requestedResource
)
}
// addPod adds pod information to this NodeInfo.
func
(
n
*
NodeInfo
)
addPod
(
pod
*
api
.
Pod
)
{
cpu
,
mem
:=
calculateResource
(
pod
)
n
.
requestedResource
.
MilliCPU
+=
cpu
n
.
requestedResource
.
Memory
+=
mem
n
.
pods
=
append
(
n
.
pods
,
pod
)
}
func
calculateResource
(
pod
*
api
.
Pod
)
(
int64
,
int64
)
{
var
cpu
,
mem
int64
for
_
,
c
:=
range
pod
.
Spec
.
Containers
{
req
:=
c
.
Resources
.
Requests
cpu
+=
req
.
Cpu
()
.
MilliValue
()
mem
+=
req
.
Memory
()
.
Value
()
}
return
cpu
,
mem
}
plugin/pkg/scheduler/schedulercache/util.go
0 → 100644
View file @
9236e4a0
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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
schedulercache
import
"k8s.io/kubernetes/pkg/api"
// CreateNodeNameToInfoMap obtains a list of pods and pivots that list into a map where the keys are node names
// and the values are the aggregated information for that node.
func
CreateNodeNameToInfoMap
(
pods
[]
*
api
.
Pod
)
map
[
string
]
*
NodeInfo
{
nodeNameToInfo
:=
make
(
map
[
string
]
*
NodeInfo
)
for
_
,
pod
:=
range
pods
{
nodeName
:=
pod
.
Spec
.
NodeName
nodeInfo
,
ok
:=
nodeNameToInfo
[
nodeName
]
if
!
ok
{
nodeInfo
=
NewNodeInfo
()
nodeNameToInfo
[
nodeName
]
=
nodeInfo
}
nodeInfo
.
addPod
(
pod
)
}
return
nodeNameToInfo
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment