From 38d8da12157d8036504fc2b2f15d3058b9b83b3e Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Wed, 8 Mar 2017 10:09:43 -0800 Subject: [PATCH] FakeDockerClient: add creation timestamp This is necessary for kubemark to work correctly. --- pkg/kubelet/dockershim/docker_container_test.go | 4 ++-- pkg/kubelet/dockershim/docker_sandbox_test.go | 4 ++-- pkg/kubelet/dockertools/convert.go | 1 + pkg/kubelet/dockertools/fake_docker_client.go | 10 ++++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/kubelet/dockershim/docker_container_test.go b/pkg/kubelet/dockershim/docker_container_test.go index 3427f76ca6c..ee737b7d692 100644 --- a/pkg/kubelet/dockershim/docker_container_test.go +++ b/pkg/kubelet/dockershim/docker_container_test.go @@ -47,7 +47,7 @@ func makeContainerConfig(sConfig *runtimeapi.PodSandboxConfig, name, image strin // TestListContainers creates several containers and then list them to check // whether the correct metadatas, states, and labels are returned. func TestListContainers(t *testing.T) { - ds, _, _ := newTestDockerService() + ds, _, fakeClock := newTestDockerService() podName, namespace := "foo", "bar" containerName, image := "sidecar", "logger" @@ -66,7 +66,7 @@ func TestListContainers(t *testing.T) { expected := []*runtimeapi.Container{} state := runtimeapi.ContainerState_CONTAINER_RUNNING - var createdAt int64 = 0 + var createdAt int64 = fakeClock.Now().UnixNano() for i := range configs { // We don't care about the sandbox id; pass a bogus one. sandboxID := fmt.Sprintf("sandboxid%d", i) diff --git a/pkg/kubelet/dockershim/docker_sandbox_test.go b/pkg/kubelet/dockershim/docker_sandbox_test.go index bd1cda1ce72..87123e49490 100644 --- a/pkg/kubelet/dockershim/docker_sandbox_test.go +++ b/pkg/kubelet/dockershim/docker_sandbox_test.go @@ -52,7 +52,7 @@ func makeSandboxConfigWithLabelsAndAnnotations(name, namespace, uid string, atte // TestListSandboxes creates several sandboxes and then list them to check // whether the correct metadatas, states, and labels are returned. func TestListSandboxes(t *testing.T) { - ds, _, _ := newTestDockerService() + ds, _, fakeClock := newTestDockerService() name, namespace := "foo", "bar" configs := []*runtimeapi.PodSandboxConfig{} for i := 0; i < 3; i++ { @@ -66,7 +66,7 @@ func TestListSandboxes(t *testing.T) { expected := []*runtimeapi.PodSandbox{} state := runtimeapi.PodSandboxState_SANDBOX_READY - var createdAt int64 = 0 + var createdAt int64 = fakeClock.Now().UnixNano() for i := range configs { id, err := ds.RunPodSandbox(configs[i]) assert.NoError(t, err) diff --git a/pkg/kubelet/dockertools/convert.go b/pkg/kubelet/dockertools/convert.go index 2bbf3758dce..6a3bf882dc3 100644 --- a/pkg/kubelet/dockertools/convert.go +++ b/pkg/kubelet/dockertools/convert.go @@ -28,6 +28,7 @@ import ( // (kubecontainer) types. const ( statusRunningPrefix = "Up" + statusCreatedPrefix = "Created" statusExitedPrefix = "Exited" ) diff --git a/pkg/kubelet/dockertools/fake_docker_client.go b/pkg/kubelet/dockertools/fake_docker_client.go index b3bf57b28b8..5830cb4fa19 100644 --- a/pkg/kubelet/dockertools/fake_docker_client.go +++ b/pkg/kubelet/dockertools/fake_docker_client.go @@ -516,12 +516,13 @@ func (f *FakeDockerClient) CreateContainer(c dockertypes.ContainerCreateConfig) name := "/" + c.Name id := GetFakeContainerID(name) f.appendContainerTrace("Created", id) + timestamp := f.Clock.Now() // The newest container should be in front, because we assume so in GetPodStatus() f.RunningContainerList = append([]dockertypes.Container{ - {ID: id, Names: []string{name}, Image: c.Config.Image, Labels: c.Config.Labels}, + {ID: id, Names: []string{name}, Image: c.Config.Image, Created: timestamp.Unix(), State: statusCreatedPrefix, Labels: c.Config.Labels}, }, f.RunningContainerList...) f.ContainerMap[id] = convertFakeContainer(&FakeContainer{ - ID: id, Name: name, Config: c.Config, HostConfig: c.HostConfig, CreatedAt: f.Clock.Now()}) + ID: id, Name: name, Config: c.Config, HostConfig: c.HostConfig, CreatedAt: timestamp}) f.normalSleep(100, 25, 25) @@ -539,12 +540,13 @@ func (f *FakeDockerClient) StartContainer(id string) error { } f.appendContainerTrace("Started", id) container, ok := f.ContainerMap[id] + timestamp := f.Clock.Now() if !ok { - container = convertFakeContainer(&FakeContainer{ID: id, Name: id}) + container = convertFakeContainer(&FakeContainer{ID: id, Name: id, CreatedAt: timestamp}) } container.State.Running = true container.State.Pid = os.Getpid() - container.State.StartedAt = dockerTimestampToString(f.Clock.Now()) + container.State.StartedAt = dockerTimestampToString(timestamp) container.NetworkSettings.IPAddress = "2.3.4.5" f.ContainerMap[id] = container f.updateContainerStatus(id, statusRunningPrefix)