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
55d3c827
Commit
55d3c827
authored
Mar 06, 2017
by
ravisantoshgudimetla
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Selector spreading improving code readability
parent
2ebf6ede
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
15 deletions
+41
-15
selector_spreading.go
.../pkg/scheduler/algorithm/priorities/selector_spreading.go
+18
-15
selector_spreading_test.go
...scheduler/algorithm/priorities/selector_spreading_test.go
+23
-0
No files found.
plugin/pkg/scheduler/algorithm/priorities/selector_spreading.go
View file @
55d3c827
...
@@ -61,7 +61,7 @@ func NewSelectorSpreadPriority(
...
@@ -61,7 +61,7 @@ func NewSelectorSpreadPriority(
// Returns selectors of services, RCs and RSs matching the given pod.
// Returns selectors of services, RCs and RSs matching the given pod.
func
getSelectors
(
pod
*
v1
.
Pod
,
sl
algorithm
.
ServiceLister
,
cl
algorithm
.
ControllerLister
,
rsl
algorithm
.
ReplicaSetLister
,
ssl
algorithm
.
StatefulSetLister
)
[]
labels
.
Selector
{
func
getSelectors
(
pod
*
v1
.
Pod
,
sl
algorithm
.
ServiceLister
,
cl
algorithm
.
ControllerLister
,
rsl
algorithm
.
ReplicaSetLister
,
ssl
algorithm
.
StatefulSetLister
)
[]
labels
.
Selector
{
selectors
:=
make
([]
labels
.
Selector
,
0
,
3
)
var
selectors
[]
labels
.
Selector
if
services
,
err
:=
sl
.
GetPodServices
(
pod
);
err
==
nil
{
if
services
,
err
:=
sl
.
GetPodServices
(
pod
);
err
==
nil
{
for
_
,
service
:=
range
services
{
for
_
,
service
:=
range
services
{
selectors
=
append
(
selectors
,
labels
.
SelectorFromSet
(
service
.
Spec
.
Selector
))
selectors
=
append
(
selectors
,
labels
.
SelectorFromSet
(
service
.
Spec
.
Selector
))
...
@@ -206,6 +206,21 @@ func NewServiceAntiAffinityPriority(podLister algorithm.PodLister, serviceLister
...
@@ -206,6 +206,21 @@ func NewServiceAntiAffinityPriority(podLister algorithm.PodLister, serviceLister
return
antiAffinity
.
CalculateAntiAffinityPriority
return
antiAffinity
.
CalculateAntiAffinityPriority
}
}
// Classifies nodes into ones with labels and without labels.
func
(
s
*
ServiceAntiAffinity
)
getNodeClassificationByLabels
(
nodes
[]
*
v1
.
Node
)
(
map
[
string
]
string
,
[]
string
)
{
labeledNodes
:=
map
[
string
]
string
{}
nonLabeledNodes
:=
[]
string
{}
for
_
,
node
:=
range
nodes
{
if
labels
.
Set
(
node
.
Labels
)
.
Has
(
s
.
label
)
{
label
:=
labels
.
Set
(
node
.
Labels
)
.
Get
(
s
.
label
)
labeledNodes
[
node
.
Name
]
=
label
}
else
{
nonLabeledNodes
=
append
(
nonLabeledNodes
,
node
.
Name
)
}
}
return
labeledNodes
,
nonLabeledNodes
}
// 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).
...
@@ -228,17 +243,7 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNam
...
@@ -228,17 +243,7 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNam
}
}
// separate out the nodes that have the label from the ones that don't
// separate out the nodes that have the label from the ones that don't
otherNodes
:=
[]
string
{}
labeledNodes
,
nonLabeledNodes
:=
s
.
getNodeClassificationByLabels
(
nodes
)
labeledNodes
:=
map
[
string
]
string
{}
for
_
,
node
:=
range
nodes
{
if
labels
.
Set
(
node
.
Labels
)
.
Has
(
s
.
label
)
{
label
:=
labels
.
Set
(
node
.
Labels
)
.
Get
(
s
.
label
)
labeledNodes
[
node
.
Name
]
=
label
}
else
{
otherNodes
=
append
(
otherNodes
,
node
.
Name
)
}
}
podCounts
:=
map
[
string
]
int
{}
podCounts
:=
map
[
string
]
int
{}
for
_
,
pod
:=
range
nsServicePods
{
for
_
,
pod
:=
range
nsServicePods
{
label
,
exists
:=
labeledNodes
[
pod
.
Spec
.
NodeName
]
label
,
exists
:=
labeledNodes
[
pod
.
Spec
.
NodeName
]
...
@@ -247,7 +252,6 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNam
...
@@ -247,7 +252,6 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNam
}
}
podCounts
[
label
]
++
podCounts
[
label
]
++
}
}
numServicePods
:=
len
(
nsServicePods
)
numServicePods
:=
len
(
nsServicePods
)
result
:=
[]
schedulerapi
.
HostPriority
{}
result
:=
[]
schedulerapi
.
HostPriority
{}
//score int - scale of 0-maxPriority
//score int - scale of 0-maxPriority
...
@@ -261,9 +265,8 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNam
...
@@ -261,9 +265,8 @@ func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod *v1.Pod, nodeNam
result
=
append
(
result
,
schedulerapi
.
HostPriority
{
Host
:
node
,
Score
:
int
(
fScore
)})
result
=
append
(
result
,
schedulerapi
.
HostPriority
{
Host
:
node
,
Score
:
int
(
fScore
)})
}
}
// add the open nodes with a score of 0
// add the open nodes with a score of 0
for
_
,
node
:=
range
other
Nodes
{
for
_
,
node
:=
range
nonLabeled
Nodes
{
result
=
append
(
result
,
schedulerapi
.
HostPriority
{
Host
:
node
,
Score
:
0
})
result
=
append
(
result
,
schedulerapi
.
HostPriority
{
Host
:
node
,
Score
:
0
})
}
}
return
result
,
nil
return
result
,
nil
}
}
plugin/pkg/scheduler/algorithm/priorities/selector_spreading_test.go
View file @
55d3c827
...
@@ -739,6 +739,29 @@ func TestZoneSpreadPriority(t *testing.T) {
...
@@ -739,6 +739,29 @@ func TestZoneSpreadPriority(t *testing.T) {
}
}
}
}
func
TestGetNodeClassificationByLabels
(
t
*
testing
.
T
)
{
const
machine01
=
"machine01"
const
machine02
=
"machine02"
const
zoneA
=
"zoneA"
zone1
:=
map
[
string
]
string
{
"zone"
:
zoneA
,
}
labeledNodes
:=
map
[
string
]
map
[
string
]
string
{
machine01
:
zone1
,
}
expectedNonLabeledNodes
:=
[]
string
{
machine02
}
serviceAffinity
:=
ServiceAntiAffinity
{
label
:
"zone"
}
newLabeledNodes
,
noNonLabeledNodes
:=
serviceAffinity
.
getNodeClassificationByLabels
(
makeLabeledNodeList
(
labeledNodes
))
noLabeledNodes
,
newnonLabeledNodes
:=
serviceAffinity
.
getNodeClassificationByLabels
(
makeNodeList
(
expectedNonLabeledNodes
))
label
,
_
:=
newLabeledNodes
[
machine01
]
if
label
!=
zoneA
&&
len
(
noNonLabeledNodes
)
!=
0
{
t
.
Errorf
(
"Expected only labeled node with label zoneA and no noNonLabeledNodes"
)
}
if
len
(
noLabeledNodes
)
!=
0
&&
newnonLabeledNodes
[
0
]
!=
machine02
{
t
.
Errorf
(
"Expected only non labled nodes"
)
}
}
func
makeLabeledNodeList
(
nodeMap
map
[
string
]
map
[
string
]
string
)
[]
*
v1
.
Node
{
func
makeLabeledNodeList
(
nodeMap
map
[
string
]
map
[
string
]
string
)
[]
*
v1
.
Node
{
nodes
:=
make
([]
*
v1
.
Node
,
0
,
len
(
nodeMap
))
nodes
:=
make
([]
*
v1
.
Node
,
0
,
len
(
nodeMap
))
for
nodeName
,
labels
:=
range
nodeMap
{
for
nodeName
,
labels
:=
range
nodeMap
{
...
...
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