This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #303 from bergwolf/vmfactory
add vm factory support
- Loading branch information
Showing
49 changed files
with
2,387 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
vc "github.com/kata-containers/runtime/virtcontainers" | ||
vf "github.com/kata-containers/runtime/virtcontainers/factory" | ||
"github.com/kata-containers/runtime/virtcontainers/pkg/oci" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
var factorySubCmds = []cli.Command{ | ||
initFactoryCommand, | ||
destroyFactoryCommand, | ||
} | ||
|
||
var factoryCLICommand = cli.Command{ | ||
Name: "factory", | ||
Usage: "manage vm factory", | ||
Subcommands: factorySubCmds, | ||
Action: func(context *cli.Context) { | ||
cli.ShowSubcommandHelp(context) | ||
}, | ||
} | ||
|
||
var initFactoryCommand = cli.Command{ | ||
Name: "init", | ||
Usage: "initialize a VM factory based on kata-runtime configuration", | ||
Action: func(context *cli.Context) error { | ||
runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig) | ||
if !ok { | ||
return errors.New("invalid runtime config") | ||
} | ||
|
||
if runtimeConfig.FactoryConfig.Template { | ||
factoryConfig := vf.Config{ | ||
Template: true, | ||
VMConfig: vc.VMConfig{ | ||
HypervisorType: runtimeConfig.HypervisorType, | ||
HypervisorConfig: runtimeConfig.HypervisorConfig, | ||
AgentType: runtimeConfig.AgentType, | ||
AgentConfig: runtimeConfig.AgentConfig, | ||
}, | ||
} | ||
kataLog.WithField("factory", factoryConfig).Info("create vm factory") | ||
_, err := vf.NewFactory(factoryConfig, false) | ||
if err != nil { | ||
kataLog.WithError(err).Error("create vm factory failed") | ||
return err | ||
} | ||
fmt.Println("vm factory initialized") | ||
} else { | ||
kataLog.Error("vm factory is not enabled") | ||
fmt.Println("vm factory is not enabled") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var destroyFactoryCommand = cli.Command{ | ||
Name: "destroy", | ||
Usage: "destroy the VM factory", | ||
Action: func(context *cli.Context) error { | ||
runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig) | ||
if !ok { | ||
return errors.New("invalid runtime config") | ||
} | ||
|
||
if runtimeConfig.FactoryConfig.Template { | ||
factoryConfig := vf.Config{ | ||
Template: true, | ||
VMConfig: vc.VMConfig{ | ||
HypervisorType: runtimeConfig.HypervisorType, | ||
HypervisorConfig: runtimeConfig.HypervisorConfig, | ||
AgentType: runtimeConfig.AgentType, | ||
AgentConfig: runtimeConfig.AgentConfig, | ||
}, | ||
} | ||
kataLog.WithField("factory", factoryConfig).Info("load vm factory") | ||
f, err := vf.NewFactory(factoryConfig, true) | ||
if err != nil { | ||
kataLog.WithError(err).Error("load vm factory failed") | ||
// ignore error | ||
} else { | ||
f.CloseFactory() | ||
} | ||
} | ||
fmt.Println("vm factory destroyed") | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli" | ||
|
||
vc "github.com/kata-containers/runtime/virtcontainers" | ||
) | ||
|
||
func TestFactoryCLIFunctionNoRuntimeConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
app := cli.NewApp() | ||
ctx := cli.NewContext(app, nil, nil) | ||
app.Name = "foo" | ||
ctx.App.Metadata = map[string]interface{}{ | ||
"foo": "bar", | ||
} | ||
|
||
fn, ok := initFactoryCommand.Action.(func(context *cli.Context) error) | ||
assert.True(ok) | ||
err := fn(ctx) | ||
// no runtime config in the Metadata | ||
assert.Error(err) | ||
|
||
fn, ok = destroyFactoryCommand.Action.(func(context *cli.Context) error) | ||
assert.True(ok) | ||
err = fn(ctx) | ||
// no runtime config in the Metadata | ||
assert.Error(err) | ||
} | ||
|
||
func TestFactoryCLIFunctionInit(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
tmpdir, err := ioutil.TempDir("", "") | ||
assert.NoError(err) | ||
defer os.RemoveAll(tmpdir) | ||
|
||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true) | ||
assert.NoError(err) | ||
|
||
set := flag.NewFlagSet("", 0) | ||
|
||
set.String("console-socket", "", "") | ||
|
||
app := cli.NewApp() | ||
ctx := cli.NewContext(app, set, nil) | ||
app.Name = "foo" | ||
|
||
// No template | ||
ctx.App.Metadata = map[string]interface{}{ | ||
"runtimeConfig": runtimeConfig, | ||
} | ||
fn, ok := initFactoryCommand.Action.(func(context *cli.Context) error) | ||
assert.True(ok) | ||
err = fn(ctx) | ||
assert.Nil(err) | ||
|
||
// With template | ||
runtimeConfig.FactoryConfig.Template = true | ||
runtimeConfig.HypervisorType = vc.MockHypervisor | ||
runtimeConfig.AgentType = vc.NoopAgentType | ||
ctx.App.Metadata["runtimeConfig"] = runtimeConfig | ||
fn, ok = initFactoryCommand.Action.(func(context *cli.Context) error) | ||
assert.True(ok) | ||
err = fn(ctx) | ||
assert.Nil(err) | ||
} | ||
|
||
func TestFactoryCLIFunctionDestroy(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
tmpdir, err := ioutil.TempDir("", "") | ||
assert.NoError(err) | ||
defer os.RemoveAll(tmpdir) | ||
|
||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true) | ||
assert.NoError(err) | ||
|
||
set := flag.NewFlagSet("", 0) | ||
|
||
set.String("console-socket", "", "") | ||
|
||
app := cli.NewApp() | ||
ctx := cli.NewContext(app, set, nil) | ||
app.Name = "foo" | ||
|
||
// No template | ||
ctx.App.Metadata = map[string]interface{}{ | ||
"runtimeConfig": runtimeConfig, | ||
} | ||
fn, ok := destroyFactoryCommand.Action.(func(context *cli.Context) error) | ||
assert.True(ok) | ||
err = fn(ctx) | ||
assert.Nil(err) | ||
|
||
// With template | ||
runtimeConfig.FactoryConfig.Template = true | ||
runtimeConfig.HypervisorType = vc.MockHypervisor | ||
runtimeConfig.AgentType = vc.NoopAgentType | ||
ctx.App.Metadata["runtimeConfig"] = runtimeConfig | ||
fn, ok = destroyFactoryCommand.Action.(func(context *cli.Context) error) | ||
assert.True(ok) | ||
err = fn(ctx) | ||
assert.Nil(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.