Skip to content

Commit

Permalink
pass command context
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey committed Nov 21, 2024
1 parent 939802f commit 2174d77
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
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)
}
20 changes: 8 additions & 12 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 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,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")

Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 2174d77

Please sign in to comment.