2014-06-28 22:35:51 +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 (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2014-07-12 06:06:51 +00:00
|
|
|
"sync"
|
2014-06-28 22:35:51 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
|
|
|
)
|
|
|
|
|
2014-07-12 14:14:39 +00:00
|
|
|
// RandomFitScheduler is a Scheduler which schedules a Pod on a random machine which matches its requirement.
|
|
|
|
type RandomFitScheduler struct {
|
2014-07-12 06:06:51 +00:00
|
|
|
podLister PodLister
|
|
|
|
random *rand.Rand
|
|
|
|
randomLock sync.Mutex
|
2014-06-28 22:35:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 11:18:21 +00:00
|
|
|
func NewRandomFitScheduler(podLister PodLister, random *rand.Rand) Scheduler {
|
2014-07-12 14:14:39 +00:00
|
|
|
return &RandomFitScheduler{
|
2014-06-28 22:35:51 +00:00
|
|
|
podLister: podLister,
|
|
|
|
random: random,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:14:39 +00:00
|
|
|
func (s *RandomFitScheduler) containsPort(pod api.Pod, port api.Port) bool {
|
2014-06-28 22:35:51 +00:00
|
|
|
for _, container := range pod.DesiredState.Manifest.Containers {
|
|
|
|
for _, podPort := range container.Ports {
|
|
|
|
if podPort.HostPort == port.HostPort {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:06:51 +00:00
|
|
|
// Schedule schedules a pod on a random machine which matches its requirement.
|
2014-07-12 14:14:39 +00:00
|
|
|
func (s *RandomFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
|
2014-06-28 22:35:51 +00:00
|
|
|
machines, err := minionLister.List()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
machineToPods := map[string][]api.Pod{}
|
2014-08-03 07:01:28 +00:00
|
|
|
// TODO: perform more targeted query...
|
2014-06-28 22:35:51 +00:00
|
|
|
pods, err := s.podLister.ListPods(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, scheduledPod := range pods {
|
|
|
|
host := scheduledPod.CurrentState.Host
|
|
|
|
machineToPods[host] = append(machineToPods[host], scheduledPod)
|
|
|
|
}
|
|
|
|
var machineOptions []string
|
|
|
|
for _, machine := range machines {
|
|
|
|
podFits := true
|
|
|
|
for _, scheduledPod := range machineToPods[machine] {
|
|
|
|
for _, container := range pod.DesiredState.Manifest.Containers {
|
|
|
|
for _, port := range container.Ports {
|
2014-08-19 22:18:49 +00:00
|
|
|
if port.HostPort == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-28 22:35:51 +00:00
|
|
|
if s.containsPort(scheduledPod, port) {
|
|
|
|
podFits = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if podFits {
|
|
|
|
machineOptions = append(machineOptions, machine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(machineOptions) == 0 {
|
|
|
|
return "", fmt.Errorf("failed to find fit for %#v", pod)
|
|
|
|
}
|
2014-07-12 06:06:51 +00:00
|
|
|
s.randomLock.Lock()
|
|
|
|
defer s.randomLock.Unlock()
|
2014-07-11 13:01:12 +00:00
|
|
|
return machineOptions[s.random.Int()%len(machineOptions)], nil
|
2014-06-28 22:35:51 +00:00
|
|
|
}
|