Return not exist error in metadata store
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -107,17 +107,18 @@ func isContainerdContainerNotExistError(grpcError error) bool {
|
||||
// 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 {
|
||||
if err != nil && !metadata.IsNotExistError(err) {
|
||||
return nil, fmt.Errorf("sandbox metadata not found: %v", err)
|
||||
}
|
||||
if sandbox != nil {
|
||||
if err == nil {
|
||||
return sandbox, nil
|
||||
}
|
||||
// sandbox is not found in metadata store, try to extract full id.
|
||||
id, err = c.sandboxIDIndex.Get(id)
|
||||
if err != nil {
|
||||
if err == truncindex.ErrNotExist {
|
||||
return nil, nil
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -35,25 +35,34 @@ func TestGetSandbox(t *testing.T) {
|
||||
assert.NoError(t, c.sandboxIDIndex.Add(testID))
|
||||
|
||||
for desc, test := range map[string]struct {
|
||||
id string
|
||||
expected *metadata.SandboxMetadata
|
||||
id string
|
||||
expected *metadata.SandboxMetadata
|
||||
expectErr bool
|
||||
}{
|
||||
"full id": {
|
||||
id: testID,
|
||||
expected: &testSandbox,
|
||||
id: testID,
|
||||
expected: &testSandbox,
|
||||
expectErr: false,
|
||||
},
|
||||
"partial id": {
|
||||
id: testID[:3],
|
||||
expected: &testSandbox,
|
||||
id: testID[:3],
|
||||
expected: &testSandbox,
|
||||
expectErr: false,
|
||||
},
|
||||
"non-exist id": {
|
||||
id: "gfedcba",
|
||||
expected: nil,
|
||||
id: "gfedcba",
|
||||
expected: nil,
|
||||
expectErr: true,
|
||||
},
|
||||
} {
|
||||
t.Logf("TestCase %q", desc)
|
||||
sb, err := c.getSandbox(test.id)
|
||||
assert.NoError(t, err)
|
||||
if test.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.True(t, metadata.IsNotExistError(err))
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, test.expected, sb)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ import (
|
||||
"github.com/containerd/containerd/api/services/execution"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata"
|
||||
)
|
||||
|
||||
// RemovePodSandbox removes the sandbox. If there are running containers in the
|
||||
@@ -39,9 +41,10 @@ func (c *criContainerdService) RemovePodSandbox(ctx context.Context, r *runtime.
|
||||
|
||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find sandbox %q: %v", r.GetPodSandboxId(), err)
|
||||
}
|
||||
if sandbox == nil {
|
||||
if !metadata.IsNotExistError(err) {
|
||||
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
||||
r.GetPodSandboxId(), err)
|
||||
}
|
||||
// Do not return error if the id doesn't exist.
|
||||
glog.V(5).Infof("RemovePodSandbox called for sandbox %q that does not exist",
|
||||
r.GetPodSandboxId())
|
||||
|
||||
@@ -111,7 +111,8 @@ func TestRemovePodSandbox(t *testing.T) {
|
||||
_, err = c.sandboxIDIndex.Get(testID)
|
||||
assert.Error(t, err, "sandbox id should be removed")
|
||||
meta, err := c.sandboxStore.Get(testID)
|
||||
assert.NoError(t, err)
|
||||
assert.Error(t, err)
|
||||
assert.True(t, metadata.IsNotExistError(err))
|
||||
assert.Nil(t, meta, "sandbox metadata should be removed")
|
||||
res, err = c.RemovePodSandbox(context.Background(), &runtime.RemovePodSandboxRequest{
|
||||
PodSandboxId: testID,
|
||||
|
||||
@@ -41,10 +41,8 @@ func (c *criContainerdService) PodSandboxStatus(ctx context.Context, r *runtime.
|
||||
|
||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find sandbox %q: %v", r.GetPodSandboxId(), err)
|
||||
}
|
||||
if sandbox == nil {
|
||||
return nil, fmt.Errorf("sandbox %q does not exist", r.GetPodSandboxId())
|
||||
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
||||
r.GetPodSandboxId(), err)
|
||||
}
|
||||
// Use the full sandbox id.
|
||||
id := sandbox.ID
|
||||
|
||||
@@ -39,10 +39,8 @@ func (c *criContainerdService) StopPodSandbox(ctx context.Context, r *runtime.St
|
||||
|
||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find sandbox %q: %v", r.GetPodSandboxId(), err)
|
||||
}
|
||||
if sandbox == nil {
|
||||
return nil, fmt.Errorf("sandbox %q does not exist", r.GetPodSandboxId())
|
||||
return nil, fmt.Errorf("an error occurred when try to find sandbox %q: %v",
|
||||
r.GetPodSandboxId(), err)
|
||||
}
|
||||
// Use the full sandbox id.
|
||||
id := sandbox.ID
|
||||
|
||||
Reference in New Issue
Block a user