From 481e46a644b352dbc3cddfe3b1461b161b1872df Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Fri, 5 Apr 2024 17:45:51 +0200 Subject: [PATCH] windows: Remove system networking Should return an error when trying to set the "system networking" option. But I think this is already the case, or it's not possible to choose this option in release builds? Signed-off-by: Christophe Fergeau --- pkg/crc/machine/hyperv/constants_windows.go | 6 - pkg/crc/machine/hyperv/driver_windows.go | 10 -- pkg/crc/preflight/preflight_checks_windows.go | 26 ---- pkg/crc/preflight/preflight_windows.go | 15 --- pkg/crc/preflight/preflight_windows_test.go | 4 +- pkg/crc/services/dns/dns_windows.go | 42 +------ pkg/drivers/hyperv/hyperv_windows.go | 117 +----------------- pkg/os/windows/network/vswitch_windows.go | 47 ------- 8 files changed, 8 insertions(+), 259 deletions(-) delete mode 100644 pkg/crc/machine/hyperv/constants_windows.go delete mode 100644 pkg/os/windows/network/vswitch_windows.go diff --git a/pkg/crc/machine/hyperv/constants_windows.go b/pkg/crc/machine/hyperv/constants_windows.go deleted file mode 100644 index ec04f1b6ac..0000000000 --- a/pkg/crc/machine/hyperv/constants_windows.go +++ /dev/null @@ -1,6 +0,0 @@ -package hyperv - -const ( - // Alternative - AlternativeNetwork = "crc" -) diff --git a/pkg/crc/machine/hyperv/driver_windows.go b/pkg/crc/machine/hyperv/driver_windows.go index 252dfe3b4c..2efdc459b1 100644 --- a/pkg/crc/machine/hyperv/driver_windows.go +++ b/pkg/crc/machine/hyperv/driver_windows.go @@ -6,9 +6,7 @@ import ( "github.com/crc-org/crc/v2/pkg/crc/constants" "github.com/crc-org/crc/v2/pkg/crc/machine/config" - "github.com/crc-org/crc/v2/pkg/crc/network" "github.com/crc-org/crc/v2/pkg/drivers/hyperv" - winnet "github.com/crc-org/crc/v2/pkg/os/windows/network" "github.com/crc-org/machine/libmachine/drivers" ) @@ -19,14 +17,6 @@ func CreateHost(machineConfig config.MachineConfig) *hyperv.Driver { hypervDriver.DisableDynamicMemory = true - if machineConfig.NetworkMode == network.UserNetworkingMode { - hypervDriver.VirtualSwitch = "" - } else { - // Determine the Virtual Switch to be used - _, switchName := winnet.SelectSwitchByNameOrDefault(AlternativeNetwork) - hypervDriver.VirtualSwitch = switchName - } - hypervDriver.SharedDirs = configureShareDirs(machineConfig) return hypervDriver } diff --git a/pkg/crc/preflight/preflight_checks_windows.go b/pkg/crc/preflight/preflight_checks_windows.go index 18fae462f5..3f61aeccce 100644 --- a/pkg/crc/preflight/preflight_checks_windows.go +++ b/pkg/crc/preflight/preflight_checks_windows.go @@ -8,11 +8,9 @@ import ( "github.com/crc-org/crc/v2/pkg/crc/logging" "github.com/crc-org/crc/v2/pkg/crc/adminhelper" - winnet "github.com/crc-org/crc/v2/pkg/os/windows/network" "github.com/crc-org/crc/v2/pkg/os/windows/powershell" "github.com/crc-org/crc/v2/pkg/crc/constants" - "github.com/crc-org/crc/v2/pkg/crc/machine/hyperv" ) const ( @@ -166,19 +164,6 @@ func fixUserPartOfCrcUsersAndHypervAdminsGroup() error { return errReboot } -func checkIfHyperVVirtualSwitchExists() error { - switchName := hyperv.AlternativeNetwork - - // use winnet instead - exists, foundName := winnet.SelectSwitchByNameOrDefault(switchName) - if exists { - logging.Info("Found Virtual Switch to use: ", foundName) - return nil - } - - return fmt.Errorf("Virtual Switch not found") -} - func checkIfRunningAsNormalUser() error { if !powershell.IsAdmin() { return nil @@ -187,17 +172,6 @@ func checkIfRunningAsNormalUser() error { return fmt.Errorf("crc should be run in a shell without administrator rights") } -func removeDNSServerAddress() error { - resetDNSCommand := `Set-DnsClientServerAddress -InterfaceAlias ("vEthernet (crc)") -ResetServerAddresses` - if exist, defaultSwitch := winnet.GetDefaultSwitchName(); exist { - resetDNSCommand = fmt.Sprintf(`Set-DnsClientServerAddress -InterfaceAlias ("vEthernet (%s)","vEthernet (crc)") -ResetServerAddresses`, defaultSwitch) - } - if _, _, err := powershell.ExecuteAsAdmin("Remove dns entry for default switch", resetDNSCommand); err != nil { - return err - } - return nil -} - func removeCrcVM() (err error) { if _, _, err := powershell.Execute("Get-VM -Name crc"); err != nil { // This means that there is no crc VM exist diff --git a/pkg/crc/preflight/preflight_windows.go b/pkg/crc/preflight/preflight_windows.go index 2d8ea876bd..03e488a0d1 100644 --- a/pkg/crc/preflight/preflight_windows.go +++ b/pkg/crc/preflight/preflight_windows.go @@ -60,21 +60,6 @@ var hypervPreflightChecks = []Check{ labels: labels{Os: Windows}, }, - { - configKeySuffix: "check-hyperv-switch", - checkDescription: "Checking if the Hyper-V virtual switch exists", - check: checkIfHyperVVirtualSwitchExists, - flags: StartUpOnly, - - labels: labels{Os: Windows, NetworkMode: System}, - }, - { - cleanupDescription: "Removing dns server from interface", - cleanup: removeDNSServerAddress, - flags: CleanUpOnly, - - labels: labels{Os: Windows, NetworkMode: System}, - }, } var cleanupCheckRemoveCrcVM = Check{ diff --git a/pkg/crc/preflight/preflight_windows_test.go b/pkg/crc/preflight/preflight_windows_test.go index b0b7834d79..55b1705ca2 100644 --- a/pkg/crc/preflight/preflight_windows_test.go +++ b/pkg/crc/preflight/preflight_windows_test.go @@ -17,8 +17,8 @@ func TestCountConfigurationOptions(t *testing.T) { } func TestCountPreflights(t *testing.T) { - assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 24) - assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 24) + assert.Len(t, getPreflightChecks(false, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 22) + assert.Len(t, getPreflightChecks(true, network.SystemNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 22) assert.Len(t, getPreflightChecks(false, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 23) assert.Len(t, getPreflightChecks(true, network.UserNetworkingMode, constants.GetDefaultBundlePath(preset.OpenShift), preset.OpenShift, false), 23) diff --git a/pkg/crc/services/dns/dns_windows.go b/pkg/crc/services/dns/dns_windows.go index 908ad511c0..e7cdfd779c 100644 --- a/pkg/crc/services/dns/dns_windows.go +++ b/pkg/crc/services/dns/dns_windows.go @@ -2,50 +2,14 @@ package dns import ( "fmt" - "time" "github.com/crc-org/crc/v2/pkg/crc/network" "github.com/crc-org/crc/v2/pkg/crc/services" - winnet "github.com/crc-org/crc/v2/pkg/os/windows/network" - "github.com/crc-org/crc/v2/pkg/os/windows/powershell" - "github.com/crc-org/crc/v2/pkg/os/windows/win32" - crcstrings "github.com/crc-org/crc/v2/pkg/strings" -) - -const ( - // Alternative - AlternativeNetwork = "crc" ) func runPostStartForOS(serviceConfig services.ServicePostStartConfig) error { - if serviceConfig.NetworkMode == network.UserNetworkingMode { - return addOpenShiftHosts(serviceConfig) - } - - _, switchName := winnet.SelectSwitchByNameOrDefault(AlternativeNetwork) - networkInterface := fmt.Sprintf("vEthernet (%s)", switchName) - - setInterfaceNameserverValue(networkInterface, serviceConfig.IP) - - time.Sleep(2 * time.Second) - - if !crcstrings.Contains(getInterfaceNameserverValues(networkInterface), serviceConfig.IP) { - return fmt.Errorf("Nameserver %s not successfully set on interface %s. Perhaps you can try this new network mode: https://github.com/crc-org/crc/wiki/VPN-support--with-an--userland-network-stack", serviceConfig.IP, networkInterface) + if serviceConfig.NetworkMode != network.UserNetworkingMode { + return fmt.Errorf("only user-mode networking is supported on Windows") } - return nil -} - -func getInterfaceNameserverValues(iface string) []string { - getDNSServerCommand := fmt.Sprintf(`(Get-DnsClientServerAddress "%s")[0].ServerAddresses`, iface) - stdOut, _, _ := powershell.Execute(getDNSServerCommand) - - return crcstrings.SplitLines(stdOut) -} - -func setInterfaceNameserverValue(iface string, address string) { - exe := "netsh" - args := fmt.Sprintf(`interface ip set dns "%s" static %s primary`, iface, address) - - // ignore the error as this is useless (prefer not to use nolint here) - _ = win32.ShellExecuteAsAdmin(fmt.Sprintf("add dns server address to interface %s", iface), win32.HwndDesktop, exe, args, "", 0) + return addOpenShiftHosts(serviceConfig) } diff --git a/pkg/drivers/hyperv/hyperv_windows.go b/pkg/drivers/hyperv/hyperv_windows.go index 40705bf69b..c7e01c09fa 100644 --- a/pkg/drivers/hyperv/hyperv_windows.go +++ b/pkg/drivers/hyperv/hyperv_windows.go @@ -2,7 +2,6 @@ package hyperv import ( "encoding/json" - "errors" "fmt" "os/exec" "strconv" @@ -19,7 +18,6 @@ import ( type Driver struct { *drivers.VMDriver - VirtualSwitch string MacAddress string DisableDynamicMemory bool } @@ -130,16 +128,7 @@ func (d *Driver) PreCreateCheck() error { return ErrNotAdministrator } - if d.VirtualSwitch == "" { - return nil - } - - // Check that there is a virtual switch already configured - if _, err := d.chooseVirtualSwitch(); err != nil { - return err - } - - return err + return nil } func (d *Driver) getDiskPath() string { @@ -183,24 +172,14 @@ func (d *Driver) Create() error { "-Path", fmt.Sprintf("'%s'", d.ResolveStorePath(".")), "-MemoryStartupBytes", toMb(d.Memory), } - if d.VirtualSwitch != "" { - virtualSwitch, err := d.chooseVirtualSwitch() - if err != nil { - return err - } - log.Debugf("Using switch %q", virtualSwitch) - args = append(args, "-SwitchName", quote(virtualSwitch)) - } log.Debugf("Creating VM...") if err := cmd(args...); err != nil { return err } - if d.VirtualSwitch == "" { - if err := cmd("Hyper-V\\Remove-VMNetworkAdapter", "-VMName", d.MachineName); err != nil { - return err - } + if err := cmd("Hyper-V\\Remove-VMNetworkAdapter", "-VMName", d.MachineName); err != nil { + return err } if d.DisableDynamicMemory { @@ -219,14 +198,6 @@ func (d *Driver) Create() error { } } - if d.VirtualSwitch != "" && d.MacAddress != "" { - if err := cmd("Hyper-V\\Set-VMNetworkAdapter", - "-VMName", d.MachineName, - "-StaticMacAddress", fmt.Sprintf("\"%s\"", d.MacAddress)); err != nil { - return err - } - } - // Disables creating checkpoints and Automatic Start // Shuts down the VM when host shuts down if err := cmd("Hyper-V\\Set-VM", @@ -247,51 +218,6 @@ func (d *Driver) Create() error { } -func (d *Driver) chooseVirtualSwitch() (string, error) { - if d.VirtualSwitch == "" { - return "", errors.New("no virtual switch given") - } - - stdout, err := cmdOut("[Console]::OutputEncoding = [Text.Encoding]::UTF8; (Hyper-V\\Get-VMSwitch).Name") - if err != nil { - return "", err - } - - switches := crcstrings.SplitLines(stdout) - - found := false - for _, name := range switches { - if name == d.VirtualSwitch { - found = true - break - } - } - - if !found { - return "", fmt.Errorf("virtual switch %q not found", d.VirtualSwitch) - } - - return d.VirtualSwitch, nil -} - -// waitForIP waits until the host has a valid IP -func (d *Driver) waitForIP() (string, error) { - if d.VirtualSwitch == "" { - return "", errors.New("no virtual switch given") - } - - log.Debugf("Waiting for host to start...") - - for { - ip, _ := d.GetIP() - if ip != "" { - return ip, nil - } - - time.Sleep(1 * time.Second) - } -} - // waitStopped waits until the host is stopped func (d *Driver) waitStopped() error { log.Debugf("Waiting for host to stop...") @@ -316,17 +242,6 @@ func (d *Driver) Start() error { return err } - if d.VirtualSwitch == "" { - return nil - } - - ip, err := d.waitForIP() - if err != nil { - return err - } - - d.IPAddress = ip - return nil } @@ -380,32 +295,6 @@ func (d *Driver) Kill() error { return nil } -func (d *Driver) GetIP() (string, error) { - if d.VirtualSwitch == "" { - return "", errors.New("no virtual switch given") - } - - s, err := d.GetState() - if err != nil { - return "", err - } - if s != state.Running { - return "", drivers.ErrHostIsNotRunning - } - - stdout, err := cmdOut("((", "Hyper-V\\Get-VM", d.MachineName, ").networkadapters[0]).ipaddresses[0]") - if err != nil { - return "", err - } - - resp := crcstrings.FirstLine(stdout) - if resp == "" { - return "", fmt.Errorf("IP not found") - } - - return resp, nil -} - func (d *Driver) GetSharedDirs() ([]drivers.SharedDir, error) { for _, dir := range d.SharedDirs { if !smbShareExists(dir.Tag) { diff --git a/pkg/os/windows/network/vswitch_windows.go b/pkg/os/windows/network/vswitch_windows.go deleted file mode 100644 index bf5cf5786a..0000000000 --- a/pkg/os/windows/network/vswitch_windows.go +++ /dev/null @@ -1,47 +0,0 @@ -package network - -import ( - "fmt" - "strings" - - "github.com/crc-org/crc/v2/pkg/os/windows/powershell" -) - -const hypervDefaultVirtualSwitchID = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444" - -func SelectSwitchByNameOrDefault(name string) (bool, string) { - // if named exists - if ExistsSwitchByName(name) { - return true, name - } - - // else use Default - return GetDefaultSwitchName() -} - -func ExistsSwitchByName(name string) bool { - getSwitchByNameCmd := fmt.Sprintf("Get-VMSwitch %s | ForEach-Object { $_.Name }", name) - stdOut, stdErr, _ := powershell.Execute(getSwitchByNameCmd) - - // If stdErr contains the command then execution failed - if strings.Contains(stdErr, "Get-VMSwitch") { - return false - } - - if strings.Contains(stdOut, name) { - return true - } - return false -} - -func GetDefaultSwitchName() (bool, string) { - getDefaultSwitchNameCmd := fmt.Sprintf("[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-VMSwitch -Id %s | ForEach-Object { $_.Name }", hypervDefaultVirtualSwitchID) - stdOut, stdErr, _ := powershell.Execute(getDefaultSwitchNameCmd) - - // If stdErr contains the command then execution failed - if strings.Contains(stdErr, "Get-VMSwitch") { - return false, "" - } - - return true, strings.TrimSpace(stdOut) -}