mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-25 15:02:45 +00:00
virtcontainers: Have CreateVM use a Network reference
We are replacing the NetworkingNamespace structure with the Network one, so we should have the hypervisor interface switching to it as well. Signed-off-by: Samuel Ortiz <s.ortiz@apple.com>
This commit is contained in:
parent
d7b67a7d1a
commit
844eb61992
@ -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
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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})
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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())
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user