Skip to content

Commit

Permalink
feat: forward cluster dns to local
Browse files Browse the repository at this point in the history
  • Loading branch information
linfan committed Feb 20, 2022
1 parent 82a26f9 commit 6c60b15
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 17 deletions.
15 changes: 13 additions & 2 deletions pkg/kt/command/connect/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
opt "github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/kt/service/cluster"
"github.com/alibaba/kt-connect/pkg/kt/service/dns"
"github.com/alibaba/kt-connect/pkg/kt/transmission"
"github.com/alibaba/kt-connect/pkg/kt/util"
"github.com/rs/zerolog/log"
coreV1 "k8s.io/api/core/v1"
"strings"
"time"
)

func setupDns(shadowPodIp string) error {
func setupDns(shadowPodName, shadowPodIp string) error {
namespace := opt.Get().Namespace
if strings.HasPrefix(opt.Get().ConnectOptions.DnsMode, util.DnsModeHosts) {
log.Info().Msgf("Setting up dns in hosts mode")
dump2HostsNamespaces := ""
pos := len(util.DnsModeHosts)
if len(opt.Get().ConnectOptions.DnsMode) > pos + 1 && opt.Get().ConnectOptions.DnsMode[pos:pos+1] == ":" {
Expand All @@ -25,8 +27,10 @@ func setupDns(shadowPodIp string) error {
return err
}
} else if opt.Get().ConnectOptions.DnsMode == util.DnsModePodDns {
log.Info().Msgf("Setting up dns in pod mode")
return dns.Ins().SetNameServer(shadowPodIp)
} else if opt.Get().ConnectOptions.DnsMode == util.DnsModeLocalDns {
log.Info().Msgf("Setting up dns in local mode")
headlessPods, err := dumpCurrentNamespaceToHost(namespace)
if err != nil {
return err
Expand All @@ -49,13 +53,20 @@ func setupDns(shadowPodIp string) error {
headlessPods, _ = dumpCurrentNamespaceToHost(namespace)
}
}, nil)

forwardedPodPort, err := util.GetRandomTcpPort()
if err != nil {
return err
}
transmission.SetupPortForwardToLocal(shadowPodName, common.StandardDnsPort, forwardedPodPort)

dnsPort := util.AlternativeDnsPort
if util.IsWindows() {
dnsPort = common.StandardDnsPort
}
// must setup name server before change dns config
// otherwise the upstream name server address will be incorrect in linux
if err = dns.SetupLocalDns(shadowPodIp, dnsPort); err != nil {
if err = dns.SetupLocalDns(forwardedPodPort, dnsPort); err != nil {
log.Error().Err(err).Msgf("Failed to setup local dns server")
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kt/command/connect/sshuttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connect

import (
"context"
"github.com/alibaba/kt-connect/pkg/common"
opt "github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/kt/service/cluster"
"github.com/alibaba/kt-connect/pkg/kt/service/sshuttle"
Expand All @@ -27,7 +28,7 @@ func BySshuttle() error {
if err != nil {
return err
}
stop, err := transmission.ForwardSSHTunnelToLocal(podName, localSshPort)
stop, err := transmission.SetupPortForwardToLocal(podName, common.StandardSshPort, localSshPort)
if err != nil {
return err
}
Expand All @@ -41,7 +42,7 @@ func BySshuttle() error {
return err
}

return setupDns(podIP)
return setupDns(podName, podIP)
}

func checkSshuttleInstalled() {
Expand Down
7 changes: 4 additions & 3 deletions pkg/kt/command/connect/tun2socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connect

import (
"fmt"
"github.com/alibaba/kt-connect/pkg/common"
opt "github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/kt/service/cluster"
"github.com/alibaba/kt-connect/pkg/kt/service/sshchannel"
Expand All @@ -24,7 +25,7 @@ func ByTun2Socks() error {
if err != nil {
return err
}
if _, err = transmission.ForwardSSHTunnelToLocal(podName, localSshPort); err != nil {
if _, err = transmission.SetupPortForwardToLocal(podName, common.StandardSshPort, localSshPort); err != nil {
return err
}
if err = startSocks5Connection(privateKeyPath, localSshPort); err != nil {
Expand All @@ -34,7 +35,7 @@ func ByTun2Socks() error {
if opt.Get().ConnectOptions.DisableTunDevice {
showSetupSocksMessage(opt.Get().ConnectOptions.SocksPort)
if strings.HasPrefix(opt.Get().ConnectOptions.DnsMode, util.DnsModeHosts) {
return setupDns(podIP)
return setupDns(podName, podIP)
} else {
return nil
}
Expand All @@ -54,7 +55,7 @@ func ByTun2Socks() error {
}
log.Info().Msgf("Route to tun device completed")
}
return setupDns(podIP)
return setupDns(podName, podIP)
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/kt/service/dns/dnsserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ type DnsServer struct {
upstreamDnsAddr string
}

func SetupLocalDns(shadowPodIp string, dnsPort int) error {
func SetupLocalDns(remoteDnsPort, localDnsPort int) error {
var success = make(chan error)
go func() {
time.Sleep(1 * time.Second)
success <-nil
}()
go func() {
nameserver := GetNameServer()
log.Debug().Msgf("Setup local DNS with shadow pod %s and upstream %s", shadowPodIp, nameserver)
log.Debug().Msgf("Setup local DNS with shadow pod %s:%d and upstream %s:%d",
common.Localhost, remoteDnsPort, nameserver, common.StandardDnsPort)
success <-common.SetupDnsServer(&DnsServer{
clusterDnsAddr: fmt.Sprintf("%s:%d", shadowPodIp, common.StandardDnsPort),
clusterDnsAddr: fmt.Sprintf("%s:%d", common.Localhost, remoteDnsPort),
upstreamDnsAddr: fmt.Sprintf("%s:%d", nameserver, common.StandardDnsPort),
}, dnsPort, "udp")
}, localDnsPort, "udp")
}()
return <-success
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kt/transmission/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transmission

import (
"fmt"
"github.com/alibaba/kt-connect/pkg/common"
opt "github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/kt/service/sshchannel"
"github.com/alibaba/kt-connect/pkg/kt/util"
Expand All @@ -19,7 +20,7 @@ func ForwardPodToLocal(exposePorts, podName, privateKey string) (int, error) {
}

// port forward pod 22 -> local <random port>
if _, err = ForwardSSHTunnelToLocal(podName, localSSHPort); err != nil {
if _, err = SetupPortForwardToLocal(podName, common.StandardSshPort, localSSHPort); err != nil {
return -1, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package transmission
import (
"context"
"fmt"
"github.com/alibaba/kt-connect/pkg/common"
opt "github.com/alibaba/kt-connect/pkg/kt/options"
"github.com/alibaba/kt-connect/pkg/kt/process"
"github.com/alibaba/kt-connect/pkg/kt/service/cluster"
Expand All @@ -19,17 +18,17 @@ import (
"strings"
)

// ForwardSSHTunnelToLocal mapping local port to shadow pod ssh port
func ForwardSSHTunnelToLocal(podName string, localPort int) (chan struct{}, error) {
// SetupPortForwardToLocal mapping local port to shadow pod ssh port
func SetupPortForwardToLocal(podName string, remotePort, localPort int) (chan struct{}, error) {
stop := make(chan struct{})
_, cancel := context.WithCancel(context.Background())
// one of the background process start failed and will cancel the started process
go func() {
process.Stop(<-stop, cancel)
}()
go func() {
if err := portForward(podName, common.StandardSshPort, localPort, stop); err != nil {
log.Error().Err(err).Msgf("Port forward to %d -> %d pod %s interrupted", localPort, common.StandardSshPort, podName)
if err := portForward(podName, remotePort, localPort, stop); err != nil {
log.Error().Err(err).Msgf("Port forward to %d -> %d pod %s interrupted", localPort, remotePort, podName)
stop <- struct{}{}
}
}()
Expand Down
File renamed without changes.

0 comments on commit 6c60b15

Please sign in to comment.