From 302ec0f37bc468f5f6a6dfc0bbc2859f7acd06de Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 18 Jun 2014 22:24:06 -0700 Subject: [PATCH] Wire in the pod cache. Just used for List for now. --- cmd/integration/integration.go | 2 +- pkg/master/master.go | 4 +++- pkg/master/pod_cache.go | 3 +-- pkg/master/pod_cache_test.go | 9 ++++++--- pkg/registry/pod_registry.go | 24 +++++++++++++++++++++++- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd/integration/integration.go b/cmd/integration/integration.go index 8a621dfd78..7a78ea10f2 100644 --- a/cmd/integration/integration.go +++ b/cmd/integration/integration.go @@ -44,7 +44,7 @@ func main() { reg := registry.MakeEtcdRegistry(etcdClient, machineList) apiserver := apiserver.New(map[string]apiserver.RESTStorage{ - "pods": registry.MakePodRegistryStorage(reg, &client.FakeContainerInfo{}, registry.MakeRoundRobinScheduler(machineList), nil), + "pods": registry.MakePodRegistryStorage(reg, &client.FakeContainerInfo{}, registry.MakeRoundRobinScheduler(machineList), nil, nil), "replicationControllers": registry.MakeControllerRegistryStorage(reg), }, "/api/v1beta1") server := httptest.NewServer(apiserver) diff --git a/pkg/master/master.go b/pkg/master/master.go index 701836ebee..7150726322 100644 --- a/pkg/master/master.go +++ b/pkg/master/master.go @@ -71,8 +71,10 @@ func (m *Master) init(minions []string, cloud cloudprovider.Interface) { m.minions = minions m.random = rand.New(rand.NewSource(int64(time.Now().Nanosecond()))) + podCache := NewPodCache(containerInfo, m.podRegistry, time.Second*30) + go podCache.Loop() m.storage = map[string]apiserver.RESTStorage{ - "pods": registry.MakePodRegistryStorage(m.podRegistry, containerInfo, registry.MakeFirstFitScheduler(m.minions, m.podRegistry, m.random), cloud), + "pods": registry.MakePodRegistryStorage(m.podRegistry, containerInfo, registry.MakeFirstFitScheduler(m.minions, m.podRegistry, m.random), cloud, podCache), "replicationControllers": registry.MakeControllerRegistryStorage(m.controllerRegistry), "services": registry.MakeServiceRegistryStorage(m.serviceRegistry, cloud, m.minions), } diff --git a/pkg/master/pod_cache.go b/pkg/master/pod_cache.go index 2b4c7cc255..d0355b360d 100644 --- a/pkg/master/pod_cache.go +++ b/pkg/master/pod_cache.go @@ -17,7 +17,6 @@ limitations under the License. package master import ( - "fmt" "log" "sync" "time" @@ -54,7 +53,7 @@ func (p *PodCache) GetContainerInfo(host, id string) (interface{}, error) { defer p.podLock.Unlock() value, ok := p.podInfo[id] if !ok { - return nil, fmt.Errorf("Couldn't find any information for %s", id) + return nil, nil } else { return value, nil } diff --git a/pkg/master/pod_cache_test.go b/pkg/master/pod_cache_test.go index e622ee81a0..9db9d8db4b 100644 --- a/pkg/master/pod_cache_test.go +++ b/pkg/master/pod_cache_test.go @@ -58,9 +58,12 @@ func TestPodCacheGet(t *testing.T) { func TestPodCacheGetMissing(t *testing.T) { cache := NewPodCache(nil, nil, time.Second*1) - _, err := cache.GetContainerInfo("host", "foo") - if err == nil { - t.Errorf("Unexpected non-error") + info, err := cache.GetContainerInfo("host", "foo") + if err != nil { + t.Errorf("Unexpected error: %#v", err) + } + if info != nil { + t.Errorf("Unexpected info: %#v", info) } } diff --git a/pkg/registry/pod_registry.go b/pkg/registry/pod_registry.go index 272238c689..c81ba7b563 100644 --- a/pkg/registry/pod_registry.go +++ b/pkg/registry/pod_registry.go @@ -32,16 +32,25 @@ import ( type PodRegistryStorage struct { registry PodRegistry containerInfo client.ContainerInfo + podCache client.ContainerInfo scheduler Scheduler cloud cloudprovider.Interface } -func MakePodRegistryStorage(registry PodRegistry, containerInfo client.ContainerInfo, scheduler Scheduler, cloud cloudprovider.Interface) apiserver.RESTStorage { +// MakePodRegistryStorage makes a RESTStorage object for a pod registry. +// Parameters: +// registry The pod registry +// containerInfo Source of fresh container info +// scheduler The scheduler for assigning pods to machines +// cloud Interface to a cloud provider (may be null) +// podCache Source of cached container info +func MakePodRegistryStorage(registry PodRegistry, containerInfo client.ContainerInfo, scheduler Scheduler, cloud cloudprovider.Interface, podCache client.ContainerInfo) apiserver.RESTStorage { return &PodRegistryStorage{ registry: registry, containerInfo: containerInfo, scheduler: scheduler, cloud: cloud, + podCache: podCache, } } @@ -50,7 +59,20 @@ func (storage *PodRegistryStorage) List(query labels.Query) (interface{}, error) pods, err := storage.registry.ListPods(query) if err == nil { result.Items = pods + // Get cached info for the list currently. + // TODO: Optionally use fresh info + if storage.podCache != nil { + for ix, pod := range pods { + info, err := storage.podCache.GetContainerInfo(pod.CurrentState.Host, pod.ID) + if err != nil { + log.Printf("Error getting container info: %#v", err) + continue + } + result.Items[ix].CurrentState.Info = info + } + } } + result.Kind = "cluster#podList" return result, err }