k3s/pkg/scheduler/scheduler.go

143 lines
3.6 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +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.
*/
2014-06-23 18:32:11 +00:00
2014-06-06 23:40:48 +00:00
package registry
import (
"fmt"
"math/rand"
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
2014-06-06 23:40:48 +00:00
)
2014-06-28 21:21:08 +00:00
// Anything that can list minions for a scheduler.
type MinionLister interface {
List() (machines []string, err error)
}
// Make a MinionLister from a []string
type StringMinionLister []string
func (s StringMinionLister) List() ([]string, error) {
return []string(s), nil
}
2014-06-09 05:38:45 +00:00
// Scheduler is an interface implemented by things that know how to schedule pods onto machines.
2014-06-06 23:40:48 +00:00
type Scheduler interface {
2014-06-28 21:21:08 +00:00
Schedule(api.Pod, MinionLister) (string, error)
2014-06-06 23:40:48 +00:00
}
// RandomScheduler choses machines uniformly at random.
type RandomScheduler struct {
2014-06-28 21:21:08 +00:00
random rand.Rand
2014-06-06 23:40:48 +00:00
}
2014-06-28 21:21:08 +00:00
func MakeRandomScheduler(random rand.Rand) Scheduler {
2014-06-06 23:40:48 +00:00
return &RandomScheduler{
2014-06-28 21:21:08 +00:00
random: random,
2014-06-06 23:40:48 +00:00
}
}
2014-06-28 21:21:08 +00:00
func (s *RandomScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
machines, err := minionLister.List()
if err != nil {
return "", err
}
return machines[s.random.Int()%len(machines)], nil
2014-06-06 23:40:48 +00:00
}
// RoundRobinScheduler chooses machines in order.
type RoundRobinScheduler struct {
currentIndex int
}
2014-06-28 21:21:08 +00:00
func MakeRoundRobinScheduler() Scheduler {
2014-06-06 23:40:48 +00:00
return &RoundRobinScheduler{
currentIndex: -1,
2014-06-06 23:40:48 +00:00
}
}
2014-06-28 21:21:08 +00:00
func (s *RoundRobinScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
machines, err := minionLister.List()
if err != nil {
return "", err
}
s.currentIndex = (s.currentIndex + 1) % len(machines)
result := machines[s.currentIndex]
2014-06-06 23:40:48 +00:00
return result, nil
}
type FirstFitScheduler struct {
2014-06-09 04:39:57 +00:00
registry PodRegistry
random *rand.Rand
2014-06-06 23:40:48 +00:00
}
2014-06-28 21:21:08 +00:00
func MakeFirstFitScheduler(registry PodRegistry, random *rand.Rand) Scheduler {
2014-06-06 23:40:48 +00:00
return &FirstFitScheduler{
registry: registry,
2014-06-12 21:09:40 +00:00
random: random,
2014-06-06 23:40:48 +00:00
}
}
2014-06-12 20:17:34 +00:00
func (s *FirstFitScheduler) containsPort(pod api.Pod, port api.Port) bool {
2014-06-09 05:38:45 +00:00
for _, container := range pod.DesiredState.Manifest.Containers {
for _, podPort := range container.Ports {
if podPort.HostPort == port.HostPort {
2014-06-06 23:40:48 +00:00
return true
}
}
}
return false
}
2014-06-28 21:21:08 +00:00
func (s *FirstFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
machines, err := minionLister.List()
if err != nil {
return "", err
}
2014-06-12 20:17:34 +00:00
machineToPods := map[string][]api.Pod{}
pods, err := s.registry.ListPods(labels.Everything())
2014-06-06 23:40:48 +00:00
if err != nil {
return "", err
}
2014-06-09 05:38:45 +00:00
for _, scheduledPod := range pods {
host := scheduledPod.CurrentState.Host
machineToPods[host] = append(machineToPods[host], scheduledPod)
2014-06-06 23:40:48 +00:00
}
var machineOptions []string
for _, machine := range machines {
2014-06-09 05:38:45 +00:00
podFits := true
for _, scheduledPod := range machineToPods[machine] {
for _, container := range pod.DesiredState.Manifest.Containers {
2014-06-06 23:40:48 +00:00
for _, port := range container.Ports {
2014-06-09 05:38:45 +00:00
if s.containsPort(scheduledPod, port) {
podFits = false
2014-06-06 23:40:48 +00:00
}
}
}
}
2014-06-09 05:38:45 +00:00
if podFits {
machineOptions = append(machineOptions, machine)
2014-06-06 23:40:48 +00:00
}
}
if len(machineOptions) == 0 {
return "", fmt.Errorf("failed to find fit for %#v", pod)
} else {
return machineOptions[s.random.Int()%len(machineOptions)], nil
}
2014-06-06 23:40:48 +00:00
}