2014-09-23 22:38:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. 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 scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
|
|
|
)
|
|
|
|
|
2014-12-12 22:29:20 +00:00
|
|
|
type ServiceSpread struct {
|
|
|
|
serviceLister ServiceLister
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServiceSpreadPriority(serviceLister ServiceLister) PriorityFunction {
|
|
|
|
serviceSpread := &ServiceSpread{
|
|
|
|
serviceLister: serviceLister,
|
|
|
|
}
|
|
|
|
return serviceSpread.CalculateSpreadPriority
|
|
|
|
}
|
|
|
|
|
2015-01-13 17:52:37 +00:00
|
|
|
// CalculateSpreadPriority spreads pods by minimizing the number of pods belonging to the same service
|
|
|
|
// on the same machine.
|
2014-12-12 22:29:20 +00:00
|
|
|
func (s *ServiceSpread) CalculateSpreadPriority(pod api.Pod, podLister PodLister, minionLister MinionLister) (HostPriorityList, error) {
|
|
|
|
var maxCount int
|
2015-03-06 22:29:44 +00:00
|
|
|
var nsServicePods []api.Pod
|
2014-12-12 22:29:20 +00:00
|
|
|
|
2015-01-05 22:51:22 +00:00
|
|
|
services, err := s.serviceLister.GetPodServices(pod)
|
2014-12-12 22:29:20 +00:00
|
|
|
if err == nil {
|
2015-01-05 22:51:22 +00:00
|
|
|
// 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
|
|
|
|
selector := labels.SelectorFromSet(services[0].Spec.Selector)
|
2015-03-06 22:29:44 +00:00
|
|
|
pods, err := podLister.List(selector)
|
2014-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-06 22:29:44 +00:00
|
|
|
// consider only the pods that belong to the same namespace
|
|
|
|
for _, nsPod := range pods {
|
|
|
|
if nsPod.Namespace == pod.Namespace {
|
|
|
|
nsServicePods = append(nsServicePods, nsPod)
|
|
|
|
}
|
|
|
|
}
|
2014-09-23 22:38:10 +00:00
|
|
|
}
|
2014-12-12 22:29:20 +00:00
|
|
|
|
2014-09-23 22:38:10 +00:00
|
|
|
minions, err := minionLister.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
counts := map[string]int{}
|
2015-03-06 22:29:44 +00:00
|
|
|
if len(nsServicePods) > 0 {
|
|
|
|
for _, pod := range nsServicePods {
|
2015-04-02 12:52:03 +00:00
|
|
|
counts[pod.Spec.Host]++
|
2014-12-08 20:51:05 +00:00
|
|
|
// Compute the maximum number of pods hosted on any minion
|
2015-04-02 12:52:03 +00:00
|
|
|
if counts[pod.Spec.Host] > maxCount {
|
|
|
|
maxCount = counts[pod.Spec.Host]
|
2014-12-08 20:51:05 +00:00
|
|
|
}
|
2014-11-20 22:42:31 +00:00
|
|
|
}
|
2014-09-23 22:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := []HostPriority{}
|
2014-11-26 02:10:25 +00:00
|
|
|
//score int - scale of 0-10
|
|
|
|
// 0 being the lowest priority and 10 being the highest
|
2014-10-06 22:58:18 +00:00
|
|
|
for _, minion := range minions.Items {
|
2014-12-12 22:29:20 +00:00
|
|
|
// initializing to the default/max minion score of 10
|
|
|
|
fScore := float32(10)
|
2014-11-20 22:42:31 +00:00
|
|
|
if maxCount > 0 {
|
2014-11-26 02:10:25 +00:00
|
|
|
fScore = 10 * (float32(maxCount-counts[minion.Name]) / float32(maxCount))
|
2014-11-20 22:42:31 +00:00
|
|
|
}
|
|
|
|
result = append(result, HostPriority{host: minion.Name, score: int(fScore)})
|
2014-09-23 22:38:10 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-12-22 21:54:41 +00:00
|
|
|
type ServiceAntiAffinity struct {
|
2014-12-12 22:29:20 +00:00
|
|
|
serviceLister ServiceLister
|
2014-12-22 21:54:41 +00:00
|
|
|
label string
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 21:54:41 +00:00
|
|
|
func NewServiceAntiAffinityPriority(serviceLister ServiceLister, label string) PriorityFunction {
|
|
|
|
antiAffinity := &ServiceAntiAffinity{
|
2014-12-12 22:29:20 +00:00
|
|
|
serviceLister: serviceLister,
|
2014-12-22 21:54:41 +00:00
|
|
|
label: label,
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
2014-12-22 21:54:41 +00:00
|
|
|
return antiAffinity.CalculateAntiAffinityPriority
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-13 17:30:16 +00:00
|
|
|
// CalculateAntiAffinityPriority spreads pods by minimizing the number of pods belonging to the same service
|
|
|
|
// on machines with the same value for a particular label.
|
|
|
|
// The label to be considered is provided to the struct (ServiceAntiAffinity).
|
2014-12-22 21:54:41 +00:00
|
|
|
func (s *ServiceAntiAffinity) CalculateAntiAffinityPriority(pod api.Pod, podLister PodLister, minionLister MinionLister) (HostPriorityList, error) {
|
2015-03-06 22:29:44 +00:00
|
|
|
var nsServicePods []api.Pod
|
2014-12-12 22:29:20 +00:00
|
|
|
|
2015-01-05 22:51:22 +00:00
|
|
|
services, err := s.serviceLister.GetPodServices(pod)
|
2014-12-12 22:29:20 +00:00
|
|
|
if err == nil {
|
2015-01-05 22:51:22 +00:00
|
|
|
// 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
|
|
|
|
selector := labels.SelectorFromSet(services[0].Spec.Selector)
|
2015-03-06 22:29:44 +00:00
|
|
|
pods, err := podLister.List(selector)
|
2014-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-06 22:29:44 +00:00
|
|
|
// consider only the pods that belong to the same namespace
|
|
|
|
for _, nsPod := range pods {
|
|
|
|
if nsPod.Namespace == pod.Namespace {
|
|
|
|
nsServicePods = append(nsServicePods, nsPod)
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
minions, err := minionLister.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-05 22:51:22 +00:00
|
|
|
// separate out the minions that have the label from the ones that don't
|
|
|
|
otherMinions := []string{}
|
|
|
|
labeledMinions := map[string]string{}
|
2014-12-12 22:29:20 +00:00
|
|
|
for _, minion := range minions.Items {
|
2014-12-22 21:54:41 +00:00
|
|
|
if labels.Set(minion.Labels).Has(s.label) {
|
2015-01-05 22:51:22 +00:00
|
|
|
label := labels.Set(minion.Labels).Get(s.label)
|
|
|
|
labeledMinions[minion.Name] = label
|
2014-12-12 22:29:20 +00:00
|
|
|
} else {
|
2015-01-05 22:51:22 +00:00
|
|
|
otherMinions = append(otherMinions, minion.Name)
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
podCounts := map[string]int{}
|
2015-03-06 22:29:44 +00:00
|
|
|
for _, pod := range nsServicePods {
|
2015-04-02 12:52:03 +00:00
|
|
|
label, exists := labeledMinions[pod.Spec.Host]
|
2015-01-05 22:51:22 +00:00
|
|
|
if !exists {
|
|
|
|
continue
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
2015-01-13 17:30:16 +00:00
|
|
|
podCounts[label]++
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 22:29:44 +00:00
|
|
|
numServicePods := len(nsServicePods)
|
2014-12-12 22:29:20 +00:00
|
|
|
result := []HostPriority{}
|
|
|
|
//score int - scale of 0-10
|
|
|
|
// 0 being the lowest priority and 10 being the highest
|
2015-01-05 22:51:22 +00:00
|
|
|
for minion := range labeledMinions {
|
2014-12-12 22:29:20 +00:00
|
|
|
// initializing to the default/max minion score of 10
|
|
|
|
fScore := float32(10)
|
|
|
|
if numServicePods > 0 {
|
2015-01-05 22:51:22 +00:00
|
|
|
fScore = 10 * (float32(numServicePods-podCounts[labeledMinions[minion]]) / float32(numServicePods))
|
2014-12-12 22:29:20 +00:00
|
|
|
}
|
|
|
|
result = append(result, HostPriority{host: minion, score: int(fScore)})
|
|
|
|
}
|
|
|
|
// add the open minions with a score of 0
|
2015-01-05 22:51:22 +00:00
|
|
|
for _, minion := range otherMinions {
|
2014-12-12 22:29:20 +00:00
|
|
|
result = append(result, HostPriority{host: minion, score: 0})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2014-09-23 22:38:10 +00:00
|
|
|
}
|