Merge pull request #161 from Random-Liu/sandbox-createdat-from-containerd

Get CreatedAt from containerd instead of maintaining it ourselves.
This commit is contained in:
Lantao Liu 2017-08-24 12:01:50 -07:00 committed by GitHub
commit 6f679fd175
8 changed files with 101 additions and 27 deletions

View File

@ -18,6 +18,7 @@ package server
import ( import (
"fmt" "fmt"
"time"
tasks "github.com/containerd/containerd/api/services/tasks/v1" tasks "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/api/types/task" "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 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()) 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. // 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{ return &runtime.PodSandbox{
Id: meta.ID, Id: meta.ID,
Metadata: meta.Config.GetMetadata(), Metadata: meta.Config.GetMetadata(),
State: state, State: state,
CreatedAt: meta.CreatedAt, CreatedAt: createdAt.UnixNano(),
Labels: meta.Config.GetLabels(), Labels: meta.Config.GetLabels(),
Annotations: meta.Config.GetAnnotations(), Annotations: meta.Config.GetAnnotations(),
} }

View File

@ -37,12 +37,11 @@ func TestToCRISandbox(t *testing.T) {
Labels: map[string]string{"a": "b"}, Labels: map[string]string{"a": "b"},
Annotations: map[string]string{"c": "d"}, Annotations: map[string]string{"c": "d"},
} }
createdAt := time.Now().UnixNano() createdAt := time.Now()
meta := sandboxstore.Metadata{ meta := sandboxstore.Metadata{
ID: "test-id", ID: "test-id",
Name: "test-name", Name: "test-name",
Config: config, Config: config,
CreatedAt: createdAt,
NetNSPath: "test-netns", NetNSPath: "test-netns",
} }
state := runtime.PodSandboxState_SANDBOX_READY state := runtime.PodSandboxState_SANDBOX_READY
@ -50,11 +49,11 @@ func TestToCRISandbox(t *testing.T) {
Id: "test-id", Id: "test-id",
Metadata: config.GetMetadata(), Metadata: config.GetMetadata(),
State: state, State: state,
CreatedAt: createdAt, CreatedAt: createdAt.UnixNano(),
Labels: config.GetLabels(), Labels: config.GetLabels(),
Annotations: config.GetAnnotations(), Annotations: config.GetAnnotations(),
} }
s := toCRISandbox(meta, state) s := toCRISandbox(meta, state, createdAt)
assert.Equal(t, expect, s) assert.Equal(t, expect, s)
} }

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/cri-o/ocicni" "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. // 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: // 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; // 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; // 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.
metaBytes, err := sandbox.Metadata.Encode() metaBytes, err := sandbox.Metadata.Encode()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to convert sandbox metadata: %+v, %v", sandbox.Metadata, err) 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. // Add sandbox into sandbox store.
sandbox.CreatedAt = time.Now().UnixNano()
sandbox.Container = container sandbox.Container = container
if err := c.sandboxStore.Add(sandbox); err != nil { if err := c.sandboxStore.Add(sandbox); err != nil {
return nil, fmt.Errorf("failed to add sandbox %+v into store: %v", sandbox, err) return nil, fmt.Errorf("failed to add sandbox %+v into store: %v", sandbox, err)

View File

@ -18,12 +18,12 @@ package server
import ( import (
"fmt" "fmt"
"time"
"github.com/golang/glog"
"golang.org/x/net/context"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/golang/glog"
"golang.org/x/net/context"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
sandboxstore "github.com/kubernetes-incubator/cri-containerd/pkg/store/sandbox" 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) 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. // 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() nsOpts := meta.Config.GetLinux().GetSecurityContext().GetNamespaceOptions()
return &runtime.PodSandboxStatus{ return &runtime.PodSandboxStatus{
Id: meta.ID, Id: meta.ID,
Metadata: meta.Config.GetMetadata(), Metadata: meta.Config.GetMetadata(),
State: state, State: state,
CreatedAt: meta.CreatedAt, CreatedAt: createdAt.UnixNano(),
Network: &runtime.PodSandboxNetworkStatus{Ip: ip}, Network: &runtime.PodSandboxNetworkStatus{Ip: ip},
Linux: &runtime.LinuxPodSandboxStatus{ Linux: &runtime.LinuxPodSandboxStatus{
Namespaces: &runtime.Namespace{ Namespaces: &runtime.Namespace{

View 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)
}

View File

@ -46,9 +46,6 @@ type Metadata struct {
Name string Name string
// Config is the CRI sandbox config. // Config is the CRI sandbox config.
Config *runtime.PodSandboxConfig 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 is the process id of the sandbox.
Pid uint32 Pid uint32
// NetNSPath is the network namespace used by the sandbox. // NetNSPath is the network namespace used by the sandbox.

View File

@ -19,7 +19,6 @@ package sandbox
import ( import (
"encoding/json" "encoding/json"
"testing" "testing"
"time"
assertlib "github.com/stretchr/testify/assert" assertlib "github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
@ -37,7 +36,6 @@ func TestMetadataEncodeDecode(t *testing.T) {
Attempt: 1, Attempt: 1,
}, },
}, },
CreatedAt: time.Now().UnixNano(),
} }
assert := assertlib.New(t) assert := assertlib.New(t)
data, err := meta.Encode() data, err := meta.Encode()

View File

@ -18,7 +18,6 @@ package sandbox
import ( import (
"testing" "testing"
"time"
assertlib "github.com/stretchr/testify/assert" assertlib "github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
@ -40,7 +39,6 @@ func TestSandboxStore(t *testing.T) {
Attempt: 1, Attempt: 1,
}, },
}, },
CreatedAt: time.Now().UnixNano(),
Pid: 1001, Pid: 1001,
NetNSPath: "TestNetNS-1", NetNSPath: "TestNetNS-1",
}, },
@ -55,7 +53,6 @@ func TestSandboxStore(t *testing.T) {
Attempt: 2, Attempt: 2,
}, },
}, },
CreatedAt: time.Now().UnixNano(),
Pid: 1002, Pid: 1002,
NetNSPath: "TestNetNS-2", NetNSPath: "TestNetNS-2",
}, },
@ -70,7 +67,6 @@ func TestSandboxStore(t *testing.T) {
Attempt: 3, Attempt: 3,
}, },
}, },
CreatedAt: time.Now().UnixNano(),
Pid: 1003, Pid: 1003,
NetNSPath: "TestNetNS-3", NetNSPath: "TestNetNS-3",
}, },