Skip to content

Commit

Permalink
fix: restore mapping ports functionality (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredallard authored Sep 30, 2020
1 parent 6fa2c77 commit c5f8cae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
31 changes: 16 additions & 15 deletions cmd/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ":")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/expose/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/ssh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c5f8cae

Please sign in to comment.