Merge pull request #31091 from feiskyer/kuberuntime-getnetns

Automatic merge from submit-queue

Kubelet: implement GetNetNS for new runtime api

Kubelet: implement GetNetNS for new runtime api. 

CC @yujuhong @thockin @kubernetes/sig-node @kubernetes/sig-rktnetes
This commit is contained in:
Kubernetes Submit Queue 2016-08-24 13:40:40 -07:00 committed by GitHub
commit f488d118ad
2 changed files with 65 additions and 5 deletions

View File

@ -28,6 +28,7 @@ import (
"k8s.io/kubernetes/pkg/client/record"
"k8s.io/kubernetes/pkg/credentialprovider"
internalApi "k8s.io/kubernetes/pkg/kubelet/api"
runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/images"
"k8s.io/kubernetes/pkg/kubelet/lifecycle"
@ -296,11 +297,37 @@ func (m *kubeGenericRuntimeManager) GetPodStatus(uid kubetypes.UID, name, namesp
// Returns the filesystem path of the pod's network namespace; if the
// runtime does not handle namespace creation itself, or cannot return
// the network namespace path, it should return an error.
// TODO: Change ContainerID to a Pod ID since the namespace is shared
// by all containers in the pod.
func (m *kubeGenericRuntimeManager) GetNetNS(containerID kubecontainer.ContainerID) (string, error) {
return "", fmt.Errorf("not implemented")
// the network namespace path, it returns an 'not supported' error.
// TODO: Rename param name to sandboxID in kubecontainer.Runtime.GetNetNS().
// TODO: Remove GetNetNS after networking is delegated to the container runtime.
func (m *kubeGenericRuntimeManager) GetNetNS(sandboxID kubecontainer.ContainerID) (string, error) {
readyState := runtimeApi.PodSandBoxState_READY
filter := &runtimeApi.PodSandboxFilter{
State: &readyState,
Id: &sandboxID.ID,
LabelSelector: map[string]string{kubernetesManagedLabel: "true"},
}
sandboxes, err := m.runtimeService.ListPodSandbox(filter)
if err != nil {
glog.Errorf("ListPodSandbox with filter %q failed: %v", filter, err)
return "", err
}
if len(sandboxes) == 0 {
glog.Errorf("No sandbox is found with filter %q", filter)
return "", fmt.Errorf("Sandbox %q is not found", sandboxID)
}
sandboxStatus, err := m.runtimeService.PodSandboxStatus(sandboxes[0].GetId())
if err != nil {
glog.Errorf("PodSandboxStatus with id %q failed: %v", sandboxes[0].GetId(), err)
return "", err
}
if sandboxStatus.Linux != nil && sandboxStatus.Linux.Namespaces != nil {
return sandboxStatus.Linux.Namespaces.GetNetwork(), nil
}
return "", fmt.Errorf("not supported")
}
// GetPodContainerID gets pod sandbox ID

View File

@ -236,3 +236,36 @@ func TestGetPods(t *testing.T) {
t.Errorf("expected %#v, got %#v", expected, actual)
}
}
func TestGetNetNS(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.
sandbox, _, err := makeAndSetFakePod(m, fakeRuntime, pod)
assert.NoError(t, err)
actual, err := m.GetNetNS(kubecontainer.ContainerID{ID: sandbox.GetId()})
assert.Equal(t, "", actual)
assert.Equal(t, "not supported", err.Error())
}