Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
runtime: Fix /var/lib/vc/sbs/${sid} dir residual
Browse files Browse the repository at this point in the history
Create and delete a kata container everytime, the directory of
/var/lib/vc/sbs/ will have a new directory which's name is the
${sandbox-id}, e.g.
d3e0482b22b9e25cd3268608b12ab8c1eb666960c4fa9a6a72a3e4d0b1606551

Fixes: #2921

Signed-off-by: Shukui Yang <keloyangsk@gmail.com>
  • Loading branch information
keloyang committed Oct 13, 2020
1 parent 9d82056 commit ad3eec5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
18 changes: 11 additions & 7 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,14 @@ func fetchSandbox(ctx context.Context, sandboxID string) (sandbox *Sandbox, err

var config SandboxConfig

// Try to load sandbox config from old store at first.
c, ctx, err := loadSandboxConfigFromOldStore(ctx, sandboxID)
// Try to load sandbox config from new store at first.
c, err := loadSandboxConfig(sandboxID)
if err != nil {
virtLog.Warningf("failed to get sandbox config from old store: %v", err)
// If we failed to load sandbox config from old store, try again with new store.
c, err = loadSandboxConfig(sandboxID)
virtLog.Warningf("failed to get sandbox config from new store: %v", err)
// If we failed to load sandbox config from new store, try again with old store.
c, ctx, err = loadSandboxConfigFromOldStore(ctx, sandboxID)
if err != nil {
virtLog.Warningf("failed to get sandbox config from new store: %v", err)
virtLog.Warningf("failed to get sandbox config from old store: %v", err)
return nil, err
}
}
Expand Down Expand Up @@ -834,7 +834,11 @@ func (s *Sandbox) Delete() error {
}

s.agent.cleanup(s)

if useOldStore(s.ctx) && s.store != nil {
if err := s.store.Delete(); err != nil {
s.Logger().WithError(err).Error("store delete failed")
}
}
return s.newStore.Destroy(s.id)
}

Expand Down
39 changes: 39 additions & 0 deletions virtcontainers/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
Expand All @@ -24,6 +25,7 @@ import (
exp "github.com/kata-containers/runtime/virtcontainers/experimental"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
"github.com/kata-containers/runtime/virtcontainers/store"
"github.com/kata-containers/runtime/virtcontainers/types"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1736,3 +1738,40 @@ func TestGetSandboxCpuSet(t *testing.T) {
})
}
}

func TestSandboxStoreClean(t *testing.T) {
ctx := context.Background()
contID := "SandboxStore"
contConfig := newTestContainerConfigNoop(contID)
hConfig := newHypervisorConfig(nil, nil)
assert := assert.New(t)

// create a sandbox
p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil)
assert.NoError(err)
defer cleanUp()

l := len(p.GetAllContainers())
assert.Equal(l, 1)

// persist to disk
err = p.storeSandbox()
assert.NoError(err)

loadSandboxConfigFromOldStore(ctx, p.ID())

runtimeSidPath := store.SandboxConfigurationRoot(p.ID())
runtimeSidPath = strings.TrimPrefix(runtimeSidPath, "file://")

p.store, err = store.NewVCSandboxStore(ctx, testSandboxID)
assert.Nil(err)

p.ctx = context.WithValue(ctx, oldstoreKey, true)
err = p.Delete()
assert.NoError(err)

// expect runtimeSidPath not exist, if exist, it means this case failed.
_, err = os.Stat(runtimeSidPath)
assert.Error(err)
assert.True(os.IsNotExist(err))
}

0 comments on commit ad3eec5

Please sign in to comment.