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

Commit

Permalink
Merge pull request #14 from dweomer/client/builder-auto-install
Browse files Browse the repository at this point in the history
client: first pass at automatic installation
  • Loading branch information
dweomer committed Feb 4, 2021
2 parents 28c64c1 + 6dfa98d commit d969507
Show file tree
Hide file tree
Showing 30 changed files with 598 additions and 332 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ make ORG=<your-dockerhub-org> build publish
Have a working `k3s` installation with a working `$HOME/.kube/config` or `$KUBECONFIG`, then:

```bash
# Installation on a single-node cluster
./bin/kim install --agent-image=docker.io/${ORG}/kim
```

```bash
# Installation on a single-node cluster is automatic
# Installation on a multi-node cluster, targeting a Node named "my-builder-node"
./bin/kim install --agent-image=docker.io/${ORG}/kim --selector k3s.io/hostname=my-builder-node
./bin/kim install --selector k3s.io/hostname=my-builder-node

```

Expand All @@ -76,27 +72,28 @@ Usage:
kim [command]
Examples:
kim build --tag your/image:tag .
kim image build --tag your/image:tag .
Available Commands:
build Build an image
help Help about any command
image Manage Images
system Manage KIM
Images Shortcuts:
build Build an image
images List images
info Display builder information
install Install builder component(s)
pull Pull an image
push Push an image
rmi Remove an image
tag Tag an image
uninstall Uninstall builder component(s)
Flags:
-x, --context string kubeconfig context for authentication
--debug
--debug-level int
-h, --help help for kim
-k, --kubeconfig string kubeconfig for authentication
-n, --namespace string namespace (default "kim")
-n, --namespace string namespace (default "kube-image")
-v, --version version for kim
Use "kim [command] --help" for more information about a command.
Expand Down
77 changes: 55 additions & 22 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package cli

import (
"github.com/rancher/kim/pkg/cli/commands/agent"
"github.com/rancher/kim/pkg/cli/commands/build"
"github.com/rancher/kim/pkg/cli/commands/images"
"github.com/rancher/kim/pkg/cli/commands/info"
"github.com/rancher/kim/pkg/cli/commands/install"
"github.com/rancher/kim/pkg/cli/commands/pull"
"github.com/rancher/kim/pkg/cli/commands/push"
"github.com/rancher/kim/pkg/cli/commands/rmi"
"github.com/rancher/kim/pkg/cli/commands/tag"
"github.com/rancher/kim/pkg/cli/commands/uninstall"
"strings"

"github.com/rancher/kim/pkg/cli/command/agent"
"github.com/rancher/kim/pkg/cli/command/image"
"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/cli/command/system"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/credential/provider"
"github.com/rancher/kim/pkg/version"
Expand All @@ -30,8 +31,11 @@ Aliases:
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Available Commands:{{range .Commands}}{{if (and (not (index .Annotations "shortcut-root")) (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}{{if (eq (index .Annotations "shortcuts") "image")}}
Images Shortcuts:{{range .Commands}}{{if (and .IsAvailableCommand (eq (index .Annotations "shortcut-root") "image"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Expand All @@ -55,25 +59,54 @@ func Main() *cobra.Command {
Use: "kim [OPTIONS] COMMAND",
Short: "Kubernetes Image Manager -- in ur kubernetes buildin ur imagez",
Version: version.FriendlyVersion(),
Example: "kim build --tag your/image:tag .",
Example: "kim image build --tag your/image:tag .",
DisableFlagsInUseLine: true,
Annotations: map[string]string{
"shortcuts": "image",
},
})
cmd.AddCommand(
agent.Command(),
info.Command(),
images.Command(),
install.Command(),
uninstall.Command(),
build.Command(),
pull.Command(),
push.Command(),
rmi.Command(),
tag.Command(),
image.Command(),
system.Command(),
)
// image subsystem shortcuts
AddShortcut(cmd, build.Use, "image", "build")
AddShortcut(cmd, list.Use("images"), "image", "list")
AddShortcut(cmd, pull.Use, "image", "pull")
AddShortcut(cmd, push.Use, "image", "push")
AddShortcut(cmd, remove.Use("rmi"), "image", "remove")
AddShortcut(cmd, tag.Use, "image", "tag")
cmd.SetUsageTemplate(defaultUsageTemplate)
return cmd
}

func AddShortcut(cmd *cobra.Command, use string, path ...string) {
sub, _, err := cmd.Find(path)
if err != nil {
panic(err)
}
target := strings.Join(path, " ")
shortcut := *sub
shortcut.Use = use
//shortcut.Short = fmt.Sprintf("%s (shortcut to `%s %s`)", sub.Short, cmd.Name(), target)
shortcut.Aliases = []string{target}
shortcut.Annotations = map[string]string{
"shortcut-root": path[0],
}
for pre := sub; ; pre = pre.Parent() {
if pre.Name() == path[0] {
if pre.PersistentPreRunE != nil {
shortcut.PersistentPreRunE = func(alias *cobra.Command, args []string) error {
return pre.PersistentPreRunE(alias, args)
}
}
break
}
}
cmd.AddCommand(&shortcut)
}

type App struct {
wrangler.DebugConfig
client.Config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func Command() *cobra.Command {
return wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "agent [OPTIONS]",
Short: "Run the controller daemon",
Short: "Run the controller daemon (on supported platforms)",
Hidden: true,
DisableFlagsInUseLine: true,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import (
"os"

"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/action"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)

const (
Use = "build [OPTIONS] PATH"
Short = "Build an image"
)

func Command() *cobra.Command {
return wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "build [OPTIONS] PATH",
Short: "Build an image",
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
})
}

type CommandSpec struct {
action.BuildImage
image.Build
}

func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -38,5 +43,5 @@ func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return c.BuildImage.Invoke(cmd.Context(), k8s, path)
return c.Build.Do(cmd.Context(), k8s, path)
}
60 changes: 60 additions & 0 deletions pkg/cli/command/image/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package image

import (
"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/cli/command/system/install"
"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"
)

func Command() *cobra.Command {
cmd := wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "image [OPTIONS] COMMAND",
Short: "Manage Images",
DisableFlagsInUseLine: true,
//TraverseChildren: true,
})
cmd.AddCommand(
build.Command(),
list.Command(),
pull.Command(),
push.Command(),
remove.Command(),
tag.Command(),
)
return cmd
}

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()
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package images
package list

import (
"fmt"

"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/action"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)

const (
Short = "List images"
)

func Use(sub string) string {
return fmt.Sprintf("%s [OPTIONS] [REPOSITORY[:TAG]]", sub)
}

func Command() *cobra.Command {
return wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "images [OPTIONS] [REPOSITORY[:TAG]]",
Short: "List images",
Use: Use("ls"),
Short: Short,
DisableFlagsInUseLine: true,
Aliases: []string{"list"},
})
}

type CommandSpec struct {
action.ListImages
image.List
}

func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -25,5 +36,5 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
return err
}

return s.ListImages.Invoke(cmd.Context(), k8s, args)
return s.List.Do(cmd.Context(), k8s, args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ package pull
import (
"github.com/pkg/errors"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/action"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)

const (
Use = "pull [OPTIONS] IMAGE"
Short = "Pull an image"
)

func Command() *cobra.Command {
return wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "pull [OPTIONS] IMAGE",
Short: "Pull an image",
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
})
}

type CommandSpec struct {
action.PullImage
image.Pull
}

func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -28,5 +34,5 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return s.PullImage.Invoke(cmd.Context(), k8s, args[0])
return s.Pull.Do(cmd.Context(), k8s, args[0])
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ package push
import (
"github.com/pkg/errors"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/action"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)

const (
Use = "push [OPTIONS] IMAGE"
Short = "Push an image"
)

func Command() *cobra.Command {
return wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "push [OPTIONS] IMAGE",
Short: "Push an image",
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
})
}

type CommandSpec struct {
action.PushImage
image.Push
}

func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -28,5 +34,5 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return s.PushImage.Invoke(cmd.Context(), k8s, args[0])
return s.Push.Do(cmd.Context(), k8s, args[0])
}
Loading

0 comments on commit d969507

Please sign in to comment.