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 #23 from dweomer/feature/9/kubectl-dropin
Browse files Browse the repository at this point in the history
initial support for running as kubectl drop-in
  • Loading branch information
dweomer authored Feb 18, 2021
2 parents 8515c6f + 1af81aa commit 46cfbfb
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 79 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ require (
github.com/containerd/console v1.0.1
github.com/containerd/containerd v1.4.3
github.com/containerd/typeurl v1.0.1
github.com/docker/cli v20.10.0-beta1.0.20201029214301-1d20b15adc38+incompatible
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker-credential-helpers v0.6.3
github.com/docker/go-units v0.4.0
github.com/fvbommel/sortorder v1.0.2 // indirect
github.com/gogo/googleapis v1.3.2
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.4.3
Expand All @@ -51,6 +53,7 @@ require (
github.com/rancher/wrangler-cli v0.0.0-20210209192715-4335f41916a5
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
github.com/theupdateframework/notary v0.7.0 // indirect
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
google.golang.org/grpc v1.29.1
k8s.io/api v0.19.0
Expand Down
50 changes: 50 additions & 0 deletions go.sum

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package main

import (
"os"
"path/filepath"

"github.com/containerd/containerd/pkg/seed"
"github.com/rancher/kim/pkg/cli"
command "github.com/rancher/wrangler-cli"
Expand All @@ -16,5 +19,12 @@ func init() {
}

func main() {
command.Main(cli.Main())
switch _, exe := filepath.Split(os.Args[0]); exe {
case "kubectl-builder":
command.Main(cli.Builder(exe))
case "kubectl-image":
command.Main(cli.Image(exe))
default:
command.Main(cli.Main())
}
}
49 changes: 36 additions & 13 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package cli

import (
"fmt"
"strings"

"github.com/rancher/kim/pkg/cli/command/agent"
"github.com/rancher/kim/pkg/cli/command/builder"
"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 Down Expand Up @@ -55,7 +56,7 @@ var (
)

func Main() *cobra.Command {
cmd := wrangler.Command(&App{}, cobra.Command{
app := wrangler.Command(&App{}, cobra.Command{
Use: "kim [OPTIONS] COMMAND",
Short: "Kubernetes Image Manager -- in ur kubernetes buildin ur imagez",
Version: version.FriendlyVersion(),
Expand All @@ -65,20 +66,42 @@ func Main() *cobra.Command {
"shortcuts": "image",
},
})
cmd.AddCommand(
app.SetUsageTemplate(defaultUsageTemplate)
app.AddCommand(
agent.Command(),
image.Command(),
system.Command(),
builder.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
AddShortcut(app, build.Use, "image", "build")
AddShortcut(app, list.Use("images"), "image", "list")
AddShortcut(app, pull.Use, "image", "pull")
AddShortcut(app, push.Use, "image", "push")
AddShortcut(app, remove.Use("rmi"), "image", "remove")
AddShortcut(app, tag.Use, "image", "tag")
return app
}

func Image(exe string) *cobra.Command {
cmd := image.Command()
app := wrangler.Command(&App{}, *cmd)
app.SetUsageTemplate(defaultUsageTemplate)
app.Use = image.Use(exe)
app.Version = version.FriendlyVersion()
app.Example = fmt.Sprintf("%s build --tag your/image:tag .", strings.Replace(exe, "kubectl-", "kubectl ", 1))
cmd.PersistentFlags().AddFlagSet(app.PersistentFlags())
return app
}

func Builder(exe string) *cobra.Command {
cmd := builder.Command()
app := wrangler.Command(&App{}, *cmd)
app.SetUsageTemplate(defaultUsageTemplate)
app.Use = builder.Use(exe)
app.Version = version.FriendlyVersion()
app.Example = fmt.Sprintf("%s install --selector k3s.io/hostname=my-builder", strings.Replace(exe, "kubectl-", "kubectl ", 1))
cmd.PersistentFlags().AddFlagSet(app.PersistentFlags())
return app
}

func AddShortcut(cmd *cobra.Command, use string, path ...string) {
Expand All @@ -94,7 +117,7 @@ func AddShortcut(cmd *cobra.Command, use string, path ...string) {
shortcut.Annotations = map[string]string{
"shortcut-root": path[0],
}
for pre := sub; ; pre = pre.Parent() {
for pre := sub; pre != nil; pre = pre.Parent() {
if pre.Name() == path[0] {
if pre.PersistentPreRunE != nil {
shortcut.PersistentPreRunE = func(alias *cobra.Command, args []string) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package system
package builder

import (
"github.com/rancher/kim/pkg/cli/command/system/info"
"github.com/rancher/kim/pkg/cli/command/system/install"
"github.com/rancher/kim/pkg/cli/command/system/uninstall"
"fmt"

"github.com/rancher/kim/pkg/cli/command/builder/install"
"github.com/rancher/kim/pkg/cli/command/builder/uninstall"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)

const (
Short = "Manage Builder(s)"
)

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

func Command() *cobra.Command {
cmd := wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "system [OPTIONS] COMMAND",
Short: "Manage KIM",
Use: Use("builder"),
Short: Short,
DisableFlagsInUseLine: true,
})
cmd.AddCommand(
info.Command(),
install.Command(),
uninstall.Command(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package install

import (
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/system/builder"
"github.com/rancher/kim/pkg/client/builder"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package uninstall

import (
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/system/builder"
"github.com/rancher/kim/pkg/client/builder"
wrangler "github.com/rancher/wrangler-cli"
"github.com/spf13/cobra"
)
Expand Down
6 changes: 1 addition & 5 deletions pkg/cli/command/image/build/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package build

import (
"errors"
"os"

"github.com/rancher/kim/pkg/client"
Expand All @@ -20,6 +19,7 @@ func Command() *cobra.Command {
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
})
}

Expand All @@ -28,10 +28,6 @@ type CommandSpec struct {
}

func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("exactly one argument is required")
}

k8s, err := client.DefaultConfig.Interface()
if err != nil {
return err
Expand Down
16 changes: 13 additions & 3 deletions pkg/cli/command/image/image.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
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/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"
)

const (
Short = "Manage Images"
)

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

func Command() *cobra.Command {
cmd := wrangler.Command(&CommandSpec{}, cobra.Command{
Use: "image [OPTIONS] COMMAND",
Short: "Manage Images",
Use: Use("image"),
Short: Short,
DisableFlagsInUseLine: true,
//TraverseChildren: true,
})
Expand Down
1 change: 0 additions & 1 deletion pkg/cli/command/image/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

return s.List.Do(cmd.Context(), k8s, args)
}
6 changes: 1 addition & 5 deletions pkg/cli/command/image/pull/pull.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pull

import (
"github.com/pkg/errors"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
Expand All @@ -18,6 +17,7 @@ func Command() *cobra.Command {
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
})
}

Expand All @@ -26,10 +26,6 @@ type CommandSpec struct {
}

func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("exactly one argument is required")
}

k8s, err := client.DefaultConfig.Interface()
if err != nil {
return err
Expand Down
6 changes: 1 addition & 5 deletions pkg/cli/command/image/push/push.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package push

import (
"github.com/pkg/errors"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
Expand All @@ -18,6 +17,7 @@ func Command() *cobra.Command {
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
})
}

Expand All @@ -26,10 +26,6 @@ type CommandSpec struct {
}

func (s *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("exactly one argument is required")
}

k8s, err := client.DefaultConfig.Interface()
if err != nil {
return err
Expand Down
6 changes: 1 addition & 5 deletions pkg/cli/command/image/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package remove
import (
"fmt"

"github.com/pkg/errors"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
Expand All @@ -24,6 +23,7 @@ func Command() *cobra.Command {
Short: Short,
Aliases: []string{"remove"},
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(1),
})
}

Expand All @@ -32,10 +32,6 @@ type CommandSpec struct {
}

func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("exactly one argument is required")
}

k8s, err := client.DefaultConfig.Interface()
if err != nil {
return err
Expand Down
6 changes: 1 addition & 5 deletions pkg/cli/command/image/tag/tag.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tag

import (
"github.com/pkg/errors"
"github.com/rancher/kim/pkg/client"
"github.com/rancher/kim/pkg/client/image"
wrangler "github.com/rancher/wrangler-cli"
Expand All @@ -18,6 +17,7 @@ func Command() *cobra.Command {
Use: Use,
Short: Short,
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(2),
})
}

Expand All @@ -26,10 +26,6 @@ type CommandSpec struct {
}

func (c *CommandSpec) Run(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("at least two arguments are required")
}

k8s, err := client.DefaultConfig.Interface()
if err != nil {
return err
Expand Down
22 changes: 0 additions & 22 deletions pkg/cli/command/system/info/info.go

This file was deleted.

Loading

0 comments on commit 46cfbfb

Please sign in to comment.