mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-30 09:13:29 +00:00
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:
parent
3bdc40bfd0
commit
0549a70d93
@ -219,6 +219,11 @@ enable_iothreads = @DEFENABLEIOTHREADS@
|
|||||||
# Default false
|
# Default false
|
||||||
#enable_template = true
|
#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:
|
# The number of caches of VMCache:
|
||||||
# unspecified or == 0 --> VMCache is disabled
|
# unspecified or == 0 --> VMCache is disabled
|
||||||
# > 0 --> will be set to the specified number
|
# > 0 --> will be set to the specified number
|
||||||
|
@ -159,9 +159,10 @@ var initFactoryCommand = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
factoryConfig := vf.Config{
|
factoryConfig := vf.Config{
|
||||||
Template: runtimeConfig.FactoryConfig.Template,
|
Template: runtimeConfig.FactoryConfig.Template,
|
||||||
Cache: runtimeConfig.FactoryConfig.VMCacheNumber,
|
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
|
||||||
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
|
Cache: runtimeConfig.FactoryConfig.VMCacheNumber,
|
||||||
|
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
|
||||||
VMConfig: vc.VMConfig{
|
VMConfig: vc.VMConfig{
|
||||||
HypervisorType: runtimeConfig.HypervisorType,
|
HypervisorType: runtimeConfig.HypervisorType,
|
||||||
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
||||||
@ -250,7 +251,8 @@ var destroyFactoryCommand = cli.Command{
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
} else if runtimeConfig.FactoryConfig.Template {
|
} else if runtimeConfig.FactoryConfig.Template {
|
||||||
factoryConfig := vf.Config{
|
factoryConfig := vf.Config{
|
||||||
Template: true,
|
Template: true,
|
||||||
|
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
|
||||||
VMConfig: vc.VMConfig{
|
VMConfig: vc.VMConfig{
|
||||||
HypervisorType: runtimeConfig.HypervisorType,
|
HypervisorType: runtimeConfig.HypervisorType,
|
||||||
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
||||||
@ -305,7 +307,8 @@ var statusFactoryCommand = cli.Command{
|
|||||||
}
|
}
|
||||||
if runtimeConfig.FactoryConfig.Template {
|
if runtimeConfig.FactoryConfig.Template {
|
||||||
factoryConfig := vf.Config{
|
factoryConfig := vf.Config{
|
||||||
Template: true,
|
Template: true,
|
||||||
|
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
|
||||||
VMConfig: vc.VMConfig{
|
VMConfig: vc.VMConfig{
|
||||||
HypervisorType: runtimeConfig.HypervisorType,
|
HypervisorType: runtimeConfig.HypervisorType,
|
||||||
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
||||||
|
@ -70,6 +70,7 @@ func TestFactoryCLIFunctionInit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runtimeConfig.FactoryConfig.Template = true
|
runtimeConfig.FactoryConfig.Template = true
|
||||||
|
runtimeConfig.FactoryConfig.TemplatePath = "/run/vc/vm/template"
|
||||||
runtimeConfig.HypervisorType = vc.MockHypervisor
|
runtimeConfig.HypervisorType = vc.MockHypervisor
|
||||||
runtimeConfig.AgentType = vc.NoopAgentType
|
runtimeConfig.AgentType = vc.NoopAgentType
|
||||||
runtimeConfig.ProxyType = vc.NoopProxyType
|
runtimeConfig.ProxyType = vc.NoopProxyType
|
||||||
|
@ -43,6 +43,7 @@ const defaultHotplugVFIOOnRootBus bool = false
|
|||||||
const defaultEntropySource = "/dev/urandom"
|
const defaultEntropySource = "/dev/urandom"
|
||||||
const defaultGuestHookPath string = ""
|
const defaultGuestHookPath string = ""
|
||||||
|
|
||||||
|
const defaultTemplatePath string = "/run/vc/vm/template"
|
||||||
const defaultVMCacheEndpoint string = "/var/run/kata-containers/cache.sock"
|
const defaultVMCacheEndpoint string = "/var/run/kata-containers/cache.sock"
|
||||||
|
|
||||||
// Default config file used by stateless systems.
|
// Default config file used by stateless systems.
|
||||||
|
@ -76,6 +76,7 @@ type tomlConfig struct {
|
|||||||
|
|
||||||
type factory struct {
|
type factory struct {
|
||||||
Template bool `toml:"enable_template"`
|
Template bool `toml:"enable_template"`
|
||||||
|
TemplatePath string `toml:"template_path"`
|
||||||
VMCacheNumber uint `toml:"vm_cache_number"`
|
VMCacheNumber uint `toml:"vm_cache_number"`
|
||||||
VMCacheEndpoint string `toml:"vm_cache_endpoint"`
|
VMCacheEndpoint string `toml:"vm_cache_endpoint"`
|
||||||
}
|
}
|
||||||
@ -546,11 +547,15 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newFactoryConfig(f factory) (oci.FactoryConfig, error) {
|
func newFactoryConfig(f factory) (oci.FactoryConfig, error) {
|
||||||
|
if f.TemplatePath == "" {
|
||||||
|
f.TemplatePath = defaultTemplatePath
|
||||||
|
}
|
||||||
if f.VMCacheEndpoint == "" {
|
if f.VMCacheEndpoint == "" {
|
||||||
f.VMCacheEndpoint = defaultVMCacheEndpoint
|
f.VMCacheEndpoint = defaultVMCacheEndpoint
|
||||||
}
|
}
|
||||||
return oci.FactoryConfig{
|
return oci.FactoryConfig{
|
||||||
Template: f.Template,
|
Template: f.Template,
|
||||||
|
TemplatePath: f.TemplatePath,
|
||||||
VMCacheNumber: f.VMCacheNumber,
|
VMCacheNumber: f.VMCacheNumber,
|
||||||
VMCacheEndpoint: f.VMCacheEndpoint,
|
VMCacheEndpoint: f.VMCacheEndpoint,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -185,6 +185,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
factoryConfig := oci.FactoryConfig{
|
factoryConfig := oci.FactoryConfig{
|
||||||
|
TemplatePath: defaultTemplatePath,
|
||||||
VMCacheEndpoint: defaultVMCacheEndpoint,
|
VMCacheEndpoint: defaultVMCacheEndpoint,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -633,6 +634,7 @@ func TestMinimalRuntimeConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expectedFactoryConfig := oci.FactoryConfig{
|
expectedFactoryConfig := oci.FactoryConfig{
|
||||||
|
TemplatePath: defaultTemplatePath,
|
||||||
VMCacheEndpoint: defaultVMCacheEndpoint,
|
VMCacheEndpoint: defaultVMCacheEndpoint,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1419,6 +1421,7 @@ func TestUpdateRuntimeConfigurationFactoryConfig(t *testing.T) {
|
|||||||
config := oci.RuntimeConfig{}
|
config := oci.RuntimeConfig{}
|
||||||
expectedFactoryConfig := oci.FactoryConfig{
|
expectedFactoryConfig := oci.FactoryConfig{
|
||||||
Template: true,
|
Template: true,
|
||||||
|
TemplatePath: defaultTemplatePath,
|
||||||
VMCacheEndpoint: defaultVMCacheEndpoint,
|
VMCacheEndpoint: defaultVMCacheEndpoint,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,6 +124,7 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon
|
|||||||
}
|
}
|
||||||
factoryConfig := vf.Config{
|
factoryConfig := vf.Config{
|
||||||
Template: runtimeConfig.FactoryConfig.Template,
|
Template: runtimeConfig.FactoryConfig.Template,
|
||||||
|
TemplatePath: runtimeConfig.FactoryConfig.TemplatePath,
|
||||||
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
|
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
|
||||||
VMCacheEndpoint: runtimeConfig.FactoryConfig.VMCacheEndpoint,
|
VMCacheEndpoint: runtimeConfig.FactoryConfig.VMCacheEndpoint,
|
||||||
VMConfig: vc.VMConfig{
|
VMConfig: vc.VMConfig{
|
||||||
|
@ -25,10 +25,10 @@ var factoryLogger = logrus.FieldLogger(logrus.New())
|
|||||||
|
|
||||||
// Config is a collection of VM factory configurations.
|
// Config is a collection of VM factory configurations.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Template bool
|
Template bool
|
||||||
|
|
||||||
VMCache bool
|
VMCache bool
|
||||||
Cache uint
|
Cache uint
|
||||||
|
TemplatePath string
|
||||||
VMCacheEndpoint string
|
VMCacheEndpoint string
|
||||||
|
|
||||||
VMConfig vc.VMConfig
|
VMConfig vc.VMConfig
|
||||||
@ -70,12 +70,12 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
|
|||||||
} else {
|
} else {
|
||||||
if config.Template {
|
if config.Template {
|
||||||
if fetchOnly {
|
if fetchOnly {
|
||||||
b, err = template.Fetch(config.VMConfig)
|
b, err = template.Fetch(config.VMConfig, config.TemplatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
b, err = template.New(ctx, config.VMConfig)
|
b, err = template.New(ctx, config.VMConfig, config.TemplatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,7 @@ func TestNewFactory(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
config.Template = true
|
config.Template = true
|
||||||
|
config.TemplatePath = testDir
|
||||||
f, err = NewFactory(ctx, config, false)
|
f, err = NewFactory(ctx, config, false)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
f.CloseFactory(ctx)
|
f.CloseFactory(ctx)
|
||||||
@ -210,7 +211,7 @@ func TestFactoryGetVM(t *testing.T) {
|
|||||||
f.CloseFactory(ctx)
|
f.CloseFactory(ctx)
|
||||||
|
|
||||||
// template factory
|
// 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)
|
assert.Nil(err)
|
||||||
|
|
||||||
vm, err = f.GetVM(ctx, vmConfig)
|
vm, err = f.GetVM(ctx, vmConfig)
|
||||||
@ -222,10 +223,10 @@ func TestFactoryGetVM(t *testing.T) {
|
|||||||
f.CloseFactory(ctx)
|
f.CloseFactory(ctx)
|
||||||
|
|
||||||
// fetch template factory
|
// 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)
|
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)
|
assert.Error(err)
|
||||||
|
|
||||||
vm, err = f.GetVM(ctx, vmConfig)
|
vm, err = f.GetVM(ctx, vmConfig)
|
||||||
@ -249,7 +250,7 @@ func TestFactoryGetVM(t *testing.T) {
|
|||||||
f.CloseFactory(ctx)
|
f.CloseFactory(ctx)
|
||||||
|
|
||||||
// cache factory over template factory
|
// 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)
|
assert.Nil(err)
|
||||||
|
|
||||||
vm, err = f.GetVM(ctx, vmConfig)
|
vm, err = f.GetVM(ctx, vmConfig)
|
||||||
|
@ -16,7 +16,6 @@ import (
|
|||||||
pb "github.com/kata-containers/runtime/protocols/cache"
|
pb "github.com/kata-containers/runtime/protocols/cache"
|
||||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/factory/base"
|
"github.com/kata-containers/runtime/virtcontainers/factory/base"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/store"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type template struct {
|
type template struct {
|
||||||
@ -29,9 +28,8 @@ var templateWaitForAgent = 2 * time.Second
|
|||||||
|
|
||||||
// Fetch finds and returns a pre-built template factory.
|
// Fetch finds and returns a pre-built template factory.
|
||||||
// TODO: save template metadata and fetch from storage.
|
// TODO: save template metadata and fetch from storage.
|
||||||
func Fetch(config vc.VMConfig) (base.FactoryBase, error) {
|
func Fetch(config vc.VMConfig, templatePath string) (base.FactoryBase, error) {
|
||||||
statePath := store.RunVMStoragePath + "/template"
|
t := &template{templatePath, config}
|
||||||
t := &template{statePath, config}
|
|
||||||
|
|
||||||
err := t.checkTemplateVM()
|
err := t.checkTemplateVM()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -42,13 +40,12 @@ func Fetch(config vc.VMConfig) (base.FactoryBase, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new VM template factory.
|
// New creates a new VM template factory.
|
||||||
func New(ctx context.Context, config vc.VMConfig) (base.FactoryBase, error) {
|
func New(ctx context.Context, config vc.VMConfig, templatePath string) (base.FactoryBase, error) {
|
||||||
statePath := store.RunVMStoragePath + "/template"
|
t := &template{templatePath, config}
|
||||||
t := &template{statePath, config}
|
|
||||||
|
|
||||||
err := t.checkTemplateVM()
|
err := t.checkTemplateVM()
|
||||||
if err == nil {
|
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()
|
err = t.prepareTemplateFiles()
|
||||||
|
@ -46,7 +46,7 @@ func TestTemplateFactory(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// New
|
// New
|
||||||
f, err := New(ctx, vmConfig)
|
f, err := New(ctx, vmConfig, testDir)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
|
@ -98,6 +98,9 @@ type FactoryConfig struct {
|
|||||||
// Template enables VM templating support in VM factory.
|
// Template enables VM templating support in VM factory.
|
||||||
Template bool
|
Template bool
|
||||||
|
|
||||||
|
// TemplatePath specifies the path of template.
|
||||||
|
TemplatePath string
|
||||||
|
|
||||||
// VMCacheNumber specifies the the number of caches of VMCache.
|
// VMCacheNumber specifies the the number of caches of VMCache.
|
||||||
VMCacheNumber uint
|
VMCacheNumber uint
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user