Skip to content

Commit

Permalink
Issue #8 Use maininterface instead for Hyper-V
Browse files Browse the repository at this point in the history
  • Loading branch information
gbraad committed Aug 1, 2019
1 parent c98f8db commit c37d2e2
Showing 1 changed file with 60 additions and 8 deletions.
68 changes: 60 additions & 8 deletions pkg/crc/services/dns/dns_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dns

import (
"bufio"
"fmt"
"strings"
"time"
Expand All @@ -12,17 +13,15 @@ import (
)

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

time.Sleep(2 * time.Second)
setInterfaceNameserverValues(mainInterface, serverAddresses)

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

if !strings.Contains(stdOut, serviceConfig.IP) {
if !contains(getInterfaceNameserverValues(mainInterface), serviceConfig.IP) {
err := errors.New("Nameserver not successfully set")
result.Success = false
result.Error = err.Error()
Expand All @@ -32,3 +31,56 @@ func runPostStartForOS(serviceConfig services.ServicePostStartConfig, result *se
result.Success = true
return *result, nil
}

func getInterfaceNameserverValues(iface string) []string {
getDNSServerCommand := fmt.Sprintf(`(Get-DnsClientServerAddress "%s")[0].ServerAddresses`, iface)
stdOut, _, _ := powershell.Execute(getDNSServerCommand)

return parseLines(stdOut)
}

func contains(s []string, e string) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}

func formatValues(serverAddresses []string) string {
var out string
for index, serverAddress := range serverAddresses {
out = fmt.Sprintf(`%s"%s"`, out, serverAddress)
if index < len(serverAddresses)-1 {
out = fmt.Sprintf(`%s, `, out)
}
}

return out
}

func setInterfaceNameserverValues(iface string, serverAddresses []string) {
setDNSServerCommand := fmt.Sprintf(`Set-DNSClientServerAddress "%s" -ServerAddresses (%s)`,
iface, formatValues(serverAddresses))

powershell.ExecuteAsAdmin(setDNSServerCommand)
}

func getMainInterface() string {
getMainInterfaceCommand := `(Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending)[0].Name`
mainInterface, _, _ := powershell.Execute(getMainInterfaceCommand)

return strings.TrimSpace(mainInterface)
}

func parseLines(input string) []string {
output := []string{}

s := bufio.NewScanner(strings.NewReader(input))
for s.Scan() {
output = append(output, s.Text())
}

return output
}

0 comments on commit c37d2e2

Please sign in to comment.