factory: Add new factory option template_path

Add new factory option template_path that specifies the path of template.

Fixes: #1549

Signed-off-by: Hui Zhu <teawater@hyper.sh>
This commit is contained in:
Hui Zhu 2019-04-17 17:04:07 +08:00
parent 3bdc40bfd0
commit 0549a70d93
12 changed files with 42 additions and 22 deletions

View File

@ -219,6 +219,11 @@ enable_iothreads = @DEFENABLEIOTHREADS@
# Default false
#enable_template = true
# Specifies the path of template.
#
# Default "/run/vc/vm/template"
#template_path = "/run/vc/vm/template"
# The number of caches of VMCache:
# unspecified or == 0 --> VMCache is disabled
# > 0 --> will be set to the specified number

View File

@ -159,9 +159,10 @@ var initFactoryCommand = cli.Command{
}
factoryConfig := vf.Config{
Template: runtimeConfig.FactoryConfig.Template,
Cache: runtimeConfig.FactoryConfig.VMCacheNumber,
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
Template: runtimeConfig.FactoryConfig.Template,
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
Cache: runtimeConfig.FactoryConfig.VMCacheNumber,
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
VMConfig: vc.VMConfig{
HypervisorType: runtimeConfig.HypervisorType,
HypervisorConfig: runtimeConfig.HypervisorConfig,
@ -250,7 +251,8 @@ var destroyFactoryCommand = cli.Command{
time.Sleep(time.Second)
} else if runtimeConfig.FactoryConfig.Template {
factoryConfig := vf.Config{
Template: true,
Template: true,
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
VMConfig: vc.VMConfig{
HypervisorType: runtimeConfig.HypervisorType,
HypervisorConfig: runtimeConfig.HypervisorConfig,
@ -305,7 +307,8 @@ var statusFactoryCommand = cli.Command{
}
if runtimeConfig.FactoryConfig.Template {
factoryConfig := vf.Config{
Template: true,
Template: true,
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
VMConfig: vc.VMConfig{
HypervisorType: runtimeConfig.HypervisorType,
HypervisorConfig: runtimeConfig.HypervisorConfig,

View File

@ -70,6 +70,7 @@ func TestFactoryCLIFunctionInit(t *testing.T) {
}
runtimeConfig.FactoryConfig.Template = true
runtimeConfig.FactoryConfig.TemplatePath = "/run/vc/vm/template"
runtimeConfig.HypervisorType = vc.MockHypervisor
runtimeConfig.AgentType = vc.NoopAgentType
runtimeConfig.ProxyType = vc.NoopProxyType

View File

@ -43,6 +43,7 @@ const defaultHotplugVFIOOnRootBus bool = false
const defaultEntropySource = "/dev/urandom"
const defaultGuestHookPath string = ""
const defaultTemplatePath string = "/run/vc/vm/template"
const defaultVMCacheEndpoint string = "/var/run/kata-containers/cache.sock"
// Default config file used by stateless systems.

View File

@ -76,6 +76,7 @@ type tomlConfig struct {
type factory struct {
Template bool `toml:"enable_template"`
TemplatePath string `toml:"template_path"`
VMCacheNumber uint `toml:"vm_cache_number"`
VMCacheEndpoint string `toml:"vm_cache_endpoint"`
}
@ -546,11 +547,15 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
}
func newFactoryConfig(f factory) (oci.FactoryConfig, error) {
if f.TemplatePath == "" {
f.TemplatePath = defaultTemplatePath
}
if f.VMCacheEndpoint == "" {
f.VMCacheEndpoint = defaultVMCacheEndpoint
}
return oci.FactoryConfig{
Template: f.Template,
TemplatePath: f.TemplatePath,
VMCacheNumber: f.VMCacheNumber,
VMCacheEndpoint: f.VMCacheEndpoint,
}, nil

View File

@ -185,6 +185,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf
}
factoryConfig := oci.FactoryConfig{
TemplatePath: defaultTemplatePath,
VMCacheEndpoint: defaultVMCacheEndpoint,
}
@ -633,6 +634,7 @@ func TestMinimalRuntimeConfig(t *testing.T) {
}
expectedFactoryConfig := oci.FactoryConfig{
TemplatePath: defaultTemplatePath,
VMCacheEndpoint: defaultVMCacheEndpoint,
}
@ -1419,6 +1421,7 @@ func TestUpdateRuntimeConfigurationFactoryConfig(t *testing.T) {
config := oci.RuntimeConfig{}
expectedFactoryConfig := oci.FactoryConfig{
Template: true,
TemplatePath: defaultTemplatePath,
VMCacheEndpoint: defaultVMCacheEndpoint,
}

View File

@ -124,6 +124,7 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon
}
factoryConfig := vf.Config{
Template: runtimeConfig.FactoryConfig.Template,
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
VMCacheEndpoint: runtimeConfig.FactoryConfig.VMCacheEndpoint,
VMConfig: vc.VMConfig{

View File

@ -25,10 +25,10 @@ var factoryLogger = logrus.FieldLogger(logrus.New())
// Config is a collection of VM factory configurations.
type Config struct {
Template bool
Template bool
VMCache bool
Cache uint
TemplatePath string
VMCacheEndpoint string
VMConfig vc.VMConfig
@ -70,12 +70,12 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
} else {
if config.Template {
if fetchOnly {
b, err = template.Fetch(config.VMConfig)
b, err = template.Fetch(config.VMConfig, config.TemplatePath)
if err != nil {
return nil, err
}
} else {
b, err = template.New(ctx, config.VMConfig)
b, err = template.New(ctx, config.VMConfig, config.TemplatePath)
if err != nil {
return nil, err
}

View File

@ -61,6 +61,7 @@ func TestNewFactory(t *testing.T) {
}
config.Template = true
config.TemplatePath = testDir
f, err = NewFactory(ctx, config, false)
assert.Nil(err)
f.CloseFactory(ctx)
@ -210,7 +211,7 @@ func TestFactoryGetVM(t *testing.T) {
f.CloseFactory(ctx)
// template factory
f, err = NewFactory(ctx, Config{Template: true, VMConfig: vmConfig}, false)
f, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, VMConfig: vmConfig}, false)
assert.Nil(err)
vm, err = f.GetVM(ctx, vmConfig)
@ -222,10 +223,10 @@ func TestFactoryGetVM(t *testing.T) {
f.CloseFactory(ctx)
// fetch template factory
f, err = NewFactory(ctx, Config{Template: true, VMConfig: vmConfig}, false)
f, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, VMConfig: vmConfig}, false)
assert.Nil(err)
_, err = NewFactory(ctx, Config{Template: true, VMConfig: vmConfig}, true)
_, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, VMConfig: vmConfig}, true)
assert.Error(err)
vm, err = f.GetVM(ctx, vmConfig)
@ -249,7 +250,7 @@ func TestFactoryGetVM(t *testing.T) {
f.CloseFactory(ctx)
// cache factory over template factory
f, err = NewFactory(ctx, Config{Template: true, Cache: 2, VMConfig: vmConfig}, false)
f, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, Cache: 2, VMConfig: vmConfig}, false)
assert.Nil(err)
vm, err = f.GetVM(ctx, vmConfig)

View File

@ -16,7 +16,6 @@ import (
pb "github.com/kata-containers/runtime/protocols/cache"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory/base"
"github.com/kata-containers/runtime/virtcontainers/store"
)
type template struct {
@ -29,9 +28,8 @@ var templateWaitForAgent = 2 * time.Second
// Fetch finds and returns a pre-built template factory.
// TODO: save template metadata and fetch from storage.
func Fetch(config vc.VMConfig) (base.FactoryBase, error) {
statePath := store.RunVMStoragePath + "/template"
t := &template{statePath, config}
func Fetch(config vc.VMConfig, templatePath string) (base.FactoryBase, error) {
t := &template{templatePath, config}
err := t.checkTemplateVM()
if err != nil {
@ -42,13 +40,12 @@ func Fetch(config vc.VMConfig) (base.FactoryBase, error) {
}
// New creates a new VM template factory.
func New(ctx context.Context, config vc.VMConfig) (base.FactoryBase, error) {
statePath := store.RunVMStoragePath + "/template"
t := &template{statePath, config}
func New(ctx context.Context, config vc.VMConfig, templatePath string) (base.FactoryBase, error) {
t := &template{templatePath, config}
err := t.checkTemplateVM()
if err == nil {
return nil, fmt.Errorf("There is already a VM template in %s", statePath)
return nil, fmt.Errorf("There is already a VM template in %s", templatePath)
}
err = t.prepareTemplateFiles()

View File

@ -46,7 +46,7 @@ func TestTemplateFactory(t *testing.T) {
ctx := context.Background()
// New
f, err := New(ctx, vmConfig)
f, err := New(ctx, vmConfig, testDir)
assert.Nil(err)
// Config

View File

@ -98,6 +98,9 @@ type FactoryConfig struct {
// Template enables VM templating support in VM factory.
Template bool
// TemplatePath specifies the path of template.
TemplatePath string
// VMCacheNumber specifies the the number of caches of VMCache.
VMCacheNumber uint