Skip to content

Commit

Permalink
fixed bug headless service forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Oct 26, 2020
1 parent 79af4b7 commit a09a699
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
10 changes: 6 additions & 4 deletions pkg/fwdIp/fwdIp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fwdnet/fwdnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/fwdport/fwdport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
46 changes: 23 additions & 23 deletions pkg/fwdservice/fwdservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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",
Expand Down

0 comments on commit a09a699

Please sign in to comment.