mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 23:37:01 +00:00
Change scheduler to poll for minions
This commit is contained in:
parent
4c4ca59050
commit
b2349bc66a
@ -30,6 +30,8 @@ import (
|
|||||||
verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag"
|
verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler"
|
"github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -38,11 +40,11 @@ var (
|
|||||||
|
|
||||||
// storeToMinionLister turns a store into a minion lister. The store must contain (only) minions.
|
// storeToMinionLister turns a store into a minion lister. The store must contain (only) minions.
|
||||||
type storeToMinionLister struct {
|
type storeToMinionLister struct {
|
||||||
s cache.Store
|
cache.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s storeToMinionLister) List() (machines []string, err error) {
|
func (s *storeToMinionLister) List() (machines []string, err error) {
|
||||||
for _, m := range s.s.List() {
|
for _, m := range s.Store.List() {
|
||||||
machines = append(machines, m.(*api.Minion).ID)
|
machines = append(machines, m.(*api.Minion).ID)
|
||||||
}
|
}
|
||||||
return machines, nil
|
return machines, nil
|
||||||
@ -50,11 +52,11 @@ func (s storeToMinionLister) List() (machines []string, err error) {
|
|||||||
|
|
||||||
// storeToPodLister turns a store into a pod lister. The store must contain (only) pods.
|
// storeToPodLister turns a store into a pod lister. The store must contain (only) pods.
|
||||||
type storeToPodLister struct {
|
type storeToPodLister struct {
|
||||||
s cache.Store
|
cache.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s storeToPodLister) ListPods(selector labels.Selector) (pods []api.Pod, err error) {
|
func (s *storeToPodLister) ListPods(selector labels.Selector) (pods []api.Pod, err error) {
|
||||||
for _, m := range s.s.List() {
|
for _, m := range s.List() {
|
||||||
pod := m.(*api.Pod)
|
pod := m.(*api.Pod)
|
||||||
if selector.Matches(labels.Set(pod.Labels)) {
|
if selector.Matches(labels.Set(pod.Labels)) {
|
||||||
pods = append(pods, *pod)
|
pods = append(pods, *pod)
|
||||||
@ -63,13 +65,31 @@ func (s storeToPodLister) ListPods(selector labels.Selector) (pods []api.Pod, er
|
|||||||
return pods, nil
|
return pods, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// minionEnumerator allows a cache.Poller to enumerate items in an api.PodList
|
||||||
|
type minionEnumerator struct {
|
||||||
|
*api.MinionList
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the number of items in the pod list.
|
||||||
|
func (me *minionEnumerator) Len() int {
|
||||||
|
if me.MinionList == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return len(me.Items)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the item (and ID) with the particular index.
|
||||||
|
func (me *minionEnumerator) Get(index int) (string, interface{}) {
|
||||||
|
return me.Items[index].ID, &me.Items[index]
|
||||||
|
}
|
||||||
|
|
||||||
type binder struct {
|
type binder struct {
|
||||||
kubeClient *client.Client
|
*client.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind just does a POST binding RPC.
|
// Bind just does a POST binding RPC.
|
||||||
func (b binder) Bind(binding *api.Binding) error {
|
func (b *binder) Bind(binding *api.Binding) error {
|
||||||
return b.kubeClient.Post().Path("bindings").Body(binding).Do().Error()
|
return b.Post().Path("bindings").Body(binding).Do().Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -90,8 +110,8 @@ func main() {
|
|||||||
// This query will only find pods with no assigned host.
|
// This query will only find pods with no assigned host.
|
||||||
return kubeClient.
|
return kubeClient.
|
||||||
Get().
|
Get().
|
||||||
Path("pods").
|
|
||||||
Path("watch").
|
Path("watch").
|
||||||
|
Path("pods").
|
||||||
SelectorParam("fields", labels.Set{"DesiredState.Host": ""}.AsSelector()).
|
SelectorParam("fields", labels.Set{"DesiredState.Host": ""}.AsSelector()).
|
||||||
UintParam("resourceVersion", resourceVersion).
|
UintParam("resourceVersion", resourceVersion).
|
||||||
Watch()
|
Watch()
|
||||||
@ -104,8 +124,8 @@ func main() {
|
|||||||
// This query will only find pods that do have an assigned host.
|
// This query will only find pods that do have an assigned host.
|
||||||
return kubeClient.
|
return kubeClient.
|
||||||
Get().
|
Get().
|
||||||
Path("pods").
|
|
||||||
Path("watch").
|
Path("watch").
|
||||||
|
Path("pods").
|
||||||
ParseSelectorParam("fields", "DesiredState.Host!=").
|
ParseSelectorParam("fields", "DesiredState.Host!=").
|
||||||
UintParam("resourceVersion", resourceVersion).
|
UintParam("resourceVersion", resourceVersion).
|
||||||
Watch()
|
Watch()
|
||||||
@ -114,25 +134,44 @@ func main() {
|
|||||||
// Watch minions.
|
// Watch minions.
|
||||||
// Minions may be listed frequently, so provide a local up-to-date cache.
|
// Minions may be listed frequently, so provide a local up-to-date cache.
|
||||||
minionCache := cache.NewStore()
|
minionCache := cache.NewStore()
|
||||||
cache.NewReflector(func(resourceVersion uint64) (watch.Interface, error) {
|
if false {
|
||||||
// This query will only find pods that do have an assigned host.
|
// Disable this code until minions support watches.
|
||||||
return kubeClient.
|
cache.NewReflector(func(resourceVersion uint64) (watch.Interface, error) {
|
||||||
Get().
|
// This query will only find pods that do have an assigned host.
|
||||||
Path("minions").
|
return kubeClient.
|
||||||
Path("watch").
|
Get().
|
||||||
UintParam("resourceVersion", resourceVersion).
|
Path("watch").
|
||||||
Watch()
|
Path("minions").
|
||||||
}, &api.Minion{}, minionCache).Run()
|
UintParam("resourceVersion", resourceVersion).
|
||||||
|
Watch()
|
||||||
|
}, &api.Minion{}, minionCache).Run()
|
||||||
|
} else {
|
||||||
|
cache.NewPoller(func() (cache.Enumerator, error) {
|
||||||
|
// This query will only find pods that do have an assigned host.
|
||||||
|
list := &api.MinionList{}
|
||||||
|
err := kubeClient.Get().Path("minions").Do().Into(list)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &minionEnumerator{list}, nil
|
||||||
|
}, 10*time.Second, minionCache).Run()
|
||||||
|
}
|
||||||
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
algo := algorithm.NewRandomFitScheduler(
|
algo := algorithm.NewRandomFitScheduler(
|
||||||
storeToPodLister{podCache}, r)
|
&storeToPodLister{podCache}, r)
|
||||||
|
|
||||||
s := scheduler.New(&scheduler.Config{
|
s := scheduler.New(&scheduler.Config{
|
||||||
MinionLister: storeToMinionLister{minionCache},
|
MinionLister: &storeToMinionLister{minionCache},
|
||||||
Algorithm: algo,
|
Algorithm: algo,
|
||||||
NextPod: func() *api.Pod { return podQueue.Pop().(*api.Pod) },
|
Binder: &binder{kubeClient},
|
||||||
Binder: binder{kubeClient},
|
NextPod: func() *api.Pod {
|
||||||
|
return podQueue.Pop().(*api.Pod)
|
||||||
|
},
|
||||||
|
Error: func(pod *api.Pod, err error) {
|
||||||
|
glog.Errorf("Error scheduling %v: %v; retrying", pod.ID, err)
|
||||||
|
podQueue.Add(pod.ID, pod)
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Run()
|
s.Run()
|
||||||
|
@ -31,7 +31,7 @@ type Binder interface {
|
|||||||
// Scheduler watches for new unscheduled pods. It attempts to find
|
// Scheduler watches for new unscheduled pods. It attempts to find
|
||||||
// minions that they fit on and writes bindings back to the api server.
|
// minions that they fit on and writes bindings back to the api server.
|
||||||
type Scheduler struct {
|
type Scheduler struct {
|
||||||
c *Config
|
config *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -53,28 +53,28 @@ type Config struct {
|
|||||||
// New returns a new scheduler.
|
// New returns a new scheduler.
|
||||||
func New(c *Config) *Scheduler {
|
func New(c *Config) *Scheduler {
|
||||||
s := &Scheduler{
|
s := &Scheduler{
|
||||||
c: c,
|
config: c,
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run begins watching and scheduling.
|
// Run begins watching and scheduling. It starts a goroutine and returns immediately.
|
||||||
func (s *Scheduler) Run() {
|
func (s *Scheduler) Run() {
|
||||||
go util.Forever(s.scheduleOne, 0)
|
go util.Forever(s.scheduleOne, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scheduler) scheduleOne() {
|
func (s *Scheduler) scheduleOne() {
|
||||||
pod := s.c.NextPod()
|
pod := s.config.NextPod()
|
||||||
dest, err := s.c.Algorithm.Schedule(*pod, s.c.MinionLister)
|
dest, err := s.config.Algorithm.Schedule(*pod, s.config.MinionLister)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.c.Error(pod, err)
|
s.config.Error(pod, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b := &api.Binding{
|
b := &api.Binding{
|
||||||
PodID: pod.ID,
|
PodID: pod.ID,
|
||||||
Host: dest,
|
Host: dest,
|
||||||
}
|
}
|
||||||
if err := s.c.Binder.Bind(b); err != nil {
|
if err := s.config.Binder.Bind(b); err != nil {
|
||||||
s.c.Error(pod, err)
|
s.config.Error(pod, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user