Get CreatedAt from containerd instead of maintaining it ourselves.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -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(),
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
84
pkg/server/sandbox_status_test.go
Normal file
84
pkg/server/sandbox_status_test.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user