From bd8f03a5ef50be8d18a3473d6aa94445b9e0f872 Mon Sep 17 00:00:00 2001 From: bin liu Date: Thu, 2 Jul 2020 17:15:03 +0800 Subject: [PATCH] runtime: remove agent abstraction This PR will delete agent abstraction and use Kata agent as the only one agent. Fixes: #377 Signed-off-by: bin liu --- src/runtime/cli/factory.go | 3 - src/runtime/cli/factory_test.go | 13 +- src/runtime/cli/kata-env.go | 23 +- src/runtime/cli/kata-env_test.go | 30 +-- src/runtime/cli/main_test.go | 1 - src/runtime/containerd-shim-v2/utils_test.go | 1 - src/runtime/pkg/katautils/config.go | 65 ++--- src/runtime/pkg/katautils/config_test.go | 28 -- src/runtime/pkg/katautils/create.go | 1 - src/runtime/pkg/katautils/create_test.go | 1 - src/runtime/virtcontainers/agent.go | 86 ++---- src/runtime/virtcontainers/agent_test.go | 108 -------- src/runtime/virtcontainers/api.go | 1 - src/runtime/virtcontainers/api_test.go | 159 +++++------ src/runtime/virtcontainers/container_test.go | 4 +- .../documentation/api/1.0/api.md | 24 -- .../virtcontainers/example_pod_run_test.go | 1 - .../factory/cache/cache_test.go | 3 +- .../factory/direct/direct_test.go | 3 +- src/runtime/virtcontainers/factory/factory.go | 8 - .../virtcontainers/factory/factory_test.go | 9 - .../factory/template/template_test.go | 1 - src/runtime/virtcontainers/iostream_test.go | 2 +- src/runtime/virtcontainers/kata_agent.go | 18 +- .../{noop_agent.go => mock_agent.go} | 102 +++---- src/runtime/virtcontainers/monitor_test.go | 4 +- src/runtime/virtcontainers/noop_agent_test.go | 248 ------------------ src/runtime/virtcontainers/persist.go | 25 +- .../virtcontainers/persist/api/config.go | 1 - src/runtime/virtcontainers/pkg/oci/utils.go | 9 +- .../virtcontainers/pkg/oci/utils_test.go | 2 - .../virtcontainers/pkg/vcmock/mock_test.go | 1 - src/runtime/virtcontainers/sandbox.go | 17 +- src/runtime/virtcontainers/sandbox_test.go | 50 ++-- src/runtime/virtcontainers/vm.go | 23 +- src/runtime/virtcontainers/vm_test.go | 7 +- 36 files changed, 263 insertions(+), 819 deletions(-) delete mode 100644 src/runtime/virtcontainers/agent_test.go rename src/runtime/virtcontainers/{noop_agent.go => mock_agent.go} (63%) delete mode 100644 src/runtime/virtcontainers/noop_agent_test.go diff --git a/src/runtime/cli/factory.go b/src/runtime/cli/factory.go index 2e1a3b843b..47d2d1f66c 100644 --- a/src/runtime/cli/factory.go +++ b/src/runtime/cli/factory.go @@ -166,7 +166,6 @@ var initFactoryCommand = cli.Command{ VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, - AgentType: runtimeConfig.AgentType, AgentConfig: runtimeConfig.AgentConfig, ProxyType: runtimeConfig.ProxyType, ProxyConfig: runtimeConfig.ProxyConfig, @@ -256,7 +255,6 @@ var destroyFactoryCommand = cli.Command{ VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, - AgentType: runtimeConfig.AgentType, AgentConfig: runtimeConfig.AgentConfig, ProxyType: runtimeConfig.ProxyType, ProxyConfig: runtimeConfig.ProxyConfig, @@ -314,7 +312,6 @@ var statusFactoryCommand = cli.Command{ VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, - AgentType: runtimeConfig.AgentType, AgentConfig: runtimeConfig.AgentConfig, ProxyType: runtimeConfig.ProxyType, ProxyConfig: runtimeConfig.ProxyConfig, diff --git a/src/runtime/cli/factory_test.go b/src/runtime/cli/factory_test.go index fbe1edc817..20ced45e8a 100644 --- a/src/runtime/cli/factory_test.go +++ b/src/runtime/cli/factory_test.go @@ -6,6 +6,7 @@ package main import ( + "context" "flag" "io/ioutil" "os" @@ -73,11 +74,19 @@ 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 ctx.App.Metadata["runtimeConfig"] = runtimeConfig fn, ok = initFactoryCommand.Action.(func(context *cli.Context) error) assert.True(ok) + + // config mock agent + stdCtx, err := cliContextToContext(ctx) + if err != nil { + stdCtx = context.Background() + } + stdCtx = vc.WithNewAgentFunc(stdCtx, vc.NewMockAgent) + ctx.App.Metadata["context"] = stdCtx + err = fn(ctx) assert.Nil(err) } @@ -109,7 +118,6 @@ func TestFactoryCLIFunctionDestroy(t *testing.T) { // 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) @@ -145,7 +153,6 @@ func TestFactoryCLIFunctionStatus(t *testing.T) { // With template runtimeConfig.FactoryConfig.Template = true runtimeConfig.HypervisorType = vc.MockHypervisor - runtimeConfig.AgentType = vc.NoopAgentType ctx.App.Metadata["runtimeConfig"] = runtimeConfig err = fn(ctx) assert.Nil(err) diff --git a/src/runtime/cli/kata-env.go b/src/runtime/cli/kata-env.go index 1d3bbcc437..3710742aaa 100644 --- a/src/runtime/cli/kata-env.go +++ b/src/runtime/cli/kata-env.go @@ -114,7 +114,6 @@ type ProxyInfo struct { // AgentInfo stores agent details type AgentInfo struct { - Type string Debug bool Trace bool TraceMode string @@ -296,23 +295,13 @@ func getCommandVersion(cmd string) (string, error) { } func getAgentInfo(config oci.RuntimeConfig) (AgentInfo, error) { - agent := AgentInfo{ - Type: string(config.AgentType), - } + agent := AgentInfo{} - switch config.AgentType { - case vc.KataContainersAgent: - agentConfig, ok := config.AgentConfig.(vc.KataAgentConfig) - if !ok { - return AgentInfo{}, errors.New("cannot determine Kata agent config") - } - agent.Debug = agentConfig.Debug - agent.Trace = agentConfig.Trace - agent.TraceMode = agentConfig.TraceMode - agent.TraceType = agentConfig.TraceType - default: - // Nothing useful to report for the other agent types - } + agentConfig := config.AgentConfig + agent.Debug = agentConfig.Debug + agent.Trace = agentConfig.Trace + agent.TraceMode = agentConfig.TraceMode + agent.TraceType = agentConfig.TraceType return agent, nil } diff --git a/src/runtime/cli/kata-env_test.go b/src/runtime/cli/kata-env_test.go index 4b60d6dc23..69474c1dbb 100644 --- a/src/runtime/cli/kata-env_test.go +++ b/src/runtime/cli/kata-env_test.go @@ -198,13 +198,8 @@ func getExpectedNetmonDetails(config oci.RuntimeConfig) (NetmonInfo, error) { func getExpectedAgentDetails(config oci.RuntimeConfig) (AgentInfo, error) { - agentConfig, ok := config.AgentConfig.(vc.KataAgentConfig) - if !ok { - return AgentInfo{}, fmt.Errorf("expected KataAgentConfig, got %T", config.AgentConfig) - } - + agentConfig := config.AgentConfig return AgentInfo{ - Type: string(config.AgentType), Debug: agentConfig.Debug, Trace: agentConfig.Trace, @@ -490,7 +485,6 @@ func TestEnvGetEnvInfo(t *testing.T) { runtimeDebug = toggle runtimeTrace = toggle agentDebug = toggle - agentTrace = toggle configFile, config, err := makeRuntimeConfig(tmpdir) assert.NoError(t, err) @@ -536,13 +530,8 @@ func TestEnvGetEnvInfoAgentError(t *testing.T) { assert.NoError(err) defer os.RemoveAll(tmpdir) - configFile, config, err := makeRuntimeConfig(tmpdir) + _, _, err = makeRuntimeConfig(tmpdir) assert.NoError(err) - - config.AgentConfig = "invalid agent config" - - _, err = getEnvInfo(configFile, config) - assert.Error(err) } func TestEnvGetEnvInfoNoOSRelease(t *testing.T) { @@ -728,8 +717,7 @@ func TestEnvGetAgentInfo(t *testing.T) { assert.Equal(t, expectedAgent, agent) - agentConfig, ok := config.AgentConfig.(vc.KataAgentConfig) - assert.True(t, ok) + agentConfig := config.AgentConfig agentConfig.Debug = true config.AgentConfig = agentConfig @@ -746,10 +734,6 @@ func TestEnvGetAgentInfo(t *testing.T) { assert.True(t, agent.Trace) assert.Equal(t, agent.TraceMode, "traceMode") assert.Equal(t, agent.TraceType, "traceType") - - config.AgentConfig = "I am the wrong type" - _, err = getAgentInfo(config) - assert.Error(t, err) } func testEnvShowTOMLSettings(t *testing.T, tmpdir string, tmpfile *os.File) error { @@ -777,9 +761,7 @@ func testEnvShowTOMLSettings(t *testing.T, tmpdir string, tmpfile *os.File) erro Debug: false, } - agent := AgentInfo{ - Type: "agent-type", - } + agent := AgentInfo{} expectedHostDetails, err := getExpectedHostDetails(tmpdir) assert.NoError(t, err) @@ -839,9 +821,7 @@ func testEnvShowJSONSettings(t *testing.T, tmpdir string, tmpfile *os.File) erro Debug: false, } - agent := AgentInfo{ - Type: "agent-type", - } + agent := AgentInfo{} expectedHostDetails, err := getExpectedHostDetails(tmpdir) assert.NoError(t, err) diff --git a/src/runtime/cli/main_test.go b/src/runtime/cli/main_test.go index ba2fbfb52d..58374fa800 100644 --- a/src/runtime/cli/main_test.go +++ b/src/runtime/cli/main_test.go @@ -242,7 +242,6 @@ func newTestRuntimeConfig(dir, consolePath string, create bool) (oci.RuntimeConf return oci.RuntimeConfig{ HypervisorType: vc.QemuHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: vc.KataContainersAgent, ProxyType: vc.KataProxyType, Console: consolePath, }, nil diff --git a/src/runtime/containerd-shim-v2/utils_test.go b/src/runtime/containerd-shim-v2/utils_test.go index 5cf14502b8..eb813e014b 100644 --- a/src/runtime/containerd-shim-v2/utils_test.go +++ b/src/runtime/containerd-shim-v2/utils_test.go @@ -209,7 +209,6 @@ func newTestRuntimeConfig(dir, consolePath string, create bool) (oci.RuntimeConf return oci.RuntimeConfig{ HypervisorType: vc.QemuHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: vc.KataContainersAgent, ProxyType: vc.KataBuiltInProxyType, Console: consolePath, }, nil diff --git a/src/runtime/pkg/katautils/config.go b/src/runtime/pkg/katautils/config.go index a84b2c6142..5ab2d28995 100644 --- a/src/runtime/pkg/katautils/config.go +++ b/src/runtime/pkg/katautils/config.go @@ -25,7 +25,6 @@ import ( const ( defaultHypervisor = vc.QemuHypervisor - defaultAgent = vc.KataContainersAgent ) var ( @@ -56,9 +55,6 @@ const ( // supported proxy component types kataProxyTableType = "kata" - // supported agent component types - kataAgentTableType = "kata" - // the maximum amount of PCI bridges that can be cold plugged in a VM maxPCIBridges uint32 = 5 ) @@ -932,37 +928,24 @@ func updateRuntimeConfigProxy(configPath string, tomlConf tomlConfig, config *oc func updateRuntimeConfigAgent(configPath string, tomlConf tomlConfig, config *oci.RuntimeConfig, builtIn bool) error { if builtIn { - var agentConfig vc.KataAgentConfig - - // If the agent config section isn't a Kata one, just default - // to everything being disabled. - agentConfig, _ = config.AgentConfig.(vc.KataAgentConfig) - - config.AgentType = vc.KataContainersAgent config.AgentConfig = vc.KataAgentConfig{ LongLiveConn: true, UseVSock: config.HypervisorConfig.UseVSock, - Debug: agentConfig.Debug, - KernelModules: agentConfig.KernelModules, + Debug: config.AgentConfig.Debug, + KernelModules: config.AgentConfig.KernelModules, } return nil } - for k, agent := range tomlConf.Agent { - switch k { - case kataAgentTableType: - config.AgentType = vc.KataContainersAgent - config.AgentConfig = vc.KataAgentConfig{ - UseVSock: config.HypervisorConfig.UseVSock, - Debug: agent.debug(), - Trace: agent.trace(), - TraceMode: agent.traceMode(), - TraceType: agent.traceType(), - KernelModules: agent.kernelModules(), - } - default: - return fmt.Errorf("%s agent type is not supported", k) + for _, agent := range tomlConf.Agent { + config.AgentConfig = vc.KataAgentConfig{ + UseVSock: config.HypervisorConfig.UseVSock, + Debug: agent.debug(), + Trace: agent.trace(), + TraceMode: agent.traceMode(), + TraceType: agent.traceType(), + KernelModules: agent.kernelModules(), } } @@ -1005,19 +988,17 @@ func SetKernelParams(runtimeConfig *oci.RuntimeConfig) error { } // next, check for agent specific kernel params - if agentConfig, ok := runtimeConfig.AgentConfig.(vc.KataAgentConfig); ok { - err := vc.KataAgentSetDefaultTraceConfigOptions(&agentConfig) - if err != nil { + err := vc.KataAgentSetDefaultTraceConfigOptions(&runtimeConfig.AgentConfig) + if err != nil { + return err + } + + params := vc.KataAgentKernelParams(runtimeConfig.AgentConfig) + + for _, p := range params { + if err := runtimeConfig.AddKernelParam(p); err != nil { return err } - - params := vc.KataAgentKernelParams(agentConfig) - - for _, p := range params { - if err := runtimeConfig.AddKernelParam(p); err != nil { - return err - } - } } // now re-add the user-specified values so that they take priority. @@ -1106,19 +1087,16 @@ func GetDefaultHypervisorConfig() vc.HypervisorConfig { } func initConfig() (config oci.RuntimeConfig, err error) { - var defaultAgentConfig interface{} - err = config.InterNetworkModel.SetModel(defaultInterNetworkingModel) if err != nil { return oci.RuntimeConfig{}, err } - defaultAgentConfig = vc.KataAgentConfig{} + defaultAgentConfig := vc.KataAgentConfig{} config = oci.RuntimeConfig{ HypervisorType: defaultHypervisor, HypervisorConfig: GetDefaultHypervisorConfig(), - AgentType: defaultAgent, AgentConfig: defaultAgentConfig, ProxyType: defaultProxy, } @@ -1280,9 +1258,6 @@ func checkFactoryConfig(config oci.RuntimeConfig) error { if config.HypervisorType != vc.QemuHypervisor { return errors.New("VM cache just support qemu") } - if config.AgentType != vc.KataContainersAgent { - return errors.New("VM cache just support kata agent") - } } return nil diff --git a/src/runtime/pkg/katautils/config_test.go b/src/runtime/pkg/katautils/config_test.go index 3c345de4f2..f75591ae58 100644 --- a/src/runtime/pkg/katautils/config_test.go +++ b/src/runtime/pkg/katautils/config_test.go @@ -190,7 +190,6 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf HypervisorType: defaultHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: defaultAgent, AgentConfig: agentConfig, ProxyType: defaultProxy, @@ -609,7 +608,6 @@ func TestMinimalRuntimeConfig(t *testing.T) { HypervisorType: defaultHypervisor, HypervisorConfig: expectedHypervisorConfig, - AgentType: defaultAgent, AgentConfig: expectedAgentConfig, ProxyType: defaultProxy, @@ -1516,30 +1514,6 @@ func TestDefaultCPUFeatures(t *testing.T) { assert.Equal(cpuFeatures, h.cpuFeatures()) } -func TestUpdateRuntimeConfiguration(t *testing.T) { - assert := assert.New(t) - - assert.Equal(defaultAgent, vc.KataContainersAgent) - - config := oci.RuntimeConfig{} - - tomlConf := tomlConfig{ - Agent: map[string]agent{ - // force a non-default value - kataAgentTableType: {}, - }, - } - - assert.NotEqual(config.AgentType, vc.AgentType(kataAgentTableType)) - assert.NotEqual(config.AgentConfig, vc.KataAgentConfig{}) - - err := updateRuntimeConfig("", tomlConf, &config, false) - assert.NoError(err) - - assert.Equal(config.AgentType, vc.AgentType(kataAgentTableType)) - assert.Equal(config.AgentConfig, vc.KataAgentConfig{}) -} - func TestUpdateRuntimeConfigurationVMConfig(t *testing.T) { assert := assert.New(t) @@ -1589,8 +1563,6 @@ func TestUpdateRuntimeConfigurationFactoryConfig(t *testing.T) { func TestUpdateRuntimeConfigurationInvalidKernelParams(t *testing.T) { assert := assert.New(t) - assert.Equal(defaultAgent, vc.KataContainersAgent) - config := oci.RuntimeConfig{} tomlConf := tomlConfig{} diff --git a/src/runtime/pkg/katautils/create.go b/src/runtime/pkg/katautils/create.go index f58d687526..be9cbfce8d 100644 --- a/src/runtime/pkg/katautils/create.go +++ b/src/runtime/pkg/katautils/create.go @@ -64,7 +64,6 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, - AgentType: runtimeConfig.AgentType, AgentConfig: runtimeConfig.AgentConfig, ProxyType: runtimeConfig.ProxyType, ProxyConfig: runtimeConfig.ProxyConfig, diff --git a/src/runtime/pkg/katautils/create_test.go b/src/runtime/pkg/katautils/create_test.go index e376c129c4..69c89dbc8b 100644 --- a/src/runtime/pkg/katautils/create_test.go +++ b/src/runtime/pkg/katautils/create_test.go @@ -105,7 +105,6 @@ func newTestRuntimeConfig(dir, consolePath string, create bool) (oci.RuntimeConf return oci.RuntimeConfig{ HypervisorType: vc.QemuHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: vc.KataContainersAgent, ProxyType: vc.KataProxyType, Console: consolePath, }, nil diff --git a/src/runtime/virtcontainers/agent.go b/src/runtime/virtcontainers/agent.go index c0bcc71d8a..9a583d5b39 100644 --- a/src/runtime/virtcontainers/agent.go +++ b/src/runtime/virtcontainers/agent.go @@ -6,7 +6,6 @@ package virtcontainers import ( - "fmt" "syscall" "time" @@ -14,13 +13,29 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc" vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" - "github.com/mitchellh/mapstructure" specs "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/net/context" ) -// AgentType describes the type of guest agent a Sandbox should run. -type AgentType string +type newAgentFuncKey struct{} + +type newAgentFuncType func() agent + +// getAgentFunc used to pass mock agent creation func to CreateSandbox passed in `ctx` +func getNewAgentFunc(ctx context.Context) newAgentFuncType { + v := ctx.Value(newAgentFuncKey{}) + if v != nil { + if vv, ok := v.(newAgentFuncType); ok { + return vv + } + } + return newKataAgent +} + +// WithNewAgentFunc set newAgentFuncKey in `ctx` +func WithNewAgentFunc(ctx context.Context, f newAgentFuncType) context.Context { + return context.WithValue(ctx, newAgentFuncKey{}, f) +} // ProcessListOptions contains the options used to list running // processes inside the container @@ -39,12 +54,6 @@ type ProcessListOptions struct { type ProcessList []byte const ( - // NoopAgentType is the No-Op agent. - NoopAgentType AgentType = "noop" - - // KataContainersAgent is the Kata Containers agent. - KataContainersAgent AgentType = "kata" - // SocketTypeVSOCK is a VSOCK socket type for talking to an agent. SocketTypeVSOCK = "vsock" @@ -53,61 +62,6 @@ const ( SocketTypeUNIX = "unix" ) -// Set sets an agent type based on the input string. -func (agentType *AgentType) Set(value string) error { - switch value { - case "noop": - *agentType = NoopAgentType - return nil - case "kata": - *agentType = KataContainersAgent - return nil - default: - return fmt.Errorf("Unknown agent type %s", value) - } -} - -// String converts an agent type to a string. -func (agentType *AgentType) String() string { - switch *agentType { - case NoopAgentType: - return string(NoopAgentType) - case KataContainersAgent: - return string(KataContainersAgent) - default: - return "" - } -} - -// newAgent returns an agent from an agent type. -func newAgent(agentType AgentType) agent { - switch agentType { - case NoopAgentType: - return &noopAgent{} - case KataContainersAgent: - return &kataAgent{} - default: - return &noopAgent{} - } -} - -// newAgentConfig returns an agent config from a generic SandboxConfig interface. -func newAgentConfig(agentType AgentType, agentConfig interface{}) (interface{}, error) { - switch agentType { - case NoopAgentType: - return nil, nil - case KataContainersAgent: - var kataAgentConfig KataAgentConfig - err := mapstructure.Decode(agentConfig, &kataAgentConfig) - if err != nil { - return nil, err - } - return kataAgentConfig, nil - default: - return nil, nil - } -} - // agent is the virtcontainers agent interface. // Agents are running in the guest VM and handling // communications between the host and guest. @@ -117,7 +71,7 @@ type agent interface { // init(). // After init() is called, agent implementations should be initialized and ready // to handle all other Agent interface methods. - init(ctx context.Context, sandbox *Sandbox, config interface{}) (disableVMShutdown bool, err error) + init(ctx context.Context, sandbox *Sandbox, config KataAgentConfig) (disableVMShutdown bool, err error) // capabilities should return a structure that specifies the capabilities // supported by the agent. diff --git a/src/runtime/virtcontainers/agent_test.go b/src/runtime/virtcontainers/agent_test.go deleted file mode 100644 index 1045176aa9..0000000000 --- a/src/runtime/virtcontainers/agent_test.go +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright (c) 2016 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 -// - -package virtcontainers - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func testSetAgentType(t *testing.T, value string, expected AgentType) { - var agentType AgentType - assert := assert.New(t) - - err := (&agentType).Set(value) - assert.NoError(err) - assert.Equal(agentType, expected) -} - -func TestSetNoopAgentType(t *testing.T) { - testSetAgentType(t, "noop", NoopAgentType) -} - -func TestSetKataAgentType(t *testing.T) { - testSetAgentType(t, "kata", KataContainersAgent) -} - -func TestSetUnknownAgentType(t *testing.T) { - var agentType AgentType - assert := assert.New(t) - - err := (&agentType).Set("unknown") - assert.Error(err) - assert.NotEqual(agentType, NoopAgentType) -} - -func testStringFromAgentType(t *testing.T, agentType AgentType, expected string) { - agentTypeStr := (&agentType).String() - assert.Equal(t, agentTypeStr, expected) -} - -func TestStringFromNoopAgentType(t *testing.T) { - testStringFromAgentType(t, NoopAgentType, "noop") -} - -func TestStringFromKataAgentType(t *testing.T) { - testStringFromAgentType(t, KataContainersAgent, "kata") -} - -func TestStringFromUnknownAgentType(t *testing.T) { - var agentType AgentType - testStringFromAgentType(t, agentType, "") -} - -func testNewAgentFromAgentType(t *testing.T, agentType AgentType, expected agent) { - ag := newAgent(agentType) - assert.Exactly(t, ag, expected) -} - -func TestNewAgentFromNoopAgentType(t *testing.T) { - testNewAgentFromAgentType(t, NoopAgentType, &noopAgent{}) -} - -func TestNewAgentFromKataAgentType(t *testing.T) { - testNewAgentFromAgentType(t, KataContainersAgent, &kataAgent{}) -} - -func TestNewAgentFromUnknownAgentType(t *testing.T) { - var agentType AgentType - testNewAgentFromAgentType(t, agentType, &noopAgent{}) -} - -func testNewAgentConfig(t *testing.T, config SandboxConfig, expected interface{}) { - agentConfig, err := newAgentConfig(config.AgentType, config.AgentConfig) - assert.NoError(t, err) - assert.Exactly(t, agentConfig, expected) -} - -func TestNewAgentConfigFromNoopAgentType(t *testing.T) { - var agentConfig interface{} - - sandboxConfig := SandboxConfig{ - AgentType: NoopAgentType, - AgentConfig: agentConfig, - } - - testNewAgentConfig(t, sandboxConfig, agentConfig) -} - -func TestNewAgentConfigFromKataAgentType(t *testing.T) { - agentConfig := KataAgentConfig{UseVSock: true} - - sandboxConfig := SandboxConfig{ - AgentType: KataContainersAgent, - AgentConfig: agentConfig, - } - - testNewAgentConfig(t, sandboxConfig, agentConfig) -} - -func TestNewAgentConfigFromUnknownAgentType(t *testing.T) { - var agentConfig interface{} - - testNewAgentConfig(t, SandboxConfig{}, agentConfig) -} diff --git a/src/runtime/virtcontainers/api.go b/src/runtime/virtcontainers/api.go index 4a864b24fd..13d58d206e 100644 --- a/src/runtime/virtcontainers/api.go +++ b/src/runtime/virtcontainers/api.go @@ -382,7 +382,6 @@ func StatusSandbox(ctx context.Context, sandboxID string) (SandboxStatus, error) State: s.state, Hypervisor: s.config.HypervisorType, HypervisorConfig: s.config.HypervisorConfig, - Agent: s.config.AgentType, ContainersStatus: contStatusList, Annotations: s.config.Annotations, } diff --git a/src/runtime/virtcontainers/api_test.go b/src/runtime/virtcontainers/api_test.go index ab0a318623..5f63d4db92 100644 --- a/src/runtime/virtcontainers/api_test.go +++ b/src/runtime/virtcontainers/api_test.go @@ -31,6 +31,8 @@ const ( containerID = "1" ) +var newMockAgent = NewMockAgent + var sandboxAnnotations = map[string]string{ "sandbox.foo": "sandbox.bar", "sandbox.hello": "sandbox.world", @@ -112,13 +114,12 @@ func newTestSandboxConfigNoop() SandboxConfig { HypervisorType: MockHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: NoopAgentType, - Containers: []ContainerConfig{container}, Annotations: sandboxAnnotations, - ProxyType: NoopProxyType, + ProxyType: NoopProxyType, + AgentConfig: KataAgentConfig{}, } configFile := filepath.Join(bundlePath, "config.json") @@ -137,8 +138,6 @@ func newTestSandboxConfigNoop() SandboxConfig { func newTestSandboxConfigKataAgent() SandboxConfig { sandboxConfig := newTestSandboxConfigNoop() - sandboxConfig.AgentType = KataContainersAgent - sandboxConfig.AgentConfig = KataAgentConfig{} sandboxConfig.Containers = nil return sandboxConfig @@ -150,12 +149,15 @@ func TestCreateSandboxNoopAgentSuccessful(t *testing.T) { config := newTestSandboxConfigNoop() - p, err := CreateSandbox(context.Background(), config, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) s, ok := p.(*Sandbox) assert.True(ok) + assert.NotNil(s) + sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID()) _, err = os.Stat(sandboxDir) assert.NoError(err) @@ -189,7 +191,8 @@ func TestCreateSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) defer kataProxyMock.Stop() - p, err := CreateSandbox(context.Background(), config, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -206,7 +209,8 @@ func TestCreateSandboxFailing(t *testing.T) { config := SandboxConfig{} - p, err := CreateSandbox(context.Background(), config, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := CreateSandbox(ctx, config, nil) assert.Error(err) assert.Nil(p.(*Sandbox)) } @@ -215,7 +219,7 @@ func TestDeleteSandboxNoopAgentSuccessful(t *testing.T) { defer cleanUp() assert := assert.New(t) - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) config := newTestSandboxConfigNoop() p, err := CreateSandbox(ctx, config, nil) @@ -264,7 +268,7 @@ func TestDeleteSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) defer kataProxyMock.Stop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -287,7 +291,8 @@ func TestDeleteSandboxFailing(t *testing.T) { defer cleanUp() assert := assert.New(t) - p, err := DeleteSandbox(context.Background(), testSandboxID) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := DeleteSandbox(ctx, testSandboxID) assert.Error(err) assert.Nil(p) } @@ -298,7 +303,8 @@ func TestStartSandboxNoopAgentSuccessful(t *testing.T) { config := newTestSandboxConfigNoop() - p, _, err := createAndStartSandbox(context.Background(), config) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, _, err := createAndStartSandbox(ctx, config) assert.NoError(err) assert.NotNil(p) } @@ -330,7 +336,7 @@ func TestStartSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) defer kataProxyMock.Stop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, _, err := createAndStartSandbox(ctx, config) assert.NoError(err) assert.NotNil(p) @@ -348,7 +354,8 @@ func TestStartSandboxFailing(t *testing.T) { defer cleanUp() assert := assert.New(t) - p, err := StartSandbox(context.Background(), testSandboxID) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := StartSandbox(ctx, testSandboxID) assert.Error(err) assert.Nil(p) } @@ -362,7 +369,7 @@ func TestStopSandboxNoopAgentSuccessful(t *testing.T) { config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, _, err := createAndStartSandbox(ctx, config) assert.NoError(err) assert.NotNil(p) @@ -399,7 +406,7 @@ func TestStopSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) defer kataProxyMock.Stop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, _, err := createAndStartSandbox(ctx, config) assert.NoError(err) assert.NotNil(p) @@ -412,7 +419,8 @@ func TestStopSandboxKataAgentSuccessful(t *testing.T) { func TestStopSandboxFailing(t *testing.T) { defer cleanUp() - p, err := StopSandbox(context.Background(), testSandboxID, false) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := StopSandbox(ctx, testSandboxID, false) assert.Error(t, err) assert.Nil(t, p) } @@ -423,7 +431,8 @@ func TestRunSandboxNoopAgentSuccessful(t *testing.T) { config := newTestSandboxConfigNoop() - p, err := RunSandbox(context.Background(), config, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := RunSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -462,7 +471,7 @@ func TestRunSandboxKataAgentSuccessful(t *testing.T) { assert.NoError(err) defer kataProxyMock.Stop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := RunSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -484,7 +493,8 @@ func TestRunSandboxFailing(t *testing.T) { config := SandboxConfig{} - p, err := RunSandbox(context.Background(), config, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + p, err := RunSandbox(ctx, config, nil) assert.Error(err) assert.Nil(p) } @@ -495,7 +505,7 @@ func TestListSandboxSuccessful(t *testing.T) { config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -507,7 +517,8 @@ func TestListSandboxSuccessful(t *testing.T) { func TestListSandboxNoSandboxDirectory(t *testing.T) { defer cleanUp() - _, err := ListSandbox(context.Background()) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + _, err := ListSandbox(ctx) assert.NoError(t, err) } @@ -540,7 +551,6 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) { }, Hypervisor: MockHypervisor, HypervisorConfig: hypervisorConfig, - Agent: NoopAgentType, ContainersStatus: []ContainerStatus{ { ID: containerID, @@ -556,7 +566,7 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) { }, } - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -568,7 +578,7 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) { // value will be. expectedStatus.ContainersStatus[0].StartTime = status.ContainersStatus[0].StartTime - assert.Equal(status, expectedStatus) + assert.Equal(expectedStatus, status) } func TestStatusSandboxSuccessfulStateRunning(t *testing.T) { @@ -600,7 +610,6 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) { }, Hypervisor: MockHypervisor, HypervisorConfig: hypervisorConfig, - Agent: NoopAgentType, ContainersStatus: []ContainerStatus{ { ID: containerID, @@ -616,7 +625,7 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) { }, } - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -632,7 +641,7 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) { // value will be. expectedStatus.ContainersStatus[0].StartTime = status.ContainersStatus[0].StartTime - assert.Exactly(status, expectedStatus) + assert.Exactly(expectedStatus, status) } func TestStatusSandboxFailingFetchSandboxConfig(t *testing.T) { @@ -641,7 +650,7 @@ func TestStatusSandboxFailingFetchSandboxConfig(t *testing.T) { config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -659,7 +668,7 @@ func TestStatusPodSandboxFailingFetchSandboxState(t *testing.T) { config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -691,7 +700,7 @@ func TestCreateContainerSuccessful(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -720,7 +729,7 @@ func TestCreateContainerFailingNoSandbox(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -749,7 +758,7 @@ func TestDeleteContainerSuccessful(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -783,7 +792,9 @@ func TestDeleteContainerFailingNoSandbox(t *testing.T) { assert := assert.New(t) contID := "100" - c, err := DeleteContainer(context.Background(), testSandboxID, contID) + + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + c, err := DeleteContainer(ctx, testSandboxID, contID) assert.Error(err) assert.Nil(c) } @@ -795,7 +806,7 @@ func TestDeleteContainerFailingNoContainer(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -818,7 +829,7 @@ func TestStartContainerNoopAgentSuccessful(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, sandboxDir, err := createAndStartSandbox(ctx, config) assert.NoError(err) @@ -842,7 +853,8 @@ func TestStartContainerFailingNoSandbox(t *testing.T) { defer cleanUp() contID := "100" - c, err := StartContainer(context.Background(), testSandboxID, contID) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + c, err := StartContainer(ctx, testSandboxID, contID) assert.Error(t, err) assert.Nil(t, c) } @@ -854,7 +866,7 @@ func TestStartContainerFailingNoContainer(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -877,7 +889,7 @@ func TestStartContainerFailingSandboxNotStarted(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -912,7 +924,7 @@ func TestStopContainerNoopAgentSuccessful(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, sandboxDir, err := createAndStartSandbox(ctx, config) assert.NoError(err) @@ -944,7 +956,8 @@ func TestStopContainerFailingNoSandbox(t *testing.T) { defer cleanUp() contID := "100" - c, err := StopContainer(context.Background(), testSandboxID, contID) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + c, err := StopContainer(ctx, testSandboxID, contID) assert.Error(t, err) assert.Nil(t, c) } @@ -959,7 +972,7 @@ func TestStopContainerFailingNoContainer(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -982,7 +995,7 @@ func testKillContainerFromContReadySuccessful(t *testing.T, signal syscall.Signa contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, sandboxDir, err := createAndStartSandbox(ctx, config) assert.NoError(err) @@ -1020,7 +1033,7 @@ func TestEnterContainerNoopAgentSuccessful(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, sandboxDir, err := createAndStartSandbox(ctx, config) assert.NoError(err) @@ -1053,7 +1066,8 @@ func TestEnterContainerFailingNoSandbox(t *testing.T) { contID := "100" cmd := newBasicTestCmd() - _, c, _, err := EnterContainer(context.Background(), testSandboxID, contID, cmd) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + _, c, _, err := EnterContainer(ctx, testSandboxID, contID, cmd) assert.Error(err) assert.Nil(c) } @@ -1065,7 +1079,7 @@ func TestEnterContainerFailingNoContainer(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -1090,7 +1104,7 @@ func TestEnterContainerFailingContNotStarted(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, sandboxDir, err := createAndStartSandbox(ctx, config) assert.NoError(err) @@ -1120,7 +1134,7 @@ func TestStatusContainerSuccessful(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -1162,7 +1176,7 @@ func TestStatusContainerStateReady(t *testing.T) { cgroupPath, err := vccgroups.RenameCgroupPath(vccgroups.DefaultCgroupPath) assert.NoError(err) - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -1209,7 +1223,7 @@ func TestStatusContainerStateReady(t *testing.T) { // value will be. expectedStatus.StartTime = status.StartTime - assert.Exactly(status, expectedStatus) + assert.Exactly(expectedStatus, status) } func TestStatusContainerStateRunning(t *testing.T) { @@ -1223,7 +1237,7 @@ func TestStatusContainerStateRunning(t *testing.T) { cgroupPath, err := vccgroups.RenameCgroupPath(vccgroups.DefaultCgroupPath) assert.NoError(err) - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -1278,7 +1292,7 @@ func TestStatusContainerStateRunning(t *testing.T) { // value will be. expectedStatus.StartTime = status.StartTime - assert.Exactly(status, expectedStatus) + assert.Exactly(expectedStatus, status) } func TestStatusContainerFailing(t *testing.T) { @@ -1288,7 +1302,7 @@ func TestStatusContainerFailing(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -1307,7 +1321,7 @@ func TestStatsContainerFailing(t *testing.T) { contID := "100" config := newTestSandboxConfigNoop() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, err := CreateSandbox(ctx, config, nil) assert.NoError(err) assert.NotNil(p) @@ -1325,7 +1339,7 @@ func TestStatsContainer(t *testing.T) { assert := assert.New(t) contID := "100" - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) _, err := StatsContainer(ctx, "", "") assert.Error(err) @@ -1360,7 +1374,7 @@ func TestStatsContainer(t *testing.T) { stats, err := StatsContainer(ctx, pImpl.id, contID) assert.NoError(err) - assert.Equal(stats, ContainerStats{}) + assert.Equal(ContainerStats{}, stats) } func TestProcessListContainer(t *testing.T) { @@ -1374,7 +1388,7 @@ func TestProcessListContainer(t *testing.T) { Args: []string{"-ef"}, } - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) _, err := ProcessListContainer(ctx, "", "", options) assert.Error(err) @@ -1412,7 +1426,7 @@ func TestProcessListContainer(t *testing.T) { * Benchmarks */ -func createNewSandboxConfig(hType HypervisorType, aType AgentType, aConfig interface{}) SandboxConfig { +func createNewSandboxConfig(hType HypervisorType) SandboxConfig { hypervisorConfig := HypervisorConfig{ KernelPath: "/usr/share/kata-containers/vmlinux.container", ImagePath: "/usr/share/kata-containers/kata-containers.img", @@ -1426,8 +1440,7 @@ func createNewSandboxConfig(hType HypervisorType, aType AgentType, aConfig inter HypervisorType: hType, HypervisorConfig: hypervisorConfig, - AgentType: aType, - AgentConfig: aConfig, + AgentConfig: KataAgentConfig{}, NetworkConfig: netConfig, @@ -1466,7 +1479,7 @@ func createAndStartSandbox(ctx context.Context, config SandboxConfig) (sandbox V } func createStartStopDeleteSandbox(b *testing.B, sandboxConfig SandboxConfig) { - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, _, err := createAndStartSandbox(ctx, sandboxConfig) if p == nil || err != nil { @@ -1488,14 +1501,14 @@ func createStartStopDeleteSandbox(b *testing.B, sandboxConfig SandboxConfig) { func BenchmarkCreateStartStopDeleteSandboxQemuHypervisorNoopAgentNetworkNoop(b *testing.B) { for i := 0; i < b.N; i++ { - sandboxConfig := createNewSandboxConfig(QemuHypervisor, NoopAgentType, nil) + sandboxConfig := createNewSandboxConfig(QemuHypervisor) createStartStopDeleteSandbox(b, sandboxConfig) } } func BenchmarkCreateStartStopDeleteSandboxMockHypervisorNoopAgentNetworkNoop(b *testing.B) { for i := 0; i < b.N; i++ { - sandboxConfig := createNewSandboxConfig(MockHypervisor, NoopAgentType, nil) + sandboxConfig := createNewSandboxConfig(MockHypervisor) createStartStopDeleteSandbox(b, sandboxConfig) } } @@ -1505,8 +1518,7 @@ func TestFetchSandbox(t *testing.T) { config := newTestSandboxConfigNoop() - ctx := context.Background() - + ctx := WithNewAgentFunc(context.Background(), newMockAgent) s, err := CreateSandbox(ctx, config, nil) assert.NoError(t, err) assert.NotNil(t, s) @@ -1523,8 +1535,7 @@ func TestFetchStatefulSandbox(t *testing.T) { config.Stateful = true - ctx := context.Background() - + ctx := WithNewAgentFunc(context.Background(), newMockAgent) s, err := CreateSandbox(ctx, config, nil) assert.NoError(t, err) assert.NotNil(t, s) @@ -1537,7 +1548,8 @@ func TestFetchStatefulSandbox(t *testing.T) { func TestFetchNonExistingSandbox(t *testing.T) { defer cleanUp() - _, err := FetchSandbox(context.Background(), "some-non-existing-sandbox-name") + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + _, err := FetchSandbox(ctx, "some-non-existing-sandbox-name") assert.NotNil(t, err, "fetch non-existing sandbox should fail") } @@ -1546,7 +1558,8 @@ func TestReleaseSandbox(t *testing.T) { config := newTestSandboxConfigNoop() - s, err := CreateSandbox(context.Background(), config, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + s, err := CreateSandbox(ctx, config, nil) assert.NoError(t, err) assert.NotNil(t, s) @@ -1561,7 +1574,7 @@ func TestUpdateContainer(t *testing.T) { defer cleanUp() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) period := uint64(1000) quota := int64(2000) @@ -1614,7 +1627,7 @@ func TestPauseResumeContainer(t *testing.T) { defer cleanUp() - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) assert := assert.New(t) err := PauseContainer(ctx, "", "") @@ -1669,7 +1682,7 @@ func TestNetworkOperation(t *testing.T) { } inf.IPAddresses = append(inf.IPAddresses, &ip) - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) _, err := AddInterface(ctx, "", inf) assert.Error(err) @@ -1710,7 +1723,7 @@ func TestCleanupContainer(t *testing.T) { config := newTestSandboxConfigNoop() assert := assert.New(t) - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) p, _, err := createAndStartSandbox(ctx, config) if p == nil || err != nil { diff --git a/src/runtime/virtcontainers/container_test.go b/src/runtime/virtcontainers/container_test.go index 27457391d5..c4a98834da 100644 --- a/src/runtime/virtcontainers/container_test.go +++ b/src/runtime/virtcontainers/container_test.go @@ -308,7 +308,7 @@ func TestContainerAddDriveDir(t *testing.T) { id: testSandboxID, devManager: manager.NewDeviceManager(manager.VirtioSCSI, false, "", nil), hypervisor: &mockHypervisor{}, - agent: &noopAgent{}, + agent: &mockAgent{}, config: &SandboxConfig{ HypervisorConfig: HypervisorConfig{ DisableBlockDeviceUse: false, @@ -364,7 +364,7 @@ func TestContainerRootfsPath(t *testing.T) { sandbox := &Sandbox{ ctx: context.Background(), id: "rootfstestsandbox", - agent: &noopAgent{}, + agent: &mockAgent{}, hypervisor: &mockHypervisor{}, config: &SandboxConfig{ HypervisorConfig: HypervisorConfig{ diff --git a/src/runtime/virtcontainers/documentation/api/1.0/api.md b/src/runtime/virtcontainers/documentation/api/1.0/api.md index b690f193b5..11457663b1 100644 --- a/src/runtime/virtcontainers/documentation/api/1.0/api.md +++ b/src/runtime/virtcontainers/documentation/api/1.0/api.md @@ -36,7 +36,6 @@ sandbox lifecycle through the rest of the [sandbox API](#sandbox-functions). * [`Resources`](#resources) * [`HypervisorType`](#hypervisortype) * [`HypervisorConfig`](#hypervisorconfig) - * [`AgentType`](#agenttype) * [`ProxyType`](#proxytype) * [`ProxyConfig`](#proxyconfig) * [`ShimType`](#shimtype) @@ -66,7 +65,6 @@ type SandboxConfig struct { HypervisorType HypervisorType HypervisorConfig HypervisorConfig - AgentType AgentType AgentConfig interface{} ProxyType ProxyType @@ -201,27 +199,6 @@ type HypervisorConfig struct { } ``` -##### `AgentType` -```Go -// AgentType describes the type of guest agent a Sandbox should run. -type AgentType string - -const ( - // NoopAgentType is the No-Op agent. - NoopAgentType AgentType = "noop" - - // KataContainersAgent is the Kata Containers agent. - KataContainersAgent AgentType = "kata" - - // SocketTypeVSOCK is a VSOCK socket type for talking to an agent. - SocketTypeVSOCK = "vsock" - - // SocketTypeUNIX is a UNIX socket type for talking to an agent. - // It typically means the agent is living behind a host proxy. - SocketTypeUNIX = "unix" -) -``` - ##### `ProxyType` ```Go // ProxyType describes a proxy type. @@ -891,7 +868,6 @@ func Example_createAndStartSandbox() { HypervisorType: vc.QemuHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: vc.KataContainersAgent AgentConfig: agConfig, Containers: []vc.ContainerConfig{container}, diff --git a/src/runtime/virtcontainers/example_pod_run_test.go b/src/runtime/virtcontainers/example_pod_run_test.go index 336bb29b90..ae2bc88b64 100644 --- a/src/runtime/virtcontainers/example_pod_run_test.go +++ b/src/runtime/virtcontainers/example_pod_run_test.go @@ -58,7 +58,6 @@ func Example_createAndStartSandbox() { HypervisorType: vc.QemuHypervisor, HypervisorConfig: hypervisorConfig, - AgentType: vc.KataContainersAgent, AgentConfig: agConfig, Containers: []vc.ContainerConfig{container}, diff --git a/src/runtime/virtcontainers/factory/cache/cache_test.go b/src/runtime/virtcontainers/factory/cache/cache_test.go index 9239278d63..6a0e22ab8c 100644 --- a/src/runtime/virtcontainers/factory/cache/cache_test.go +++ b/src/runtime/virtcontainers/factory/cache/cache_test.go @@ -28,12 +28,11 @@ func TestTemplateFactory(t *testing.T) { } vmConfig := vc.VMConfig{ HypervisorType: vc.MockHypervisor, - AgentType: vc.NoopAgentType, ProxyType: vc.NoopProxyType, HypervisorConfig: hyperConfig, } - ctx := context.Background() + ctx := vc.WithNewAgentFunc(context.Background(), vc.NewMockAgent) // New f := New(ctx, 2, direct.New(ctx, vmConfig)) diff --git a/src/runtime/virtcontainers/factory/direct/direct_test.go b/src/runtime/virtcontainers/factory/direct/direct_test.go index 4b14e34a8c..9e091179c4 100644 --- a/src/runtime/virtcontainers/factory/direct/direct_test.go +++ b/src/runtime/virtcontainers/factory/direct/direct_test.go @@ -27,12 +27,11 @@ func TestTemplateFactory(t *testing.T) { } vmConfig := vc.VMConfig{ HypervisorType: vc.MockHypervisor, - AgentType: vc.NoopAgentType, ProxyType: vc.NoopProxyType, HypervisorConfig: hyperConfig, } - ctx := context.Background() + ctx := vc.WithNewAgentFunc(context.Background(), vc.NewMockAgent) // New f := New(ctx, vmConfig) diff --git a/src/runtime/virtcontainers/factory/factory.go b/src/runtime/virtcontainers/factory/factory.go index cc309b06c5..fb6e609991 100644 --- a/src/runtime/virtcontainers/factory/factory.go +++ b/src/runtime/virtcontainers/factory/factory.go @@ -121,10 +121,6 @@ func checkVMConfig(config1, config2 vc.VMConfig) error { return fmt.Errorf("hypervisor type does not match: %s vs. %s", config1.HypervisorType, config2.HypervisorType) } - if config1.AgentType != config2.AgentType { - return fmt.Errorf("agent type does not match: %s vs. %s", config1.AgentType, config2.AgentType) - } - // check hypervisor config details resetHypervisorConfig(&config1) resetHypervisorConfig(&config2) @@ -143,10 +139,6 @@ func (f *factory) checkConfig(config vc.VMConfig) error { } func (f *factory) validateNewVMConfig(config vc.VMConfig) error { - if len(config.AgentType.String()) == 0 { - return fmt.Errorf("Missing agent type") - } - if len(config.ProxyType.String()) == 0 { return fmt.Errorf("Missing proxy type") } diff --git a/src/runtime/virtcontainers/factory/factory_test.go b/src/runtime/virtcontainers/factory/factory_test.go index 6913854454..880691491c 100644 --- a/src/runtime/virtcontainers/factory/factory_test.go +++ b/src/runtime/virtcontainers/factory/factory_test.go @@ -33,7 +33,6 @@ func TestNewFactory(t *testing.T) { config.VMConfig = vc.VMConfig{ HypervisorType: vc.MockHypervisor, - AgentType: vc.NoopAgentType, ProxyType: vc.NoopProxyType, } @@ -122,7 +121,6 @@ func TestVMConfigValid(t *testing.T) { err := f.validateNewVMConfig(config) assert.NotNil(err) - config.AgentType = vc.NoopAgentType err = f.validateNewVMConfig(config) assert.NotNil(err) @@ -148,11 +146,6 @@ func TestCheckVMConfig(t *testing.T) { err = checkVMConfig(config1, config2) assert.Nil(err) - config1.AgentType = vc.NoopAgentType - err = checkVMConfig(config1, config2) - assert.Error(err) - - config2.AgentType = vc.NoopAgentType err = checkVMConfig(config1, config2) assert.Nil(err) @@ -187,7 +180,6 @@ func TestFactoryGetVM(t *testing.T) { vmConfig := vc.VMConfig{ HypervisorType: vc.MockHypervisor, HypervisorConfig: hyperConfig, - AgentType: vc.NoopAgentType, ProxyType: vc.NoopProxyType, } @@ -335,7 +327,6 @@ func TestDeepCompare(t *testing.T) { ctx := context.Background() config.VMConfig = vc.VMConfig{ HypervisorType: vc.MockHypervisor, - AgentType: vc.NoopAgentType, ProxyType: vc.NoopProxyType, } testDir := fs.MockStorageRootPath() diff --git a/src/runtime/virtcontainers/factory/template/template_test.go b/src/runtime/virtcontainers/factory/template/template_test.go index a444b5d1bc..9623cd5c19 100644 --- a/src/runtime/virtcontainers/factory/template/template_test.go +++ b/src/runtime/virtcontainers/factory/template/template_test.go @@ -38,7 +38,6 @@ func TestTemplateFactory(t *testing.T) { vmConfig := vc.VMConfig{ HypervisorType: vc.MockHypervisor, HypervisorConfig: hyperConfig, - AgentType: vc.NoopAgentType, ProxyType: vc.NoopProxyType, } diff --git a/src/runtime/virtcontainers/iostream_test.go b/src/runtime/virtcontainers/iostream_test.go index a7c97ca6c6..fb098471e6 100644 --- a/src/runtime/virtcontainers/iostream_test.go +++ b/src/runtime/virtcontainers/iostream_test.go @@ -13,7 +13,7 @@ import ( func TestIOStream(t *testing.T) { hConfig := newHypervisorConfig(nil, nil) - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{}, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, []ContainerConfig{}, nil) assert.NoError(t, err) defer cleanUp() diff --git a/src/runtime/virtcontainers/kata_agent.go b/src/runtime/virtcontainers/kata_agent.go index 093ab10c97..cca82739dc 100644 --- a/src/runtime/virtcontainers/kata_agent.go +++ b/src/runtime/virtcontainers/kata_agent.go @@ -131,6 +131,11 @@ const ( grpcGetMetricsRequest = "grpc.GetMetricsRequest" ) +// newKataAgent returns an agent from an agent type. +func newKataAgent() agent { + return &kataAgent{} +} + // The function is declared this way for mocking in unit tests var kataHostSharedDir = func() string { if rootless.IsRootless() { @@ -316,21 +321,16 @@ func (k *kataAgent) handleTraceSettings(config KataAgentConfig) bool { return disableVMShutdown } -func (k *kataAgent) init(ctx context.Context, sandbox *Sandbox, config interface{}) (disableVMShutdown bool, err error) { +func (k *kataAgent) init(ctx context.Context, sandbox *Sandbox, config KataAgentConfig) (disableVMShutdown bool, err error) { // save k.ctx = sandbox.ctx span, _ := k.trace("init") defer span.Finish() - switch c := config.(type) { - case KataAgentConfig: - disableVMShutdown = k.handleTraceSettings(c) - k.keepConn = c.LongLiveConn - k.kmodules = c.KernelModules - default: - return false, vcTypes.ErrInvalidConfigType - } + disableVMShutdown = k.handleTraceSettings(config) + k.keepConn = config.LongLiveConn + k.kmodules = config.KernelModules k.proxy, err = newProxy(sandbox.config.ProxyType) if err != nil { diff --git a/src/runtime/virtcontainers/noop_agent.go b/src/runtime/virtcontainers/mock_agent.go similarity index 63% rename from src/runtime/virtcontainers/noop_agent.go rename to src/runtime/virtcontainers/mock_agent.go index 2a97dc8651..9401fabb7b 100644 --- a/src/runtime/virtcontainers/noop_agent.go +++ b/src/runtime/virtcontainers/mock_agent.go @@ -17,230 +17,234 @@ import ( "golang.org/x/net/context" ) -// noopAgent a.k.a. NO-OP Agent is an empty Agent implementation, for testing and +// mockAgent is an empty Agent implementation, for testing and // mocking purposes. -type noopAgent struct { +type mockAgent struct { +} + +func NewMockAgent() agent { + return &mockAgent{} } //start the proxy to watch the vm console. It does nothing. -func (n *noopAgent) startProxy(sandbox *Sandbox) error { +func (n *mockAgent) startProxy(sandbox *Sandbox) error { return nil } // init initializes the Noop agent, i.e. it does nothing. -func (n *noopAgent) init(ctx context.Context, sandbox *Sandbox, config interface{}) (bool, error) { +func (n *mockAgent) init(ctx context.Context, sandbox *Sandbox, config KataAgentConfig) (bool, error) { return false, nil } -func (n *noopAgent) longLiveConn() bool { +func (n *mockAgent) longLiveConn() bool { return false } // createSandbox is the Noop agent sandbox creation implementation. It does nothing. -func (n *noopAgent) createSandbox(sandbox *Sandbox) error { +func (n *mockAgent) createSandbox(sandbox *Sandbox) error { return nil } // capabilities returns empty capabilities, i.e no capabilties are supported. -func (n *noopAgent) capabilities() types.Capabilities { +func (n *mockAgent) capabilities() types.Capabilities { return types.Capabilities{} } // disconnect is the Noop agent connection closer. It does nothing. -func (n *noopAgent) disconnect() error { +func (n *mockAgent) disconnect() error { return nil } // exec is the Noop agent command execution implementation. It does nothing. -func (n *noopAgent) exec(sandbox *Sandbox, c Container, cmd types.Cmd) (*Process, error) { +func (n *mockAgent) exec(sandbox *Sandbox, c Container, cmd types.Cmd) (*Process, error) { return nil, nil } // startSandbox is the Noop agent Sandbox starting implementation. It does nothing. -func (n *noopAgent) startSandbox(sandbox *Sandbox) error { +func (n *mockAgent) startSandbox(sandbox *Sandbox) error { return nil } // stopSandbox is the Noop agent Sandbox stopping implementation. It does nothing. -func (n *noopAgent) stopSandbox(sandbox *Sandbox) error { +func (n *mockAgent) stopSandbox(sandbox *Sandbox) error { return nil } // createContainer is the Noop agent Container creation implementation. It does nothing. -func (n *noopAgent) createContainer(sandbox *Sandbox, c *Container) (*Process, error) { +func (n *mockAgent) createContainer(sandbox *Sandbox, c *Container) (*Process, error) { return &Process{}, nil } // startContainer is the Noop agent Container starting implementation. It does nothing. -func (n *noopAgent) startContainer(sandbox *Sandbox, c *Container) error { +func (n *mockAgent) startContainer(sandbox *Sandbox, c *Container) error { return nil } // stopContainer is the Noop agent Container stopping implementation. It does nothing. -func (n *noopAgent) stopContainer(sandbox *Sandbox, c Container) error { +func (n *mockAgent) stopContainer(sandbox *Sandbox, c Container) error { return nil } // signalProcess is the Noop agent Container signaling implementation. It does nothing. -func (n *noopAgent) signalProcess(c *Container, processID string, signal syscall.Signal, all bool) error { +func (n *mockAgent) signalProcess(c *Container, processID string, signal syscall.Signal, all bool) error { return nil } // processListContainer is the Noop agent Container ps implementation. It does nothing. -func (n *noopAgent) processListContainer(sandbox *Sandbox, c Container, options ProcessListOptions) (ProcessList, error) { +func (n *mockAgent) processListContainer(sandbox *Sandbox, c Container, options ProcessListOptions) (ProcessList, error) { return nil, nil } // updateContainer is the Noop agent Container update implementation. It does nothing. -func (n *noopAgent) updateContainer(sandbox *Sandbox, c Container, resources specs.LinuxResources) error { +func (n *mockAgent) updateContainer(sandbox *Sandbox, c Container, resources specs.LinuxResources) error { return nil } // memHotplugByProbe is the Noop agent notify meomory hotplug event via probe interface implementation. It does nothing. -func (n *noopAgent) memHotplugByProbe(addr uint64, sizeMB uint32, memorySectionSizeMB uint32) error { +func (n *mockAgent) memHotplugByProbe(addr uint64, sizeMB uint32, memorySectionSizeMB uint32) error { return nil } // onlineCPUMem is the Noop agent Container online CPU and Memory implementation. It does nothing. -func (n *noopAgent) onlineCPUMem(cpus uint32, cpuOnly bool) error { +func (n *mockAgent) onlineCPUMem(cpus uint32, cpuOnly bool) error { return nil } // updateInterface is the Noop agent Interface update implementation. It does nothing. -func (n *noopAgent) updateInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { +func (n *mockAgent) updateInterface(inf *vcTypes.Interface) (*vcTypes.Interface, error) { return nil, nil } // listInterfaces is the Noop agent Interfaces list implementation. It does nothing. -func (n *noopAgent) listInterfaces() ([]*vcTypes.Interface, error) { +func (n *mockAgent) listInterfaces() ([]*vcTypes.Interface, error) { return nil, nil } // updateRoutes is the Noop agent Routes update implementation. It does nothing. -func (n *noopAgent) updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) { +func (n *mockAgent) updateRoutes(routes []*vcTypes.Route) ([]*vcTypes.Route, error) { return nil, nil } // listRoutes is the Noop agent Routes list implementation. It does nothing. -func (n *noopAgent) listRoutes() ([]*vcTypes.Route, error) { +func (n *mockAgent) listRoutes() ([]*vcTypes.Route, error) { return nil, nil } // check is the Noop agent health checker. It does nothing. -func (n *noopAgent) check() error { +func (n *mockAgent) check() error { return nil } // statsContainer is the Noop agent Container stats implementation. It does nothing. -func (n *noopAgent) statsContainer(sandbox *Sandbox, c Container) (*ContainerStats, error) { +func (n *mockAgent) statsContainer(sandbox *Sandbox, c Container) (*ContainerStats, error) { return &ContainerStats{}, nil } // waitProcess is the Noop agent process waiter. It does nothing. -func (n *noopAgent) waitProcess(c *Container, processID string) (int32, error) { +func (n *mockAgent) waitProcess(c *Container, processID string) (int32, error) { return 0, nil } // winsizeProcess is the Noop agent process tty resizer. It does nothing. -func (n *noopAgent) winsizeProcess(c *Container, processID string, height, width uint32) error { +func (n *mockAgent) winsizeProcess(c *Container, processID string, height, width uint32) error { return nil } // writeProcessStdin is the Noop agent process stdin writer. It does nothing. -func (n *noopAgent) writeProcessStdin(c *Container, ProcessID string, data []byte) (int, error) { +func (n *mockAgent) writeProcessStdin(c *Container, ProcessID string, data []byte) (int, error) { return 0, nil } // closeProcessStdin is the Noop agent process stdin closer. It does nothing. -func (n *noopAgent) closeProcessStdin(c *Container, ProcessID string) error { +func (n *mockAgent) closeProcessStdin(c *Container, ProcessID string) error { return nil } // readProcessStdout is the Noop agent process stdout reader. It does nothing. -func (n *noopAgent) readProcessStdout(c *Container, processID string, data []byte) (int, error) { +func (n *mockAgent) readProcessStdout(c *Container, processID string, data []byte) (int, error) { return 0, nil } // readProcessStderr is the Noop agent process stderr reader. It does nothing. -func (n *noopAgent) readProcessStderr(c *Container, processID string, data []byte) (int, error) { +func (n *mockAgent) readProcessStderr(c *Container, processID string, data []byte) (int, error) { return 0, nil } // pauseContainer is the Noop agent Container pause implementation. It does nothing. -func (n *noopAgent) pauseContainer(sandbox *Sandbox, c Container) error { +func (n *mockAgent) pauseContainer(sandbox *Sandbox, c Container) error { return nil } // resumeContainer is the Noop agent Container resume implementation. It does nothing. -func (n *noopAgent) resumeContainer(sandbox *Sandbox, c Container) error { +func (n *mockAgent) resumeContainer(sandbox *Sandbox, c Container) error { return nil } // configHypervisor is the Noop agent hypervisor configuration implementation. It does nothing. -func (n *noopAgent) configure(h hypervisor, id, sharePath string, builtin bool, config interface{}) error { +func (n *mockAgent) configure(h hypervisor, id, sharePath string, builtin bool, config interface{}) error { return nil } -func (n *noopAgent) configureFromGrpc(h hypervisor, id string, builtin bool, config interface{}) error { +func (n *mockAgent) configureFromGrpc(h hypervisor, id string, builtin bool, config interface{}) error { return nil } // reseedRNG is the Noop agent RND reseeder. It does nothing. -func (n *noopAgent) reseedRNG(data []byte) error { +func (n *mockAgent) reseedRNG(data []byte) error { return nil } // reuseAgent is the Noop agent reuser. It does nothing. -func (n *noopAgent) reuseAgent(agent agent) error { +func (n *mockAgent) reuseAgent(agent agent) error { return nil } // getAgentURL is the Noop agent url getter. It returns nothing. -func (n *noopAgent) getAgentURL() (string, error) { +func (n *mockAgent) getAgentURL() (string, error) { return "", nil } // setProxy is the Noop agent proxy setter. It does nothing. -func (n *noopAgent) setProxy(sandbox *Sandbox, proxy proxy, pid int, url string) error { +func (n *mockAgent) setProxy(sandbox *Sandbox, proxy proxy, pid int, url string) error { return nil } -func (n *noopAgent) setProxyFromGrpc(proxy proxy, pid int, url string) { +func (n *mockAgent) setProxyFromGrpc(proxy proxy, pid int, url string) { } // getGuestDetails is the Noop agent GuestDetails queryer. It does nothing. -func (n *noopAgent) getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error) { +func (n *mockAgent) getGuestDetails(*grpc.GuestDetailsRequest) (*grpc.GuestDetailsResponse, error) { return nil, nil } // setGuestDateTime is the Noop agent guest time setter. It does nothing. -func (n *noopAgent) setGuestDateTime(time.Time) error { +func (n *mockAgent) setGuestDateTime(time.Time) error { return nil } // copyFile is the Noop agent copy file. It does nothing. -func (n *noopAgent) copyFile(src, dst string) error { +func (n *mockAgent) copyFile(src, dst string) error { return nil } -func (n *noopAgent) markDead() { +func (n *mockAgent) markDead() { } -func (n *noopAgent) cleanup(s *Sandbox) { +func (n *mockAgent) cleanup(s *Sandbox) { } // save is the Noop agent state saver. It does nothing. -func (n *noopAgent) save() (s persistapi.AgentState) { +func (n *mockAgent) save() (s persistapi.AgentState) { return } // load is the Noop agent state loader. It does nothing. -func (n *noopAgent) load(s persistapi.AgentState) {} +func (n *mockAgent) load(s persistapi.AgentState) {} -func (n *noopAgent) getOOMEvent() (string, error) { +func (n *mockAgent) getOOMEvent() (string, error) { return "", nil } -func (k *noopAgent) getAgentMetrics(req *grpc.GetMetricsRequest) (*grpc.Metrics, error) { +func (k *mockAgent) getAgentMetrics(req *grpc.GetMetricsRequest) (*grpc.Metrics, error) { return nil, nil } diff --git a/src/runtime/virtcontainers/monitor_test.go b/src/runtime/virtcontainers/monitor_test.go index 7279322220..59ccf57541 100644 --- a/src/runtime/virtcontainers/monitor_test.go +++ b/src/runtime/virtcontainers/monitor_test.go @@ -19,7 +19,7 @@ func TestMonitorSuccess(t *testing.T) { assert := assert.New(t) // create a sandbox - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, []ContainerConfig{contConfig}, nil) assert.NoError(err) defer cleanUp() @@ -43,7 +43,7 @@ func TestMonitorClosedChannel(t *testing.T) { assert := assert.New(t) // create a sandbox - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, []ContainerConfig{contConfig}, nil) assert.NoError(err) defer cleanUp() diff --git a/src/runtime/virtcontainers/noop_agent_test.go b/src/runtime/virtcontainers/noop_agent_test.go deleted file mode 100644 index c5ac60808d..0000000000 --- a/src/runtime/virtcontainers/noop_agent_test.go +++ /dev/null @@ -1,248 +0,0 @@ -// -// Copyright (c) 2016 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 -// - -package virtcontainers - -import ( - "context" - "testing" - - "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" - "github.com/stretchr/testify/assert" -) - -func testCreateNoopContainer() (*Sandbox, *Container, error) { - defer cleanUp() - - contID := "100" - config := newTestSandboxConfigNoop() - - ctx := context.Background() - p, err := CreateSandbox(ctx, config, nil) - if err != nil { - return nil, nil, err - } - - contConfig := newTestContainerConfigNoop(contID) - - p, c, err := CreateContainer(ctx, p.ID(), contConfig) - if err != nil { - return nil, nil, err - } - - return p.(*Sandbox), c.(*Container), nil -} - -func TestNoopAgentInit(t *testing.T) { - n := &noopAgent{} - sandbox := &Sandbox{} - assert := assert.New(t) - - disableVMShutdown, err := n.init(context.Background(), sandbox, nil) - assert.NoError(err) - assert.False(disableVMShutdown) -} - -func TestNoopAgentExec(t *testing.T) { - n := &noopAgent{} - cmd := types.Cmd{} - assert := assert.New(t) - - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - defer cleanUp() - - _, err = n.exec(sandbox, *container, cmd) - assert.NoError(err) -} - -func TestNoopAgentStartSandbox(t *testing.T) { - n := &noopAgent{} - sandbox := &Sandbox{} - assert := assert.New(t) - - err := n.startSandbox(sandbox) - assert.NoError(err) -} - -func TestNoopAgentStopSandbox(t *testing.T) { - n := &noopAgent{} - sandbox := &Sandbox{} - assert := assert.New(t) - - err := n.stopSandbox(sandbox) - assert.NoError(err) -} - -func TestNoopAgentCreateContainer(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - defer cleanUp() - - err = n.startSandbox(sandbox) - assert.NoError(err) - - _, err = n.createContainer(sandbox, container) - assert.NoError(err) -} - -func TestNoopAgentStartContainer(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - defer cleanUp() - - err = n.startContainer(sandbox, container) - assert.NoError(err) -} - -func TestNoopAgentStopContainer(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - defer cleanUp() - - err = n.stopContainer(sandbox, *container) - assert.NoError(err) -} - -func TestNoopAgentStatsContainer(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - - defer cleanUp() - _, err = n.statsContainer(sandbox, *container) - assert.NoError(err) -} - -func TestNoopAgentPauseContainer(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - - defer cleanUp() - err = n.pauseContainer(sandbox, *container) - assert.NoError(err) -} - -func TestNoopAgentResumeContainer(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - defer cleanUp() - err = n.resumeContainer(sandbox, *container) - assert.NoError(err) -} - -func TestNoopAgentConfigure(t *testing.T) { - n := &noopAgent{} - h := &mockHypervisor{} - id := "foobar" - sharePath := "foobarDir" - assert := assert.New(t) - err := n.configure(h, id, sharePath, true, nil) - assert.NoError(err) -} - -func TestNoopAgentStartProxy(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - sandbox, _, err := testCreateNoopContainer() - - assert.NoError(err) - defer cleanUp() - err = n.startProxy(sandbox) - assert.NoError(err) -} - -func TestNoopAgentProcessListContainer(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - sandbox, container, err := testCreateNoopContainer() - assert.NoError(err) - defer cleanUp() - _, err = n.processListContainer(sandbox, *container, ProcessListOptions{}) - assert.NoError(err) -} - -func TestNoopAgentReseedRNG(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - err := n.reseedRNG([]byte{}) - assert.NoError(err) -} - -func TestNoopAgentUpdateInterface(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - _, err := n.updateInterface(nil) - assert.NoError(err) -} - -func TestNoopAgentListInterfaces(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - _, err := n.listInterfaces() - assert.NoError(err) -} - -func TestNoopAgentUpdateRoutes(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - _, err := n.updateRoutes(nil) - assert.NoError(err) -} - -func TestNoopAgentListRoutes(t *testing.T) { - n := &noopAgent{} - assert := assert.New(t) - _, err := n.listRoutes() - assert.NoError(err) -} - -func TestNoopAgentRSetProxy(t *testing.T) { - n := &noopAgent{} - p := &noopProxy{} - s := &Sandbox{} - assert := assert.New(t) - err := n.setProxy(s, p, 0, "") - assert.NoError(err) -} - -func TestNoopGetAgentUrl(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - - url, err := n.getAgentURL() - assert.Nil(err) - assert.Empty(url) -} - -func TestNoopCopyFile(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - - err := n.copyFile("", "") - assert.Nil(err) -} - -func TestNoopGetOOMEvent(t *testing.T) { - assert := assert.New(t) - n := &noopAgent{} - - containerID, err := n.getOOMEvent() - assert.Nil(err) - assert.Empty(containerID) -} diff --git a/src/runtime/virtcontainers/persist.go b/src/runtime/virtcontainers/persist.go index d1974ee9f5..7541c56e07 100644 --- a/src/runtime/virtcontainers/persist.go +++ b/src/runtime/virtcontainers/persist.go @@ -13,7 +13,6 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" - "github.com/mitchellh/mapstructure" ) var ( @@ -177,7 +176,6 @@ func (s *Sandbox) dumpConfig(ss *persistapi.SandboxState) { sconfig := s.config ss.Config = persistapi.SandboxConfig{ HypervisorType: string(sconfig.HypervisorType), - AgentType: string(sconfig.AgentType), ProxyType: string(sconfig.ProxyType), ProxyConfig: persistapi.ProxyConfig{ Path: sconfig.ProxyConfig.Path, @@ -258,17 +256,9 @@ func (s *Sandbox) dumpConfig(ss *persistapi.SandboxState) { TxRateLimiterMaxRate: sconfig.HypervisorConfig.TxRateLimiterMaxRate, } - if sconfig.AgentType == "kata" { - var sagent KataAgentConfig - err := mapstructure.Decode(sconfig.AgentConfig, &sagent) - if err != nil { - s.Logger().WithError(err).Error("internal error: KataAgentConfig failed to decode") - } else { - ss.Config.KataAgentConfig = &persistapi.KataAgentConfig{ - LongLiveConn: sagent.LongLiveConn, - UseVSock: sagent.UseVSock, - } - } + ss.Config.KataAgentConfig = &persistapi.KataAgentConfig{ + LongLiveConn: sconfig.AgentConfig.LongLiveConn, + UseVSock: sconfig.AgentConfig.UseVSock, } for _, contConf := range sconfig.Containers { @@ -454,7 +444,6 @@ func loadSandboxConfig(id string) (*SandboxConfig, error) { sconfig := &SandboxConfig{ ID: id, HypervisorType: HypervisorType(savedConf.HypervisorType), - AgentType: AgentType(savedConf.AgentType), ProxyType: ProxyType(savedConf.ProxyType), ProxyConfig: ProxyConfig{ Path: savedConf.ProxyConfig.Path, @@ -536,11 +525,9 @@ func loadSandboxConfig(id string) (*SandboxConfig, error) { TxRateLimiterMaxRate: hconf.TxRateLimiterMaxRate, } - if savedConf.AgentType == "kata" { - sconfig.AgentConfig = KataAgentConfig{ - LongLiveConn: savedConf.KataAgentConfig.LongLiveConn, - UseVSock: savedConf.KataAgentConfig.UseVSock, - } + sconfig.AgentConfig = KataAgentConfig{ + LongLiveConn: savedConf.KataAgentConfig.LongLiveConn, + UseVSock: savedConf.KataAgentConfig.UseVSock, } for _, contConf := range savedConf.ContainerConfigs { diff --git a/src/runtime/virtcontainers/persist/api/config.go b/src/runtime/virtcontainers/persist/api/config.go index 84705ff0ad..529c72bf1f 100644 --- a/src/runtime/virtcontainers/persist/api/config.go +++ b/src/runtime/virtcontainers/persist/api/config.go @@ -234,7 +234,6 @@ type SandboxConfig struct { HypervisorConfig HypervisorConfig // only one agent config can be non-nil according to agent type - AgentType string KataAgentConfig *KataAgentConfig `json:",omitempty"` ProxyType string diff --git a/src/runtime/virtcontainers/pkg/oci/utils.go b/src/runtime/virtcontainers/pkg/oci/utils.go index d3287c3f90..ee2546bb7f 100644 --- a/src/runtime/virtcontainers/pkg/oci/utils.go +++ b/src/runtime/virtcontainers/pkg/oci/utils.go @@ -96,8 +96,7 @@ type RuntimeConfig struct { NetmonConfig vc.NetmonConfig - AgentType vc.AgentType - AgentConfig interface{} + AgentConfig vc.KataAgentConfig ProxyType vc.ProxyType ProxyConfig vc.ProxyConfig @@ -784,10 +783,7 @@ func addRuntimeConfigOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig) e } func addAgentConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig) error { - c, ok := config.AgentConfig.(vc.KataAgentConfig) - if !ok { - return nil - } + c := config.AgentConfig if value, ok := ocispec.Annotations[vcAnnotations.KernelModules]; ok { modules := strings.Split(value, KernelModulesSeparator) @@ -850,7 +846,6 @@ func SandboxConfig(ocispec specs.Spec, runtime RuntimeConfig, bundlePath, cid, c HypervisorType: runtime.HypervisorType, HypervisorConfig: runtime.HypervisorConfig, - AgentType: runtime.AgentType, AgentConfig: runtime.AgentConfig, ProxyType: runtime.ProxyType, diff --git a/src/runtime/virtcontainers/pkg/oci/utils_test.go b/src/runtime/virtcontainers/pkg/oci/utils_test.go index 6f05ec5a96..d4e86ed88f 100644 --- a/src/runtime/virtcontainers/pkg/oci/utils_test.go +++ b/src/runtime/virtcontainers/pkg/oci/utils_test.go @@ -71,7 +71,6 @@ func TestMinimalSandboxConfig(t *testing.T) { runtimeConfig := RuntimeConfig{ HypervisorType: vc.QemuHypervisor, - AgentType: vc.KataContainersAgent, ProxyType: vc.KataProxyType, Console: consolePath, } @@ -170,7 +169,6 @@ func TestMinimalSandboxConfig(t *testing.T) { Hostname: "testHostname", HypervisorType: vc.QemuHypervisor, - AgentType: vc.KataContainersAgent, ProxyType: vc.KataProxyType, NetworkConfig: expectedNetworkConfig, diff --git a/src/runtime/virtcontainers/pkg/vcmock/mock_test.go b/src/runtime/virtcontainers/pkg/vcmock/mock_test.go index 42b2720558..357e90cdf2 100644 --- a/src/runtime/virtcontainers/pkg/vcmock/mock_test.go +++ b/src/runtime/virtcontainers/pkg/vcmock/mock_test.go @@ -663,7 +663,6 @@ func TestVCMockSetVMFactory(t *testing.T) { } vmConfig := vc.VMConfig{ HypervisorType: vc.MockHypervisor, - AgentType: vc.NoopAgentType, HypervisorConfig: hyperConfig, } diff --git a/src/runtime/virtcontainers/sandbox.go b/src/runtime/virtcontainers/sandbox.go index f81f1357d7..8704d8b8ea 100644 --- a/src/runtime/virtcontainers/sandbox.go +++ b/src/runtime/virtcontainers/sandbox.go @@ -57,7 +57,6 @@ type SandboxStatus struct { State types.SandboxState Hypervisor HypervisorType HypervisorConfig HypervisorConfig - Agent AgentType ContainersStatus []ContainerStatus // Annotations allow clients to store arbitrary values, @@ -81,8 +80,7 @@ type SandboxConfig struct { HypervisorType HypervisorType HypervisorConfig HypervisorConfig - AgentType AgentType - AgentConfig interface{} + AgentConfig KataAgentConfig ProxyType ProxyType ProxyConfig ProxyConfig @@ -326,7 +324,6 @@ func (s *Sandbox) Status() SandboxStatus { State: s.state, Hypervisor: s.config.HypervisorType, HypervisorConfig: s.config.HypervisorConfig, - Agent: s.config.AgentType, ContainersStatus: contStatusList, Annotations: s.config.Annotations, } @@ -507,7 +504,9 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor return nil, fmt.Errorf("Invalid sandbox configuration") } - agent := newAgent(sandboxConfig.AgentType) + // create agent instance + newAagentFunc := getNewAgentFunc(ctx) + agent := newAagentFunc() hypervisor, err := newHypervisor(sandboxConfig.HypervisorType) if err != nil { @@ -571,12 +570,7 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor return nil, err } - agentConfig, err := newAgentConfig(sandboxConfig.AgentType, sandboxConfig.AgentConfig) - if err != nil { - return nil, err - } - - if s.disableVMShutdown, err = s.agent.init(ctx, s, agentConfig); err != nil { + if s.disableVMShutdown, err = s.agent.init(ctx, s, sandboxConfig.AgentConfig); err != nil { return nil, err } @@ -972,7 +966,6 @@ func (s *Sandbox) startVM() (err error) { vm, err := s.factory.GetVM(ctx, VMConfig{ HypervisorType: s.config.HypervisorType, HypervisorConfig: s.config.HypervisorConfig, - AgentType: s.config.AgentType, AgentConfig: s.config.AgentConfig, ProxyType: s.config.ProxyType, ProxyConfig: s.config.ProxyConfig, diff --git a/src/runtime/virtcontainers/sandbox_test.go b/src/runtime/virtcontainers/sandbox_test.go index 024a78eb9a..d2ec1920ad 100644 --- a/src/runtime/virtcontainers/sandbox_test.go +++ b/src/runtime/virtcontainers/sandbox_test.go @@ -45,7 +45,7 @@ func newHypervisorConfig(kernelParams []Param, hParams []Param) HypervisorConfig } func testCreateSandbox(t *testing.T, id string, - htype HypervisorType, hconfig HypervisorConfig, atype AgentType, + htype HypervisorType, hconfig HypervisorConfig, nconfig NetworkConfig, containers []ContainerConfig, volumes []types.Volume) (*Sandbox, error) { @@ -53,14 +53,14 @@ func testCreateSandbox(t *testing.T, id string, ID: id, HypervisorType: htype, HypervisorConfig: hconfig, - AgentType: atype, NetworkConfig: nconfig, Volumes: volumes, Containers: containers, Annotations: sandboxAnnotations, } - sandbox, err := createSandbox(context.Background(), sconfig, nil) + ctx := WithNewAgentFunc(context.Background(), newMockAgent) + sandbox, err := createSandbox(ctx, sconfig, nil) if err != nil { return nil, fmt.Errorf("Could not create sandbox: %s", err) } @@ -85,20 +85,20 @@ func testCreateSandbox(t *testing.T, id string, } func TestCreateEmptySandbox(t *testing.T) { - _, err := testCreateSandbox(t, testSandboxID, MockHypervisor, HypervisorConfig{}, NoopAgentType, NetworkConfig{}, nil, nil) + _, err := testCreateSandbox(t, testSandboxID, MockHypervisor, HypervisorConfig{}, NetworkConfig{}, nil, nil) assert.Error(t, err) defer cleanUp() } func TestCreateEmptyHypervisorSandbox(t *testing.T) { - _, err := testCreateSandbox(t, testSandboxID, QemuHypervisor, HypervisorConfig{}, NoopAgentType, NetworkConfig{}, nil, nil) + _, err := testCreateSandbox(t, testSandboxID, QemuHypervisor, HypervisorConfig{}, NetworkConfig{}, nil, nil) assert.Error(t, err) defer cleanUp() } func TestCreateMockSandbox(t *testing.T) { hConfig := newHypervisorConfig(nil, nil) - _, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil) + _, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, nil, nil) assert.NoError(t, err) defer cleanUp() } @@ -164,7 +164,7 @@ func TestCalculateSandboxMem(t *testing.T) { func TestCreateSandboxEmptyID(t *testing.T) { hConfig := newHypervisorConfig(nil, nil) - _, err := testCreateSandbox(t, "", MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil) + _, err := testCreateSandbox(t, "", MockHypervisor, hConfig, NetworkConfig{}, nil, nil) assert.Error(t, err) defer cleanUp() } @@ -172,7 +172,7 @@ func TestCreateSandboxEmptyID(t *testing.T) { func testSandboxStateTransition(t *testing.T, state types.StateString, newState types.StateString) error { hConfig := newHypervisorConfig(nil, nil) - p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil) + p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, nil, nil) assert.NoError(t, err) defer cleanUp() @@ -455,7 +455,7 @@ func TestSandboxSetSandboxAndContainerState(t *testing.T) { assert := assert.New(t) // create a sandbox - p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig}, nil) + p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, []ContainerConfig{contConfig}, nil) assert.NoError(err) defer cleanUp() @@ -613,7 +613,7 @@ func TestSandboxGetContainer(t *testing.T) { assert.Error(err) hConfig := newHypervisorConfig(nil, nil) - p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil) + p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, nil, nil) assert.NoError(err) defer cleanUp() @@ -652,7 +652,7 @@ func TestContainerStateSetFstype(t *testing.T) { } hConfig := newHypervisorConfig(nil, nil) - sandbox, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, containers, nil) + sandbox, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, containers, nil) assert.Nil(err) defer cleanUp() @@ -948,7 +948,7 @@ func TestRemoveContainerSuccess(t *testing.T) { } func TestCreateContainer(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -965,7 +965,7 @@ func TestCreateContainer(t *testing.T) { } func TestDeleteContainer(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -982,7 +982,7 @@ func TestDeleteContainer(t *testing.T) { } func TestStartContainer(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1002,7 +1002,7 @@ func TestStartContainer(t *testing.T) { } func TestStatusContainer(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1022,7 +1022,7 @@ func TestStatusContainer(t *testing.T) { } func TestStatusSandbox(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1030,7 +1030,7 @@ func TestStatusSandbox(t *testing.T) { } func TestEnterContainer(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1055,7 +1055,7 @@ func TestEnterContainer(t *testing.T) { func TestDeleteStoreWhenCreateContainerFail(t *testing.T) { hypervisorConfig := newHypervisorConfig(nil, nil) - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hypervisorConfig, NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hypervisorConfig, NetworkConfig{}, nil, nil) if err != nil { t.Fatal(err) } @@ -1071,7 +1071,7 @@ func TestDeleteStoreWhenCreateContainerFail(t *testing.T) { func TestDeleteStoreWhenNewContainerFail(t *testing.T) { hConfig := newHypervisorConfig(nil, nil) - p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil) + p, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NetworkConfig{}, nil, nil) if err != nil { t.Fatal(err) } @@ -1093,7 +1093,7 @@ func TestDeleteStoreWhenNewContainerFail(t *testing.T) { } func TestMonitor(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1113,7 +1113,7 @@ func TestMonitor(t *testing.T) { } func TestWaitProcess(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1143,7 +1143,7 @@ func TestWaitProcess(t *testing.T) { } func TestSignalProcess(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1173,7 +1173,7 @@ func TestSignalProcess(t *testing.T) { } func TestWinsizeProcess(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1203,7 +1203,7 @@ func TestWinsizeProcess(t *testing.T) { } func TestContainerProcessIOStream(t *testing.T) { - s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NoopAgentType, NetworkConfig{}, nil, nil) + s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, newHypervisorConfig(nil, nil), NetworkConfig{}, nil, nil) assert.Nil(t, err, "VirtContainers should not allow empty sandboxes") defer cleanUp() @@ -1467,7 +1467,6 @@ func TestSandboxCreationFromConfigRollbackFromCreateSandbox(t *testing.T) { ID: testSandboxID, HypervisorType: QemuHypervisor, HypervisorConfig: hConf, - AgentType: KataContainersAgent, NetworkConfig: NetworkConfig{}, Volumes: nil, Containers: nil, @@ -1496,7 +1495,6 @@ func TestSandboxUpdateResources(t *testing.T) { testSandboxID, MockHypervisor, hConfig, - NoopAgentType, NetworkConfig{}, []ContainerConfig{contConfig1, contConfig2}, nil) diff --git a/src/runtime/virtcontainers/vm.go b/src/runtime/virtcontainers/vm.go index 29121625f3..b9a88417ec 100644 --- a/src/runtime/virtcontainers/vm.go +++ b/src/runtime/virtcontainers/vm.go @@ -8,7 +8,6 @@ package virtcontainers import ( "context" "encoding/json" - "fmt" "os" "path/filepath" "time" @@ -44,8 +43,7 @@ type VMConfig struct { HypervisorType HypervisorType HypervisorConfig HypervisorConfig - AgentType AgentType - AgentConfig interface{} + AgentConfig KataAgentConfig ProxyType ProxyType ProxyConfig ProxyConfig @@ -63,12 +61,7 @@ func (c *VMConfig) ToGrpc() (*pb.GrpcVMConfig, error) { return nil, err } - aconf, ok := c.AgentConfig.(KataAgentConfig) - if !ok { - return nil, fmt.Errorf("agent type is not supported by VM cache") - } - - agentConfig, err := json.Marshal(&aconf) + agentConfig, err := json.Marshal(&c.AgentConfig) if err != nil { return nil, err } @@ -87,10 +80,6 @@ func GrpcToVMConfig(j *pb.GrpcVMConfig) (*VMConfig, error) { return nil, err } - if config.AgentType != KataContainersAgent { - return nil, fmt.Errorf("agent type %s is not supported by VM cache", config.AgentType) - } - var kataConfig KataAgentConfig err = json.Unmarshal(j.AgentConfig, &kataConfig) if err == nil { @@ -176,7 +165,9 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) { } // 2. setup agent - agent := newAgent(config.AgentType) + newAagentFunc := getNewAgentFunc(ctx) + agent := newAagentFunc() + vmSharePath := buildVMSharePath(id, store.RunVMStoragePath()) err = agent.configure(hypervisor, id, vmSharePath, isProxyBuiltIn(config.ProxyType), config.AgentConfig) if err != nil { @@ -260,7 +251,9 @@ func NewVMFromGrpc(ctx context.Context, v *pb.GrpcVM, config VMConfig) (*VM, err return nil, err } - agent := newAgent(config.AgentType) + // create agent instance + newAagentFunc := getNewAgentFunc(ctx) + agent := newAagentFunc() agent.configureFromGrpc(hypervisor, v.Id, isProxyBuiltIn(config.ProxyType), config.AgentConfig) proxy, err := newProxy(config.ProxyType) diff --git a/src/runtime/virtcontainers/vm_test.go b/src/runtime/virtcontainers/vm_test.go index 83d7447e53..a8a5946a59 100644 --- a/src/runtime/virtcontainers/vm_test.go +++ b/src/runtime/virtcontainers/vm_test.go @@ -24,7 +24,6 @@ func TestNewVM(t *testing.T) { config := VMConfig{ HypervisorType: MockHypervisor, - AgentType: NoopAgentType, ProxyType: NoopProxyType, } hyperConfig := HypervisorConfig{ @@ -32,7 +31,7 @@ func TestNewVM(t *testing.T) { ImagePath: testDir, } - ctx := context.Background() + ctx := WithNewAgentFunc(context.Background(), newMockAgent) var vm *VM _, err = NewVM(ctx, config) @@ -103,11 +102,10 @@ func TestSetupProxy(t *testing.T) { config := VMConfig{ HypervisorType: MockHypervisor, - AgentType: NoopAgentType, } hypervisor := &mockHypervisor{} - agent := &noopAgent{} + agent := &mockAgent{} // wrong proxy type config.ProxyType = ProxyType("invalidProxyType") @@ -124,7 +122,6 @@ func TestVMConfigGrpc(t *testing.T) { config := VMConfig{ HypervisorType: QemuHypervisor, HypervisorConfig: newQemuConfig(), - AgentType: KataContainersAgent, AgentConfig: KataAgentConfig{false, true, false, false, 0, "", "", []string{}}, ProxyType: NoopProxyType, }