Skip to content

Commit

Permalink
windows: Remove system networking
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cfergeau authored and praveenkumar committed Sep 23, 2024
1 parent 5122f5e commit 593eaab
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 259 deletions.
6 changes: 0 additions & 6 deletions pkg/crc/machine/hyperv/constants_windows.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/crc/machine/hyperv/driver_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
26 changes: 0 additions & 26 deletions pkg/crc/preflight/preflight_checks_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 0 additions & 15 deletions pkg/crc/preflight/preflight_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/preflight/preflight_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 3 additions & 39 deletions pkg/crc/services/dns/dns_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
117 changes: 3 additions & 114 deletions pkg/drivers/hyperv/hyperv_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hyperv

import (
"encoding/json"
"errors"
"fmt"
"os/exec"
"strconv"
Expand All @@ -19,7 +18,6 @@ import (

type Driver struct {
*drivers.VMDriver
VirtualSwitch string
MacAddress string
DisableDynamicMemory bool
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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...")
Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 0 additions & 47 deletions pkg/os/windows/network/vswitch_windows.go

This file was deleted.

0 comments on commit 593eaab

Please sign in to comment.