2016-07-25 23:25:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dockershim
|
|
|
|
|
|
|
|
import (
|
2017-03-09 00:15:06 +00:00
|
|
|
"errors"
|
2016-08-26 23:15:06 +00:00
|
|
|
"fmt"
|
2017-03-09 00:15:06 +00:00
|
|
|
"net"
|
2016-07-25 23:25:36 +00:00
|
|
|
"testing"
|
2016-08-30 00:40:28 +00:00
|
|
|
"time"
|
2016-07-25 23:25:36 +00:00
|
|
|
|
2016-08-26 23:15:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-07-25 23:25:36 +00:00
|
|
|
|
2017-06-07 08:54:28 +00:00
|
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-10-29 00:14:33 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2017-05-03 17:46:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
|
2016-12-13 22:19:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
2016-10-01 00:08:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/types"
|
2016-07-25 23:25:36 +00:00
|
|
|
)
|
|
|
|
|
2016-08-26 23:15:06 +00:00
|
|
|
// A helper to create a basic config.
|
2016-11-30 07:27:27 +00:00
|
|
|
func makeSandboxConfig(name, namespace, uid string, attempt uint32) *runtimeapi.PodSandboxConfig {
|
2016-09-15 20:08:51 +00:00
|
|
|
return makeSandboxConfigWithLabelsAndAnnotations(name, namespace, uid, attempt, map[string]string{}, map[string]string{})
|
|
|
|
}
|
|
|
|
|
2016-11-30 07:27:27 +00:00
|
|
|
func makeSandboxConfigWithLabelsAndAnnotations(name, namespace, uid string, attempt uint32, labels, annotations map[string]string) *runtimeapi.PodSandboxConfig {
|
|
|
|
return &runtimeapi.PodSandboxConfig{
|
|
|
|
Metadata: &runtimeapi.PodSandboxMetadata{
|
2017-01-20 01:55:37 +00:00
|
|
|
Name: name,
|
|
|
|
Namespace: namespace,
|
|
|
|
Uid: uid,
|
|
|
|
Attempt: attempt,
|
2016-08-17 08:04:25 +00:00
|
|
|
},
|
2016-09-15 20:08:51 +00:00
|
|
|
Labels: labels,
|
|
|
|
Annotations: annotations,
|
2016-08-17 08:04:25 +00:00
|
|
|
}
|
2016-08-26 23:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestListSandboxes creates several sandboxes and then list them to check
|
|
|
|
// whether the correct metadatas, states, and labels are returned.
|
|
|
|
func TestListSandboxes(t *testing.T) {
|
2017-03-08 18:09:43 +00:00
|
|
|
ds, _, fakeClock := newTestDockerService()
|
2016-08-26 23:15:06 +00:00
|
|
|
name, namespace := "foo", "bar"
|
2016-11-30 07:27:27 +00:00
|
|
|
configs := []*runtimeapi.PodSandboxConfig{}
|
2016-08-26 23:15:06 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2016-09-15 20:08:51 +00:00
|
|
|
c := makeSandboxConfigWithLabelsAndAnnotations(fmt.Sprintf("%s%d", name, i),
|
|
|
|
fmt.Sprintf("%s%d", namespace, i), fmt.Sprintf("%d", i), 0,
|
|
|
|
map[string]string{"label": fmt.Sprintf("foo%d", i)},
|
|
|
|
map[string]string{"annotation": fmt.Sprintf("bar%d", i)},
|
|
|
|
)
|
2016-08-26 23:15:06 +00:00
|
|
|
configs = append(configs, c)
|
2016-07-25 23:25:36 +00:00
|
|
|
}
|
2016-08-26 23:15:06 +00:00
|
|
|
|
2016-11-30 07:27:27 +00:00
|
|
|
expected := []*runtimeapi.PodSandbox{}
|
|
|
|
state := runtimeapi.PodSandboxState_SANDBOX_READY
|
2017-03-08 18:09:43 +00:00
|
|
|
var createdAt int64 = fakeClock.Now().UnixNano()
|
2016-08-26 23:15:06 +00:00
|
|
|
for i := range configs {
|
|
|
|
id, err := ds.RunPodSandbox(configs[i])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// Prepend to the expected list because ListPodSandbox returns
|
|
|
|
// the most recent sandbox first.
|
2016-11-30 07:27:27 +00:00
|
|
|
expected = append([]*runtimeapi.PodSandbox{{
|
2016-09-15 20:08:51 +00:00
|
|
|
Metadata: configs[i].Metadata,
|
2017-01-20 01:55:37 +00:00
|
|
|
Id: id,
|
|
|
|
State: state,
|
|
|
|
CreatedAt: createdAt,
|
2016-09-15 20:08:51 +00:00
|
|
|
Labels: configs[i].Labels,
|
|
|
|
Annotations: configs[i].Annotations,
|
2016-08-26 23:15:06 +00:00
|
|
|
}}, expected...)
|
2016-07-25 23:25:36 +00:00
|
|
|
}
|
2016-08-26 23:15:06 +00:00
|
|
|
sandboxes, err := ds.ListPodSandbox(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, sandboxes, len(expected))
|
|
|
|
assert.Equal(t, expected, sandboxes)
|
2016-07-25 23:25:36 +00:00
|
|
|
}
|
2016-08-30 00:40:28 +00:00
|
|
|
|
|
|
|
// TestSandboxStatus tests the basic lifecycle operations and verify that
|
|
|
|
// the status returned reflects the operations performed.
|
|
|
|
func TestSandboxStatus(t *testing.T) {
|
2016-10-01 00:08:32 +00:00
|
|
|
ds, fDocker, fClock := newTestDockerService()
|
2016-09-15 20:08:51 +00:00
|
|
|
labels := map[string]string{"label": "foobar1"}
|
|
|
|
annotations := map[string]string{"annotation": "abc"}
|
|
|
|
config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations)
|
2016-08-30 00:40:28 +00:00
|
|
|
|
|
|
|
// TODO: The following variables depend on the internal
|
|
|
|
// implementation of FakeDockerClient, and should be fixed.
|
|
|
|
fakeIP := "2.3.4.5"
|
|
|
|
|
2016-11-30 07:27:27 +00:00
|
|
|
state := runtimeapi.PodSandboxState_SANDBOX_READY
|
2016-08-30 00:40:28 +00:00
|
|
|
ct := int64(0)
|
2016-10-29 00:14:33 +00:00
|
|
|
hostNetwork := false
|
2016-11-30 07:27:27 +00:00
|
|
|
expected := &runtimeapi.PodSandboxStatus{
|
2017-01-20 01:55:37 +00:00
|
|
|
State: state,
|
|
|
|
CreatedAt: ct,
|
2016-09-15 20:08:51 +00:00
|
|
|
Metadata: config.Metadata,
|
2017-01-20 01:55:37 +00:00
|
|
|
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP},
|
2017-05-01 13:40:17 +00:00
|
|
|
Linux: &runtimeapi.LinuxPodSandboxStatus{Namespaces: &runtimeapi.Namespace{Options: &runtimeapi.NamespaceOption{HostNetwork: hostNetwork}}},
|
2016-09-15 20:08:51 +00:00
|
|
|
Labels: labels,
|
|
|
|
Annotations: annotations,
|
2016-08-30 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the sandbox.
|
|
|
|
fClock.SetTime(time.Now())
|
2017-01-20 01:55:37 +00:00
|
|
|
expected.CreatedAt = fClock.Now().UnixNano()
|
2016-08-30 00:40:28 +00:00
|
|
|
id, err := ds.RunPodSandbox(config)
|
2016-10-01 00:08:32 +00:00
|
|
|
|
|
|
|
// Check internal labels
|
|
|
|
c, err := fDocker.InspectContainer(id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, c.Config.Labels[containerTypeLabelKey], containerTypeLabelSandbox)
|
|
|
|
assert.Equal(t, c.Config.Labels[types.KubernetesContainerNameLabel], sandboxContainerName)
|
|
|
|
|
2017-01-20 01:55:37 +00:00
|
|
|
expected.Id = id // ID is only known after the creation.
|
2016-08-30 00:40:28 +00:00
|
|
|
status, err := ds.PodSandboxStatus(id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, status)
|
|
|
|
|
|
|
|
// Stop the sandbox.
|
2017-01-20 01:55:37 +00:00
|
|
|
expected.State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
|
2016-08-30 00:40:28 +00:00
|
|
|
err = ds.StopPodSandbox(id)
|
|
|
|
assert.NoError(t, err)
|
2017-04-05 00:31:54 +00:00
|
|
|
// IP not valid after sandbox stop
|
|
|
|
expected.Network.Ip = ""
|
2016-08-30 00:40:28 +00:00
|
|
|
status, err = ds.PodSandboxStatus(id)
|
|
|
|
assert.Equal(t, expected, status)
|
|
|
|
|
|
|
|
// Remove the container.
|
|
|
|
err = ds.RemovePodSandbox(id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
status, err = ds.PodSandboxStatus(id)
|
|
|
|
assert.Error(t, err, fmt.Sprintf("status of sandbox: %+v", status))
|
|
|
|
}
|
2016-10-29 00:14:33 +00:00
|
|
|
|
2017-04-05 00:31:54 +00:00
|
|
|
// TestSandboxStatusAfterRestart tests that retrieving sandbox status returns
|
|
|
|
// an IP address even if RunPodSandbox() was not yet called for this pod, as
|
|
|
|
// would happen on kubelet restart
|
|
|
|
func TestSandboxStatusAfterRestart(t *testing.T) {
|
|
|
|
ds, _, fClock := newTestDockerService()
|
|
|
|
config := makeSandboxConfig("foo", "bar", "1", 0)
|
|
|
|
|
|
|
|
// TODO: The following variables depend on the internal
|
|
|
|
// implementation of FakeDockerClient, and should be fixed.
|
|
|
|
fakeIP := "2.3.4.5"
|
|
|
|
|
|
|
|
state := runtimeapi.PodSandboxState_SANDBOX_READY
|
|
|
|
ct := int64(0)
|
|
|
|
hostNetwork := false
|
|
|
|
expected := &runtimeapi.PodSandboxStatus{
|
|
|
|
State: state,
|
|
|
|
CreatedAt: ct,
|
|
|
|
Metadata: config.Metadata,
|
|
|
|
Network: &runtimeapi.PodSandboxNetworkStatus{Ip: fakeIP},
|
|
|
|
Linux: &runtimeapi.LinuxPodSandboxStatus{Namespaces: &runtimeapi.Namespace{Options: &runtimeapi.NamespaceOption{HostNetwork: hostNetwork}}},
|
|
|
|
Labels: map[string]string{},
|
|
|
|
Annotations: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the sandbox.
|
|
|
|
fClock.SetTime(time.Now())
|
|
|
|
expected.CreatedAt = fClock.Now().UnixNano()
|
|
|
|
|
|
|
|
createConfig, err := ds.makeSandboxDockerConfig(config, defaultSandboxImage)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
createResp, err := ds.client.CreateContainer(*createConfig)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ds.client.StartContainer(createResp.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check status without RunPodSandbox() having set up networking
|
|
|
|
expected.Id = createResp.ID // ID is only known after the creation.
|
|
|
|
status, err := ds.PodSandboxStatus(createResp.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, status)
|
|
|
|
}
|
|
|
|
|
2016-10-29 00:14:33 +00:00
|
|
|
// TestNetworkPluginInvocation checks that the right SetUpPod and TearDownPod
|
|
|
|
// calls are made when we run/stop a sandbox.
|
|
|
|
func TestNetworkPluginInvocation(t *testing.T) {
|
|
|
|
ds, _, _ := newTestDockerService()
|
|
|
|
mockPlugin := newTestNetworkPlugin(t)
|
2016-12-13 22:19:04 +00:00
|
|
|
ds.network = network.NewPluginManager(mockPlugin)
|
2016-10-29 00:14:33 +00:00
|
|
|
defer mockPlugin.Finish()
|
|
|
|
|
|
|
|
name := "foo0"
|
|
|
|
ns := "bar0"
|
|
|
|
c := makeSandboxConfigWithLabelsAndAnnotations(
|
|
|
|
name, ns, "0", 0,
|
|
|
|
map[string]string{"label": name},
|
|
|
|
map[string]string{"annotation": ns},
|
|
|
|
)
|
2017-05-03 17:46:35 +00:00
|
|
|
cID := kubecontainer.ContainerID{Type: runtimeName, ID: libdocker.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))}
|
2016-10-29 00:14:33 +00:00
|
|
|
|
2016-12-13 22:19:04 +00:00
|
|
|
mockPlugin.EXPECT().Name().Return("mockNetworkPlugin").AnyTimes()
|
2016-10-29 00:14:33 +00:00
|
|
|
setup := mockPlugin.EXPECT().SetUpPod(ns, name, cID)
|
|
|
|
// StopPodSandbox performs a lookup on status to figure out if the sandbox
|
|
|
|
// is running with hostnetworking, as all its given is the ID.
|
|
|
|
mockPlugin.EXPECT().GetPodNetworkStatus(ns, name, cID)
|
|
|
|
mockPlugin.EXPECT().TearDownPod(ns, name, cID).After(setup)
|
|
|
|
|
|
|
|
_, err := ds.RunPodSandbox(c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ds.StopPodSandbox(cID.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHostNetworkPluginInvocation checks that *no* SetUp/TearDown calls happen
|
|
|
|
// for host network sandboxes.
|
|
|
|
func TestHostNetworkPluginInvocation(t *testing.T) {
|
|
|
|
ds, _, _ := newTestDockerService()
|
|
|
|
mockPlugin := newTestNetworkPlugin(t)
|
2016-12-13 22:19:04 +00:00
|
|
|
ds.network = network.NewPluginManager(mockPlugin)
|
2016-10-29 00:14:33 +00:00
|
|
|
defer mockPlugin.Finish()
|
|
|
|
|
|
|
|
name := "foo0"
|
|
|
|
ns := "bar0"
|
|
|
|
c := makeSandboxConfigWithLabelsAndAnnotations(
|
|
|
|
name, ns, "0", 0,
|
|
|
|
map[string]string{"label": name},
|
|
|
|
map[string]string{"annotation": ns},
|
|
|
|
)
|
|
|
|
hostNetwork := true
|
2016-11-30 07:27:27 +00:00
|
|
|
c.Linux = &runtimeapi.LinuxPodSandboxConfig{
|
|
|
|
SecurityContext: &runtimeapi.LinuxSandboxSecurityContext{
|
|
|
|
NamespaceOptions: &runtimeapi.NamespaceOption{
|
2017-01-20 01:55:37 +00:00
|
|
|
HostNetwork: hostNetwork,
|
2016-11-04 11:54:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-05-03 17:46:35 +00:00
|
|
|
cID := kubecontainer.ContainerID{Type: runtimeName, ID: libdocker.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))}
|
2016-10-29 00:14:33 +00:00
|
|
|
|
|
|
|
// No calls to network plugin are expected
|
|
|
|
_, err := ds.RunPodSandbox(c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, ds.StopPodSandbox(cID.ID))
|
|
|
|
}
|
2017-03-09 00:15:06 +00:00
|
|
|
|
|
|
|
// TestSetUpPodFailure checks that the sandbox should be not ready when it
|
|
|
|
// hits a SetUpPod failure.
|
|
|
|
func TestSetUpPodFailure(t *testing.T) {
|
|
|
|
ds, _, _ := newTestDockerService()
|
|
|
|
mockPlugin := newTestNetworkPlugin(t)
|
|
|
|
ds.network = network.NewPluginManager(mockPlugin)
|
|
|
|
defer mockPlugin.Finish()
|
|
|
|
|
|
|
|
name := "foo0"
|
|
|
|
ns := "bar0"
|
|
|
|
c := makeSandboxConfigWithLabelsAndAnnotations(
|
|
|
|
name, ns, "0", 0,
|
|
|
|
map[string]string{"label": name},
|
|
|
|
map[string]string{"annotation": ns},
|
|
|
|
)
|
2017-05-03 17:46:35 +00:00
|
|
|
cID := kubecontainer.ContainerID{Type: runtimeName, ID: libdocker.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))}
|
2017-03-09 00:15:06 +00:00
|
|
|
mockPlugin.EXPECT().Name().Return("mockNetworkPlugin").AnyTimes()
|
|
|
|
mockPlugin.EXPECT().SetUpPod(ns, name, cID).Return(errors.New("setup pod error")).AnyTimes()
|
|
|
|
// Assume network plugin doesn't return error, dockershim should still be able to return not ready correctly.
|
|
|
|
mockPlugin.EXPECT().GetPodNetworkStatus(ns, name, cID).Return(&network.PodNetworkStatus{IP: net.IP("127.0.0.01")}, nil).AnyTimes()
|
|
|
|
|
|
|
|
t.Logf("RunPodSandbox should return error")
|
|
|
|
_, err := ds.RunPodSandbox(c)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
t.Logf("PodSandboxStatus should be not ready")
|
|
|
|
status, err := ds.PodSandboxStatus(cID.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, status.State)
|
|
|
|
|
|
|
|
t.Logf("ListPodSandbox should also show not ready")
|
|
|
|
sandboxes, err := ds.ListPodSandbox(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var sandbox *runtimeapi.PodSandbox
|
|
|
|
for _, s := range sandboxes {
|
|
|
|
if s.Id == cID.ID {
|
|
|
|
sandbox = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.NotNil(t, sandbox)
|
|
|
|
assert.Equal(t, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, sandbox.State)
|
|
|
|
}
|