scheduler.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package scheduler
  14. // Note: if you change code in this file, you might need to change code in
  15. // contrib/mesos/pkg/scheduler/.
  16. import (
  17. "time"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/client/record"
  20. "k8s.io/kubernetes/pkg/util/wait"
  21. "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm"
  22. "k8s.io/kubernetes/plugin/pkg/scheduler/metrics"
  23. "k8s.io/kubernetes/plugin/pkg/scheduler/schedulercache"
  24. "github.com/golang/glog"
  25. )
  26. // Binder knows how to write a binding.
  27. type Binder interface {
  28. Bind(binding *api.Binding) error
  29. }
  30. type PodConditionUpdater interface {
  31. Update(pod *api.Pod, podCondition *api.PodCondition) error
  32. }
  33. // Scheduler watches for new unscheduled pods. It attempts to find
  34. // nodes that they fit on and writes bindings back to the api server.
  35. type Scheduler struct {
  36. config *Config
  37. }
  38. type Config struct {
  39. // It is expected that changes made via SchedulerCache will be observed
  40. // by NodeLister and Algorithm.
  41. SchedulerCache schedulercache.Cache
  42. NodeLister algorithm.NodeLister
  43. Algorithm algorithm.ScheduleAlgorithm
  44. Binder Binder
  45. // PodConditionUpdater is used only in case of scheduling errors. If we succeed
  46. // with scheduling, PodScheduled condition will be updated in apiserver in /bind
  47. // handler so that binding and setting PodCondition it is atomic.
  48. PodConditionUpdater PodConditionUpdater
  49. // NextPod should be a function that blocks until the next pod
  50. // is available. We don't use a channel for this, because scheduling
  51. // a pod may take some amount of time and we don't want pods to get
  52. // stale while they sit in a channel.
  53. NextPod func() *api.Pod
  54. // Error is called if there is an error. It is passed the pod in
  55. // question, and the error
  56. Error func(*api.Pod, error)
  57. // Recorder is the EventRecorder to use
  58. Recorder record.EventRecorder
  59. // Close this to shut down the scheduler.
  60. StopEverything chan struct{}
  61. }
  62. // New returns a new scheduler.
  63. func New(c *Config) *Scheduler {
  64. s := &Scheduler{
  65. config: c,
  66. }
  67. metrics.Register()
  68. return s
  69. }
  70. // Run begins watching and scheduling. It starts a goroutine and returns immediately.
  71. func (s *Scheduler) Run() {
  72. go wait.Until(s.scheduleOne, 0, s.config.StopEverything)
  73. }
  74. func (s *Scheduler) scheduleOne() {
  75. pod := s.config.NextPod()
  76. glog.V(3).Infof("Attempting to schedule pod: %v/%v", pod.Namespace, pod.Name)
  77. start := time.Now()
  78. dest, err := s.config.Algorithm.Schedule(pod, s.config.NodeLister)
  79. if err != nil {
  80. glog.V(1).Infof("Failed to schedule pod: %v/%v", pod.Namespace, pod.Name)
  81. s.config.Error(pod, err)
  82. s.config.Recorder.Eventf(pod, api.EventTypeWarning, "FailedScheduling", "%v", err)
  83. s.config.PodConditionUpdater.Update(pod, &api.PodCondition{
  84. Type: api.PodScheduled,
  85. Status: api.ConditionFalse,
  86. Reason: "Unschedulable",
  87. })
  88. return
  89. }
  90. metrics.SchedulingAlgorithmLatency.Observe(metrics.SinceInMicroseconds(start))
  91. // Optimistically assume that the binding will succeed and send it to apiserver
  92. // in the background.
  93. // If the binding fails, scheduler will release resources allocated to assumed pod
  94. // immediately.
  95. assumed := *pod
  96. assumed.Spec.NodeName = dest
  97. if err := s.config.SchedulerCache.AssumePod(&assumed); err != nil {
  98. glog.Errorf("scheduler cache AssumePod failed: %v", err)
  99. }
  100. go func() {
  101. defer metrics.E2eSchedulingLatency.Observe(metrics.SinceInMicroseconds(start))
  102. b := &api.Binding{
  103. ObjectMeta: api.ObjectMeta{Namespace: pod.Namespace, Name: pod.Name},
  104. Target: api.ObjectReference{
  105. Kind: "Node",
  106. Name: dest,
  107. },
  108. }
  109. bindingStart := time.Now()
  110. // If binding succeeded then PodScheduled condition will be updated in apiserver so that
  111. // it's atomic with setting host.
  112. err := s.config.Binder.Bind(b)
  113. if err != nil {
  114. glog.V(1).Infof("Failed to bind pod: %v/%v", pod.Namespace, pod.Name)
  115. if err := s.config.SchedulerCache.ForgetPod(&assumed); err != nil {
  116. glog.Errorf("scheduler cache ForgetPod failed: %v", err)
  117. }
  118. s.config.Error(pod, err)
  119. s.config.Recorder.Eventf(pod, api.EventTypeNormal, "FailedScheduling", "Binding rejected: %v", err)
  120. s.config.PodConditionUpdater.Update(pod, &api.PodCondition{
  121. Type: api.PodScheduled,
  122. Status: api.ConditionFalse,
  123. Reason: "BindingRejected",
  124. })
  125. return
  126. }
  127. metrics.BindingLatency.Observe(metrics.SinceInMicroseconds(bindingStart))
  128. s.config.Recorder.Eventf(pod, api.EventTypeNormal, "Scheduled", "Successfully assigned %v to %v", pod.Name, dest)
  129. }()
  130. }