diff --git a/pkg/server/sandbox_list.go b/pkg/server/sandbox_list.go index b1b8c5330..ba2e04170 100644 --- a/pkg/server/sandbox_list.go +++ b/pkg/server/sandbox_list.go @@ -18,6 +18,7 @@ package server import ( "fmt" + "time" tasks "github.com/containerd/containerd/api/services/tasks/v1" "github.com/containerd/containerd/api/types/task" @@ -54,7 +55,8 @@ func (c *criContainerdService) ListPodSandbox(ctx context.Context, r *runtime.Li state = runtime.PodSandboxState_SANDBOX_READY } - sandboxes = append(sandboxes, toCRISandbox(sandboxInStore.Metadata, state)) + createdAt := sandboxInStore.Container.Info().CreatedAt + sandboxes = append(sandboxes, toCRISandbox(sandboxInStore.Metadata, state, createdAt)) } sandboxes = c.filterCRISandboxes(sandboxes, r.GetFilter()) @@ -62,12 +64,12 @@ func (c *criContainerdService) ListPodSandbox(ctx context.Context, r *runtime.Li } // toCRISandbox converts sandbox metadata into CRI pod sandbox. -func toCRISandbox(meta sandboxstore.Metadata, state runtime.PodSandboxState) *runtime.PodSandbox { +func toCRISandbox(meta sandboxstore.Metadata, state runtime.PodSandboxState, createdAt time.Time) *runtime.PodSandbox { return &runtime.PodSandbox{ Id: meta.ID, Metadata: meta.Config.GetMetadata(), State: state, - CreatedAt: meta.CreatedAt, + CreatedAt: createdAt.UnixNano(), Labels: meta.Config.GetLabels(), Annotations: meta.Config.GetAnnotations(), } diff --git a/pkg/server/sandbox_list_test.go b/pkg/server/sandbox_list_test.go index ad0fc979c..ca0b4b851 100644 --- a/pkg/server/sandbox_list_test.go +++ b/pkg/server/sandbox_list_test.go @@ -37,12 +37,11 @@ func TestToCRISandbox(t *testing.T) { Labels: map[string]string{"a": "b"}, Annotations: map[string]string{"c": "d"}, } - createdAt := time.Now().UnixNano() + createdAt := time.Now() meta := sandboxstore.Metadata{ ID: "test-id", Name: "test-name", Config: config, - CreatedAt: createdAt, NetNSPath: "test-netns", } state := runtime.PodSandboxState_SANDBOX_READY @@ -50,11 +49,11 @@ func TestToCRISandbox(t *testing.T) { Id: "test-id", Metadata: config.GetMetadata(), State: state, - CreatedAt: createdAt, + CreatedAt: createdAt.UnixNano(), Labels: config.GetLabels(), Annotations: config.GetAnnotations(), } - s := toCRISandbox(meta, state) + s := toCRISandbox(meta, state, createdAt) assert.Equal(t, expect, s) } diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index 3f0ed4a49..f01be8458 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -20,7 +20,6 @@ import ( "fmt" "os" "strings" - "time" "github.com/containerd/containerd" "github.com/cri-o/ocicni" @@ -123,9 +122,7 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run // Actually the Pid, NetNS and CreatedAt underlying are not included here. // I'm fine with this for now. After this PR is merged, we could: // 1.Get Pid from containerd, so that we don't need to checkpoint it ourselves; - // 2.Use permanent NetNS after #138 is merged, so that we'll have network namespace here; - // 3.Get CreatedAt from containerd, which have been checkpointed by containerd - // https://github.com/containerd/containerd/blob/master/containers/containers.go#L14. + // 2.Use permanent NetNS after #138 is merged, so that we'll have network namespace here. metaBytes, err := sandbox.Metadata.Encode() if err != nil { return nil, fmt.Errorf("failed to convert sandbox metadata: %+v, %v", sandbox.Metadata, err) @@ -204,7 +201,6 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run } // Add sandbox into sandbox store. - sandbox.CreatedAt = time.Now().UnixNano() sandbox.Container = container if err := c.sandboxStore.Add(sandbox); err != nil { return nil, fmt.Errorf("failed to add sandbox %+v into store: %v", sandbox, err) diff --git a/pkg/server/sandbox_status.go b/pkg/server/sandbox_status.go index 54bc8bec7..c2cd1c825 100644 --- a/pkg/server/sandbox_status.go +++ b/pkg/server/sandbox_status.go @@ -18,12 +18,12 @@ package server import ( "fmt" - - "github.com/golang/glog" - "golang.org/x/net/context" + "time" "github.com/containerd/containerd" "github.com/containerd/containerd/errdefs" + "github.com/golang/glog" + "golang.org/x/net/context" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" sandboxstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/sandbox" @@ -64,17 +64,19 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime. glog.V(4).Infof("GetPodNetworkStatus returns error: %v", err) } - return &runtime.PodSandboxStatusResponse{Status: toCRISandboxStatus(sandbox.Metadata, state, ip)}, nil + createdAt := sandbox.Container.Info().CreatedAt + status := toCRISandboxStatus(sandbox.Metadata, state, createdAt, ip) + return &runtime.PodSandboxStatusResponse{Status: status}, nil } // toCRISandboxStatus converts sandbox metadata into CRI pod sandbox status. -func toCRISandboxStatus(meta sandboxstore.Metadata, state runtime.PodSandboxState, ip string) *runtime.PodSandboxStatus { +func toCRISandboxStatus(meta sandboxstore.Metadata, state runtime.PodSandboxState, createdAt time.Time, ip string) *runtime.PodSandboxStatus { nsOpts := meta.Config.GetLinux().GetSecurityContext().GetNamespaceOptions() return &runtime.PodSandboxStatus{ Id: meta.ID, Metadata: meta.Config.GetMetadata(), State: state, - CreatedAt: meta.CreatedAt, + CreatedAt: createdAt.UnixNano(), Network: &runtime.PodSandboxNetworkStatus{Ip: ip}, Linux: &runtime.LinuxPodSandboxStatus{ Namespaces: &runtime.Namespace{ diff --git a/pkg/server/sandbox_status_test.go b/pkg/server/sandbox_status_test.go new file mode 100644 index 000000000..004d6230d --- /dev/null +++ b/pkg/server/sandbox_status_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2017 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 server + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" + + sandboxstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/sandbox" +) + +func TestPodSandboxStatus(t *testing.T) { + const ( + id = "test-id" + ip = "10.10.10.10" + ) + createdAt := time.Now() + config := &runtime.PodSandboxConfig{ + Metadata: &runtime.PodSandboxMetadata{ + Name: "test-name", + Uid: "test-uid", + Namespace: "test-ns", + Attempt: 1, + }, + Linux: &runtime.LinuxPodSandboxConfig{ + SecurityContext: &runtime.LinuxSandboxSecurityContext{ + NamespaceOptions: &runtime.NamespaceOption{ + HostNetwork: true, + HostPid: false, + HostIpc: true, + }, + }, + }, + Labels: map[string]string{"a": "b"}, + Annotations: map[string]string{"c": "d"}, + } + sandbox := &sandboxstore.Sandbox{ + Metadata: sandboxstore.Metadata{ + ID: id, + Name: "test-name", + Config: config, + }, + } + state := runtime.PodSandboxState_SANDBOX_NOTREADY + + expected := &runtime.PodSandboxStatus{ + Id: id, + Metadata: config.GetMetadata(), + State: state, + CreatedAt: createdAt.UnixNano(), + Network: &runtime.PodSandboxNetworkStatus{Ip: ip}, + Linux: &runtime.LinuxPodSandboxStatus{ + Namespaces: &runtime.Namespace{ + Options: &runtime.NamespaceOption{ + HostNetwork: true, + HostPid: false, + HostIpc: true, + }, + }, + }, + Labels: config.GetLabels(), + Annotations: config.GetAnnotations(), + } + + got := toCRISandboxStatus(sandbox.Metadata, state, createdAt, ip) + assert.Equal(t, expected, got) +} diff --git a/pkg/store/sandbox/metadata.go b/pkg/store/sandbox/metadata.go index c0ac96dc3..3051f3d4e 100644 --- a/pkg/store/sandbox/metadata.go +++ b/pkg/store/sandbox/metadata.go @@ -46,9 +46,6 @@ type Metadata struct { Name string // Config is the CRI sandbox config. Config *runtime.PodSandboxConfig - // CreatedAt is the created timestamp. - // TODO(random-liu): Use containerd container CreatedAt (containerd#933) - CreatedAt int64 // Pid is the process id of the sandbox. Pid uint32 // NetNSPath is the network namespace used by the sandbox. diff --git a/pkg/store/sandbox/metadata_test.go b/pkg/store/sandbox/metadata_test.go index 01aa3477c..1f471ab1e 100644 --- a/pkg/store/sandbox/metadata_test.go +++ b/pkg/store/sandbox/metadata_test.go @@ -19,7 +19,6 @@ package sandbox import ( "encoding/json" "testing" - "time" assertlib "github.com/stretchr/testify/assert" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" @@ -37,7 +36,6 @@ func TestMetadataEncodeDecode(t *testing.T) { Attempt: 1, }, }, - CreatedAt: time.Now().UnixNano(), } assert := assertlib.New(t) data, err := meta.Encode() diff --git a/pkg/store/sandbox/sandbox_test.go b/pkg/store/sandbox/sandbox_test.go index ee9fd9d8b..089273f02 100644 --- a/pkg/store/sandbox/sandbox_test.go +++ b/pkg/store/sandbox/sandbox_test.go @@ -18,7 +18,6 @@ package sandbox import ( "testing" - "time" assertlib "github.com/stretchr/testify/assert" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" @@ -40,7 +39,6 @@ func TestSandboxStore(t *testing.T) { Attempt: 1, }, }, - CreatedAt: time.Now().UnixNano(), Pid: 1001, NetNSPath: "TestNetNS-1", }, @@ -55,7 +53,6 @@ func TestSandboxStore(t *testing.T) { Attempt: 2, }, }, - CreatedAt: time.Now().UnixNano(), Pid: 1002, NetNSPath: "TestNetNS-2", }, @@ -70,7 +67,6 @@ func TestSandboxStore(t *testing.T) { Attempt: 3, }, }, - CreatedAt: time.Now().UnixNano(), Pid: 1003, NetNSPath: "TestNetNS-3", },