Skip to content

Commit

Permalink
feat(bin): Add graceful shutdown for helm command
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
  • Loading branch information
idsulik committed Nov 16, 2024
1 parent 1385a7d commit a09c6eb
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pkg/skaffold/helm/bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"regexp"
"time"

"github.com/blang/semver"
shell "github.com/kballard/go-shellquote"
Expand Down Expand Up @@ -127,7 +128,39 @@ func generateHelmCommand(ctx context.Context, h Client, useSecrets bool, env []s
args = append([]string{"secrets"}, args...)
}

cmd := exec.CommandContext(ctx, "helm", args...)
cmd := exec.Command("helm", args...)

// Set up context cancellation handler
go func() {
<-ctx.Done()

if cmd.Process != nil {
// Create a channel to detect if helm finishes cleanup
done := make(chan struct{})
go func() {
cmd.Wait()
close(done)
}()

// Send SIGINT for graceful shutdown
if err := cmd.Process.Signal(os.Interrupt); err != nil {
// Log error but continue to wait for timeout
fmt.Printf("Failed to send interrupt signal: %v\n", err)
}

// Wait for either cleanup completion or timeout
select {
case <-time.After(2 * time.Minute):
if err := cmd.Process.Kill(); err != nil {
fmt.Printf("Failed to kill process: %v\n", err)
}
case <-done:
fmt.Println("Helm cleanup completed")
return
}
}
}()

if len(env) > 0 {
cmd.Env = env
}
Expand Down

0 comments on commit a09c6eb

Please sign in to comment.