From a09a69921aa8c26088e0318427953c2679c79b30 Mon Sep 17 00:00:00 2001 From: Craig Johnston Date: Mon, 26 Oct 2020 15:06:59 -0700 Subject: [PATCH] fixed bug headless service forwarding --- go.sum | 3 +++ pkg/fwdIp/fwdIp.go | 10 ++++---- pkg/fwdnet/fwdnet.go | 4 ++-- pkg/fwdport/fwdport.go | 1 + pkg/fwdservice/fwdservice.go | 46 ++++++++++++++++++------------------ 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/go.sum b/go.sum index 6b9b87ec..40e53288 100644 --- a/go.sum +++ b/go.sum @@ -258,6 +258,7 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXT golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -282,6 +283,7 @@ golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191004110552-13f9640d40b9 h1:rjwSpXsdiK0dV8/Naq3kAw9ymfAeJIyd0upUIElB+lI= golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -311,6 +313,7 @@ golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456 h1:ng0gs1AKnRRuEMZoTLLlbOd+C golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191115151921-52ab43148777 h1:wejkGHRTr38uaKRqECZlsCsJ1/TGxIyFbH32x5zUdu4= golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/pkg/fwdIp/fwdIp.go b/pkg/fwdIp/fwdIp.go index 7ae3f45e..d0a5e1d7 100644 --- a/pkg/fwdIp/fwdIp.go +++ b/pkg/fwdIp/fwdIp.go @@ -20,16 +20,18 @@ var ipRegistry *Registry func init() { ipRegistry = &Registry{ mutex: &sync.Mutex{}, - inc: map[int]map[int]int{0: {0: 0}}, - reg: make(map[string]net.IP), + // counter for the service cluster and namespace + inc: map[int]map[int]int{0: {0: 0}}, + reg: make(map[string]net.IP), } } -func GetIp(podName string, clusterN int, NamespaceN int) (net.IP, error) { +func GetIp(svcName string, podName string, clusterN int, NamespaceN int) (net.IP, error) { ipRegistry.mutex.Lock() defer ipRegistry.mutex.Unlock() - regKey := fmt.Sprintf("%d-%d-%s", clusterN, NamespaceN, podName) + regKey := fmt.Sprintf("%d-%d-%s-%s", clusterN, NamespaceN, svcName, podName) + if ip, ok := ipRegistry.reg[regKey]; ok { return ip, nil } diff --git a/pkg/fwdnet/fwdnet.go b/pkg/fwdnet/fwdnet.go index 492b10cd..30791cb6 100644 --- a/pkg/fwdnet/fwdnet.go +++ b/pkg/fwdnet/fwdnet.go @@ -13,9 +13,9 @@ import ( // ReadyInterface prepares a local IP address on // the loopback interface. -func ReadyInterface(podName string, clusterN int, namespaceN int, port string) (net.IP, error) { +func ReadyInterface(svcName string, podName string, clusterN int, namespaceN int, port string) (net.IP, error) { - ip, _ := fwdIp.GetIp(podName, clusterN, namespaceN) + ip, _ := fwdIp.GetIp(svcName, podName, clusterN, namespaceN) // lo means we are probably on linux and not mac _, err := net.InterfaceByName("lo") diff --git a/pkg/fwdport/fwdport.go b/pkg/fwdport/fwdport.go index 712af4c9..5f70b6b7 100644 --- a/pkg/fwdport/fwdport.go +++ b/pkg/fwdport/fwdport.go @@ -163,6 +163,7 @@ func (pfo *PortForwardOpts) PortForward() error { // Blocking call if err = fw.ForwardPorts(); err != nil { + log.Errorf("ForwardPorts error: %s", err.Error()) pfo.Stop() return err } diff --git a/pkg/fwdservice/fwdservice.go b/pkg/fwdservice/fwdservice.go index 44e49cf0..20b4ddb1 100644 --- a/pkg/fwdservice/fwdservice.go +++ b/pkg/fwdservice/fwdservice.go @@ -202,13 +202,32 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost } podPort := "" - svcName := "" - localIp, err := fwdnet.ReadyInterface(pod.Name, svcFwd.ClusterN, svcFwd.NamespaceN, podPort) + serviceHostName := svcFwd.Svc.Name + svcName := svcFwd.Svc.Name + + if includePodNameInHost { + serviceHostName = pod.Name + "." + svcFwd.Svc.Name + svcName = pod.Name + "." + svcFwd.Svc.Name + } + + localIp, err := fwdnet.ReadyInterface(svcName, pod.Name, svcFwd.ClusterN, svcFwd.NamespaceN, podPort) if err != nil { log.Warnf("WARNING: error readying interface: %s\n", err) } + // if this is not the first namespace on the + // first cluster then append the namespace + if svcFwd.NamespaceN > 0 { + serviceHostName = serviceHostName + "." + pod.Namespace + } + + // if this is not the first cluster append the full + // host name + if svcFwd.ClusterN > 0 { + serviceHostName = serviceHostName + "." + svcFwd.Context + } + for _, port := range svcFwd.Svc.Spec.Ports { podPort = port.TargetPort.String() @@ -221,29 +240,10 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost } } - serviceHostName := svcFwd.Svc.Name - svcName = svcFwd.Svc.Name - - if includePodNameInHost { - serviceHostName = pod.Name + "." + serviceHostName - svcName = pod.Name + "." + serviceHostName - } - - // if this is not the first namespace on the - // first cluster then append the namespace - if svcFwd.NamespaceN > 0 { - serviceHostName = serviceHostName + "." + pod.Namespace - } - - // if this is not the first cluster append the full - // host name - if svcFwd.ClusterN > 0 { - serviceHostName = serviceHostName + "." + svcFwd.Context - } - - log.Debugf("Resolving: %s to %s\n", + log.Debugf("Resolving: %s to %s (%s)\n", serviceHostName, localIp.String(), + svcName, ) log.Printf("Port-Forward: %s %s:%d to pod %s:%s\n",