From 960e16c3972ba061565e10757bba22be6fbf0798 Mon Sep 17 00:00:00 2001 From: Prashanth Balasubramanian Date: Mon, 16 Mar 2015 20:50:16 -0700 Subject: [PATCH] Embed etcdgeneric in RESTStorage for pods --- pkg/master/master.go | 1 + pkg/registry/pod/etcd/etcd.go | 59 +++++------------------------- pkg/registry/pod/etcd/etcd_test.go | 40 ++++++++++---------- 3 files changed, 30 insertions(+), 70 deletions(-) diff --git a/pkg/master/master.go b/pkg/master/master.go index 2097b53f9ee68..e5abe43af3cfc 100644 --- a/pkg/master/master.go +++ b/pkg/master/master.go @@ -382,6 +382,7 @@ func (m *Master) init(c *Config) { if c.SyncPodStatus { go util.Forever(podCache.UpdateAllContainers, m.cacheTimeout) go util.Forever(podCache.GarbageCollectPodStatus, time.Minute*30) + // Note the pod cache needs access to an un-decorated RESTStorage podStorage = podStorage.WithPodStatus(podCache) } diff --git a/pkg/registry/pod/etcd/etcd.go b/pkg/registry/pod/etcd/etcd.go index 15e7f1528f45c..a1f0861d219e8 100644 --- a/pkg/registry/pod/etcd/etcd.go +++ b/pkg/registry/pod/etcd/etcd.go @@ -30,12 +30,11 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/tools" - "github.com/GoogleCloudPlatform/kubernetes/pkg/watch" ) // rest implements a RESTStorage for pods against etcd type REST struct { - store *etcdgeneric.Etcd + etcdgeneric.Etcd } // NewREST returns a RESTStorage object that will work against pods. @@ -71,61 +70,21 @@ func NewREST(h tools.EtcdHelper) (*REST, *BindingREST, *StatusREST) { statusStore.UpdateStrategy = pod.StatusStrategy - return &REST{store: store}, &BindingREST{store: store}, &StatusREST{store: &statusStore} + return &REST{*store}, &BindingREST{store: store}, &StatusREST{store: &statusStore} +} + +// ResourceLocation returns a pods location from its HostIP +func (r *REST) ResourceLocation(ctx api.Context, name string) (string, error) { + return pod.ResourceLocation(r, ctx, name) } // WithPodStatus returns a rest object that decorates returned responses with extra // status information. func (r *REST) WithPodStatus(cache pod.PodStatusGetter) *REST { - store := *r.store + store := *r store.Decorator = pod.PodStatusDecorator(cache) store.AfterDelete = rest.AllFuncs(store.AfterDelete, pod.PodStatusReset(cache)) - return &REST{store: &store} -} - -// New returns a new object -func (r *REST) New() runtime.Object { - return r.store.NewFunc() -} - -// NewList returns a new list object -func (r *REST) NewList() runtime.Object { - return r.store.NewListFunc() -} - -// List obtains a list of pods with labels that match selector. -func (r *REST) List(ctx api.Context, label labels.Selector, field fields.Selector) (runtime.Object, error) { - return r.store.List(ctx, label, field) -} - -// Watch begins watching for new, changed, or deleted pods. -func (r *REST) Watch(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { - return r.store.Watch(ctx, label, field, resourceVersion) -} - -// Get gets a specific pod specified by its ID. -func (r *REST) Get(ctx api.Context, name string) (runtime.Object, error) { - return r.store.Get(ctx, name) -} - -// Create creates a pod based on a specification. -func (r *REST) Create(ctx api.Context, obj runtime.Object) (runtime.Object, error) { - return r.store.Create(ctx, obj) -} - -// Update changes a pod specification. -func (r *REST) Update(ctx api.Context, obj runtime.Object) (runtime.Object, bool, error) { - return r.store.Update(ctx, obj) -} - -// Delete deletes an existing pod specified by its ID. -func (r *REST) Delete(ctx api.Context, name string) (runtime.Object, error) { - return r.store.Delete(ctx, name) -} - -// ResourceLocation returns a pods location from its HostIP -func (r *REST) ResourceLocation(ctx api.Context, name string) (string, error) { - return pod.ResourceLocation(r, ctx, name) + return &store } // BindingREST implements the REST endpoint for binding pods to nodes when etcd is in use. diff --git a/pkg/registry/pod/etcd/etcd_test.go b/pkg/registry/pod/etcd/etcd_test.go index 145ff3fa96fc0..694eb865b71b8 100644 --- a/pkg/registry/pod/etcd/etcd_test.go +++ b/pkg/registry/pod/etcd/etcd_test.go @@ -704,8 +704,8 @@ func TestEtcdGetDifferentNamespace(t *testing.T) { ctx1 := api.NewDefaultContext() ctx2 := api.WithNamespace(api.NewContext(), "other") - key1, _ := registry.store.KeyFunc(ctx1, "foo") - key2, _ := registry.store.KeyFunc(ctx2, "foo") + key1, _ := registry.KeyFunc(ctx1, "foo") + key2, _ := registry.KeyFunc(ctx2, "foo") fakeClient.Set(key1, runtime.EncodeOrDie(latest.Codec, &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "default", Name: "foo"}}), 0) fakeClient.Set(key2, runtime.EncodeOrDie(latest.Codec, &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "other", Name: "foo"}}), 0) @@ -739,7 +739,7 @@ func TestEtcdGetDifferentNamespace(t *testing.T) { func TestEtcdGet(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0) obj, err := registry.Get(ctx, "foo") if err != nil { @@ -754,7 +754,7 @@ func TestEtcdGet(t *testing.T) { func TestEtcdGetNotFound(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -771,7 +771,7 @@ func TestEtcdCreate(t *testing.T) { registry, bindingRegistry, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -815,7 +815,7 @@ func TestEtcdCreateBindingNoPod(t *testing.T) { ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -861,7 +861,7 @@ func TestEtcdCreateFailsWithoutNamespace(t *testing.T) { func TestEtcdCreateAlreadyExisting(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ @@ -880,7 +880,7 @@ func TestEtcdCreateWithContainersNotFound(t *testing.T) { registry, bindingRegistry, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -927,7 +927,7 @@ func TestEtcdCreateWithConflict(t *testing.T) { registry, bindingRegistry, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -964,7 +964,7 @@ func TestEtcdCreateWithExistingContainers(t *testing.T) { registry, bindingRegistry, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -1046,7 +1046,7 @@ func TestEtcdCreateBinding(t *testing.T) { }, } for k, test := range testCases { - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -1075,7 +1075,7 @@ func TestEtcdUpdateNotFound(t *testing.T) { ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: tools.EtcdErrorNotFound, @@ -1101,7 +1101,7 @@ func TestEtcdUpdateNotScheduled(t *testing.T) { ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewPod()), 1) podIn := validChangedPod() @@ -1125,7 +1125,7 @@ func TestEtcdUpdateScheduled(t *testing.T) { ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "foo", @@ -1188,7 +1188,7 @@ func TestEtcdUpdateStatus(t *testing.T) { ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") podStart := api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "foo", @@ -1260,7 +1260,7 @@ func TestEtcdDeletePod(t *testing.T) { ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Status: api.PodStatus{Host: "machine"}, @@ -1281,7 +1281,7 @@ func TestEtcdDeletePodMultipleContainers(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true - key, _ := registry.store.KeyFunc(ctx, "foo") + key, _ := registry.KeyFunc(ctx, "foo") fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Status: api.PodStatus{Host: "machine"}, @@ -1302,7 +1302,7 @@ func TestEtcdDeletePodMultipleContainers(t *testing.T) { func TestEtcdEmptyList(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() - key := registry.store.KeyRootFunc(ctx) + key := registry.KeyRootFunc(ctx) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ @@ -1325,7 +1325,7 @@ func TestEtcdEmptyList(t *testing.T) { func TestEtcdListNotFound(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() - key := registry.store.KeyRootFunc(ctx) + key := registry.KeyRootFunc(ctx) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: tools.EtcdErrorNotFound, @@ -1343,7 +1343,7 @@ func TestEtcdListNotFound(t *testing.T) { func TestEtcdList(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() - key := registry.store.KeyRootFunc(ctx) + key := registry.KeyRootFunc(ctx) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{