diff --git a/cmd/kubexit/main.go b/cmd/kubexit/main.go index 827cd95..10629a9 100644 --- a/cmd/kubexit/main.go +++ b/cmd/kubexit/main.go @@ -171,9 +171,9 @@ func main() { func waitForBirthDeps(birthDeps []string, namespace, podName string, timeout time.Duration) error { // Cancel context on SIGTERM to trigger graceful exit - ctx := withCancelOnSignal(context.Background(), syscall.SIGTERM) + ctxParent, parentCancel := withCancelOnSignal(context.Background(), syscall.SIGTERM) - ctx, stopPodWatcher := context.WithTimeout(ctx, timeout) + ctx, stopPodWatcher := context.WithTimeout(ctxParent, timeout) // Stop pod watcher on exit, if not sooner defer stopPodWatcher() @@ -187,6 +187,7 @@ func waitForBirthDeps(birthDeps []string, namespace, podName string, timeout tim // Block until all birth deps are ready <-ctx.Done() + parentCancel() err = ctx.Err() if err == context.DeadlineExceeded { return fmt.Errorf("timed out waiting for birth deps to be ready: %s", timeout) @@ -200,7 +201,7 @@ func waitForBirthDeps(birthDeps []string, namespace, podName string, timeout tim } // withCancelOnSignal calls cancel when one of the specified signals is recieved. -func withCancelOnSignal(ctx context.Context, signals ...os.Signal) context.Context { +func withCancelOnSignal(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) { ctx, cancel := context.WithCancel(ctx) sigCh := make(chan os.Signal, 1) @@ -214,16 +215,15 @@ func withCancelOnSignal(ctx context.Context, signals ...os.Signal) context.Conte if !ok { return } - log.Printf("Received shutdown signal: %v", s) - cancel() + log.Printf("Received shutdown signal: %v, exiting", s) + os.Exit(0) case <-ctx.Done(): signal.Reset() - close(sigCh) } } }() - return ctx + return ctx, cancel } // wait for the child to exit and return the exit code