Make replication controller use client

This commit is contained in:
Daniel Smith
2014-08-05 16:20:50 -07:00
parent 51caf759c3
commit 71709ae09e
3 changed files with 28 additions and 24 deletions

View File

@@ -85,28 +85,23 @@ func MakeReplicationManager(kubeClient client.Interface) *ReplicationManager {
}, },
} }
rm.syncHandler = rm.syncReplicationController rm.syncHandler = rm.syncReplicationController
rm.watchMaker = rm.makeAPIWatch
return rm return rm
} }
// Run begins watching and syncing. // Run begins watching and syncing.
func (rm *ReplicationManager) Run(period time.Duration) { func (rm *ReplicationManager) Run(period time.Duration) {
rm.syncTime = time.Tick(period) rm.syncTime = time.Tick(period)
go util.Forever(func() { rm.watchControllers() }, period) index := uint64(0)
go util.Forever(func() { rm.watchControllers(&index) }, period)
} }
// makeAPIWatch starts watching via the apiserver. // index is a pointer to the resource version to use/update.
func (rm *ReplicationManager) makeAPIWatch() (watch.Interface, error) { func (rm *ReplicationManager) watchControllers(index *uint64) {
// TODO: Fix this ugly type assertion. watching, err := rm.kubeClient.WatchReplicationControllers(
return rm.kubeClient.(*client.Client). labels.Everything(),
Get(). labels.Everything(),
Path("watch"). *index,
Path("replicationControllers"). )
Watch()
}
func (rm *ReplicationManager) watchControllers() {
watching, err := rm.watchMaker()
if err != nil { if err != nil {
glog.Errorf("Unexpected failure to watch: %v", err) glog.Errorf("Unexpected failure to watch: %v", err)
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
@@ -128,6 +123,8 @@ func (rm *ReplicationManager) watchControllers() {
if rc, ok := event.Object.(*api.ReplicationController); !ok { if rc, ok := event.Object.(*api.ReplicationController); !ok {
glog.Errorf("unexpected object: %#v", event.Object) glog.Errorf("unexpected object: %#v", event.Object)
} else { } else {
// If we get disconnected, start where we left off.
*index = rc.ResourceVersion + 1
rm.syncHandler(*rc) rm.syncHandler(*rc)
} }
} }

View File

@@ -28,6 +28,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools" "github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch" "github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
@@ -305,7 +306,7 @@ func TestSyncronize(t *testing.T) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
t.Errorf("Unexpected request for %v", req.RequestURI) t.Errorf("Unexpected request for %v", req.RequestURI)
}) })
testServer := httptest.NewTLSServer(mux) testServer := httptest.NewServer(mux)
client := client.New(testServer.URL, nil) client := client.New(testServer.URL, nil)
manager := MakeReplicationManager(client) manager := MakeReplicationManager(client)
fakePodControl := FakePodControl{} fakePodControl := FakePodControl{}
@@ -316,13 +317,18 @@ func TestSyncronize(t *testing.T) {
validateSyncReplication(t, &fakePodControl, 7, 0) validateSyncReplication(t, &fakePodControl, 7, 0)
} }
func TestWatchControllers(t *testing.T) { type FakeWatcher struct {
fakeWatcher := watch.NewFake() w *watch.FakeWatcher
manager := MakeReplicationManager(nil) *client.FakeClient
manager.watchMaker = func() (watch.Interface, error) { }
return fakeWatcher, nil
}
func (fw FakeWatcher) WatchReplicationControllers(l, f labels.Selector, rv uint64) (watch.Interface, error) {
return fw.w, nil
}
func TestWatchControllers(t *testing.T) {
client := FakeWatcher{watch.NewFake(), &client.FakeClient{}}
manager := MakeReplicationManager(client)
var testControllerSpec api.ReplicationController var testControllerSpec api.ReplicationController
received := make(chan struct{}) received := make(chan struct{})
manager.syncHandler = func(controllerSpec api.ReplicationController) error { manager.syncHandler = func(controllerSpec api.ReplicationController) error {
@@ -333,11 +339,12 @@ func TestWatchControllers(t *testing.T) {
return nil return nil
} }
go manager.watchControllers() index := uint64(0)
go manager.watchControllers(&index)
// Test normal case // Test normal case
testControllerSpec.ID = "foo" testControllerSpec.ID = "foo"
fakeWatcher.Add(&testControllerSpec) client.w.Add(&testControllerSpec)
select { select {
case <-received: case <-received:

View File

@@ -206,7 +206,7 @@ func (registry *EtcdRegistry) ListControllers() ([]api.ReplicationController, er
// WatchControllers begins watching for new, changed, or deleted controllers. // WatchControllers begins watching for new, changed, or deleted controllers.
func (registry *EtcdRegistry) WatchControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { func (registry *EtcdRegistry) WatchControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) {
if field.String() != "" { if !field.Empty() {
return nil, fmt.Errorf("no field selector implemented for controllers") return nil, fmt.Errorf("no field selector implemented for controllers")
} }
return registry.helper.WatchList("/registry/controllers", resourceVersion, func(obj interface{}) bool { return registry.helper.WatchList("/registry/controllers", resourceVersion, func(obj interface{}) bool {