Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce kubectl plugin #686

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
/karmada-scheduler
/karmada-webhook
/karmadactl
/kubectl-karmada
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ifeq ($(VERSION), "")
endif
endif

all: karmada-controller-manager karmada-scheduler karmadactl karmada-webhook karmada-agent
all: karmada-controller-manager karmada-scheduler karmadactl kubectl-karmada karmada-webhook karmada-agent

karmada-controller-manager: $(SOURCES)
CGO_ENABLED=0 GOOS=$(GOOS) go build \
Expand All @@ -58,6 +58,12 @@ karmadactl: $(SOURCES)
-o karmadactl \
cmd/karmadactl/karmadactl.go

kubectl-karmada: $(SOURCES)
CGO_ENABLED=0 GOOS=$(GOOS) go build \
-ldflags $(LDFLAGS) \
-o kubectl-karmada \
cmd/kubectl-karmada/kubectl-karmada.go

karmada-webhook: $(SOURCES)
CGO_ENABLED=0 GOOS=$(GOOS) go build \
-ldflags $(LDFLAGS) \
Expand All @@ -71,7 +77,7 @@ karmada-agent: $(SOURCES)
cmd/agent/main.go

clean:
rm -rf karmada-controller-manager karmada-scheduler karmadactl karmada-webhook karmada-agent
rm -rf karmada-controller-manager karmada-scheduler karmadactl kubectl-karmada karmada-webhook karmada-agent

.PHONY: update
update:
Expand Down
2 changes: 1 addition & 1 deletion cmd/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
logs.InitLogs()
defer logs.FlushLogs()

if err := karmadactl.NewKarmadaCtlCommand(os.Stdout).Execute(); err != nil {
if err := karmadactl.NewKarmadaCtlCommand(os.Stdout, "karmadactl", "karmadactl").Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand Down
20 changes: 20 additions & 0 deletions cmd/kubectl-karmada/kubectl-karmada.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"os"

"k8s.io/component-base/logs"

"github.com/karmada-io/karmada/pkg/karmadactl"
)

func main() {
logs.InitLogs()
defer logs.FlushLogs()

if err := karmadactl.NewKarmadaCtlCommand(os.Stdout, "karmada", "kubectl karmada").Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
25 changes: 13 additions & 12 deletions pkg/karmadactl/cordon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"

"github.com/spf13/cobra"
Expand All @@ -20,18 +21,18 @@ import (
)

var (
cordonLong = `Mark cluster as unschedulable.`

cordonShort = `Mark cluster as unschedulable`
cordonLong = `Mark cluster as unschedulable.`
cordonExample = `
# Mark cluster "foo" as unschedulable.
karmadactl cordon foo
%s cordon foo
`

uncordonLong = `Mark cluster as schedulable.`

uncordonShort = `Mark cluster as schedulable`
uncordonLong = `Mark cluster as schedulable.`
uncordonExample = `
# Mark cluster "foo" as schedulable.
karmadactl uncordon foo
%s uncordon foo
`
)

Expand All @@ -41,13 +42,13 @@ const (
)

// NewCmdCordon defines the `cordon` command that mark cluster as unschedulable.
func NewCmdCordon(cmdOut io.Writer, karmadaConfig KarmadaConfig) *cobra.Command {
func NewCmdCordon(cmdOut io.Writer, karmadaConfig KarmadaConfig, cmdStr string) *cobra.Command {
opts := CommandCordonOption{}
cmd := &cobra.Command{
Use: "cordon CLUSTER",
Short: "Mark cluster as unschedulable",
Short: cordonShort,
Long: cordonLong,
Example: cordonExample,
Example: fmt.Sprintf(cordonExample, cmdStr),
Run: func(cmd *cobra.Command, args []string) {
err := opts.Complete(args)
if err != nil {
Expand All @@ -72,13 +73,13 @@ func NewCmdCordon(cmdOut io.Writer, karmadaConfig KarmadaConfig) *cobra.Command
}

// NewCmdUncordon defines the `cordon` command that mark cluster as schedulable.
func NewCmdUncordon(cmdOut io.Writer, karmadaConfig KarmadaConfig) *cobra.Command {
func NewCmdUncordon(cmdOut io.Writer, karmadaConfig KarmadaConfig, cmdStr string) *cobra.Command {
opts := CommandCordonOption{}
cmd := &cobra.Command{
Use: "uncordon CLUSTER",
Short: "Mark cluster as schedulable",
Short: uncordonShort,
Long: uncordonLong,
Example: uncordonExample,
Example: fmt.Sprintf(uncordonExample, cmdStr),
Run: func(cmd *cobra.Command, args []string) {
// Set default values
err := opts.Complete(args)
Expand Down
12 changes: 6 additions & 6 deletions pkg/karmadactl/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
)

var (
joinLong = `Join registers a cluster to control plane.`

joinShort = `Register a cluster to control plane`
joinLong = `Join registers a cluster to control plane.`
joinExample = `
karmadactl join CLUSTER_NAME --cluster-kubeconfig=<KUBECONFIG>
%s join CLUSTER_NAME --cluster-kubeconfig=<KUBECONFIG>
`
)

Expand Down Expand Up @@ -65,14 +65,14 @@ const (
)

// NewCmdJoin defines the `join` command that registers a cluster.
func NewCmdJoin(cmdOut io.Writer, karmadaConfig KarmadaConfig) *cobra.Command {
func NewCmdJoin(cmdOut io.Writer, karmadaConfig KarmadaConfig, cmdStr string) *cobra.Command {
opts := CommandJoinOption{}

cmd := &cobra.Command{
Use: "join CLUSTER_NAME --cluster-kubeconfig=<KUBECONFIG>",
Short: "Register a cluster to control plane",
Short: joinShort,
Long: joinLong,
Example: joinExample,
Example: fmt.Sprintf(joinExample, cmdStr),
Run: func(cmd *cobra.Command, args []string) {
// Set default values
err := opts.Complete(args)
Expand Down
24 changes: 15 additions & 9 deletions pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package karmadactl

import (
"flag"
"fmt"
"io"

"github.com/spf13/cobra"
Expand All @@ -10,13 +11,18 @@ import (
apiserverflag "k8s.io/component-base/cli/flag"
)

var (
rootCmdShort = "%s controls a Kubernetes Cluster Federation."
rootCmdLong = "%s controls a Kubernetes Cluster Federation."
)

// NewKarmadaCtlCommand creates the `karmadactl` command.
func NewKarmadaCtlCommand(out io.Writer) *cobra.Command {
func NewKarmadaCtlCommand(out io.Writer, cmdUse, cmdStr string) *cobra.Command {
// Parent command to which all sub-commands are added.
rootCmd := &cobra.Command{
Use: "karmadactl",
Short: "karmadactl controls a Kubernetes Cluster Federation.",
Long: "karmadactl controls a Kubernetes Cluster Federation.",
Use: cmdUse,
Short: fmt.Sprintf(rootCmdShort, cmdStr),
Long: fmt.Sprintf(rootCmdLong, cmdStr),

RunE: runHelp,
}
Expand All @@ -34,11 +40,11 @@ func NewKarmadaCtlCommand(out io.Writer) *cobra.Command {
_ = flag.CommandLine.Parse(nil)

karmadaConfig := NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
rootCmd.AddCommand(NewCmdJoin(out, karmadaConfig))
rootCmd.AddCommand(NewCmdUnjoin(out, karmadaConfig))
rootCmd.AddCommand(NewCmdVersion(out))
rootCmd.AddCommand(NewCmdCordon(out, karmadaConfig))
rootCmd.AddCommand(NewCmdUncordon(out, karmadaConfig))
rootCmd.AddCommand(NewCmdJoin(out, karmadaConfig, cmdStr))
rootCmd.AddCommand(NewCmdUnjoin(out, karmadaConfig, cmdStr))
rootCmd.AddCommand(NewCmdVersion(out, cmdStr))
rootCmd.AddCommand(NewCmdCordon(out, karmadaConfig, cmdStr))
rootCmd.AddCommand(NewCmdUncordon(out, karmadaConfig, cmdStr))

return rootCmd
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/karmadactl/unjoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package karmadactl
import (
"context"
"errors"
"fmt"
"io"
"time"

Expand All @@ -22,22 +23,22 @@ import (
)

var (
unjoinLong = `Unjoin removes the registration of a cluster from control plane.`

unjoinShort = `Remove the registration of a cluster from control plane`
unjoinLong = `Unjoin removes the registration of a cluster from control plane.`
unjoinExample = `
karmadactl unjoin CLUSTER_NAME --cluster-kubeconfig=<KUBECONFIG>
%s unjoin CLUSTER_NAME --cluster-kubeconfig=<KUBECONFIG>
`
)

// NewCmdUnjoin defines the `unjoin` command that removes registration of a cluster from control plane.
func NewCmdUnjoin(cmdOut io.Writer, karmadaConfig KarmadaConfig) *cobra.Command {
func NewCmdUnjoin(cmdOut io.Writer, karmadaConfig KarmadaConfig, cmdStr string) *cobra.Command {
opts := CommandUnjoinOption{}

cmd := &cobra.Command{
Use: "unjoin CLUSTER_NAME --cluster-kubeconfig=<KUBECONFIG>",
Short: "Remove the registration of a cluster from control plane",
Short: unjoinShort,
Long: unjoinLong,
Example: unjoinExample,
Example: fmt.Sprintf(unjoinExample, cmdStr),
Run: func(cmd *cobra.Command, args []string) {
err := opts.Complete(args)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions pkg/karmadactl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
)

var (
versionLong = `Version prints the version info of this command.`

versionExample = ` # Print karmadactl command version
karmadactl version`
versionShort = `Print the version info`
versionLong = `Version prints the version info of this command.`
versionExample = ` # Print %s command version
%s version`
)

// NewCmdVersion prints out the release version info for this command binary.
func NewCmdVersion(out io.Writer) *cobra.Command {
func NewCmdVersion(out io.Writer, cmdStr string) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version info",
Short: versionShort,
Long: versionLong,
Example: versionExample,
Example: fmt.Sprintf(versionExample, cmdStr, cmdStr),
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(out, "karmadactl version: %s\n", fmt.Sprintf("%#v", version.Get()))
fmt.Fprintf(out, "%s version: %s\n", cmdStr, fmt.Sprintf("%#v", version.Get()))
},
}

Expand Down