Skip to content

Commit

Permalink
Merge pull request kubernetes#3821 from brendandburns/pod
Browse files Browse the repository at this point in the history
Clear pod cache on delete.
  • Loading branch information
dchen1107 committed Jan 27, 2015
2 parents ce25cf0 + f124842 commit 10845b5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/master/pod_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ func (p *PodCache) GetPodStatus(namespace, name string) (*api.PodStatus, error)
return &value, nil
}

func (p *PodCache) ClearPodStatus(namespace, name string) {
p.lock.Lock()
defer p.lock.Unlock()

delete(p.podStatus, objKey{namespace, name})
}

func (p *PodCache) getNodeStatusInCache(name string) (*api.NodeStatus, bool) {
p.lock.Lock()
defer p.lock.Unlock()
Expand Down
30 changes: 30 additions & 0 deletions pkg/master/pod_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@ func TestPodCacheGet(t *testing.T) {
}
}

func TestPodCacheDelete(t *testing.T) {
cache := NewPodCache(nil, nil, nil, nil)

expected := api.PodStatus{
Info: api.PodInfo{
"foo": api.ContainerStatus{},
},
}
cache.podStatus[objKey{api.NamespaceDefault, "foo"}] = expected

info, err := cache.GetPodStatus(api.NamespaceDefault, "foo")
if err != nil {
t.Errorf("Unexpected error: %+v", err)
}
if !reflect.DeepEqual(info, &expected) {
t.Errorf("Unexpected mismatch. Expected: %+v, Got: %+v", &expected, info)
}

cache.ClearPodStatus(api.NamespaceDefault, "foo")

_, err = cache.GetPodStatus(api.NamespaceDefault, "foo")
if err == nil {
t.Errorf("Unexpected non-error after deleting")
}
if err != client.ErrPodInfoNotAvailable {
t.Errorf("Unexpected error: %v, expecting: %v", err, client.ErrPodInfoNotAvailable)
}

}

func TestPodCacheGetMissing(t *testing.T) {
cache := NewPodCache(nil, nil, nil, nil)

Expand Down
7 changes: 7 additions & 0 deletions pkg/registry/pod/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

type PodStatusGetter interface {
GetPodStatus(namespace, name string) (*api.PodStatus, error)
ClearPodStatus(namespace, name string)
}

// REST implements the RESTStorage interface in terms of a PodRegistry.
Expand Down Expand Up @@ -77,6 +78,12 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE

func (rs *REST) Delete(ctx api.Context, id string) (<-chan apiserver.RESTResult, error) {
return apiserver.MakeAsync(func() (runtime.Object, error) {
namespace, found := api.NamespaceFrom(ctx)
if !found {
return &api.Status{Status: api.StatusFailure}, nil
}
rs.podCache.ClearPodStatus(namespace, id)

return &api.Status{Status: api.StatusSuccess}, rs.registry.DeletePod(ctx, id)
}), nil
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/registry/pod/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
type fakeCache struct {
requestedNamespace string
requestedName string
clearedNamespace string
clearedName string

statusToReturn *api.PodStatus
errorToReturn error
Expand All @@ -47,6 +49,11 @@ func (f *fakeCache) GetPodStatus(namespace, name string) (*api.PodStatus, error)
return f.statusToReturn, f.errorToReturn
}

func (f *fakeCache) ClearPodStatus(namespace, name string) {
f.clearedNamespace = namespace
f.clearedName = name
}

func expectApiStatusError(t *testing.T, ch <-chan apiserver.RESTResult, msg string) {
out := <-ch
status, ok := out.Object.(*api.Status)
Expand Down Expand Up @@ -583,3 +590,31 @@ func TestResourceLocation(t *testing.T) {
}
}
}

func TestDeletePod(t *testing.T) {
podRegistry := registrytest.NewPodRegistry(nil)
podRegistry.Pod = &api.Pod{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Status: api.PodStatus{Host: "machine"},
}
fakeCache := &fakeCache{}
storage := REST{
registry: podRegistry,
podCache: fakeCache,
}
ctx := api.NewDefaultContext()
channel, err := storage.Delete(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
var result apiserver.RESTResult
select {
case result = <-channel:
// Do nothing, this is expected.
case <-time.After(time.Millisecond * 100):
t.Error("Unexpected timeout on async channel")
}
if fakeCache.clearedNamespace != "default" || fakeCache.clearedName != "foo" {
t.Errorf("Unexpeceted cache delete: %s %s %#v", fakeCache.clearedName, fakeCache.clearedNamespace, result.Object)
}
}

0 comments on commit 10845b5

Please sign in to comment.