Skip to content

Commit

Permalink
stop 'kubectl port-forward' when the parent process (d8s) exits
Browse files Browse the repository at this point in the history
  • Loading branch information
damoon committed Feb 17, 2022
1 parent 2acbeec commit fef9154
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
53 changes: 35 additions & 18 deletions pkg/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package d8s

import (
"context"
"errors"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -32,12 +33,11 @@ func Run(ctx context.Context, allowContext string, command []string) error {
return fmt.Errorf("select free local port: %v", err)
}

go portForwardForever(ctx, localPort, dindPort)

err = awaitPortOpen(ctx, localPort)
cancel, err := startPortForward(ctx, localPort, dindPort)
if err != nil {
return fmt.Errorf("wait for port forward to start: %v", err)
return fmt.Errorf("starting port-forward: %v", err)
}
defer cancel()

// execute command
err = executeCommand(ctx, command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort))
Expand Down Expand Up @@ -85,23 +85,40 @@ func freePort() (int, error) {
return l.Addr().(*net.TCPAddr).Port, nil
}

func portForwardForever(ctx context.Context, localPort, dindPort int) {
err := portForward(ctx, localPort, dindPort)
if err != nil {
log.Printf("port forward failed: %v", err)
}

for {
select {
case <-ctx.Done():
return
case <-time.After(10 * time.Millisecond):
err := portForward(ctx, localPort, dindPort)
if err != nil {
log.Printf("port forward failed: %v", err)
func startPortForward(ctx context.Context, localPort, dindPort int) (func(), error) {
ctx, cancel := context.WithCancel(ctx)
done := make(chan interface{})

go func() {
defer func() {
done <- struct{}{}
}()

for {
select {
case <-ctx.Done():
return
case <-time.After(10 * time.Millisecond):
err := portForward(ctx, localPort, dindPort)
isDone := len(ctx.Done()) > 0 || errors.Is(ctx.Err(), context.Canceled)

if err != nil && !isDone {
log.Printf("port forward failed: %v", err)
}
}
}
}()

err := awaitPortOpen(ctx, localPort)
if err != nil {
cancel()
return nil, fmt.Errorf("wait for port forward to start: %v", err)
}

return func() {
cancel()
<-done
}, nil
}

func portForward(ctx context.Context, localPort, dinnerPort int) error {
Expand Down
7 changes: 3 additions & 4 deletions pkg/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ func Up(ctx context.Context, allowContext string, command []string) error {
return fmt.Errorf("select free local port: %v", err)
}

go portForwardForever(ctx, localPort, dindPort)

err = awaitPortOpen(ctx, localPort)
cancel, err := startPortForward(ctx, localPort, dindPort)
if err != nil {
return fmt.Errorf("wait for port forward to start: %v", err)
return fmt.Errorf("starting port-forward: %v", err)
}
defer cancel()

// execute command
err = executeCommand(ctx, command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort))
Expand Down

0 comments on commit fef9154

Please sign in to comment.