-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from rancher-sandbox/dns
Pass DNS addresses from en0 interface to guest instance
- Loading branch information
Showing
8 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package osutil | ||
|
||
import "github.com/lima-vm/lima/pkg/sysprof" | ||
|
||
func DNSAddresses() ([]string, error) { | ||
nwData, err := sysprof.NetworkData() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var addresses []string | ||
if len(nwData) > 0 { | ||
// Return DNS addresses from en0 interface | ||
for _, nw := range nwData { | ||
if nw.Interface == "en0" { | ||
addresses = nw.DNS.ServerAddresses | ||
break | ||
} | ||
} | ||
// In case "en0" is not found, use the addresses of the first interface | ||
if len(addresses) == 0 { | ||
addresses = nwData[0].DNS.ServerAddresses | ||
} | ||
} | ||
return addresses, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !darwin | ||
// +build !darwin | ||
|
||
package osutil | ||
|
||
func DNSAddresses() ([]string, error) { | ||
// TODO: parse /etc/resolv.conf? | ||
return []string{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package osutil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package sysprof | ||
|
||
type SPNetworkDataType struct { | ||
SPNetworkDataType []NetworkDataType `json:"SPNetworkDataType"` | ||
} | ||
|
||
type NetworkDataType struct { | ||
DNS DNS `json:"DNS"` | ||
Interface string `json:"interface"` | ||
} | ||
|
||
type DNS struct { | ||
ServerAddresses []string `json:"ServerAddresses"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sysprof | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"sync" | ||
) | ||
|
||
var ( | ||
networkDataOnce sync.Once | ||
networkDataCached SPNetworkDataType | ||
networkDataError error | ||
) | ||
|
||
func NetworkData() ([]NetworkDataType, error) { | ||
networkDataOnce.Do(func() { | ||
jsonBytes, networkDataError := SystemProfiler("SPNetworkDataType") | ||
if networkDataError == nil { | ||
networkDataError = json.Unmarshal(jsonBytes, &networkDataCached) | ||
} | ||
}) | ||
return networkDataCached.SPNetworkDataType, networkDataError | ||
} | ||
|
||
func SystemProfiler(dataType string) ([]byte, error) { | ||
var stdout, stderr bytes.Buffer | ||
cmd := exec.Command("system_profiler", dataType, "-json") | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
if err := cmd.Run(); err != nil { | ||
return nil, fmt.Errorf("failed to run %v: stdout=%q, stderr=%q: %w", | ||
cmd.Args, stdout.String(), stderr.String(), err) | ||
} | ||
return stdout.Bytes(), nil | ||
} |