k3s/pkg/controller/replication_controller.go

203 lines
5.9 KiB
Go
Raw Normal View History

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.
*/
package controller
2014-06-06 23:40:48 +00:00
import (
"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"
"github.com/golang/glog"
2014-06-06 23:40:48 +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 {
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
// To allow injection of syncReplicationController for testing.
syncHandler func(controllerSpec 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-06-12 20:17:34 +00:00
createReplica(controllerSpec api.ReplicationController)
2014-07-10 11:47:10 +00:00
// deletePod deletes the pod identified by podID.
2014-06-09 05:38:45 +00:00
deletePod(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 {
kubeClient client.Interface
2014-06-06 23:40:48 +00:00
}
2014-06-12 20:17:34 +00:00
func (r RealPodControl) createReplica(controllerSpec api.ReplicationController) {
2014-06-09 04:39:57 +00:00
labels := controllerSpec.DesiredState.PodTemplate.Labels
// TODO: don't fail to set this label just because the map isn't created.
2014-06-06 23:40:48 +00:00
if labels != nil {
labels["replicationController"] = controllerSpec.ID
}
2014-09-08 01:31:11 +00:00
pod := &api.Pod{
2014-06-09 04:39:57 +00:00
DesiredState: controllerSpec.DesiredState.PodTemplate.DesiredState,
Labels: controllerSpec.DesiredState.PodTemplate.Labels,
2014-06-06 23:40:48 +00:00
}
2014-06-09 05:38:45 +00:00
_, err := r.kubeClient.CreatePod(pod)
2014-06-06 23:40:48 +00:00
if err != nil {
glog.Errorf("%#v\n", err)
2014-06-06 23:40:48 +00:00
}
}
2014-06-09 05:38:45 +00:00
func (r RealPodControl) deletePod(podID string) error {
return r.kubeClient.DeletePod(podID)
2014-06-06 23:40:48 +00:00
}
// NewReplicationManager creates a new ReplicationManager.
func NewReplicationManager(kubeClient client.Interface) *ReplicationManager {
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,
},
}
rm.syncHandler = rm.syncReplicationController
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)
resourceVersion := uint64(0)
go util.Forever(func() { rm.watchControllers(&resourceVersion) }, period)
2014-06-17 23:42:29 +00:00
}
// resourceVersion is a pointer to the resource version to use/update.
func (rm *ReplicationManager) watchControllers(resourceVersion *uint64) {
2014-08-05 23:20:50 +00:00
watching, err := rm.kubeClient.WatchReplicationControllers(
labels.Everything(),
labels.Everything(),
*resourceVersion,
2014-08-05 23:20:50 +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()
case event, open := <-watching.ResultChan():
if !open {
// 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
}
glog.Infof("Got watch: %#v", event)
rc, ok := event.Object.(*api.ReplicationController)
if !ok {
glog.Errorf("unexpected object: %#v", event.Object)
continue
2014-06-18 00:56:18 +00:00
}
// If we get disconnected, start where we left off.
*resourceVersion = rc.ResourceVersion + 1
// Sync even if this is a deletion event, to ensure that we leave
// it in the desired state.
glog.Infof("About to sync from watch: %v", rc.ID)
rm.syncHandler(*rc)
}
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 {
if api.PodTerminated != value.CurrentState.Status {
2014-06-06 23:40:48 +00:00
result = append(result, value)
}
}
return result
}
2014-06-12 20:17:34 +00:00
func (rm *ReplicationManager) syncReplicationController(controllerSpec api.ReplicationController) error {
2014-06-23 00:02:48 +00:00
s := labels.Set(controllerSpec.DesiredState.ReplicaSelector).AsSelector()
podList, err := rm.kubeClient.ListPods(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-06-06 23:40:48 +00:00
diff := len(filteredList) - controllerSpec.DesiredState.Replicas
if diff < 0 {
diff *= -1
wait := sync.WaitGroup{}
wait.Add(diff)
glog.Infof("Too few replicas, creating %d\n", diff)
2014-06-06 23:40:48 +00:00
for i := 0; i < diff; i++ {
go func() {
defer wait.Done()
rm.podControl.createReplica(controllerSpec)
}()
2014-06-06 23:40:48 +00:00
}
wait.Wait()
2014-06-06 23:40:48 +00:00
} else if diff > 0 {
glog.Infof("Too many replicas, deleting %d\n", diff)
wait := sync.WaitGroup{}
wait.Add(diff)
2014-06-06 23:40:48 +00:00
for i := 0; i < diff; i++ {
go func(ix int) {
defer wait.Done()
rm.podControl.deletePod(filteredList[ix].ID)
}(i)
2014-06-06 23:40:48 +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() {
// TODO: remove this method completely and rely on the watch.
// Add resource version tracking to watch to make this work.
2014-06-18 00:56:18 +00:00
var controllerSpecs []api.ReplicationController
list, err := rm.kubeClient.ListReplicationControllers(labels.Everything())
2014-06-17 23:42:29 +00:00
if err != nil {
glog.Errorf("Synchronization error: %v (%#v)", err, err)
return
2014-06-17 23:42:29 +00:00
}
controllerSpecs = list.Items
wg := sync.WaitGroup{}
wg.Add(len(controllerSpecs))
for ix := range controllerSpecs {
go func(ix int) {
defer wg.Done()
glog.Infof("periodic sync of %v", controllerSpecs[ix].ID)
err := rm.syncHandler(controllerSpecs[ix])
if err != nil {
glog.Errorf("Error synchronizing: %#v", err)
}
}(ix)
2014-06-06 23:40:48 +00:00
}
wg.Wait()
2014-06-06 23:40:48 +00:00
}