diff --git a/pkg/cli/command/builder/install/install.go b/pkg/cli/command/builder/install/install.go index 0e509b9..5b3ade3 100644 --- a/pkg/cli/command/builder/install/install.go +++ b/pkg/cli/command/builder/install/install.go @@ -1,10 +1,14 @@ package install import ( + "context" + "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/builder" wrangler "github.com/rancher/wrangler-cli" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func Command() *cobra.Command { @@ -28,3 +32,22 @@ func (s *CommandSpec) Run(cmd *cobra.Command, _ []string) error { } return s.Install.Do(cmd.Context(), k8s) } + +func Check(ctx context.Context) error { + pre := CommandSpec{} + // i've tried using subcommands from the cli command tree but there be dragons + wrangler.Command(&pre, cobra.Command{}) // initialize pre.Install defaults + k8s, err := client.DefaultConfig.Interface() + if err != nil { + return err + } + // if the daemon-set is available then we don't need to do anything + daemon, err := k8s.Apps.DaemonSet().Get(k8s.Namespace, "builder", metav1.GetOptions{}) + if err == nil && daemon.Status.NumberAvailable > 0 { + return nil + } + pre.NoWait = false + pre.NoFail = true + logrus.Warnf("Cannot find available builder daemon, attempting automatic installation...") + return pre.Install.Do(ctx, k8s) +} diff --git a/pkg/cli/command/image/build/build.go b/pkg/cli/command/image/build/build.go index e410a9c..4c5dc96 100644 --- a/pkg/cli/command/image/build/build.go +++ b/pkg/cli/command/image/build/build.go @@ -3,6 +3,7 @@ package build import ( "os" + "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/image" wrangler "github.com/rancher/wrangler-cli" @@ -39,5 +40,9 @@ func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = install.Check(cmd.Context()) + if err != nil { + return err + } return c.Build.Do(cmd.Context(), k8s, path) } diff --git a/pkg/cli/command/image/image.go b/pkg/cli/command/image/image.go index 0104dca..bb00ecd 100644 --- a/pkg/cli/command/image/image.go +++ b/pkg/cli/command/image/image.go @@ -3,18 +3,14 @@ package image import ( "fmt" - "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/cli/command/image/build" "github.com/rancher/kim/pkg/cli/command/image/list" "github.com/rancher/kim/pkg/cli/command/image/pull" "github.com/rancher/kim/pkg/cli/command/image/push" "github.com/rancher/kim/pkg/cli/command/image/remove" "github.com/rancher/kim/pkg/cli/command/image/tag" - "github.com/rancher/kim/pkg/client" wrangler "github.com/rancher/wrangler-cli" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -46,25 +42,6 @@ func Command() *cobra.Command { type CommandSpec struct { } -func (s *CommandSpec) PersistentPre(cmd *cobra.Command, _ []string) error { - pre := install.CommandSpec{} - // i've tried using subcommands from the cli command tree but there be dragons - wrangler.Command(&pre, cobra.Command{}) // initialize pre.Install defaults - k8s, err := client.DefaultConfig.Interface() - if err != nil { - return err - } - // if the daemon-set is available then we don't need to do anything - daemon, err := k8s.Apps.DaemonSet().Get(k8s.Namespace, "builder", metav1.GetOptions{}) - if err == nil && daemon.Status.NumberAvailable > 0 { - return nil - } - pre.NoWait = false - pre.NoFail = true - logrus.Warnf("Cannot find available builder daemon, attempting automatic installation...") - return pre.Install.Do(cmd.Context(), k8s) -} - func (s *CommandSpec) Run(cmd *cobra.Command, _ []string) error { return cmd.Help() } diff --git a/pkg/cli/command/image/list/list.go b/pkg/cli/command/image/list/list.go index 2c6abb3..e550720 100644 --- a/pkg/cli/command/image/list/list.go +++ b/pkg/cli/command/image/list/list.go @@ -3,6 +3,7 @@ package list import ( "fmt" + "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/image" wrangler "github.com/rancher/wrangler-cli" @@ -35,5 +36,9 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = install.Check(cmd.Context()) + if err != nil { + return err + } return s.List.Do(cmd.Context(), k8s, args) } diff --git a/pkg/cli/command/image/pull/pull.go b/pkg/cli/command/image/pull/pull.go index f257609..2ed45c3 100644 --- a/pkg/cli/command/image/pull/pull.go +++ b/pkg/cli/command/image/pull/pull.go @@ -1,6 +1,7 @@ package pull import ( + "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/image" wrangler "github.com/rancher/wrangler-cli" @@ -30,5 +31,9 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = install.Check(cmd.Context()) + if err != nil { + return err + } return s.Pull.Do(cmd.Context(), k8s, args[0]) } diff --git a/pkg/cli/command/image/push/push.go b/pkg/cli/command/image/push/push.go index 30007d4..2ed593f 100644 --- a/pkg/cli/command/image/push/push.go +++ b/pkg/cli/command/image/push/push.go @@ -1,6 +1,7 @@ package push import ( + "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/image" wrangler "github.com/rancher/wrangler-cli" @@ -30,5 +31,9 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = install.Check(cmd.Context()) + if err != nil { + return err + } return s.Push.Do(cmd.Context(), k8s, args[0]) } diff --git a/pkg/cli/command/image/remove/remove.go b/pkg/cli/command/image/remove/remove.go index 1cd874b..5214f08 100644 --- a/pkg/cli/command/image/remove/remove.go +++ b/pkg/cli/command/image/remove/remove.go @@ -3,6 +3,7 @@ package remove import ( "fmt" + "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/image" wrangler "github.com/rancher/wrangler-cli" @@ -36,5 +37,9 @@ func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = install.Check(cmd.Context()) + if err != nil { + return err + } return c.Remove.Do(cmd.Context(), k8s, args[0]) } diff --git a/pkg/cli/command/image/tag/tag.go b/pkg/cli/command/image/tag/tag.go index 8fd6499..d9206f0 100644 --- a/pkg/cli/command/image/tag/tag.go +++ b/pkg/cli/command/image/tag/tag.go @@ -1,6 +1,7 @@ package tag import ( + "github.com/rancher/kim/pkg/cli/command/builder/install" "github.com/rancher/kim/pkg/client" "github.com/rancher/kim/pkg/client/image" wrangler "github.com/rancher/wrangler-cli" @@ -30,5 +31,9 @@ func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = install.Check(cmd.Context()) + if err != nil { + return err + } return c.Tag.Do(cmd.Context(), k8s, args[0], args[1:]) }