From 406cc0396b039a0043a5d1f96aae4d77cf862017 Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Wed, 4 Dec 2024 15:10:47 -0800 Subject: [PATCH] handle command interrupt (#455) --- main.go | 26 ++++++++++++++++++++++++-- pkg/build/build.go | 16 +++++++++++----- pkg/cmd/create/root.go | 8 ++++++-- pkg/cmd/get/secrets.go | 3 +-- pkg/cmd/root.go | 5 +++-- pkg/controllers/run.go | 9 +++------ 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 4f5ffcf9..71391e3f 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,29 @@ package main -import "github.com/cnoe-io/idpbuilder/pkg/cmd" +import ( + "context" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/cnoe-io/idpbuilder/pkg/cmd" +) func main() { - cmd.Execute() + interrupted := make(chan os.Signal, 1) + defer close(interrupted) + signal.Notify(interrupted, os.Interrupt, syscall.SIGTERM) + defer signal.Stop(interrupted) + + ctx, cancel := context.WithCancelCause(context.Background()) + + go func() { + select { + case <-interrupted: + cancel(fmt.Errorf("command interrupted")) + } + }() + + cmd.Execute(ctx) } diff --git a/pkg/build/build.go b/pkg/build/build.go index 0da1cb4f..50d1aaf7 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -157,8 +157,6 @@ func (b *Build) isCompatible(ctx context.Context, kubeClient client.Client) (boo } func (b *Build) Run(ctx context.Context, recreateCluster bool) error { - managerExit := make(chan error) - setupLog.Info("Creating kind cluster") if err := b.ReconcileKindCluster(ctx, recreateCluster); err != nil { return err @@ -225,6 +223,8 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return err } + managerExit := make(chan error) + setupLog.V(1).Info("Running controllers") if err := b.RunControllers(ctx, mgr, managerExit, dir); err != nil { setupLog.Error(err, "Error running controllers") @@ -266,9 +266,15 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return fmt.Errorf("creating localbuild resource: %w", err) } - err = <-managerExit - close(managerExit) - return err + select { + case mgrErr := <-managerExit: + if mgrErr != nil { + return mgrErr + } + case <-ctx.Done(): + return nil + } + return nil } func isBuildCustomizationSpecEqual(s1, s2 v1alpha1.BuildCustomizationSpec) bool { diff --git a/pkg/cmd/create/root.go b/pkg/cmd/create/root.go index 62d6a943..ac562fa5 100644 --- a/pkg/cmd/create/root.go +++ b/pkg/cmd/create/root.go @@ -15,7 +15,6 @@ import ( "github.com/cnoe-io/idpbuilder/pkg/k8s" "github.com/spf13/cobra" "k8s.io/client-go/util/homedir" - ctrl "sigs.k8s.io/controller-runtime" ) const ( @@ -90,7 +89,8 @@ func preCreateE(cmd *cobra.Command, args []string) error { } func create(cmd *cobra.Command, args []string) error { - ctx, ctxCancel := context.WithCancel(ctrl.SetupSignalHandler()) + + ctx, ctxCancel := context.WithCancel(cmd.Context()) defer ctxCancel() kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config") @@ -162,6 +162,10 @@ func create(cmd *cobra.Command, args []string) error { return err } + if cmd.Context().Err() != nil { + return context.Cause(cmd.Context()) + } + printSuccessMsg() return nil } diff --git a/pkg/cmd/get/secrets.go b/pkg/cmd/get/secrets.go index 607c0870..dc6d94c2 100644 --- a/pkg/cmd/get/secrets.go +++ b/pkg/cmd/get/secrets.go @@ -19,7 +19,6 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" "k8s.io/client-go/util/homedir" - ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" ) @@ -55,7 +54,7 @@ type TemplateData struct { } func getSecretsE(cmd *cobra.Command, args []string) error { - ctx, ctxCancel := context.WithCancel(ctrl.SetupSignalHandler()) + ctx, ctxCancel := context.WithCancel(cmd.Context()) defer ctxCancel() kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config") diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 32d97ab4..345aca66 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "fmt" "os" @@ -27,8 +28,8 @@ func init() { rootCmd.AddCommand(version.VersionCmd) } -func Execute() { - if err := rootCmd.Execute(); err != nil { +func Execute(ctx context.Context) { + if err := rootCmd.ExecuteContext(ctx); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } diff --git a/pkg/controllers/run.go b/pkg/controllers/run.go index b7187ddf..7d68d134 100644 --- a/pkg/controllers/run.go +++ b/pkg/controllers/run.go @@ -63,15 +63,12 @@ func RunControllers( if err != nil { logger.Error(err, "unable to create custom package controller") } - // Start our manager in another goroutine logger.V(1).Info("starting manager") + go func() { - if err := mgr.Start(ctx); err != nil { - logger.Error(err, "problem running manager") - exitCh <- err - } - exitCh <- nil + exitCh <- mgr.Start(ctx) + close(exitCh) }() return nil