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 (
|
|
|
|
"math/rand"
|
2014-07-12 06:06:36 +00:00
|
|
|
"sync"
|
2014-06-28 22:35:51 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
)
|
|
|
|
|
2014-07-11 13:01:12 +00:00
|
|
|
// RandomScheduler chooses machines uniformly at random.
|
2014-06-28 22:35:51 +00:00
|
|
|
type RandomScheduler struct {
|
2014-07-12 06:06:36 +00:00
|
|
|
random *rand.Rand
|
|
|
|
randomLock sync.Mutex
|
2014-06-28 22:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MakeRandomScheduler(random *rand.Rand) Scheduler {
|
|
|
|
return &RandomScheduler{
|
|
|
|
random: random,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 13:01:12 +00:00
|
|
|
// Schedule schedules a given pod to a random machine.
|
2014-06-28 22:35:51 +00:00
|
|
|
func (s *RandomScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) {
|
|
|
|
machines, err := minionLister.List()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-07-12 06:06:36 +00:00
|
|
|
|
|
|
|
s.randomLock.Lock()
|
|
|
|
defer s.randomLock.Unlock()
|
2014-06-28 22:35:51 +00:00
|
|
|
return machines[s.random.Int()%len(machines)], nil
|
|
|
|
}
|