support linux dns resolve, short domain name resolve

This commit is contained in:
p_caiwfeng
2021-08-21 23:52:53 +08:00
parent 9071c5c2d9
commit 71bf071b18
3 changed files with 38 additions and 6 deletions

30
dns/dns_linux.go Normal file
View File

@@ -0,0 +1,30 @@
// +build linux
package dns
import (
log "github.com/sirupsen/logrus"
"os"
"os/exec"
)
func DNS(ip string, namespace string) error {
tunName := os.Getenv("tunName")
if len(tunName) == 0 {
tunName = "tun0"
}
cmd := exec.Command("systemd-resolve", []string{
"--set-dns",
ip,
"--interface",
tunName,
"--set-domain=" + namespace + ".svc.cluster.local",
"--set-domain=svc.cluster.local",
}...)
output, err := cmd.CombinedOutput()
if err != nil {
log.Warnf("cmd: %s, output: %s, error: %v\n", cmd.Args, string(output), err)
}
return nil
}

View File

@@ -1,4 +1,4 @@
// +build !windows // +build darwin
package dns package dns

View File

@@ -170,11 +170,7 @@ func main() {
if err := start(); err != nil { if err := start(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
//time.Sleep(time.Second * 5)
dnsServiceIp := util.GetDNSServiceIpFromPod(clientset, restclient, config, util.TrafficManager, namespace)
if err := dns.DNS(dnsServiceIp, namespace); err != nil {
log.Fatal(err)
}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
if !util.FindRule() { if !util.FindRule() {
util.AddFirewallRule() util.AddFirewallRule()
@@ -183,6 +179,12 @@ func main() {
} }
log.Info("dns service ok") log.Info("dns service ok")
_ = exec.Command("ping", "-c", "4", "223.254.254.100").Run() _ = exec.Command("ping", "-c", "4", "223.254.254.100").Run()
//time.Sleep(time.Second * 5)
dnsServiceIp := util.GetDNSServiceIpFromPod(clientset, restclient, config, util.TrafficManager, namespace)
if err := dns.DNS(dnsServiceIp, namespace); err != nil {
log.Fatal(err)
}
select {} select {}
} }