Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
image: honor global args (#72)
Browse files Browse the repository at this point in the history
Implement the default installation check/attempt as final check before
invoking each sub-command. This removes the `PersistentPre()` impl from
the `image.CommandSpec` unblocking the global `cli.App`#`PersistentPre()`.

Signed-off-by: Jacob Blain Christen <jacob@rancher.com>
  • Loading branch information
dweomer authored Sep 1, 2021
1 parent 1d7e0d4 commit 6fe4730
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 23 deletions.
23 changes: 23 additions & 0 deletions pkg/cli/command/builder/install/install.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}
5 changes: 5 additions & 0 deletions pkg/cli/command/image/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
23 changes: 0 additions & 23 deletions pkg/cli/command/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
}
5 changes: 5 additions & 0 deletions pkg/cli/command/image/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions pkg/cli/command/image/pull/pull.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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])
}
5 changes: 5 additions & 0 deletions pkg/cli/command/image/push/push.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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])
}
5 changes: 5 additions & 0 deletions pkg/cli/command/image/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])
}
5 changes: 5 additions & 0 deletions pkg/cli/command/image/tag/tag.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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:])
}

0 comments on commit 6fe4730

Please sign in to comment.