From 939802fd5b6a786f2c439709c5a304143f8f71c0 Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Wed, 20 Nov 2024 19:49:36 +0000 Subject: [PATCH 1/3] handle command interrupt Signed-off-by: Manabu McCloskey --- pkg/build/build.go | 16 +++++++++++++--- pkg/controllers/run.go | 9 +++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/build/build.go b/pkg/build/build.go index 0da1cb4f..f325123c 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "os" + "os/signal" + "syscall" "time" "github.com/cnoe-io/idpbuilder/api/v1alpha1" @@ -266,9 +268,17 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return fmt.Errorf("creating localbuild resource: %w", err) } - err = <-managerExit - close(managerExit) - return err + interrupted := make(chan os.Signal, 1) + defer close(interrupted) + signal.Notify(interrupted, os.Interrupt, syscall.SIGTERM) + + select { + case mgrErr := <-managerExit: + return mgrErr + case <-interrupted: + b.CancelFunc() + return fmt.Errorf("command interrupted") + } } func isBuildCustomizationSpecEqual(s1, s2 v1alpha1.BuildCustomizationSpec) bool { 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 From 2174d77dc44384193ba612f5588c681a585131a2 Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Thu, 21 Nov 2024 20:57:21 +0000 Subject: [PATCH 2/3] pass command context Signed-off-by: Manabu McCloskey --- main.go | 26 ++++++++++++++++++++++++-- pkg/build/build.go | 20 ++++++++------------ pkg/cmd/create/root.go | 12 ++++++++---- pkg/cmd/get/secrets.go | 3 +-- pkg/cmd/root.go | 5 +++-- 5 files changed, 44 insertions(+), 22 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 f325123c..50d1aaf7 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -4,8 +4,6 @@ import ( "context" "fmt" "os" - "os/signal" - "syscall" "time" "github.com/cnoe-io/idpbuilder/api/v1alpha1" @@ -159,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 @@ -227,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") @@ -268,17 +266,15 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return fmt.Errorf("creating localbuild resource: %w", err) } - interrupted := make(chan os.Signal, 1) - defer close(interrupted) - signal.Notify(interrupted, os.Interrupt, syscall.SIGTERM) - select { case mgrErr := <-managerExit: - return mgrErr - case <-interrupted: - b.CancelFunc() - return fmt.Errorf("command interrupted") + 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 1e2ff4b9..6d22d7ba 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 ( @@ -89,8 +88,9 @@ func preCreateE(cmd *cobra.Command, args []string) error { } func create(cmd *cobra.Command, args []string) error { - ctx, ctxCancel := context.WithCancel(ctrl.SetupSignalHandler()) - defer ctxCancel() + + ctx, cancel := context.WithCancel(cmd.Context()) + defer cancel() kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config") @@ -152,7 +152,7 @@ func create(cmd *cobra.Command, args []string) error { PackageCustomization: o, Scheme: k8s.GetScheme(), - CancelFunc: ctxCancel, + CancelFunc: cancel, } b := build.NewBuild(opts) @@ -161,6 +161,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 5cad39c3..11b3f52d 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" ) @@ -54,7 +53,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) } From 008c3bfcc23f13b618e87131616246ddd93b3f6c Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Thu, 21 Nov 2024 21:14:17 +0000 Subject: [PATCH 3/3] revert name changes Signed-off-by: Manabu McCloskey --- pkg/cmd/create/root.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/create/root.go b/pkg/cmd/create/root.go index 6d22d7ba..1e25cf2d 100644 --- a/pkg/cmd/create/root.go +++ b/pkg/cmd/create/root.go @@ -89,8 +89,8 @@ func preCreateE(cmd *cobra.Command, args []string) error { func create(cmd *cobra.Command, args []string) error { - ctx, cancel := context.WithCancel(cmd.Context()) - defer cancel() + ctx, ctxCancel := context.WithCancel(cmd.Context()) + defer ctxCancel() kubeConfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config") @@ -152,7 +152,7 @@ func create(cmd *cobra.Command, args []string) error { PackageCustomization: o, Scheme: k8s.GetScheme(), - CancelFunc: cancel, + CancelFunc: ctxCancel, } b := build.NewBuild(opts)