From cfbef7ba696787ca46c6223d10b001032a272e63 Mon Sep 17 00:00:00 2001 From: David Sauer Date: Wed, 16 Feb 2022 09:32:51 +0100 Subject: [PATCH] add context to subcommands --- main.go | 3 ++- pkg/down.go | 9 +++++---- pkg/k8s-context.go | 10 ++++++---- pkg/run.go | 21 +++++++++++++-------- pkg/up.go | 13 +++++++------ 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index 08a271d3..39c5b936 100644 --- a/main.go +++ b/main.go @@ -55,9 +55,10 @@ func main() { } func verifyContext(c *cli.Context) error { + ctx := c.Context allowContext := c.String(allowContext.Name) - allowed, err := d8s.ContextAllowed(allowContext) + allowed, err := d8s.ContextAllowed(ctx, allowContext) if err != nil { return fmt.Errorf("verify kubernetes context: %v", err) } diff --git a/pkg/down.go b/pkg/down.go index 5387b975..568a3f87 100644 --- a/pkg/down.go +++ b/pkg/down.go @@ -11,7 +11,7 @@ import ( func Down(ctx context.Context, allowContext string) error { // verify kubernetes context in use - allowed, err := ContextAllowed(allowContext) + allowed, err := ContextAllowed(ctx, allowContext) if err != nil { return fmt.Errorf("verify kubernetes context: %v", err) } @@ -19,7 +19,7 @@ func Down(ctx context.Context, allowContext string) error { return fmt.Errorf("kubernetes context not allowed") } - err = deleteDind() + err = deleteDind(ctx) if err != nil { return fmt.Errorf("delete dind: %v", err) } @@ -27,8 +27,9 @@ func Down(ctx context.Context, allowContext string) error { return nil } -func deleteDind() error { - cmd := exec.Command( +func deleteDind(ctx context.Context) error { + cmd := exec.CommandContext( + ctx, "kubectl", "delete", "-f-", diff --git a/pkg/k8s-context.go b/pkg/k8s-context.go index fb96a25e..6dabf2c2 100644 --- a/pkg/k8s-context.go +++ b/pkg/k8s-context.go @@ -1,6 +1,7 @@ package d8s import ( + "context" _ "embed" "fmt" "os" @@ -13,8 +14,8 @@ var ( manifest string ) -func ContextAllowed(envVar string) (bool, error) { - contextName, err := kubectlContext() +func ContextAllowed(ctx context.Context, envVar string) (bool, error) { + contextName, err := kubectlContext(ctx) if err != nil { return false, err } @@ -30,8 +31,9 @@ func ContextAllowed(envVar string) (bool, error) { return false, fmt.Errorf("context %s does not appear to be a development environment", contextName) } -func kubectlContext() (string, error) { - cmd := exec.Command( +func kubectlContext(ctx context.Context) (string, error) { + cmd := exec.CommandContext( + ctx, "kubectl", "config", "current-context", diff --git a/pkg/run.go b/pkg/run.go index d76c37c9..dc9d489b 100644 --- a/pkg/run.go +++ b/pkg/run.go @@ -13,7 +13,7 @@ import ( func Run(ctx context.Context, allowContext string, command []string) error { // verify kubernetes context in use - allowed, err := ContextAllowed(allowContext) + allowed, err := ContextAllowed(ctx, allowContext) if err != nil { return fmt.Errorf("verify kubernetes context: %v", err) } @@ -22,7 +22,7 @@ func Run(ctx context.Context, allowContext string, command []string) error { } // port forward - err = awaitDind() + err = awaitDind(ctx) if err != nil { return fmt.Errorf("wait for dind to start: %v", err) } @@ -40,7 +40,7 @@ func Run(ctx context.Context, allowContext string, command []string) error { } // execute command - err = executeCommand(command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort)) + err = executeCommand(ctx, command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort)) if err != nil { return fmt.Errorf("command failed with %s", err) } @@ -48,8 +48,9 @@ func Run(ctx context.Context, allowContext string, command []string) error { return nil } -func awaitDind() error { - cmd := exec.Command( +func awaitDind(ctx context.Context) error { + cmd := exec.CommandContext( + ctx, "kubectl", "wait", "--for=condition=available", @@ -104,7 +105,8 @@ func portForwardForever(ctx context.Context, localPort, dindPort int) { } func portForward(ctx context.Context, localPort, dinnerPort int) error { - cmd := exec.Command( + cmd := exec.CommandContext( + ctx, "kubectl", "port-forward", "deployment/dind", @@ -153,8 +155,11 @@ func portOpen(ctx context.Context, host string, port string) bool { return true } -func executeCommand(command []string, dockerAddr string) error { - cmd := exec.Command(command[0], command[1:]...) +func executeCommand(ctx context.Context, command []string, dockerAddr string) error { + cmd := exec.CommandContext( + ctx, + command[0], + command[1:]...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin diff --git a/pkg/up.go b/pkg/up.go index f6332ee9..c28a398a 100644 --- a/pkg/up.go +++ b/pkg/up.go @@ -14,7 +14,7 @@ const ( func Up(ctx context.Context, allowContext string, command []string) error { // verify kubernetes context in use - allowed, err := ContextAllowed(allowContext) + allowed, err := ContextAllowed(ctx, allowContext) if err != nil { return fmt.Errorf("verify kubernetes context: %v", err) } @@ -23,13 +23,13 @@ func Up(ctx context.Context, allowContext string, command []string) error { } // deploy docker in docker - err = deployDind() + err = deployDind(ctx) if err != nil { return fmt.Errorf("deploy dind: %v", err) } // port forward - err = awaitDind() + err = awaitDind(ctx) if err != nil { return fmt.Errorf("wait for dind to start: %v", err) } @@ -47,7 +47,7 @@ func Up(ctx context.Context, allowContext string, command []string) error { } // execute command - err = executeCommand(command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort)) + err = executeCommand(ctx, command, fmt.Sprintf("tcp://127.0.0.1:%d", localPort)) if err != nil { return fmt.Errorf("command failed with %s", err) } @@ -55,8 +55,9 @@ func Up(ctx context.Context, allowContext string, command []string) error { return nil } -func deployDind() error { - cmd := exec.Command( +func deployDind(ctx context.Context) error { + cmd := exec.CommandContext( + ctx, "kubectl", "apply", "-f-",