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

Commit

Permalink
virtcontainers: store: Add a VC specific Store
Browse files Browse the repository at this point in the history
This is basically a Store dispatcher, for storing items into their right
Store (either configuration or state).
There's very little logic here, except for finding out which store an
item belongs to in the virtcontainers context.

vc.go also provides virtcontainers specific utilities.

Signed-off-by: Samuel Ortiz <[email protected]>
  • Loading branch information
Samuel Ortiz committed Feb 6, 2019
1 parent ef11bf5 commit c25c608
Show file tree
Hide file tree
Showing 3 changed files with 436 additions and 0 deletions.
36 changes: 36 additions & 0 deletions virtcontainers/store/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ package store

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

"github.com/stretchr/testify/assert"
)

const testSandboxID = "7f49d00d-1995-4156-8c79-5f5ab24ce138"

var sandboxDirConfig = ""
var sandboxFileConfig = ""
var sandboxDirState = ""
var sandboxDirLock = ""
var sandboxFileState = ""
var sandboxFileLock = ""
var storeRoot = "file:///tmp/root1/"

func TestNewStore(t *testing.T) {
Expand Down Expand Up @@ -92,3 +103,28 @@ func TestManagerFindStore(t *testing.T) {
newStore = stores.findStore(storeRoot + "foobar")
assert.Nil(t, newStore, "findStore should not have found a new store")
}

// TestMain is the common main function used by ALL the test functions
// for the store.
func TestMain(m *testing.M) {
testDir, err := ioutil.TempDir("", "store-tmp-")
if err != nil {
panic(err)
}

// allow the tests to run without affecting the host system.
ConfigStoragePath = filepath.Join(testDir, StoragePathSuffix, "config")
RunStoragePath = filepath.Join(testDir, StoragePathSuffix, "run")

// set now that ConfigStoragePath has been overridden.
sandboxDirConfig = filepath.Join(ConfigStoragePath, testSandboxID)
sandboxFileConfig = filepath.Join(ConfigStoragePath, testSandboxID, ConfigurationFile)
sandboxDirState = filepath.Join(RunStoragePath, testSandboxID)
sandboxDirLock = filepath.Join(RunStoragePath, testSandboxID)
sandboxFileState = filepath.Join(RunStoragePath, testSandboxID, StateFile)
sandboxFileLock = filepath.Join(RunStoragePath, testSandboxID, LockFile)

ret := m.Run()

os.Exit(ret)
}
282 changes: 282 additions & 0 deletions virtcontainers/store/vc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package store

import (
"context"
"encoding/json"
"fmt"
"path/filepath"

"github.com/kata-containers/runtime/virtcontainers/device/api"
"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/device/drivers"
"github.com/kata-containers/runtime/virtcontainers/types"
)

// VCStore is a virtcontainers specific Store.
// Virtcontainers typically needs a configuration Store for
// storing permanent items across reboots.
// It also needs a state Store for storing states and other run-time
// related items. Those should not survive a reboot.
//
// VCStore simply dispatches items into the right Store.
type VCStore struct {
config, state *Store
}

func (s *VCStore) itemToStore(item Item) *Store {
switch item {
case Configuration:
return s.config
case State, Network, Hypervisor, Agent, Process, Lock, Mounts, Devices, DeviceIDs:
return s.state
}

return s.state
}

// NewVCStore creates a virtcontainers specific Store.
func NewVCStore(ctx context.Context, configRoot, stateRoot string) (*VCStore, error) {
config, err := New(ctx, configRoot)
if err != nil {
return nil, err
}

state, err := New(ctx, stateRoot)
if err != nil {
return nil, err
}

return &VCStore{
config: config,
state: state,
}, nil
}

// NewVCSandboxStore creates a virtcontainers sandbox Store, with filesystem backend.
func NewVCSandboxStore(ctx context.Context, sandboxID string) (*VCStore, error) {
if sandboxID == "" {
return nil, fmt.Errorf("sandbox ID can not be empty")
}

return NewVCStore(ctx,
SandboxConfigurationRoot(sandboxID),
SandboxRuntimeRoot(sandboxID),
)
}

// NewVCContainerStore creates a virtcontainers container Store, with filesystem backend.
func NewVCContainerStore(ctx context.Context, sandboxID, containerID string) (*VCStore, error) {
if sandboxID == "" {
return nil, fmt.Errorf("sandbox ID can not be empty")
}

if containerID == "" {
return nil, fmt.Errorf("container ID can not be empty")
}

return NewVCStore(ctx,
ContainerConfigurationRoot(sandboxID, containerID),
ContainerRuntimeRoot(sandboxID, containerID),
)
}

// Store stores a virtcontainers item into the right Store.
func (s *VCStore) Store(item Item, data interface{}) error {
return s.itemToStore(item).Store(item, data)
}

// Load loads a virtcontainers item from the right Store.
func (s *VCStore) Load(item Item, data interface{}) error {
return s.itemToStore(item).Load(item, data)
}

// Delete deletes all artifacts created by a VCStore.
// Both config and state Stores are also removed from the manager.
func (s *VCStore) Delete() error {
if err := s.config.Delete(); err != nil {
return err
}

if err := s.state.Delete(); err != nil {
return err
}

return nil
}

// LoadState loads an returns a virtcontainer state
func (s *VCStore) LoadState() (types.State, error) {
var state types.State

if err := s.state.Load(State, &state); err != nil {
return types.State{}, err
}

return state, nil
}

// TypedDevice is used as an intermediate representation for marshalling
// and unmarshalling Device implementations.
type TypedDevice struct {
Type string

// Data is assigned the Device object.
// This being declared as RawMessage prevents it from being marshalled/unmarshalled.
// We do that explicitly depending on Type.
Data json.RawMessage
}

// StoreDevices stores a virtcontainers devices slice.
// The Device slice is first marshalled into a TypedDevice
// one to include the type of the Device objects.
func (s *VCStore) StoreDevices(devices []api.Device) error {
var typedDevices []TypedDevice

for _, d := range devices {
tempJSON, _ := json.Marshal(d)
typedDevice := TypedDevice{
Type: string(d.DeviceType()),
Data: tempJSON,
}
typedDevices = append(typedDevices, typedDevice)
}

return s.state.Store(Devices, typedDevices)
}

// LoadDevices loads an returns a virtcontainer devices slice.
// We need a custom unmarshalling routine for translating TypedDevices
// into api.Devices based on their type.
func (s *VCStore) LoadDevices() ([]api.Device, error) {
var typedDevices []TypedDevice
var devices []api.Device

if err := s.state.Load(Devices, &typedDevices); err != nil {
return []api.Device{}, err
}

for _, d := range typedDevices {
switch d.Type {
case string(config.DeviceVFIO):
// TODO: remove dependency of drivers package
var device drivers.VFIODevice
if err := json.Unmarshal(d.Data, &device); err != nil {
return []api.Device{}, err
}
devices = append(devices, &device)
case string(config.DeviceBlock):
// TODO: remove dependency of drivers package
var device drivers.BlockDevice
if err := json.Unmarshal(d.Data, &device); err != nil {
return []api.Device{}, err
}
devices = append(devices, &device)
case string(config.DeviceGeneric):
// TODO: remove dependency of drivers package
var device drivers.GenericDevice
if err := json.Unmarshal(d.Data, &device); err != nil {
return []api.Device{}, err
}
devices = append(devices, &device)
default:
return []api.Device{}, fmt.Errorf("Unknown device type, could not unmarshal")
}
}

return devices, nil
}

// Utilities for virtcontainers

// SandboxConfigurationRoot returns a virtcontainers sandbox configuration root URL.
// This will hold across host reboot persistent data about a sandbox configuration.
// It should look like file:///var/lib/vc/sbs/<sandboxID>/
func SandboxConfigurationRoot(id string) string {
return filesystemScheme + "://" + filepath.Join(ConfigStoragePath, id)
}

// SandboxConfigurationRootPath returns a virtcontainers sandbox configuration root path.
func SandboxConfigurationRootPath(id string) string {
return filepath.Join(ConfigStoragePath, id)
}

// SandboxConfigurationItemPath returns a virtcontainers sandbox configuration item path.
func SandboxConfigurationItemPath(id string, item Item) (string, error) {
if id == "" {
return "", fmt.Errorf("Empty sandbox ID")
}

itemFile, err := itemToFile(item)
if err != nil {
return "", err
}

return filepath.Join(ConfigStoragePath, id, itemFile), nil
}

// SandboxRuntimeRoot returns a virtcontainers sandbox runtime root URL.
// This will hold data related to a sandbox run-time state that will not
// be persistent across host reboots.
// It should look like file:///run/vc/sbs/<sandboxID>/
func SandboxRuntimeRoot(id string) string {
return filesystemScheme + "://" + filepath.Join(RunStoragePath, id)
}

// SandboxRuntimeRootPath returns a virtcontainers sandbox runtime root path.
func SandboxRuntimeRootPath(id string) string {
return filepath.Join(RunStoragePath, id)
}

// SandboxRuntimeItemPath returns a virtcontainers sandbox runtime item path.
func SandboxRuntimeItemPath(id string, item Item) (string, error) {
if id == "" {
return "", fmt.Errorf("Empty sandbox ID")
}

itemFile, err := itemToFile(item)
if err != nil {
return "", err
}

return filepath.Join(RunStoragePath, id, itemFile), nil
}

// ContainerConfigurationRoot returns a virtcontainers container configuration root URL.
// This will hold across host reboot persistent data about a container configuration.
// It should look like file:///var/lib/vc/sbs/<sandboxID>/<containerID>
func ContainerConfigurationRoot(sandboxID, containerID string) string {
return filesystemScheme + "://" + filepath.Join(ConfigStoragePath, sandboxID, containerID)
}

// ContainerConfigurationRootPath returns a virtcontainers container configuration root path.
func ContainerConfigurationRootPath(sandboxID, containerID string) string {
return filepath.Join(ConfigStoragePath, sandboxID, containerID)
}

// ContainerRuntimeRoot returns a virtcontainers container runtime root URL.
// This will hold data related to a container run-time state that will not
// be persistent across host reboots.
// It should look like file:///run/vc/sbs/<sandboxID>/<containerID>/
func ContainerRuntimeRoot(sandboxID, containerID string) string {
return filesystemScheme + "://" + filepath.Join(RunStoragePath, sandboxID, containerID)
}

// ContainerRuntimeRootPath returns a virtcontainers container runtime root path.
func ContainerRuntimeRootPath(sandboxID, containerID string) string {
return filepath.Join(RunStoragePath, sandboxID, containerID)
}

// VCSandboxStoreExists returns true if a sandbox store already exists.
func VCSandboxStoreExists(ctx context.Context, sandboxID string) bool {
s := stores.findStore(SandboxConfigurationRoot(sandboxID))
if s != nil {
return true
}

return false
}
Loading

0 comments on commit c25c608

Please sign in to comment.