Skip to content

Commit

Permalink
Embed etcdgeneric in RESTStorage for pods
Browse files Browse the repository at this point in the history
  • Loading branch information
bprashanth committed Mar 18, 2015
1 parent 4586f8c commit 960e16c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 70 deletions.
1 change: 1 addition & 0 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
59 changes: 9 additions & 50 deletions pkg/registry/pod/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 20 additions & 20 deletions pkg/registry/pod/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"},
Expand All @@ -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"},
Expand All @@ -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{
Expand All @@ -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,
Expand All @@ -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{
Expand Down

0 comments on commit 960e16c

Please sign in to comment.