Merge pull request #20699 from jiangyaoguo/add-AvoidPreviousNode
Automatic merge from submit-queue Implement alpha version of PreferAvoidPods This is part of #18853 <!-- Reviewable:start --> --- This change is [<img src="http://reviewable.k8s.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](http://reviewable.k8s.io/reviews/kubernetes/kubernetes/20699) <!-- Reviewable:end -->
This commit is contained in:
@@ -280,3 +280,79 @@ func fractionOfCapacity(requested, capacity int64) float64 {
|
||||
}
|
||||
return float64(requested) / float64(capacity)
|
||||
}
|
||||
|
||||
type NodePreferAvoidPod struct {
|
||||
controllerLister algorithm.ControllerLister
|
||||
replicaSetLister algorithm.ReplicaSetLister
|
||||
}
|
||||
|
||||
func NewNodePreferAvoidPodsPriority(controllerLister algorithm.ControllerLister, replicaSetLister algorithm.ReplicaSetLister) algorithm.PriorityFunction {
|
||||
nodePreferAvoid := &NodePreferAvoidPod{
|
||||
controllerLister: controllerLister,
|
||||
replicaSetLister: replicaSetLister,
|
||||
}
|
||||
return nodePreferAvoid.CalculateNodePreferAvoidPodsPriority
|
||||
}
|
||||
|
||||
func (npa *NodePreferAvoidPod) CalculateNodePreferAvoidPodsPriority(pod *api.Pod, nodeNameToInfo map[string]*schedulercache.NodeInfo, nodeLister algorithm.NodeLister) (schedulerapi.HostPriorityList, error) {
|
||||
var score int
|
||||
nodes, err := nodeLister.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []schedulerapi.HostPriority{}
|
||||
|
||||
// TODO: Once we have ownerReference fully implemented, use it to find controller for the pod.
|
||||
rcs, err := npa.controllerLister.GetPodControllers(pod)
|
||||
rss, err := npa.replicaSetLister.GetPodReplicaSets(pod)
|
||||
if len(rcs) == 0 && len(rss) == 0 {
|
||||
for _, node := range nodes {
|
||||
result = append(result, schedulerapi.HostPriority{Host: node.Name, Score: 10})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
avoidNodes := map[string]bool{}
|
||||
for _, node := range nodes {
|
||||
avoidNodes[node.Name] = false
|
||||
|
||||
avoids, err := api.GetAvoidPodsFromNodeAnnotations(node.Annotations)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, avoid := range avoids.PreferAvoidPods {
|
||||
for _, rc := range rcs {
|
||||
if avoid.PodSignature.PodController.Kind == "ReplicationController" && avoid.PodSignature.PodController.UID == rc.UID {
|
||||
avoidNodes[node.Name] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if avoidNodes[node.Name] {
|
||||
break
|
||||
}
|
||||
for _, rs := range rss {
|
||||
if avoid.PodSignature.PodController.Kind == "ReplicaSet" && avoid.PodSignature.PodController.UID == rs.UID {
|
||||
avoidNodes[node.Name] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if avoidNodes[node.Name] {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//score int - scale of 0-10
|
||||
// 0 being the lowest priority and 10 being the highest
|
||||
for nodeName, shouldAvoid := range avoidNodes {
|
||||
if shouldAvoid {
|
||||
score = 0
|
||||
} else {
|
||||
score = 10
|
||||
}
|
||||
result = append(result, schedulerapi.HostPriority{Host: nodeName, Score: score})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ import (
|
||||
"k8s.io/kubernetes/cmd/libs/go2idl/types"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
"k8s.io/kubernetes/pkg/util/codeinspector"
|
||||
"k8s.io/kubernetes/plugin/pkg/scheduler"
|
||||
@@ -948,3 +949,137 @@ func TestPrioritiesRegistered(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodePreferAvoidPriority(t *testing.T) {
|
||||
label1 := map[string]string{"foo": "bar"}
|
||||
label2 := map[string]string{"bar": "foo"}
|
||||
annotations1 := map[string]string{
|
||||
api.PreferAvoidPodsAnnotationKey: `
|
||||
{
|
||||
"preferAvoidPods": [
|
||||
{
|
||||
"podSignature": {
|
||||
"podController": {
|
||||
"apiVersion": "v1",
|
||||
"kind": "ReplicationController",
|
||||
"name": "foo",
|
||||
"uid": "abcdef123456",
|
||||
"controller": true
|
||||
}
|
||||
},
|
||||
"reason": "some reason",
|
||||
"message": "some message"
|
||||
}
|
||||
]
|
||||
}`,
|
||||
}
|
||||
annotations2 := map[string]string{
|
||||
api.PreferAvoidPodsAnnotationKey: `
|
||||
{
|
||||
"preferAvoidPods": [
|
||||
{
|
||||
"podSignature": {
|
||||
"podController": {
|
||||
"apiVersion": "v1",
|
||||
"kind": "ReplicaSet",
|
||||
"name": "foo",
|
||||
"uid": "qwert12345",
|
||||
"controller": true
|
||||
}
|
||||
},
|
||||
"reason": "some reason",
|
||||
"message": "some message"
|
||||
}
|
||||
]
|
||||
}`,
|
||||
}
|
||||
testNodes := []*api.Node{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "machine1", Annotations: annotations1},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "machine2", Annotations: annotations2},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{Name: "machine3"},
|
||||
},
|
||||
}
|
||||
tests := []struct {
|
||||
pod *api.Pod
|
||||
rcs []api.ReplicationController
|
||||
rss []extensions.ReplicaSet
|
||||
nodes []*api.Node
|
||||
expectedList schedulerapi.HostPriorityList
|
||||
test string
|
||||
}{
|
||||
{
|
||||
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "default", Labels: label1}},
|
||||
rcs: []api.ReplicationController{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "foo",
|
||||
UID: "abcdef123456",
|
||||
},
|
||||
Spec: api.ReplicationControllerSpec{Selector: label1},
|
||||
},
|
||||
},
|
||||
nodes: testNodes,
|
||||
expectedList: []schedulerapi.HostPriority{{"machine1", 0}, {"machine2", 10}, {"machine3", 10}},
|
||||
test: "pod managed by ReplicationController should avoid a node, this node get lowest priority score",
|
||||
},
|
||||
{
|
||||
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "default", Labels: label2}},
|
||||
rss: []extensions.ReplicaSet{
|
||||
{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "ReplicaSet",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "bar",
|
||||
UID: "qwert12345",
|
||||
},
|
||||
Spec: extensions.ReplicaSetSpec{Selector: &unversioned.LabelSelector{MatchLabels: label2}},
|
||||
},
|
||||
},
|
||||
nodes: testNodes,
|
||||
expectedList: []schedulerapi.HostPriority{{"machine1", 10}, {"machine2", 0}, {"machine3", 10}},
|
||||
test: "pod managed by ReplicaSet should avoid a node, this node get lowest priority score",
|
||||
},
|
||||
{
|
||||
pod: &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "default"}},
|
||||
rcs: []api.ReplicationController{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "foo",
|
||||
UID: "abcdef123456",
|
||||
},
|
||||
Spec: api.ReplicationControllerSpec{Selector: label1},
|
||||
},
|
||||
},
|
||||
nodes: testNodes,
|
||||
expectedList: []schedulerapi.HostPriority{{"machine1", 10}, {"machine2", 10}, {"machine3", 10}},
|
||||
test: "pod should not avoid these nodes, all nodes get highest priority score",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
prioritizer := NodePreferAvoidPod{
|
||||
controllerLister: algorithm.FakeControllerLister(test.rcs),
|
||||
replicaSetLister: algorithm.FakeReplicaSetLister(test.rss),
|
||||
}
|
||||
list, err := prioritizer.CalculateNodePreferAvoidPodsPriority(test.pod, map[string]*schedulercache.NodeInfo{}, algorithm.FakeNodeLister(test.nodes))
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
// sort the two lists to avoid failures on account of different ordering
|
||||
sort.Sort(test.expectedList)
|
||||
sort.Sort(list)
|
||||
if !reflect.DeepEqual(test.expectedList, list) {
|
||||
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user