From f5c31846875c54df7bbe03f4ec9b3d7a744e6162 Mon Sep 17 00:00:00 2001 From: changzhen Date: Thu, 2 Sep 2021 09:52:54 +0800 Subject: [PATCH] support kubectl karmada plugin Signed-off-by: changzhen --- .gitignore | 1 + Makefile | 10 ++++++++-- cmd/karmadactl/karmadactl.go | 2 +- cmd/kubectl-karmada/kubectl-karmada.go | 20 ++++++++++++++++++++ pkg/karmadactl/cordon.go | 25 +++++++++++++------------ pkg/karmadactl/join.go | 12 ++++++------ pkg/karmadactl/karmadactl.go | 24 +++++++++++++++--------- pkg/karmadactl/unjoin.go | 13 +++++++------ pkg/karmadactl/version.go | 16 ++++++++-------- 9 files changed, 79 insertions(+), 44 deletions(-) create mode 100644 cmd/kubectl-karmada/kubectl-karmada.go diff --git a/.gitignore b/.gitignore index 50b210d70975..86f00321f36c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ karmada-controller-manager karmada-scheduler karmada-webhook karmadactl +kubectl-karmada diff --git a/Makefile b/Makefile index 2484beee68f7..c8db08fd920e 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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) \ @@ -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: diff --git a/cmd/karmadactl/karmadactl.go b/cmd/karmadactl/karmadactl.go index a3017b810b9c..5592449cc9f8 100644 --- a/cmd/karmadactl/karmadactl.go +++ b/cmd/karmadactl/karmadactl.go @@ -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) } diff --git a/cmd/kubectl-karmada/kubectl-karmada.go b/cmd/kubectl-karmada/kubectl-karmada.go new file mode 100644 index 000000000000..804c5928a7af --- /dev/null +++ b/cmd/kubectl-karmada/kubectl-karmada.go @@ -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) + } +} diff --git a/pkg/karmadactl/cordon.go b/pkg/karmadactl/cordon.go index 4fb2cb82cdb9..f6d556bf1f5f 100644 --- a/pkg/karmadactl/cordon.go +++ b/pkg/karmadactl/cordon.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "github.com/spf13/cobra" @@ -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 ` ) @@ -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 { @@ -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) diff --git a/pkg/karmadactl/join.go b/pkg/karmadactl/join.go index 106ff88278ff..ba39b533f205 100644 --- a/pkg/karmadactl/join.go +++ b/pkg/karmadactl/join.go @@ -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= +%s join CLUSTER_NAME --cluster-kubeconfig= ` ) @@ -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=", - 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) diff --git a/pkg/karmadactl/karmadactl.go b/pkg/karmadactl/karmadactl.go index 83d240fc936e..5f97c3e2f811 100644 --- a/pkg/karmadactl/karmadactl.go +++ b/pkg/karmadactl/karmadactl.go @@ -2,6 +2,7 @@ package karmadactl import ( "flag" + "fmt" "io" "github.com/spf13/cobra" @@ -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, } @@ -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 } diff --git a/pkg/karmadactl/unjoin.go b/pkg/karmadactl/unjoin.go index 56f8559288b8..4b44ceafbc6a 100644 --- a/pkg/karmadactl/unjoin.go +++ b/pkg/karmadactl/unjoin.go @@ -3,6 +3,7 @@ package karmadactl import ( "context" "errors" + "fmt" "io" "time" @@ -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= +%s unjoin CLUSTER_NAME --cluster-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=", - 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 { diff --git a/pkg/karmadactl/version.go b/pkg/karmadactl/version.go index a10963dac67a..ca0c28fa59c7 100644 --- a/pkg/karmadactl/version.go +++ b/pkg/karmadactl/version.go @@ -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())) }, }