Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle command interrupt #455

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 11 additions & 5 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -89,7 +88,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")
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")

Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"os"

Expand All @@ -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)
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/controllers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading