From 7b11cbd622348caaa5e4ab2b80b59df87876e7cc Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 9 Jul 2014 22:13:07 -0700 Subject: [PATCH 1/3] Add PodIP to the info returned for a pod. --- pkg/api/types.go | 1 + pkg/registry/pod_registry.go | 6 +++++ pkg/registry/pod_registry_test.go | 39 +++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/pkg/api/types.go b/pkg/api/types.go index aa79e0c0087..c2a8b25198e 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -196,6 +196,7 @@ type PodState struct { Status PodStatus `json:"status,omitempty" yaml:"status,omitempty"` Host string `json:"host,omitempty" yaml:"host,omitempty"` HostIP string `json:"hostIP,omitempty" yaml:"hostIP,omitempty"` + PodIP string `json:"podIP,omitempty" yaml:"podIP,omitempty"` // The key of this map is the *name* of the container within the manifest; it has one // entry per container in the manifest. The value of this map is currently the output diff --git a/pkg/registry/pod_registry.go b/pkg/registry/pod_registry.go index bc6415bc294..4ca61c246f5 100644 --- a/pkg/registry/pod_registry.go +++ b/pkg/registry/pod_registry.go @@ -89,6 +89,12 @@ func (storage *PodRegistryStorage) fillPodInfo(pod *api.Pod) { return } pod.CurrentState.Info = info + netContainerInfo, ok := info["net"] + if ok { + pod.CurrentState.PodIP = netContainerInfo.NetworkSettings.IPAddress + } else { + glog.Warningf("Couldn't find network container in %v", info) + } } } diff --git a/pkg/registry/pod_registry_test.go b/pkg/registry/pod_registry_test.go index c5a42d081ba..8f7383520eb 100644 --- a/pkg/registry/pod_registry_test.go +++ b/pkg/registry/pod_registry_test.go @@ -278,3 +278,42 @@ func TestCreatePod(t *testing.T) { // Do nothing, this is expected. } } + +type FakePodInfoGetter struct { + info api.PodInfo + err error +} + +func (f *FakePodInfoGetter) GetPodInfo(host, podID string) (api.PodInfo, error) { + return f.info, f.err +} + +func TestFillPodInfo(t *testing.T) { + expectedIP := "1.2.3.4" + fakeGetter := FakePodInfoGetter{ + info: map[string]docker.Container{ + "net": { + ID: "foobar", + Path: "bin/run.sh", + NetworkSettings: &docker.NetworkSettings{ + IPAddress: expectedIP, + }, + }, + }, + } + storage := PodRegistryStorage{ + podCache: &fakeGetter, + } + + pod := api.Pod{} + + storage.fillPodInfo(&pod) + + if !reflect.DeepEqual(fakeGetter.info, pod.CurrentState.Info) { + t.Errorf("Unexpected mis-match: %#v vs %#v", fakeGetter.info, pod.CurrentState.Info) + } + + if pod.CurrentState.PodIP != expectedIP { + t.Errorf("Expected %s, saw %s", expectedIP, pod.CurrentState.PodIP) + } +} From d5b7c4eb6d00c0f9f9d3010cbdedabf3807a06e1 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Wed, 9 Jul 2014 22:31:17 -0700 Subject: [PATCH 2/3] Fix integration tests with a check for nil Add a unit test to validate behavior --- pkg/registry/pod_registry.go | 6 +++++- pkg/registry/pod_registry_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/registry/pod_registry.go b/pkg/registry/pod_registry.go index 4ca61c246f5..c63991dc15f 100644 --- a/pkg/registry/pod_registry.go +++ b/pkg/registry/pod_registry.go @@ -91,7 +91,11 @@ func (storage *PodRegistryStorage) fillPodInfo(pod *api.Pod) { pod.CurrentState.Info = info netContainerInfo, ok := info["net"] if ok { - pod.CurrentState.PodIP = netContainerInfo.NetworkSettings.IPAddress + if netContainerInfo.NetworkSettings != nil { + pod.CurrentState.PodIP = netContainerInfo.NetworkSettings.IPAddress + } else { + glog.Warningf("No network settings: %#v", netContainerInfo) + } } else { glog.Warningf("Couldn't find network container in %v", info) } diff --git a/pkg/registry/pod_registry_test.go b/pkg/registry/pod_registry_test.go index 8f7383520eb..2e6d7b826da 100644 --- a/pkg/registry/pod_registry_test.go +++ b/pkg/registry/pod_registry_test.go @@ -317,3 +317,30 @@ func TestFillPodInfo(t *testing.T) { t.Errorf("Expected %s, saw %s", expectedIP, pod.CurrentState.PodIP) } } + +func TestFillPodInfoNoData(t *testing.T) { + expectedIP := "" + fakeGetter := FakePodInfoGetter{ + info: map[string]docker.Container{ + "net": { + ID: "foobar", + Path: "bin/run.sh", + }, + }, + } + storage := PodRegistryStorage{ + podCache: &fakeGetter, + } + + pod := api.Pod{} + + storage.fillPodInfo(&pod) + + if !reflect.DeepEqual(fakeGetter.info, pod.CurrentState.Info) { + t.Errorf("Unexpected mis-match: %#v vs %#v", fakeGetter.info, pod.CurrentState.Info) + } + + if pod.CurrentState.PodIP != expectedIP { + t.Errorf("Expected %s, saw %s", expectedIP, pod.CurrentState.PodIP) + } +} From b4811abbd63696800d4251574604f6a77a894f4e Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Thu, 10 Jul 2014 09:24:49 -0700 Subject: [PATCH 3/3] Address Clayton's comment. --- pkg/registry/pod_registry_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/registry/pod_registry_test.go b/pkg/registry/pod_registry_test.go index 2e6d7b826da..8d547d02f49 100644 --- a/pkg/registry/pod_registry_test.go +++ b/pkg/registry/pod_registry_test.go @@ -310,11 +310,11 @@ func TestFillPodInfo(t *testing.T) { storage.fillPodInfo(&pod) if !reflect.DeepEqual(fakeGetter.info, pod.CurrentState.Info) { - t.Errorf("Unexpected mis-match: %#v vs %#v", fakeGetter.info, pod.CurrentState.Info) + t.Errorf("Expected: %#v, Got %#v", fakeGetter.info, pod.CurrentState.Info) } if pod.CurrentState.PodIP != expectedIP { - t.Errorf("Expected %s, saw %s", expectedIP, pod.CurrentState.PodIP) + t.Errorf("Expected %s, Got %s", expectedIP, pod.CurrentState.PodIP) } } @@ -337,10 +337,10 @@ func TestFillPodInfoNoData(t *testing.T) { storage.fillPodInfo(&pod) if !reflect.DeepEqual(fakeGetter.info, pod.CurrentState.Info) { - t.Errorf("Unexpected mis-match: %#v vs %#v", fakeGetter.info, pod.CurrentState.Info) + t.Errorf("Expected %#v, Got %#v", fakeGetter.info, pod.CurrentState.Info) } if pod.CurrentState.PodIP != expectedIP { - t.Errorf("Expected %s, saw %s", expectedIP, pod.CurrentState.PodIP) + t.Errorf("Expected %s, Got %s", expectedIP, pod.CurrentState.PodIP) } }