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-17 21:49:44 +00:00
|
|
|
|
|
|
|
package controller
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-07-25 04:55:56 +00:00
|
|
|
"sync"
|
2014-06-06 23:40:48 +00:00
|
|
|
"time"
|
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-06 23:40:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2014-06-23 00:02:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-06-06 23:40:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-08-04 03:27:38 +00:00
|
|
|
// ReplicationManager is responsible for synchronizing ReplicationController objects stored
|
|
|
|
// in the system with actual running pods.
|
2014-06-06 23:40:48 +00:00
|
|
|
type ReplicationManager struct {
|
2014-07-10 23:51:34 +00:00
|
|
|
kubeClient client.Interface
|
2014-06-09 05:38:45 +00:00
|
|
|
podControl PodControlInterface
|
2014-06-18 00:56:18 +00:00
|
|
|
syncTime <-chan time.Time
|
2014-06-18 20:10:19 +00:00
|
|
|
|
|
|
|
// To allow injection of syncReplicationController for testing.
|
2014-11-07 02:09:46 +00:00
|
|
|
syncHandler func(controller api.ReplicationController) error
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 11:47:10 +00:00
|
|
|
// PodControlInterface is an interface that knows how to add or delete pods
|
2014-06-06 23:40:48 +00:00
|
|
|
// created as an interface to allow testing.
|
2014-06-09 05:38:45 +00:00
|
|
|
type PodControlInterface interface {
|
2014-07-10 11:47:10 +00:00
|
|
|
// createReplica creates new replicated pods according to the spec.
|
2014-11-07 02:09:46 +00:00
|
|
|
createReplica(namespace string, controller api.ReplicationController)
|
2014-07-10 11:47:10 +00:00
|
|
|
// deletePod deletes the pod identified by podID.
|
2014-10-21 21:14:35 +00:00
|
|
|
deletePod(namespace string, podID string) error
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 11:47:10 +00:00
|
|
|
// RealPodControl is the default implementation of PodControllerInterface.
|
2014-06-09 05:38:45 +00:00
|
|
|
type RealPodControl struct {
|
2014-07-10 23:51:34 +00:00
|
|
|
kubeClient client.Interface
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 02:09:46 +00:00
|
|
|
func (r RealPodControl) createReplica(namespace string, controller api.ReplicationController) {
|
2014-10-23 20:55:12 +00:00
|
|
|
desiredLabels := make(labels.Set)
|
2014-11-07 02:09:46 +00:00
|
|
|
for k, v := range controller.Spec.Template.Labels {
|
2014-10-23 20:55:12 +00:00
|
|
|
desiredLabels[k] = v
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-09-08 01:31:11 +00:00
|
|
|
pod := &api.Pod{
|
2014-10-23 20:55:12 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: desiredLabels,
|
|
|
|
},
|
2014-11-07 02:09:46 +00:00
|
|
|
}
|
2014-11-13 15:52:13 +00:00
|
|
|
if err := api.Scheme.Convert(&controller.Spec.Template.Spec, &pod.Spec); err != nil {
|
2014-11-07 02:09:46 +00:00
|
|
|
glog.Errorf("Unable to convert pod template: %v", err)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-11-06 22:53:28 +00:00
|
|
|
if labels.Set(pod.Labels).AsSelector().Empty() {
|
|
|
|
glog.Errorf("Unable to create pod replica, no labels")
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-10-21 21:14:35 +00:00
|
|
|
if _, err := r.kubeClient.Pods(namespace).Create(pod); err != nil {
|
2014-10-23 20:55:12 +00:00
|
|
|
glog.Errorf("Unable to create pod replica: %v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 21:14:35 +00:00
|
|
|
func (r RealPodControl) deletePod(namespace, podID string) error {
|
|
|
|
return r.kubeClient.Pods(namespace).Delete(podID)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 04:27:19 +00:00
|
|
|
// NewReplicationManager creates a new ReplicationManager.
|
|
|
|
func NewReplicationManager(kubeClient client.Interface) *ReplicationManager {
|
2014-06-18 20:10:19 +00:00
|
|
|
rm := &ReplicationManager{
|
2014-06-06 23:40:48 +00:00
|
|
|
kubeClient: kubeClient,
|
2014-06-09 05:38:45 +00:00
|
|
|
podControl: RealPodControl{
|
2014-06-06 23:40:48 +00:00
|
|
|
kubeClient: kubeClient,
|
|
|
|
},
|
|
|
|
}
|
2014-07-20 19:00:52 +00:00
|
|
|
rm.syncHandler = rm.syncReplicationController
|
2014-06-18 20:10:19 +00:00
|
|
|
return rm
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 11:47:10 +00:00
|
|
|
// Run begins watching and syncing.
|
2014-06-17 23:42:29 +00:00
|
|
|
func (rm *ReplicationManager) Run(period time.Duration) {
|
2014-06-18 00:56:18 +00:00
|
|
|
rm.syncTime = time.Tick(period)
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion := ""
|
2014-08-08 20:50:04 +00:00
|
|
|
go util.Forever(func() { rm.watchControllers(&resourceVersion) }, period)
|
2014-06-17 23:42:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 20:50:04 +00:00
|
|
|
// resourceVersion is a pointer to the resource version to use/update.
|
2014-10-07 20:51:28 +00:00
|
|
|
func (rm *ReplicationManager) watchControllers(resourceVersion *string) {
|
2014-10-21 21:14:35 +00:00
|
|
|
watching, err := rm.kubeClient.ReplicationControllers(api.NamespaceAll).Watch(
|
2014-08-05 23:20:50 +00:00
|
|
|
labels.Everything(),
|
|
|
|
labels.Everything(),
|
2014-08-08 20:50:04 +00:00
|
|
|
*resourceVersion,
|
2014-08-05 23:20:50 +00:00
|
|
|
)
|
2014-07-20 19:00:52 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Unexpected failure to watch: %v", err)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
return
|
|
|
|
}
|
2014-06-14 01:11:32 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
for {
|
2014-06-18 00:56:18 +00:00
|
|
|
select {
|
|
|
|
case <-rm.syncTime:
|
|
|
|
rm.synchronize()
|
2014-07-20 19:00:52 +00:00
|
|
|
case event, open := <-watching.ResultChan():
|
|
|
|
if !open {
|
2014-06-18 20:10:19 +00:00
|
|
|
// watchChannel has been closed, or something else went
|
|
|
|
// wrong with our etcd watch call. Let the util.Forever()
|
|
|
|
// that called us call us again.
|
2014-06-18 00:56:18 +00:00
|
|
|
return
|
|
|
|
}
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(4).Infof("Got watch: %#v", event)
|
2014-08-19 00:43:10 +00:00
|
|
|
rc, ok := event.Object.(*api.ReplicationController)
|
|
|
|
if !ok {
|
2014-07-20 19:00:52 +00:00
|
|
|
glog.Errorf("unexpected object: %#v", event.Object)
|
2014-08-19 00:43:10 +00:00
|
|
|
continue
|
2014-06-18 00:56:18 +00:00
|
|
|
}
|
2014-08-19 00:43:10 +00:00
|
|
|
// If we get disconnected, start where we left off.
|
2014-10-07 20:51:28 +00:00
|
|
|
*resourceVersion = rc.ResourceVersion
|
2014-08-19 00:43:10 +00:00
|
|
|
// Sync even if this is a deletion event, to ensure that we leave
|
|
|
|
// it in the desired state.
|
2014-10-22 17:02:02 +00:00
|
|
|
glog.V(4).Infof("About to sync from watch: %v", rc.Name)
|
2014-10-27 17:04:39 +00:00
|
|
|
if err := rm.syncHandler(*rc); err != nil {
|
|
|
|
glog.Errorf("unexpected sync. error: %v", err)
|
|
|
|
}
|
2014-07-12 06:29:51 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
func (rm *ReplicationManager) filterActivePods(pods []api.Pod) []api.Pod {
|
|
|
|
var result []api.Pod
|
2014-06-09 05:38:45 +00:00
|
|
|
for _, value := range pods {
|
2014-11-13 15:52:13 +00:00
|
|
|
if api.PodSucceeded != value.Status.Condition &&
|
|
|
|
api.PodFailed != value.Status.Condition {
|
2014-06-06 23:40:48 +00:00
|
|
|
result = append(result, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-11-07 02:09:46 +00:00
|
|
|
func (rm *ReplicationManager) syncReplicationController(controller api.ReplicationController) error {
|
|
|
|
s := labels.Set(controller.Spec.Selector).AsSelector()
|
|
|
|
podList, err := rm.kubeClient.Pods(controller.Namespace).List(s)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
filteredList := rm.filterActivePods(podList.Items)
|
2014-11-07 02:09:46 +00:00
|
|
|
diff := len(filteredList) - controller.Spec.Replicas
|
2014-06-06 23:40:48 +00:00
|
|
|
if diff < 0 {
|
|
|
|
diff *= -1
|
2014-07-25 05:03:07 +00:00
|
|
|
wait := sync.WaitGroup{}
|
|
|
|
wait.Add(diff)
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(2).Infof("Too few replicas, creating %d\n", diff)
|
2014-06-06 23:40:48 +00:00
|
|
|
for i := 0; i < diff; i++ {
|
2014-07-25 05:03:07 +00:00
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
2014-11-07 02:09:46 +00:00
|
|
|
rm.podControl.createReplica(controller.Namespace, controller)
|
2014-07-25 05:03:07 +00:00
|
|
|
}()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-25 05:03:07 +00:00
|
|
|
wait.Wait()
|
2014-06-06 23:40:48 +00:00
|
|
|
} else if diff > 0 {
|
2014-09-18 10:46:14 +00:00
|
|
|
glog.V(2).Infof("Too many replicas, deleting %d\n", diff)
|
2014-07-25 05:03:07 +00:00
|
|
|
wait := sync.WaitGroup{}
|
|
|
|
wait.Add(diff)
|
2014-06-06 23:40:48 +00:00
|
|
|
for i := 0; i < diff; i++ {
|
2014-07-25 05:03:07 +00:00
|
|
|
go func(ix int) {
|
|
|
|
defer wait.Done()
|
2014-11-07 02:09:46 +00:00
|
|
|
rm.podControl.deletePod(controller.Namespace, filteredList[ix].Name)
|
2014-07-25 05:03:07 +00:00
|
|
|
}(i)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-25 05:03:07 +00:00
|
|
|
wait.Wait()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:42:29 +00:00
|
|
|
func (rm *ReplicationManager) synchronize() {
|
2014-08-04 03:27:38 +00:00
|
|
|
// TODO: remove this method completely and rely on the watch.
|
|
|
|
// Add resource version tracking to watch to make this work.
|
2014-11-07 02:09:46 +00:00
|
|
|
var controllers []api.ReplicationController
|
2014-10-21 21:14:35 +00:00
|
|
|
list, err := rm.kubeClient.ReplicationControllers(api.NamespaceAll).List(labels.Everything())
|
2014-06-17 23:42:29 +00:00
|
|
|
if err != nil {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Errorf("Synchronization error: %v (%#v)", err, err)
|
2014-06-18 20:10:19 +00:00
|
|
|
return
|
2014-06-17 23:42:29 +00:00
|
|
|
}
|
2014-11-07 02:09:46 +00:00
|
|
|
controllers = list.Items
|
2014-07-25 04:55:56 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2014-11-07 02:09:46 +00:00
|
|
|
wg.Add(len(controllers))
|
|
|
|
for ix := range controllers {
|
2014-07-25 04:55:56 +00:00
|
|
|
go func(ix int) {
|
|
|
|
defer wg.Done()
|
2014-11-07 02:09:46 +00:00
|
|
|
glog.V(4).Infof("periodic sync of %v", controllers[ix].Name)
|
|
|
|
err := rm.syncHandler(controllers[ix])
|
2014-07-25 04:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Error synchronizing: %#v", err)
|
|
|
|
}
|
|
|
|
}(ix)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-25 04:55:56 +00:00
|
|
|
wg.Wait()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|