From dd5e588eb06beadd8bda48bff72db09b77b7fff5 Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Thu, 20 Apr 2023 07:04:07 -0400 Subject: [PATCH] Support Bridged Network (#78) * Support Bridged Network Inspired by https://github.com/cirruslabs/tart/issues/473 * Fixed tests --- internal/command/create/vm.go | 19 +++++++++++-------- internal/tests/integration_test.go | 3 --- internal/worker/vmmanager/vm.go | 5 ++++- pkg/resource/v1/v1.go | 11 ++++++----- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/internal/command/create/vm.go b/internal/command/create/vm.go index 4d07597..5e942ad 100644 --- a/internal/command/create/vm.go +++ b/internal/command/create/vm.go @@ -13,7 +13,8 @@ var ErrVMFailed = errors.New("failed to create VM") var image string var cpu uint64 var memory uint64 -var softnet bool +var netSoftnet bool +var netBridged string var headless bool var stringToStringResources map[string]string @@ -28,7 +29,8 @@ func newCreateVMCommand() *cobra.Command { command.PersistentFlags().StringVar(&image, "image", "ghcr.io/cirruslabs/macos-ventura-base:latest", "image to use") command.PersistentFlags().Uint64Var(&cpu, "cpu", 4, "number of CPUs to use") command.PersistentFlags().Uint64Var(&memory, "memory", 8*1024, "megabytes of memory to use") - command.PersistentFlags().BoolVar(&softnet, "softnet", false, "whether to use Softnet network isolation") + command.PersistentFlags().BoolVar(&netSoftnet, "net-softnet", false, "whether to use Softnet network isolation") + command.PersistentFlags().StringVar(&netBridged, "net-bridged", "", "whether to use Bridged network mode") command.PersistentFlags().BoolVar(&headless, "headless", true, "whether to run without graphics") command.PersistentFlags().StringToStringVar(&stringToStringResources, "resources", map[string]string{}, "resources to request for this VM") @@ -54,11 +56,12 @@ func runCreateVM(cmd *cobra.Command, args []string) error { Meta: v1.Meta{ Name: name, }, - Image: image, - CPU: cpu, - Memory: memory, - Softnet: softnet, - Headless: headless, - Resources: resources, + Image: image, + CPU: cpu, + Memory: memory, + NetSoftnet: netSoftnet, + NetBridged: netBridged, + Headless: headless, + Resources: resources, }) } diff --git a/internal/tests/integration_test.go b/internal/tests/integration_test.go index e3fa49e..2bc9804 100644 --- a/internal/tests/integration_test.go +++ b/internal/tests/integration_test.go @@ -37,7 +37,6 @@ func TestSingleVM(t *testing.T) { Image: "ghcr.io/cirruslabs/macos-ventura-base:latest", CPU: 4, Memory: 8 * 1024, - Softnet: false, Headless: true, Status: v1.VMStatusPending, StartupScript: &v1.VMScript{ @@ -113,7 +112,6 @@ func TestFailedStartupScript(t *testing.T) { Image: "ghcr.io/cirruslabs/macos-ventura-base:latest", CPU: 4, Memory: 8 * 1024, - Softnet: false, Headless: true, Status: v1.VMStatusPending, StartupScript: &v1.VMScript{ @@ -213,7 +211,6 @@ func TestPortForwarding(t *testing.T) { Image: "ghcr.io/cirruslabs/macos-ventura-base:latest", CPU: 4, Memory: 8 * 1024, - Softnet: false, Headless: true, }) require.NoError(t, err) diff --git a/internal/worker/vmmanager/vm.go b/internal/worker/vmmanager/vm.go index 804587b..6aa48d6 100644 --- a/internal/worker/vmmanager/vm.go +++ b/internal/worker/vmmanager/vm.go @@ -92,9 +92,12 @@ func (vm *VM) cloneAndConfigure(ctx context.Context) error { func (vm *VM) run(ctx context.Context) error { var runArgs = []string{"run"} - if vm.Resource.Softnet { + if vm.Resource.NetSoftnet { runArgs = append(runArgs, "--net-softnet") } + if vm.Resource.NetBridged != "" { + runArgs = append(runArgs, fmt.Sprintf("--net-bridged=%s", vm.Resource.NetBridged)) + } if vm.Resource.Headless { runArgs = append(runArgs, "--no-graphics") diff --git a/pkg/resource/v1/v1.go b/pkg/resource/v1/v1.go index d4af881..1a8cc8a 100644 --- a/pkg/resource/v1/v1.go +++ b/pkg/resource/v1/v1.go @@ -19,11 +19,12 @@ type Meta struct { } type VM struct { - Image string `json:"image"` - CPU uint64 `json:"cpu"` - Memory uint64 `json:"memory"` - Softnet bool `json:"softnet"` - Headless bool `json:"headless"` + Image string `json:"image"` + CPU uint64 `json:"cpu"` + Memory uint64 `json:"memory"` + NetSoftnet bool `json:"net-softnet"` + NetBridged string `json:"net-bridged"` + Headless bool `json:"headless"` // Status field is used to track the lifecycle of the VM associated with this resource. Status VMStatus `json:"status"`