Skip to content

Commit

Permalink
support kubectl karmada plugin
Browse files Browse the repository at this point in the history
Signed-off-by: changzhen <changzhen5@huawei.com>
  • Loading branch information
XiShanYongYe-Chang committed Sep 1, 2021
1 parent dbc8745 commit 3c80ece
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ karmada-controller-manager
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
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.NewKubectlKarmadaCommand(os.Stdout).Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
35 changes: 34 additions & 1 deletion pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,40 @@ func NewKarmadaCtlCommand(out io.Writer) *cobra.Command {
karmadaConfig := NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
rootCmd.AddCommand(NewCmdJoin(out, karmadaConfig))
rootCmd.AddCommand(NewCmdUnjoin(out, karmadaConfig))
rootCmd.AddCommand(NewCmdVersion(out))
rootCmd.AddCommand(NewCmdVersion(out, "karmadactl version"))
rootCmd.AddCommand(NewCmdCordon(out, karmadaConfig))
rootCmd.AddCommand(NewCmdUncordon(out, karmadaConfig))

return rootCmd
}

// NewKubectlKarmadaCommand creates the `kubectl karmada` command.
func NewKubectlKarmadaCommand(out io.Writer) *cobra.Command {
// Parent command to which all sub-commands are added.
rootCmd := &cobra.Command{
Use: "karmada",
Short: "kubectl karmada controls a Kubernetes Cluster Federation.",
Long: "kubectl karmada controls a Kubernetes Cluster Federation.",

RunE: runHelp,
}

// Add the command line flags from other dependencies (e.g., klog), but do not
// warn if they contain underscores.
pflag.CommandLine.SetNormalizeFunc(apiserverflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
rootCmd.PersistentFlags().AddFlagSet(pflag.CommandLine)

// From this point and forward we get warnings on flags that contain "_" separators
rootCmd.SetGlobalNormalizationFunc(apiserverflag.WarnWordSepNormalizeFunc)

// Prevent klog errors about logging before parsing.
_ = flag.CommandLine.Parse(nil)

karmadaConfig := NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
rootCmd.AddCommand(NewCmdJoin(out, karmadaConfig))
rootCmd.AddCommand(NewCmdUnjoin(out, karmadaConfig))
rootCmd.AddCommand(NewCmdVersion(out, "kubectl karmada version"))
rootCmd.AddCommand(NewCmdCordon(out, karmadaConfig))
rootCmd.AddCommand(NewCmdUncordon(out, karmadaConfig))

Expand Down
4 changes: 2 additions & 2 deletions pkg/karmadactl/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var (
)

// 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",
Long: versionLong,
Example: versionExample,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(out, "karmadactl version: %s\n", fmt.Sprintf("%#v", version.Get()))
fmt.Fprintf(out, "%s: %s\n", cmdStr, fmt.Sprintf("%#v", version.Get()))
},
}

Expand Down

0 comments on commit 3c80ece

Please sign in to comment.