Skip to content

Commit 8ab2d9d

Browse files
gbraadanjannath
authored andcommitted
Issue #8 Use maininterface instead for Hyper-V
1 parent 9a67480 commit 8ab2d9d

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

pkg/crc/services/dns/dns_windows.go

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dns
22

33
import (
4+
"bufio"
45
"fmt"
56
"strings"
67
"time"
@@ -12,17 +13,15 @@ import (
1213
)
1314

1415
func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *services.ServicePostStartResult) (services.ServicePostStartResult, error) {
15-
// NOTE: this is very Hyper-V specific
16-
// TODO: localize the use of the Default Switch
17-
setDNSServerCommand := fmt.Sprintf(`Set-DnsClientServerAddress "vEthernet (Default Switch)" -ServerAddress ("%s")`, serviceConfig.IP)
18-
powershell.ExecuteAsAdmin(setDNSServerCommand)
16+
mainInterface := getMainInterface()
17+
serverAddresses := getInterfaceNameserverValues(mainInterface)
18+
serverAddresses = append([]string{serviceConfig.IP}, serverAddresses...)
1919

20-
time.Sleep(2 * time.Second)
20+
setInterfaceNameserverValues(mainInterface, serverAddresses)
2121

22-
getDNSServerCommand := `(Get-DnsClientServerAddress "vEthernet (Default Switch)").ServerAddresses`
23-
stdOut, _, _ := powershell.Execute(getDNSServerCommand)
22+
time.Sleep(2 * time.Second)
2423

25-
if !strings.Contains(stdOut, serviceConfig.IP) {
24+
if !contains(getInterfaceNameserverValues(mainInterface), serviceConfig.IP) {
2625
err := errors.New("Nameserver not successfully set")
2726
result.Success = false
2827
result.Error = err.Error()
@@ -32,3 +31,56 @@ func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *se
3231
result.Success = true
3332
return *result, nil
3433
}
34+
35+
func getInterfaceNameserverValues(iface string) []string {
36+
getDNSServerCommand := fmt.Sprintf(`(Get-DnsClientServerAddress "%s")[0].ServerAddresses`, iface)
37+
stdOut, _, _ := powershell.Execute(getDNSServerCommand)
38+
39+
return parseLines(stdOut)
40+
}
41+
42+
func contains(s []string, e string) bool {
43+
for _, v := range s {
44+
if v == e {
45+
return true
46+
}
47+
}
48+
return false
49+
}
50+
51+
func formatValues(serverAddresses []string) string {
52+
var out string
53+
for index, serverAddress := range serverAddresses {
54+
out = fmt.Sprintf(`%s"%s"`, out, serverAddress)
55+
if index < len(serverAddresses)-1 {
56+
out = fmt.Sprintf(`%s, `, out)
57+
}
58+
}
59+
60+
return out
61+
}
62+
63+
func setInterfaceNameserverValues(iface string, serverAddresses []string) {
64+
setDNSServerCommand := fmt.Sprintf(`Set-DNSClientServerAddress "%s" -ServerAddresses (%s)`,
65+
iface, formatValues(serverAddresses))
66+
67+
powershell.ExecuteAsAdmin(setDNSServerCommand)
68+
}
69+
70+
func getMainInterface() string {
71+
getMainInterfaceCommand := `(Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending)[0].Name`
72+
mainInterface, _, _ := powershell.Execute(getMainInterfaceCommand)
73+
74+
return strings.TrimSpace(mainInterface)
75+
}
76+
77+
func parseLines(input string) []string {
78+
output := []string{}
79+
80+
s := bufio.NewScanner(strings.NewReader(input))
81+
for s.Scan() {
82+
output = append(output, s.Text())
83+
}
84+
85+
return output
86+
}

0 commit comments

Comments
 (0)