factory: Make VMCache and VM templating can work together

Make VMCache and VM templating can work together.

Fixes: #1376

Signed-off-by: Hui Zhu <teawater@hyper.sh>
This commit is contained in:
Hui Zhu 2019-03-17 15:18:15 +08:00
parent fae022dc64
commit 343a0d35fe
4 changed files with 42 additions and 52 deletions

View File

@ -158,11 +158,10 @@ var initFactoryCommand = cli.Command{
return errors.New("invalid runtime config")
}
if runtimeConfig.FactoryConfig.VMCacheNumber > 0 {
factoryConfig := vf.Config{
Template: runtimeConfig.FactoryConfig.Template,
Cache: runtimeConfig.FactoryConfig.VMCacheNumber,
VMCache: true,
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
VMConfig: vc.VMConfig{
HypervisorType: runtimeConfig.HypervisorType,
HypervisorConfig: runtimeConfig.HypervisorConfig,
@ -172,6 +171,8 @@ var initFactoryCommand = cli.Command{
ProxyConfig: runtimeConfig.ProxyConfig,
},
}
if runtimeConfig.FactoryConfig.VMCacheNumber > 0 {
f, err := vf.NewFactory(ctx, factoryConfig, false)
if err != nil {
return err
@ -204,16 +205,6 @@ var initFactoryCommand = cli.Command{
}
if runtimeConfig.FactoryConfig.Template {
factoryConfig := vf.Config{
Template: true,
VMConfig: vc.VMConfig{
HypervisorType: runtimeConfig.HypervisorType,
HypervisorConfig: runtimeConfig.HypervisorConfig,
AgentType: runtimeConfig.AgentType,
AgentConfig: runtimeConfig.AgentConfig,
ProxyType: runtimeConfig.ProxyType,
},
}
kataLog.WithField("factory", factoryConfig).Info("create vm factory")
_, err := vf.NewFactory(ctx, factoryConfig, false)
if err != nil {
@ -222,8 +213,9 @@ var initFactoryCommand = cli.Command{
}
fmt.Fprintln(defaultOutputFile, "vm factory initialized")
} else {
kataLog.Error("vm factory is not enabled")
fmt.Fprintln(defaultOutputFile, "vm factory is not enabled")
const errstring = "vm factory or VMCache is not enabled"
kataLog.Error(errstring)
fmt.Fprintln(defaultOutputFile, errstring)
}
return nil

View File

@ -945,10 +945,6 @@ func checkNetNsConfig(config oci.RuntimeConfig) error {
// checkFactoryConfig ensures the VM factory configuration is valid.
func checkFactoryConfig(config oci.RuntimeConfig) error {
if config.FactoryConfig.Template && config.FactoryConfig.VMCacheNumber > 0 {
return errors.New("VM factory cannot work together with VM cache")
}
if config.FactoryConfig.Template {
if config.HypervisorConfig.InitrdPath == "" {
return errors.New("Factory option enable_template requires an initrd image")

View File

@ -122,7 +122,6 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon
if !runtimeConfig.FactoryConfig.Template && runtimeConfig.FactoryConfig.VMCacheNumber == 0 {
return
}
factoryConfig := vf.Config{
Template: runtimeConfig.FactoryConfig.Template,
VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0,
@ -142,14 +141,14 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon
kataUtilsLogger.WithField("factory", factoryConfig).Info("load vm factory")
f, err := vf.NewFactory(ctx, factoryConfig, true)
if err != nil {
if err != nil && !factoryConfig.VMCache {
kataUtilsLogger.WithError(err).Warn("load vm factory failed, about to create new one")
f, err = vf.NewFactory(ctx, factoryConfig, false)
}
if err != nil {
kataUtilsLogger.WithError(err).Warn("create vm factory failed")
return
}
}
vci.SetFactory(ctx, f)
}

View File

@ -61,6 +61,13 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
}
var b base.FactoryBase
if config.VMCache && config.Cache == 0 {
// For VMCache client
b, err = grpccache.New(ctx, config.VMCacheEndpoint)
if err != nil {
return nil, err
}
} else {
if config.Template {
if fetchOnly {
b, err = template.Fetch(config.VMConfig)
@ -73,11 +80,6 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
return nil, err
}
}
} else if config.VMCache && config.Cache == 0 {
b, err = grpccache.New(ctx, config.VMCacheEndpoint)
if err != nil {
return nil, err
}
} else {
b = direct.New(ctx, config.VMConfig)
}
@ -85,6 +87,7 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
if config.Cache > 0 {
b = cache.New(ctx, config.Cache, b)
}
}
return &factory{b}, nil
}