Merge branch 'master' into fix/golint

Conflicts:
	pkg/master/master.go
	pkg/master/pod_cache.go
	pkg/proxy/config/file.go
	pkg/proxy/proxier.go
	pkg/proxy/roundrobbin.go
	pkg/scheduler/randomfit.go
	pkg/scheduler/randomfit_test.go
This commit is contained in:
Yuki Yugui Sonoda
2014-07-15 20:18:21 +09:00
63 changed files with 1885 additions and 292 deletions

View File

@@ -21,27 +21,28 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
// Anything that can list minions for a scheduler.
// MinionLister interface represents anything that can list minions for a scheduler.
type MinionLister interface {
List() (machines []string, err error)
}
// Make a MinionLister from a []string
// FakeMinionLister implements MinionLister on a []string for test purposes.
type FakeMinionLister []string
// Returns minions as a []string
// List returns minions as a []string
func (f FakeMinionLister) List() ([]string, error) {
return []string(f), nil
}
// Anything that can list pods for a scheduler
// PodLister interface represents anything that can list pods for a scheduler
type PodLister interface {
ListPods(labels.Selector) ([]api.Pod, error)
}
// Make a MinionLister from an []api.Pods
// FakePodLister implements PodLister on an []api.Pods for test purposes.
type FakePodLister []api.Pod
// ListPods returns []api.Pod matching a query.
func (f FakePodLister) ListPods(s labels.Selector) (selected []api.Pod, err error) {
for _, pod := range f {
if s.Matches(labels.Set(pod.Labels)) {

View File

@@ -18,14 +18,15 @@ package scheduler
import (
"math/rand"
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)
// RandomScheduler choses machines uniformly at random.
// RandomScheduler chooses machines uniformly at random.
type RandomScheduler struct {
// TODO: rand.Rand is *NOT* thread safe.
random *rand.Rand
random *rand.Rand
randomLock sync.Mutex
}
func MakeRandomScheduler(random *rand.Rand) Scheduler {
@@ -34,10 +35,14 @@ func MakeRandomScheduler(random *rand.Rand) Scheduler {
}
}
// Schedule schedules a given pod to a random machine.
func (s *RandomScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
machines, err := minionLister.List()
if err != nil {
return "", err
}
s.randomLock.Lock()
defer s.randomLock.Unlock()
return machines[s.random.Int()%len(machines)], nil
}

View File

@@ -19,25 +19,27 @@ package scheduler
import (
"fmt"
"math/rand"
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
type FirstFitScheduler struct {
podLister PodLister
// TODO: *rand.Rand is *not* threadsafe
random *rand.Rand
// RandomFitScheduler is a Scheduler which schedules a Pod on a random machine which matches its requirement.
type RandomFitScheduler struct {
podLister PodLister
random *rand.Rand
randomLock sync.Mutex
}
func NewFirstFitScheduler(podLister PodLister, random *rand.Rand) Scheduler {
return &FirstFitScheduler{
func NewRandomFitScheduler(podLister PodLister, random *rand.Rand) Scheduler {
return &RandomFitScheduler{
podLister: podLister,
random: random,
}
}
func (s *FirstFitScheduler) containsPort(pod api.Pod, port api.Port) bool {
func (s *RandomFitScheduler) containsPort(pod api.Pod, port api.Port) bool {
for _, container := range pod.DesiredState.Manifest.Containers {
for _, podPort := range container.Ports {
if podPort.HostPort == port.HostPort {
@@ -48,7 +50,8 @@ func (s *FirstFitScheduler) containsPort(pod api.Pod, port api.Port) bool {
return false
}
func (s *FirstFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
// Schedule schedules a pod on a random machine which matches its requirement.
func (s *RandomFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
machines, err := minionLister.List()
if err != nil {
return "", err
@@ -80,7 +83,8 @@ func (s *FirstFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (st
}
if len(machineOptions) == 0 {
return "", fmt.Errorf("failed to find fit for %#v", pod)
} else {
return machineOptions[s.random.Int()%len(machineOptions)], nil
}
s.randomLock.Lock()
defer s.randomLock.Unlock()
return machineOptions[s.random.Int()%len(machineOptions)], nil
}

View File

@@ -23,31 +23,31 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)
func TestFirstFitSchedulerNothingScheduled(t *testing.T) {
func TestRandomFitSchedulerNothingScheduled(t *testing.T) {
fakeRegistry := FakePodLister{}
r := rand.New(rand.NewSource(0))
st := schedulerTester{
t: t,
scheduler: NewFirstFitScheduler(&fakeRegistry, r),
scheduler: NewRandomFitScheduler(&fakeRegistry, r),
minionLister: FakeMinionLister{"m1", "m2", "m3"},
}
st.expectSchedule(api.Pod{}, "m3")
}
func TestFirstFitSchedulerFirstScheduled(t *testing.T) {
func TestRandomFitSchedulerFirstScheduled(t *testing.T) {
fakeRegistry := FakePodLister{
makePod("m1", 8080),
}
r := rand.New(rand.NewSource(0))
st := schedulerTester{
t: t,
scheduler: NewFirstFitScheduler(fakeRegistry, r),
scheduler: NewRandomFitScheduler(fakeRegistry, r),
minionLister: FakeMinionLister{"m1", "m2", "m3"},
}
st.expectSchedule(makePod("", 8080), "m3")
}
func TestFirstFitSchedulerFirstScheduledComplicated(t *testing.T) {
func TestRandomFitSchedulerFirstScheduledComplicated(t *testing.T) {
fakeRegistry := FakePodLister{
makePod("m1", 80, 8080),
makePod("m2", 8081, 8082, 8083),
@@ -56,13 +56,13 @@ func TestFirstFitSchedulerFirstScheduledComplicated(t *testing.T) {
r := rand.New(rand.NewSource(0))
st := schedulerTester{
t: t,
scheduler: NewFirstFitScheduler(fakeRegistry, r),
scheduler: NewRandomFitScheduler(fakeRegistry, r),
minionLister: FakeMinionLister{"m1", "m2", "m3"},
}
st.expectSchedule(makePod("", 8080, 8081), "m3")
}
func TestFirstFitSchedulerFirstScheduledImpossible(t *testing.T) {
func TestRandomFitSchedulerFirstScheduledImpossible(t *testing.T) {
fakeRegistry := FakePodLister{
makePod("m1", 8080),
makePod("m2", 8081),
@@ -71,7 +71,7 @@ func TestFirstFitSchedulerFirstScheduledImpossible(t *testing.T) {
r := rand.New(rand.NewSource(0))
st := schedulerTester{
t: t,
scheduler: NewFirstFitScheduler(fakeRegistry, r),
scheduler: NewRandomFitScheduler(fakeRegistry, r),
minionLister: FakeMinionLister{"m1", "m2", "m3"},
}
st.expectFailure(makePod("", 8080, 8081))

View File

@@ -31,6 +31,7 @@ func MakeRoundRobinScheduler() Scheduler {
}
}
// Schedule schedules a pod on the machine next to the last scheduled machine.
func (s *RoundRobinScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
machines, err := minionLister.List()
if err != nil {