From c5f8cae69e3eeaa51d4dfbae42263bfbfcac1211 Mon Sep 17 00:00:00 2001 From: Jared Allard Date: Wed, 30 Sep 2020 16:43:53 -0700 Subject: [PATCH] fix: restore mapping ports functionality (#16) --- cmd/localizer/localizer.go | 31 ++++++++++++++++--------------- internal/expose/port.go | 1 + internal/kube/client.go | 2 +- internal/ssh/client.go | 4 ++-- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cmd/localizer/localizer.go b/cmd/localizer/localizer.go index 692653b..a43513c 100644 --- a/cmd/localizer/localizer.go +++ b/cmd/localizer/localizer.go @@ -111,6 +111,19 @@ func main() { //nolint:funlen,gocyclo return errors.Wrap(err, "failed to resolve service ports") } + // if we couldn't find endpoints, then we fall back to binding whatever the + // public port of the service is if it is named + if !exists { + for i, sp := range servicePorts { + if servicePorts[i].TargetPort.Type == intstr.String { + log.Warnf("failed to determine the value of port %s, using public port %d", sp.TargetPort.String(), sp.Port) + servicePorts[i].TargetPort = intstr.FromInt(int(sp.Port)) + } + } + + log.Debug("service has no endpoints") + } + log.Debugf("map %v", c.StringSlice("map")) for _, portOverride := range c.StringSlice("map") { spl := strings.Split(portOverride, ":") @@ -130,26 +143,14 @@ func main() { //nolint:funlen,gocyclo // TODO: this is slow... for i, sp := range servicePorts { - log.Debugf("checking if we need to map %v, using %d:%d", sp.TargetPort, rem, local) - if uint(servicePorts[i].TargetPort.IntVal) == uint(rem) { + log.Debugf("checking if we need to map %s, using %d:%d", sp.TargetPort.String(), rem, local) + if uint(servicePorts[i].TargetPort.IntValue()) == uint(rem) { + log.Debugf("mapping remote port %d -> %d locally", rem, local) servicePorts[i].MappedPort = uint(local) } } } - // if we couldn't find endpoints, then we fall back to binding whatever the - // public port of the service is - if !exists { - for i, sp := range servicePorts { - servicePorts[i].TargetPort = intstr.FromInt(int(sp.Port)) - } - } - - // if there's no endpoints - if !exists { - log.Debug("service has no endpoints") - } - e := expose.NewExposer(k, kconf, log) if err := e.Start(ctx); err != nil { return err diff --git a/internal/expose/port.go b/internal/expose/port.go index 9bc19d2..8dcb6eb 100644 --- a/internal/expose/port.go +++ b/internal/expose/port.go @@ -232,6 +232,7 @@ func (p *ServiceForward) Start(ctx context.Context) error { for i, port := range p.Ports { prt := int(port.TargetPort.IntVal) ports[i] = fmt.Sprintf("%d:%d", port.MappedPort, prt) + p.c.log.Debugf("tunneling port %v", ports[i]) } cleanupFn, localPort, err := p.createServerPodAndTransport(ctx) diff --git a/internal/kube/client.go b/internal/kube/client.go index 7fea58b..1a2e573 100644 --- a/internal/kube/client.go +++ b/internal/kube/client.go @@ -141,7 +141,7 @@ func ResolveServicePorts(ctx context.Context, k kubernetes.Interface, servicePorts[i] = ResolvedServicePort{ p, original, - uint(p.TargetPort.IntVal), + uint(p.TargetPort.IntValue()), } } diff --git a/internal/ssh/client.go b/internal/ssh/client.go index 09ae063..17d7deb 100644 --- a/internal/ssh/client.go +++ b/internal/ssh/client.go @@ -59,7 +59,7 @@ func NewReverseTunnelClient(l logrus.FieldLogger, host string, port int, ports [ panic(err) } - rport, err := strconv.Atoi(ports[0]) + rport, err := strconv.Atoi(ports[1]) if err != nil { panic(err) } @@ -114,7 +114,7 @@ func (c *Client) Start(ctx context.Context) error { defer listener.Close() defer wg.Done() - c.log.Infof("starting tunnel to %d", remotePort) + c.log.Infof("created tunnel from remote %d to %s", remotePort, localAddr) // handle incoming connections on reverse forwarded tunnel for {