mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
support specific namespace short dns resolve
This commit is contained in:
4
TODO.MD
4
TODO.MD
@@ -2,4 +2,6 @@
|
||||
|
||||
## 域名解析功能 --完成
|
||||
|
||||
## 多个service inbound -- 完成
|
||||
## 多个service inbound -- 完成
|
||||
|
||||
## 短域名解析
|
||||
@@ -10,12 +10,18 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func DNS(ip string) error {
|
||||
func DNS(ip string, namespace string) error {
|
||||
var err error
|
||||
if err = os.MkdirAll(filepath.Join("/", "etc", "resolver"), fs.ModePerm); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
filename := filepath.Join("/", "etc", "resolver", "local")
|
||||
fileContent := "nameserver " + ip
|
||||
return ioutil.WriteFile(filename, []byte(fileContent), fs.ModePerm)
|
||||
_ = ioutil.WriteFile(filename, []byte(fileContent), fs.ModePerm)
|
||||
|
||||
filename = filepath.Join("/", "etc", "resolver", namespace)
|
||||
fileContent = "nameserver " + ip + "\nsearch " + namespace + ".svc.cluster.local svc.cluster.local cluster.local\noptions ndots:5"
|
||||
_ = ioutil.WriteFile(filename, []byte(fileContent), fs.ModePerm)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func DNS(ip string) error {
|
||||
func DNS(ip string, namespace string) error {
|
||||
tunName := os.Getenv("tunName")
|
||||
fmt.Println("tun name: " + tunName)
|
||||
args := []string{
|
||||
@@ -24,9 +24,25 @@ func DNS(ip string) error {
|
||||
output, err := exec.Command("netsh", args...).CombinedOutput()
|
||||
fmt.Println(exec.Command("netsh", args...).Args)
|
||||
log.Info(string(output))
|
||||
_ = addNicSuffix(namespace)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// @see https://docs.microsoft.com/en-us/powershell/module/dnsclient/set-dnsclientglobalsetting?view=windowsserver2019-ps#example-1--set-the-dns-suffix-search-list
|
||||
func addNicSuffix(namespace string) error {
|
||||
cmd := exec.Command("PowerShell", []string{
|
||||
"Set-DnsClientGlobalSetting",
|
||||
"-SuffixSearchList",
|
||||
fmt.Sprintf("@(\"%s.svc.cluster.local\", \"svc.cluster.local\")", namespace),
|
||||
}...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
log.Info(string(output))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ func main() {
|
||||
}
|
||||
//time.Sleep(time.Second * 5)
|
||||
dnsServiceIp := util.GetDNSServiceIpFromPod(clientset, restclient, config, util.TrafficManager, namespace)
|
||||
if err := dns.DNS(dnsServiceIp); err != nil {
|
||||
if err := dns.DNS(dnsServiceIp, namespace); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
|
||||
Reference in New Issue
Block a user