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

Commit cfedf35

Browse files
committed
runtime: Fix /var/lib/vc/sbs/${sid} dir residual
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>
1 parent a1d993f commit cfedf35

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

virtcontainers/sandbox.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -728,14 +728,14 @@ func fetchSandbox(ctx context.Context, sandboxID string) (sandbox *Sandbox, err
728728

729729
var config SandboxConfig
730730

731-
// Try to load sandbox config from old store at first.
732-
c, ctx, err := loadSandboxConfigFromOldStore(ctx, sandboxID)
731+
// Try to load sandbox config from new store at first.
732+
c, err := loadSandboxConfig(sandboxID)
733733
if err != nil {
734-
virtLog.Warningf("failed to get sandbox config from old store: %v", err)
735-
// If we failed to load sandbox config from old store, try again with new store.
736-
c, err = loadSandboxConfig(sandboxID)
734+
virtLog.Warningf("failed to get sandbox config from new store: %v", err)
735+
// If we failed to load sandbox config from new store, try again with old store.
736+
c, ctx, err = loadSandboxConfigFromOldStore(ctx, sandboxID)
737737
if err != nil {
738-
virtLog.Warningf("failed to get sandbox config from new store: %v", err)
738+
virtLog.Warningf("failed to get sandbox config from old store: %v", err)
739739
return nil, err
740740
}
741741
}
@@ -837,7 +837,11 @@ func (s *Sandbox) Delete() error {
837837
}
838838

839839
s.agent.cleanup(s)
840-
840+
if useOldStore(s.ctx) && s.store != nil {
841+
if err := s.store.Delete(); err != nil {
842+
s.Logger().WithError(err).Error("store delete failed")
843+
}
844+
}
841845
return s.newStore.Destroy(s.id)
842846
}
843847

virtcontainers/sandbox_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os/exec"
1414
"path"
1515
"path/filepath"
16+
"strings"
1617
"sync"
1718
"syscall"
1819
"testing"
@@ -24,6 +25,7 @@ import (
2425
exp "github.com/kata-containers/runtime/virtcontainers/experimental"
2526
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
2627
"github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
28+
"github.com/kata-containers/runtime/virtcontainers/store"
2729
"github.com/kata-containers/runtime/virtcontainers/types"
2830
specs "github.com/opencontainers/runtime-spec/specs-go"
2931
"github.com/stretchr/testify/assert"
@@ -1527,3 +1529,40 @@ func TestGetSandboxCpuSet(t *testing.T) {
15271529
})
15281530
}
15291531
}
1532+
1533+
func TestSandboxStoreClean(t *testing.T) {
1534+
ctx := context.Background()
1535+
contID := "SandboxStore"
1536+
contConfig := newTestContainerConfigNoop(contID)
1537+
hConfig := newHypervisorConfig(nil, nil)
1538+
assert := assert.New(t)
1539+
1540+
// create a sandbox
1541+
p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil)
1542+
assert.NoError(err)
1543+
defer cleanUp()
1544+
1545+
l := len(p.GetAllContainers())
1546+
assert.Equal(l, 1)
1547+
1548+
// persist to disk
1549+
err = p.storeSandbox()
1550+
assert.NoError(err)
1551+
1552+
loadSandboxConfigFromOldStore(ctx, p.ID())
1553+
1554+
runtimeSidPath := store.SandboxConfigurationRoot(p.ID())
1555+
runtimeSidPath = strings.TrimPrefix(runtimeSidPath, "file://")
1556+
1557+
p.store, err = store.NewVCSandboxStore(ctx, testSandboxID)
1558+
assert.Nil(err)
1559+
1560+
p.ctx = context.WithValue(ctx, oldstoreKey, true)
1561+
err = p.Delete()
1562+
assert.NoError(err)
1563+
1564+
// expect runtimeSidPath not exist, if exist, it means this case failed.
1565+
_, err = os.Stat(runtimeSidPath)
1566+
assert.Error(err)
1567+
assert.True(os.IsNotExist(err))
1568+
}

0 commit comments

Comments
 (0)