From 0cc4686d85110d967af9984b1d03d7e11581d2db Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Fri, 26 Aug 2016 07:52:11 +0800 Subject: [PATCH] Kubelet: implement GetPodContainerID for new runtime API --- .../kuberuntime/kuberuntime_manager.go | 9 +++- .../kuberuntime/kuberuntime_manager_test.go | 48 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_manager.go b/pkg/kubelet/kuberuntime/kuberuntime_manager.go index 636e0f6c7dc..7dc2f01035b 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_manager.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_manager.go @@ -404,7 +404,14 @@ func (m *kubeGenericRuntimeManager) GetNetNS(sandboxID kubecontainer.ContainerID // GetPodContainerID gets pod sandbox ID func (m *kubeGenericRuntimeManager) GetPodContainerID(pod *kubecontainer.Pod) (kubecontainer.ContainerID, error) { - return kubecontainer.ContainerID{}, fmt.Errorf("not implemented") + podFullName := kubecontainer.BuildPodFullName(pod.Name, pod.Namespace) + if len(pod.Sandboxes) == 0 { + glog.Errorf("No sandboxes are found for pod %q", podFullName) + return kubecontainer.ContainerID{}, fmt.Errorf("sandboxes for pod %q not found", podFullName) + } + + // return sandboxID of the first sandbox since it is the latest one + return pod.Sandboxes[0].ID, nil } // Forward the specified port from the specified pod to the stream. diff --git a/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go b/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go index 33003652550..33868da9113 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_manager_test.go @@ -289,6 +289,54 @@ func TestGetPods(t *testing.T) { } } +func TestGetPodContainerID(t *testing.T) { + fakeRuntime, _, m, err := createTestRuntimeManager() + assert.NoError(t, err) + + pod := &api.Pod{ + ObjectMeta: api.ObjectMeta{ + UID: "12345678", + Name: "foo", + Namespace: "new", + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + { + Name: "foo1", + Image: "busybox", + }, + { + Name: "foo2", + Image: "busybox", + }, + }, + }, + } + // Set fake sandbox and fake containers to fakeRuntime. + fakeSandbox, _, err := makeAndSetFakePod(m, fakeRuntime, pod) + assert.NoError(t, err) + + // Convert fakeSandbox to kubecontainer.Container + sandbox, err := m.sandboxToKubeContainer(&runtimeApi.PodSandbox{ + Id: fakeSandbox.Id, + Metadata: fakeSandbox.Metadata, + State: fakeSandbox.State, + CreatedAt: fakeSandbox.CreatedAt, + Labels: fakeSandbox.Labels, + }) + assert.NoError(t, err) + + expectedPod := &kubecontainer.Pod{ + ID: pod.UID, + Name: pod.Name, + Namespace: pod.Namespace, + Containers: []*kubecontainer.Container{}, + Sandboxes: []*kubecontainer.Container{sandbox}, + } + actual, err := m.GetPodContainerID(expectedPod) + assert.Equal(t, fakeSandbox.GetId(), actual.ID) +} + func TestGetNetNS(t *testing.T) { fakeRuntime, _, m, err := createTestRuntimeManager() assert.NoError(t, err)