diff --git a/src/runtime/virtcontainers/acrn.go b/src/runtime/virtcontainers/acrn.go index db0f6491a..d97cf2a0c 100644 --- a/src/runtime/virtcontainers/acrn.go +++ b/src/runtime/virtcontainers/acrn.go @@ -356,7 +356,7 @@ func (a *Acrn) setConfig(config *HypervisorConfig) error { } // CreateVM is the VM creation -func (a *Acrn) CreateVM(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig) error { +func (a *Acrn) CreateVM(ctx context.Context, id string, network *Network, hypervisorConfig *HypervisorConfig) error { // Save the tracing context a.ctx = ctx diff --git a/src/runtime/virtcontainers/acrn_test.go b/src/runtime/virtcontainers/acrn_test.go index bb19b45a7..45d85fb4e 100644 --- a/src/runtime/virtcontainers/acrn_test.go +++ b/src/runtime/virtcontainers/acrn_test.go @@ -243,7 +243,9 @@ func TestAcrnCreateVM(t *testing.T) { //set PID to 1 to ignore hypercall to get UUID and set a random UUID a.state.PID = 1 a.state.UUID = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" - err = a.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + network, err := NewNetwork() + assert.NoError(err) + err = a.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Exactly(acrnConfig, a.config) } diff --git a/src/runtime/virtcontainers/clh.go b/src/runtime/virtcontainers/clh.go index da6e73e3e..5b48ae56b 100644 --- a/src/runtime/virtcontainers/clh.go +++ b/src/runtime/virtcontainers/clh.go @@ -200,7 +200,7 @@ func (clh *cloudHypervisor) setConfig(config *HypervisorConfig) error { // For cloudHypervisor this call only sets the internal structure up. // The VM will be created and started through StartVM(). -func (clh *cloudHypervisor) CreateVM(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig) error { +func (clh *cloudHypervisor) CreateVM(ctx context.Context, id string, network *Network, hypervisorConfig *HypervisorConfig) error { clh.ctx = ctx span, newCtx := katatrace.Trace(clh.ctx, clh.Logger(), "CreateVM", clhTracingTags, map[string]string{"sandbox_id": clh.id}) diff --git a/src/runtime/virtcontainers/clh_test.go b/src/runtime/virtcontainers/clh_test.go index f9452cbd3..d350dd9e9 100644 --- a/src/runtime/virtcontainers/clh_test.go +++ b/src/runtime/virtcontainers/clh_test.go @@ -243,6 +243,9 @@ func TestClhCreateVMWithInitrd(t *testing.T) { clhConfig.VMStorePath = store.RunVMStoragePath() clhConfig.RunStorePath = store.RunStoragePath() + network, err := NewNetwork() + assert.NoError(err) + clh := &cloudHypervisor{ config: clhConfig, } @@ -255,7 +258,7 @@ func TestClhCreateVMWithInitrd(t *testing.T) { }, } - err = clh.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = clh.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Exactly(clhConfig, clh.config) } @@ -273,6 +276,9 @@ func TestClhCreateVM(t *testing.T) { clhConfig.VMStorePath = store.RunVMStoragePath() clhConfig.RunStorePath = store.RunStoragePath() + network, err := NewNetwork() + assert.NoError(err) + clh := &cloudHypervisor{ config: clhConfig, } @@ -285,7 +291,7 @@ func TestClhCreateVM(t *testing.T) { }, } - err = clh.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = clh.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Exactly(clhConfig, clh.config) } diff --git a/src/runtime/virtcontainers/fc.go b/src/runtime/virtcontainers/fc.go index 66afb513d..f121132db 100644 --- a/src/runtime/virtcontainers/fc.go +++ b/src/runtime/virtcontainers/fc.go @@ -199,7 +199,7 @@ func (fc *firecracker) setConfig(config *HypervisorConfig) error { // CreateVM For firecracker this call only sets the internal structure up. // The sandbox will be created and started through startSandbox(). -func (fc *firecracker) CreateVM(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig) error { +func (fc *firecracker) CreateVM(ctx context.Context, id string, network *Network, hypervisorConfig *HypervisorConfig) error { fc.ctx = ctx span, _ := katatrace.Trace(ctx, fc.Logger(), "CreateVM", fcTracingTags, map[string]string{"sandbox_id": fc.id}) @@ -217,7 +217,7 @@ func (fc *firecracker) CreateVM(ctx context.Context, id string, networkNS Networ fc.setPaths(&fc.config) // So we need to repopulate this at StartVM where it is valid - fc.netNSPath = networkNS.NetNsPath + fc.netNSPath = network.NetNSPath // Till we create lower privileged kata user run as root // https://github.com/kata-containers/runtime/issues/1869 diff --git a/src/runtime/virtcontainers/hypervisor.go b/src/runtime/virtcontainers/hypervisor.go index 1a2b8025d..d16f82d8b 100644 --- a/src/runtime/virtcontainers/hypervisor.go +++ b/src/runtime/virtcontainers/hypervisor.go @@ -910,7 +910,7 @@ func generateVMSocket(id string, vmStogarePath string) (interface{}, error) { // hypervisor is the virtcontainers hypervisor interface. // The default hypervisor implementation is Qemu. type Hypervisor interface { - CreateVM(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig) error + CreateVM(ctx context.Context, id string, network *Network, hypervisorConfig *HypervisorConfig) error StartVM(ctx context.Context, timeout int) error // If wait is set, don't actively stop the sandbox: diff --git a/src/runtime/virtcontainers/mock_hypervisor.go b/src/runtime/virtcontainers/mock_hypervisor.go index 111707fd9..635cbbe1d 100644 --- a/src/runtime/virtcontainers/mock_hypervisor.go +++ b/src/runtime/virtcontainers/mock_hypervisor.go @@ -38,7 +38,7 @@ func (m *mockHypervisor) setConfig(config *HypervisorConfig) error { return nil } -func (m *mockHypervisor) CreateVM(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig) error { +func (m *mockHypervisor) CreateVM(ctx context.Context, id string, network *Network, hypervisorConfig *HypervisorConfig) error { if err := m.setConfig(hypervisorConfig); err != nil { return err } diff --git a/src/runtime/virtcontainers/mock_hypervisor_test.go b/src/runtime/virtcontainers/mock_hypervisor_test.go index ad52b5d92..5e89ae8a6 100644 --- a/src/runtime/virtcontainers/mock_hypervisor_test.go +++ b/src/runtime/virtcontainers/mock_hypervisor_test.go @@ -28,10 +28,13 @@ func TestMockHypervisorCreateVM(t *testing.T) { }, } + network, err := NewNetwork() + assert.NoError(err) + ctx := context.Background() // wrong config - err := m.CreateVM(ctx, sandbox.config.ID, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = m.CreateVM(ctx, sandbox.config.ID, network, &sandbox.config.HypervisorConfig) assert.Error(err) sandbox.config.HypervisorConfig = HypervisorConfig{ @@ -40,7 +43,7 @@ func TestMockHypervisorCreateVM(t *testing.T) { HypervisorPath: fmt.Sprintf("%s/%s", testDir, testHypervisor), } - err = m.CreateVM(ctx, sandbox.config.ID, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = m.CreateVM(ctx, sandbox.config.ID, network, &sandbox.config.HypervisorConfig) assert.NoError(err) } diff --git a/src/runtime/virtcontainers/network.go b/src/runtime/virtcontainers/network.go index d2f7af072..1313e1bf8 100644 --- a/src/runtime/virtcontainers/network.go +++ b/src/runtime/virtcontainers/network.go @@ -196,7 +196,17 @@ type Network struct { NetmonPID int } -func NewNetwork(config *NetworkConfig) (*Network, error) { +func NewNetwork(configs ...*NetworkConfig) (*Network, error) { + if len(configs) > 1 { + return nil, fmt.Errorf("Too many network configurations") + } + + // Empty constructor + if len(configs) == 0 { + return &Network{}, nil + } + + config := configs[0] if config == nil { return nil, fmt.Errorf("Missing network configuration") } diff --git a/src/runtime/virtcontainers/qemu.go b/src/runtime/virtcontainers/qemu.go index 28b97dfba..9f5fdb295 100644 --- a/src/runtime/virtcontainers/qemu.go +++ b/src/runtime/virtcontainers/qemu.go @@ -468,7 +468,7 @@ func (q *qemu) setConfig(config *HypervisorConfig) error { } // CreateVM is the Hypervisor VM creation implementation for govmmQemu. -func (q *qemu) CreateVM(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig) error { +func (q *qemu) CreateVM(ctx context.Context, id string, network *Network, hypervisorConfig *HypervisorConfig) error { // Save the tracing context q.ctx = ctx diff --git a/src/runtime/virtcontainers/qemu_test.go b/src/runtime/virtcontainers/qemu_test.go index 2add4a45c..ee1b6a253 100644 --- a/src/runtime/virtcontainers/qemu_test.go +++ b/src/runtime/virtcontainers/qemu_test.go @@ -99,7 +99,9 @@ func TestQemuCreateVM(t *testing.T) { parentDir := filepath.Join(store.RunStoragePath(), sandbox.id) assert.NoError(os.MkdirAll(parentDir, DirMode)) - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + network, err := NewNetwork() + assert.NoError(err) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.NoError(os.RemoveAll(parentDir)) assert.Exactly(qemuConfig, q.config) @@ -134,7 +136,9 @@ func TestQemuCreateVMMissingParentDirFail(t *testing.T) { parentDir := filepath.Join(store.RunStoragePath(), sandbox.id) assert.NoError(os.RemoveAll(parentDir)) - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + network, err := NewNetwork() + assert.NoError(err) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) } @@ -202,7 +206,9 @@ func TestQemuKnobs(t *testing.T) { RunStorePath: sandbox.store.RunStoragePath(), }, } - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + network, err := NewNetwork() + assert.NoError(err) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Equal(q.qemuConfig.Knobs.NoUserConfig, true) @@ -470,6 +476,9 @@ func TestQemuFileBackedMem(t *testing.T) { sandbox, err := createQemuSandboxConfig() assert.NoError(err) + network, err := NewNetwork() + assert.NoError(err) + q := &qemu{ config: HypervisorConfig{ VMStorePath: sandbox.store.RunVMStoragePath(), @@ -477,7 +486,7 @@ func TestQemuFileBackedMem(t *testing.T) { }, } sandbox.config.HypervisorConfig.SharedFS = config.VirtioFS - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Equal(q.qemuConfig.Knobs.FileBackedMem, true) @@ -498,7 +507,7 @@ func TestQemuFileBackedMem(t *testing.T) { sandbox.config.HypervisorConfig.SharedFS = config.VirtioFS sandbox.config.HypervisorConfig.MemoryPath = fallbackFileBackedMemDir - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) expectErr := errors.New("VM templating has been enabled with either virtio-fs or file backed memory and this configuration will not work") assert.Equal(expectErr.Error(), err.Error()) @@ -514,7 +523,7 @@ func TestQemuFileBackedMem(t *testing.T) { }, } sandbox.config.HypervisorConfig.FileBackedMemRootDir = "/tmp/xyzabc" - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Equal(q.qemuConfig.Knobs.FileBackedMem, false) assert.Equal(q.qemuConfig.Knobs.MemShared, false) @@ -532,7 +541,7 @@ func TestQemuFileBackedMem(t *testing.T) { } sandbox.config.HypervisorConfig.EnableVhostUserStore = true sandbox.config.HypervisorConfig.HugePages = true - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) assert.NoError(err) assert.Equal(q.qemuConfig.Knobs.MemShared, true) @@ -548,7 +557,7 @@ func TestQemuFileBackedMem(t *testing.T) { } sandbox.config.HypervisorConfig.EnableVhostUserStore = true sandbox.config.HypervisorConfig.HugePages = false - err = q.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig) + err = q.CreateVM(context.Background(), sandbox.id, network, &sandbox.config.HypervisorConfig) expectErr = errors.New("Vhost-user-blk/scsi is enabled without HugePages. This configuration will not work") assert.Equal(expectErr.Error(), err.Error()) diff --git a/src/runtime/virtcontainers/sandbox.go b/src/runtime/virtcontainers/sandbox.go index 2a07362a2..4f4677d94 100644 --- a/src/runtime/virtcontainers/sandbox.go +++ b/src/runtime/virtcontainers/sandbox.go @@ -579,7 +579,7 @@ func newSandbox(ctx context.Context, sandboxConfig SandboxConfig, factory Factor } // store doesn't require hypervisor to be stored immediately - if err = s.hypervisor.CreateVM(ctx, s.id, s.networkNS, &sandboxConfig.HypervisorConfig); err != nil { + if err = s.hypervisor.CreateVM(ctx, s.id, s.network, &sandboxConfig.HypervisorConfig); err != nil { return nil, err } diff --git a/src/runtime/virtcontainers/vm.go b/src/runtime/virtcontainers/vm.go index b39b75563..a9db8efc0 100644 --- a/src/runtime/virtcontainers/vm.go +++ b/src/runtime/virtcontainers/vm.go @@ -90,6 +90,11 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) { return nil, err } + network, err := NewNetwork() + if err != nil { + return nil, err + } + if err = config.Valid(); err != nil { return nil, err } @@ -111,7 +116,7 @@ func NewVM(ctx context.Context, config VMConfig) (*VM, error) { } }() - if err = hypervisor.CreateVM(ctx, id, NetworkNamespace{}, &config.HypervisorConfig); err != nil { + if err = hypervisor.CreateVM(ctx, id, network, &config.HypervisorConfig); err != nil { return nil, err }