mirror of https://github.com/k3s-io/k3s
Kubelet: implement GetPodContainerID for new runtime API
parent
b974c09819
commit
0cc4686d85
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue