Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
flupec authored and Vasilii Avtaev committed Jun 25, 2021
1 parent f0d48f7 commit 99daa4a
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions pkg/fwdservice/fwdservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fwdservice
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -211,17 +212,9 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost
Output: false,
}

// Ip address handout is a critical section for synchronization,
// use a lock which synchronizes inside each namespace.
svcFwd.NamespaceServiceLock.Lock()
defer svcFwd.NamespaceServiceLock.Unlock()

for _, pod := range pods {
// If pod is already configured to be forwarded, skip it
if _, found := svcFwd.PortForwards[pod.Name]; found {
continue
}

var localIp net.IP
podIpReady := false
podPort := ""

serviceHostName := svcFwd.Svc.Name
Expand All @@ -232,11 +225,6 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost
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 {
Expand Down Expand Up @@ -276,20 +264,6 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost
}
}

log.Debugf("Resolving: %s to %s (%s)\n",
serviceHostName,
localIp.String(),
svcName,
)

log.Printf("Port-Forward: %s %s:%d to pod %s:%s\n",
localIp.String(),
serviceHostName,
port.Port,
pod.Name,
podPort,
)

pfo := &fwdport.PortForwardOpts{
Out: publisher,
Config: svcFwd.ClientConfig,
Expand All @@ -301,7 +275,6 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost
ServiceFwd: svcFwd,
PodName: pod.Name,
PodPort: podPort,
LocalIp: localIp,
LocalPort: localPort,
HostFile: svcFwd.Hostfile,
ClusterN: svcFwd.ClusterN,
Expand All @@ -312,6 +285,35 @@ func (svcFwd *ServiceFWD) LoopPodsToForward(pods []v1.Pod, includePodNameInHost
DoneChan: make(chan struct{}),
}

// If port-forwarding on pod under exact port is already configured, then skip it
if runningPortForwards:= svcFwd.GetServicePodPortForwards(pfo.Service); len(runningPortForwards) > 0 && svcFwd.contains(runningPortForwards, pfo) {
continue
}

if !podIpReady { // We need to Ready interface only once per pod
if localIp, err = fwdnet.ReadyInterface(svcName, pod.Name, svcFwd.ClusterN, svcFwd.NamespaceN, ""); err == nil {
podIpReady = true
} else {
log.Warnf("WARNING: error readying interface: %s\n", err)
}
}

log.Debugf("Resolving: %s to %s (%s)\n",
serviceHostName,
localIp.String(),
svcName,
)

log.Printf("Port-Forward: %s %s:%d to pod %s:%s\n",
localIp.String(),
serviceHostName,
port.Port,
pod.Name,
podPort,
)

pfo.LocalIp = localIp

// Fire and forget. The stopping is done in the service.Shutdown() method.
go func() {
svcFwd.AddServicePod(pfo)
Expand Down

0 comments on commit 99daa4a

Please sign in to comment.