Remove sandbox truncindex.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
parent
a393f3a084
commit
4317e6119a
@ -47,7 +47,7 @@ func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.C
|
|||||||
|
|
||||||
config := r.GetConfig()
|
config := r.GetConfig()
|
||||||
sandboxConfig := r.GetSandboxConfig()
|
sandboxConfig := r.GetSandboxConfig()
|
||||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to find sandbox id %q: %v", r.GetPodSandboxId(), err)
|
return nil, fmt.Errorf("failed to find sandbox id %q: %v", r.GetPodSandboxId(), err)
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ func (c *criContainerdService) startContainer(ctx context.Context, id string, me
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Get sandbox config from sandbox store.
|
// Get sandbox config from sandbox store.
|
||||||
sandboxMeta, err := c.getSandbox(meta.SandboxID)
|
sandboxMeta, err := c.sandboxStore.Get(meta.SandboxID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("sandbox %q not found: %v", meta.SandboxID, err)
|
return fmt.Errorf("sandbox %q not found: %v", meta.SandboxID, err)
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ import (
|
|||||||
containerdmetadata "github.com/containerd/containerd/metadata"
|
containerdmetadata "github.com/containerd/containerd/metadata"
|
||||||
"github.com/docker/distribution/reference"
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/docker/docker/pkg/stringid"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/docker/docker/pkg/truncindex"
|
|
||||||
imagedigest "github.com/opencontainers/go-digest"
|
imagedigest "github.com/opencontainers/go-digest"
|
||||||
"github.com/opencontainers/image-spec/identity"
|
"github.com/opencontainers/image-spec/identity"
|
||||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
@ -236,29 +235,6 @@ func isRuncProcessAlreadyFinishedError(grpcError error) bool {
|
|||||||
return strings.Contains(grpc.ErrorDesc(grpcError), "os: process already finished")
|
return strings.Contains(grpc.ErrorDesc(grpcError), "os: process already finished")
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSandbox gets the sandbox metadata from the sandbox store. It returns nil without
|
|
||||||
// error if the sandbox metadata is not found. It also tries to get full sandbox id and
|
|
||||||
// retry if the sandbox metadata is not found with the initial id.
|
|
||||||
func (c *criContainerdService) getSandbox(id string) (*metadata.SandboxMetadata, error) {
|
|
||||||
sandbox, err := c.sandboxStore.Get(id)
|
|
||||||
if err != nil && !metadata.IsNotExistError(err) {
|
|
||||||
return nil, fmt.Errorf("sandbox metadata not found: %v", err)
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
return sandbox, nil
|
|
||||||
}
|
|
||||||
// sandbox is not found in metadata store, try to extract full id.
|
|
||||||
id, indexErr := c.sandboxIDIndex.Get(id)
|
|
||||||
if indexErr != nil {
|
|
||||||
if indexErr == truncindex.ErrNotExist {
|
|
||||||
// Return the original error if sandbox id is not found.
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("sandbox id not found: %v", err)
|
|
||||||
}
|
|
||||||
return c.sandboxStore.Get(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// criContainerStateToString formats CRI container state to string.
|
// criContainerStateToString formats CRI container state to string.
|
||||||
func criContainerStateToString(state runtime.ContainerState) string {
|
func criContainerStateToString(state runtime.ContainerState) string {
|
||||||
return runtime.ContainerState_name[int32(state)]
|
return runtime.ContainerState_name[int32(state)]
|
||||||
|
@ -28,7 +28,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata"
|
|
||||||
ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing"
|
ostesting "github.com/kubernetes-incubator/cri-containerd/pkg/os/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -119,49 +118,6 @@ func TestPrepareStreamingPipesError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSandbox(t *testing.T) {
|
|
||||||
c := newTestCRIContainerdService()
|
|
||||||
testID := "abcdefg"
|
|
||||||
testSandbox := metadata.SandboxMetadata{
|
|
||||||
ID: testID,
|
|
||||||
Name: "test-name",
|
|
||||||
}
|
|
||||||
assert.NoError(t, c.sandboxStore.Create(testSandbox))
|
|
||||||
assert.NoError(t, c.sandboxIDIndex.Add(testID))
|
|
||||||
|
|
||||||
for desc, test := range map[string]struct {
|
|
||||||
id string
|
|
||||||
expected *metadata.SandboxMetadata
|
|
||||||
expectErr bool
|
|
||||||
}{
|
|
||||||
"full id": {
|
|
||||||
id: testID,
|
|
||||||
expected: &testSandbox,
|
|
||||||
expectErr: false,
|
|
||||||
},
|
|
||||||
"partial id": {
|
|
||||||
id: testID[:3],
|
|
||||||
expected: &testSandbox,
|
|
||||||
expectErr: false,
|
|
||||||
},
|
|
||||||
"non-exist id": {
|
|
||||||
id: "gfedcba",
|
|
||||||
expected: nil,
|
|
||||||
expectErr: true,
|
|
||||||
},
|
|
||||||
} {
|
|
||||||
t.Logf("TestCase %q", desc)
|
|
||||||
sb, err := c.getSandbox(test.id)
|
|
||||||
if test.expectErr {
|
|
||||||
assert.Error(t, err)
|
|
||||||
assert.True(t, metadata.IsNotExistError(err))
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, test.expected, sb)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNormalizeImageRef(t *testing.T) {
|
func TestNormalizeImageRef(t *testing.T) {
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
input string
|
input string
|
||||||
|
@ -93,20 +93,10 @@ func (c *criContainerdService) filterCRISandboxes(sandboxes []*runtime.PodSandbo
|
|||||||
return sandboxes
|
return sandboxes
|
||||||
}
|
}
|
||||||
|
|
||||||
var filterID string
|
|
||||||
if filter.GetId() != "" {
|
|
||||||
// Handle truncate id. Use original filter if failed to convert.
|
|
||||||
var err error
|
|
||||||
filterID, err = c.sandboxIDIndex.Get(filter.GetId())
|
|
||||||
if err != nil {
|
|
||||||
filterID = filter.GetId()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
filtered := []*runtime.PodSandbox{}
|
filtered := []*runtime.PodSandbox{}
|
||||||
for _, s := range sandboxes {
|
for _, s := range sandboxes {
|
||||||
// Filter by id
|
// Filter by id
|
||||||
if filterID != "" && filterID != s.Id {
|
if filter.GetId() != "" && filter.GetId() != s.Id {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Filter by state
|
// Filter by state
|
||||||
|
@ -39,7 +39,7 @@ func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime.
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !metadata.IsNotExistError(err) {
|
if !metadata.IsNotExistError(err) {
|
||||||
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
||||||
@ -117,9 +117,6 @@ func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime.
|
|||||||
return nil, fmt.Errorf("failed to delete sandbox metadata for %q: %v", id, err)
|
return nil, fmt.Errorf("failed to delete sandbox metadata for %q: %v", id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release the sandbox id from id index.
|
|
||||||
c.sandboxIDIndex.Delete(id) // nolint: errcheck
|
|
||||||
|
|
||||||
// Release the sandbox name reserved for the sandbox.
|
// Release the sandbox name reserved for the sandbox.
|
||||||
c.sandboxNameIndex.ReleaseByKey(id)
|
c.sandboxNameIndex.ReleaseByKey(id)
|
||||||
|
|
||||||
|
@ -116,7 +116,6 @@ func TestRemovePodSandbox(t *testing.T) {
|
|||||||
fakeExecutionClient.SetFakeTasks(test.sandboxTasks)
|
fakeExecutionClient.SetFakeTasks(test.sandboxTasks)
|
||||||
if test.injectMetadata {
|
if test.injectMetadata {
|
||||||
c.sandboxNameIndex.Reserve(testName, testID)
|
c.sandboxNameIndex.Reserve(testName, testID)
|
||||||
c.sandboxIDIndex.Add(testID)
|
|
||||||
c.sandboxStore.Create(testMetadata)
|
c.sandboxStore.Create(testMetadata)
|
||||||
}
|
}
|
||||||
if test.removeSnapshotErr == nil {
|
if test.removeSnapshotErr == nil {
|
||||||
@ -158,8 +157,6 @@ func TestRemovePodSandbox(t *testing.T) {
|
|||||||
assert.NotNil(t, res)
|
assert.NotNil(t, res)
|
||||||
assert.NoError(t, c.sandboxNameIndex.Reserve(testName, testID),
|
assert.NoError(t, c.sandboxNameIndex.Reserve(testName, testID),
|
||||||
"sandbox name should be released")
|
"sandbox name should be released")
|
||||||
_, err = c.sandboxIDIndex.Get(testID)
|
|
||||||
assert.Error(t, err, "sandbox id should be removed")
|
|
||||||
meta, err := c.sandboxStore.Get(testID)
|
meta, err := c.sandboxStore.Get(testID)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.True(t, metadata.IsNotExistError(err))
|
assert.True(t, metadata.IsNotExistError(err))
|
||||||
@ -209,7 +206,6 @@ func TestRemoveContainersInSandbox(t *testing.T) {
|
|||||||
c := newTestCRIContainerdService()
|
c := newTestCRIContainerdService()
|
||||||
WithFakeSnapshotClient(c)
|
WithFakeSnapshotClient(c)
|
||||||
assert.NoError(t, c.sandboxNameIndex.Reserve(testName, testID))
|
assert.NoError(t, c.sandboxNameIndex.Reserve(testName, testID))
|
||||||
assert.NoError(t, c.sandboxIDIndex.Add(testID))
|
|
||||||
assert.NoError(t, c.sandboxStore.Create(testMetadata))
|
assert.NoError(t, c.sandboxStore.Create(testMetadata))
|
||||||
for _, cntr := range testContainersMetadata {
|
for _, cntr := range testContainersMetadata {
|
||||||
assert.NoError(t, c.containerNameIndex.Reserve(cntr.Name, cntr.ID))
|
assert.NoError(t, c.containerNameIndex.Reserve(cntr.Name, cntr.ID))
|
||||||
|
@ -64,16 +64,6 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run
|
|||||||
c.sandboxNameIndex.ReleaseByName(name)
|
c.sandboxNameIndex.ReleaseByName(name)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
// Register the sandbox id.
|
|
||||||
if err := c.sandboxIDIndex.Add(id); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to insert sandbox id %q: %v", id, err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
// Delete the sandbox id if the function returns with an error.
|
|
||||||
if retErr != nil {
|
|
||||||
c.sandboxIDIndex.Delete(id) // nolint: errcheck
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Create initial sandbox metadata.
|
// Create initial sandbox metadata.
|
||||||
meta := metadata.SandboxMetadata{
|
meta := metadata.SandboxMetadata{
|
||||||
|
@ -362,10 +362,6 @@ func TestRunPodSandbox(t *testing.T) {
|
|||||||
pid := info.Task.Pid
|
pid := info.Task.Pid
|
||||||
assert.Equal(t, meta.NetNS, getNetworkNamespace(pid), "metadata network namespace should be correct")
|
assert.Equal(t, meta.NetNS, getNetworkNamespace(pid), "metadata network namespace should be correct")
|
||||||
|
|
||||||
gotID, err := c.sandboxIDIndex.Get(id)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, id, gotID, "sandbox id should be indexed")
|
|
||||||
|
|
||||||
expectedCNICalls := []string{"SetUpPod"}
|
expectedCNICalls := []string{"SetUpPod"}
|
||||||
assert.Equal(t, expectedCNICalls, fakeCNIPlugin.GetCalledNames(), "expect SetUpPod should be called")
|
assert.Equal(t, expectedCNICalls, fakeCNIPlugin.GetCalledNames(), "expect SetUpPod should be called")
|
||||||
calls = fakeCNIPlugin.GetCalledDetails()
|
calls = fakeCNIPlugin.GetCalledDetails()
|
||||||
|
@ -39,7 +39,7 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
||||||
r.GetPodSandboxId(), err)
|
r.GetPodSandboxId(), err)
|
||||||
|
@ -186,7 +186,6 @@ func TestPodSandboxStatus(t *testing.T) {
|
|||||||
fakeCNIPlugin := c.netPlugin.(*servertesting.FakeCNIPlugin)
|
fakeCNIPlugin := c.netPlugin.(*servertesting.FakeCNIPlugin)
|
||||||
fake.SetFakeTasks(test.sandboxTasks)
|
fake.SetFakeTasks(test.sandboxTasks)
|
||||||
if test.injectMetadata {
|
if test.injectMetadata {
|
||||||
assert.NoError(t, c.sandboxIDIndex.Add(metadata.ID))
|
|
||||||
assert.NoError(t, c.sandboxStore.Create(*metadata))
|
assert.NoError(t, c.sandboxStore.Create(*metadata))
|
||||||
}
|
}
|
||||||
if test.injectErr != nil {
|
if test.injectErr != nil {
|
||||||
|
@ -38,7 +38,7 @@ func (c *criContainerdService) StopPodSandbox(ctx context.Context, r *runtime.St
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
sandbox, err := c.sandboxStore.Get(r.GetPodSandboxId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
||||||
r.GetPodSandboxId(), err)
|
r.GetPodSandboxId(), err)
|
||||||
|
@ -137,7 +137,6 @@ func TestStopPodSandbox(t *testing.T) {
|
|||||||
|
|
||||||
if test.injectSandbox {
|
if test.injectSandbox {
|
||||||
assert.NoError(t, c.sandboxStore.Create(testSandbox))
|
assert.NoError(t, c.sandboxStore.Create(testSandbox))
|
||||||
c.sandboxIDIndex.Add(testID)
|
|
||||||
}
|
}
|
||||||
if test.injectErr != nil {
|
if test.injectErr != nil {
|
||||||
fake.InjectError("delete", test.injectErr)
|
fake.InjectError("delete", test.injectErr)
|
||||||
@ -234,7 +233,6 @@ func TestStopContainersInSandbox(t *testing.T) {
|
|||||||
c.taskService = fake
|
c.taskService = fake
|
||||||
fake.SetFakeTasks(testContainerdContainers)
|
fake.SetFakeTasks(testContainerdContainers)
|
||||||
assert.NoError(t, c.sandboxStore.Create(testSandbox))
|
assert.NoError(t, c.sandboxStore.Create(testSandbox))
|
||||||
assert.NoError(t, c.sandboxIDIndex.Add(testID))
|
|
||||||
for _, cntr := range testContainers {
|
for _, cntr := range testContainers {
|
||||||
assert.NoError(t, c.containerStore.Create(cntr))
|
assert.NoError(t, c.containerStore.Create(cntr))
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ import (
|
|||||||
"github.com/containerd/containerd/images"
|
"github.com/containerd/containerd/images"
|
||||||
diffservice "github.com/containerd/containerd/services/diff"
|
diffservice "github.com/containerd/containerd/services/diff"
|
||||||
"github.com/containerd/containerd/snapshot"
|
"github.com/containerd/containerd/snapshot"
|
||||||
"github.com/docker/docker/pkg/truncindex"
|
|
||||||
"github.com/kubernetes-incubator/cri-o/pkg/ocicni"
|
"github.com/kubernetes-incubator/cri-o/pkg/ocicni"
|
||||||
healthapi "google.golang.org/grpc/health/grpc_health_v1"
|
healthapi "google.golang.org/grpc/health/grpc_health_v1"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||||
@ -65,10 +64,6 @@ type criContainerdService struct {
|
|||||||
// sandboxNameIndex stores all sandbox names and make sure each name
|
// sandboxNameIndex stores all sandbox names and make sure each name
|
||||||
// is unique.
|
// is unique.
|
||||||
sandboxNameIndex *registrar.Registrar
|
sandboxNameIndex *registrar.Registrar
|
||||||
// sandboxIDIndex is trie tree for truncated id indexing, e.g. after an
|
|
||||||
// id "abcdefg" is added, we could use "abcd" to identify the same thing
|
|
||||||
// as long as there is no ambiguity.
|
|
||||||
sandboxIDIndex *truncindex.TruncIndex
|
|
||||||
// containerStore stores all container metadata.
|
// containerStore stores all container metadata.
|
||||||
containerStore metadata.ContainerStore
|
containerStore metadata.ContainerStore
|
||||||
// containerNameIndex stores all container names and make sure each
|
// containerNameIndex stores all container names and make sure each
|
||||||
@ -115,11 +110,7 @@ func NewCRIContainerdService(containerdEndpoint, rootDir, networkPluginBinDir, n
|
|||||||
sandboxStore: metadata.NewSandboxStore(store.NewMetadataStore()),
|
sandboxStore: metadata.NewSandboxStore(store.NewMetadataStore()),
|
||||||
containerStore: metadata.NewContainerStore(store.NewMetadataStore()),
|
containerStore: metadata.NewContainerStore(store.NewMetadataStore()),
|
||||||
imageMetadataStore: metadata.NewImageMetadataStore(store.NewMetadataStore()),
|
imageMetadataStore: metadata.NewImageMetadataStore(store.NewMetadataStore()),
|
||||||
// TODO(random-liu): Register sandbox/container id/name for recovered sandbox/container.
|
|
||||||
// TODO(random-liu): Use the same name and id index for both container and sandbox.
|
|
||||||
sandboxNameIndex: registrar.NewRegistrar(),
|
sandboxNameIndex: registrar.NewRegistrar(),
|
||||||
sandboxIDIndex: truncindex.NewTruncIndex(nil),
|
|
||||||
// TODO(random-liu): Add container id index.
|
|
||||||
containerNameIndex: registrar.NewRegistrar(),
|
containerNameIndex: registrar.NewRegistrar(),
|
||||||
containerService: client.ContainerService(),
|
containerService: client.ContainerService(),
|
||||||
taskService: client.TaskService(),
|
taskService: client.TaskService(),
|
||||||
|
@ -23,7 +23,6 @@ import (
|
|||||||
|
|
||||||
"github.com/containerd/containerd/api/services/execution"
|
"github.com/containerd/containerd/api/services/execution"
|
||||||
snapshotservice "github.com/containerd/containerd/services/snapshot"
|
snapshotservice "github.com/containerd/containerd/services/snapshot"
|
||||||
"github.com/docker/docker/pkg/truncindex"
|
|
||||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -62,7 +61,6 @@ func newTestCRIContainerdService() *criContainerdService {
|
|||||||
sandboxStore: metadata.NewSandboxStore(store.NewMetadataStore()),
|
sandboxStore: metadata.NewSandboxStore(store.NewMetadataStore()),
|
||||||
imageMetadataStore: metadata.NewImageMetadataStore(store.NewMetadataStore()),
|
imageMetadataStore: metadata.NewImageMetadataStore(store.NewMetadataStore()),
|
||||||
sandboxNameIndex: registrar.NewRegistrar(),
|
sandboxNameIndex: registrar.NewRegistrar(),
|
||||||
sandboxIDIndex: truncindex.NewTruncIndex(nil),
|
|
||||||
containerStore: metadata.NewContainerStore(store.NewMetadataStore()),
|
containerStore: metadata.NewContainerStore(store.NewMetadataStore()),
|
||||||
containerNameIndex: registrar.NewRegistrar(),
|
containerNameIndex: registrar.NewRegistrar(),
|
||||||
taskService: servertesting.NewFakeExecutionClient(),
|
taskService: servertesting.NewFakeExecutionClient(),
|
||||||
|
Loading…
Reference in New Issue
Block a user