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

Commit

Permalink
virtcontainers/persist: update API and interface
Browse files Browse the repository at this point in the history
Update persist FS API and interface to support rootless and mock filesystem
implementations. `RunStoragePath` and `RunVMStoragePath` are part of FS
object and may change their path depending on the driver (rootless/mock/fs)

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Feb 12, 2020
1 parent 6be7481 commit 768db1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 63 deletions.
8 changes: 8 additions & 0 deletions virtcontainers/persist/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ type PersistDriver interface {
// Don't use them too much unless you have no other choice! @weizhang555
GlobalWrite(relativePath string, data []byte) error
GlobalRead(relativePath string) ([]byte, error)

// RunStoragePath is the sandbox runtime directory.
// It will contain one state.json and one lock file for each created sandbox.
RunStoragePath() string

// RunVMStoragePath is the vm directory.
// It will contain all guest vm sockets and shared mountpoints.
RunVMStoragePath() string
}
68 changes: 20 additions & 48 deletions virtcontainers/persist/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"syscall"

persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
"github.com/sirupsen/logrus"
)

Expand All @@ -40,45 +39,12 @@ const sandboxPathSuffix = "sbs"
// vmPathSuffix is the suffix used for guest VMs.
const vmPathSuffix = "vm"

var StorageRootPath = func() string {
path := filepath.Join("/run", storagePathSuffix)
if rootless.IsRootless() {
return filepath.Join(rootless.GetRootlessDir(), path)
}
return path
}

// RunStoragePath is the sandbox runtime directory.
// It will contain one state.json and one lock file for each created sandbox.
var RunStoragePath = func() string {
return filepath.Join(StorageRootPath(), sandboxPathSuffix)
}

// RunVMStoragePath is the vm directory.
// It will contain all guest vm sockets and shared mountpoints.
// The function is declared this way for mocking in unit tests
var RunVMStoragePath = func() string {
return filepath.Join(StorageRootPath(), vmPathSuffix)
}

// TestSetRunStoragePath set RunStoragePath to path
// this function is only used for testing purpose
func TestSetRunStoragePath(path string) {
RunStoragePath = func() string {
return path
}
}

func TestSetStorageRootPath(path string) {
StorageRootPath = func() string {
return path
}
}

// FS storage driver implementation
type FS struct {
sandboxState *persistapi.SandboxState
containerState map[string]persistapi.ContainerState
sandboxState *persistapi.SandboxState
containerState map[string]persistapi.ContainerState
storageRootPath string
driverName string
}

var fsLog = logrus.WithField("source", "virtcontainers/persist/fs")
Expand All @@ -87,24 +53,22 @@ var fsLog = logrus.WithField("source", "virtcontainers/persist/fs")
func (fs *FS) Logger() *logrus.Entry {
return fsLog.WithFields(logrus.Fields{
"subsystem": "persist",
"driver": fs.driverName,
})
}

// Name returns driver name
func Name() string {
return "fs"
}

// Init FS persist driver and return abstract PersistDriver
func Init() (persistapi.PersistDriver, error) {
return &FS{
sandboxState: &persistapi.SandboxState{},
containerState: make(map[string]persistapi.ContainerState),
sandboxState: &persistapi.SandboxState{},
containerState: make(map[string]persistapi.ContainerState),
storageRootPath: filepath.Join("/run", storagePathSuffix),
driverName: "fs",
}, nil
}

func (fs *FS) sandboxDir(sandboxID string) (string, error) {
return filepath.Join(RunStoragePath(), sandboxID), nil
return filepath.Join(fs.RunStoragePath(), sandboxID), nil
}

// ToDisk sandboxState and containerState to disk
Expand Down Expand Up @@ -314,7 +278,7 @@ func (fs *FS) Lock(sandboxID string, exclusive bool) (func() error, error) {
}

func (fs *FS) GlobalWrite(relativePath string, data []byte) error {
path := filepath.Join(StorageRootPath(), relativePath)
path := filepath.Join(fs.storageRootPath, relativePath)
path, err := filepath.Abs(filepath.Clean(path))
if err != nil {
return fmt.Errorf("failed to find abs path for %q: %v", relativePath, err)
Expand Down Expand Up @@ -347,7 +311,7 @@ func (fs *FS) GlobalWrite(relativePath string, data []byte) error {
}

func (fs *FS) GlobalRead(relativePath string) ([]byte, error) {
path := filepath.Join(StorageRootPath(), relativePath)
path := filepath.Join(fs.storageRootPath, relativePath)
path, err := filepath.Abs(filepath.Clean(path))
if err != nil {
return nil, fmt.Errorf("failed to find abs path for %q: %v", relativePath, err)
Expand All @@ -367,3 +331,11 @@ func (fs *FS) GlobalRead(relativePath string) ([]byte, error) {
}
return data, nil
}

func (fs *FS) RunStoragePath() string {
return filepath.Join(fs.storageRootPath, sandboxPathSuffix)
}

func (fs *FS) RunVMStoragePath() string {
return filepath.Join(fs.storageRootPath, vmPathSuffix)
}
20 changes: 5 additions & 15 deletions virtcontainers/persist/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,29 @@ package fs

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/stretchr/testify/assert"
)

func getFsDriver() (*FS, error) {
driver, err := Init()
driver, err := MockFSInit()
if err != nil {
return nil, fmt.Errorf("failed to init fs driver")
}
fs, ok := driver.(*FS)
fs, ok := driver.(*MockFS)
if !ok {
return nil, fmt.Errorf("failed to convert driver to *FS")
return nil, fmt.Errorf("failed to convert driver to *MockFS")
}

return fs, nil
return fs.FS, nil
}

func initTestDir() func() {
testDir, _ := ioutil.TempDir("", "vc-tmp-")
// allow the tests to run without affecting the host system.
rootSave := StorageRootPath()
TestSetStorageRootPath(filepath.Join(testDir, "vc"))

os.MkdirAll(testDir, dirMode)

return func() {
TestSetStorageRootPath(rootSave)
os.RemoveAll(testDir)
os.RemoveAll(MockStorageRootPath())
}
}

Expand Down

0 comments on commit 768db1b

Please sign in to comment.