From 8a0cc13630a29e30d5918973b6149cacd8ec8930 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie <1450685+LinuxSuRen@users.noreply.github.com> Date: Mon, 8 Mar 2021 06:32:16 +0800 Subject: [PATCH] Add k3s support for ks (#66) --- README.md | 1 + go.mod | 3 ++- go.sum | 5 ++--- main.go | 37 +++++++++++++++++++++++++++---------- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 50df2ab..ca9de5b 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ All features below work with [KubeSphere](https://github.com/kubsphere/kubespher * Update a component manually or automatically * Output the logs of a KubeSphere component * Edit a KubeSphere component +* Working with [k3s](https://github.com/k3s-io/k3s) via setting environment `kubernetes_type=k3s` ## Pipeline diff --git a/go.mod b/go.mod index 6047c60..54e6675 100644 --- a/go.mod +++ b/go.mod @@ -10,11 +10,12 @@ require ( github.com/huandu/xstrings v1.3.2 // indirect github.com/imdario/mergo v0.3.9 // indirect github.com/linuxsuren/cobra-extension v0.0.10 - github.com/linuxsuren/go-cli-alias v0.0.4 + github.com/linuxsuren/go-cli-alias v0.0.5 github.com/mitchellh/copystructure v1.1.1 // indirect github.com/spf13/cobra v1.1.1 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.7.0 + golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c k8s.io/api v0.19.4 diff --git a/go.sum b/go.sum index 0f46787..3d71e98 100644 --- a/go.sum +++ b/go.sum @@ -264,9 +264,8 @@ github.com/linuxsuren/cobra-extension v0.0.6 h1:JlSmHjE/KCdJBZYGTjQbgvXSpziFwzpI github.com/linuxsuren/cobra-extension v0.0.6/go.mod h1:qcEJv7BbL0UpK6MbrTESP/nKf1+z1wQdMAnE1NBl3QQ= github.com/linuxsuren/cobra-extension v0.0.10 h1:ciZDb2Bp/aAFqr4YoeVuH2uyBaBFfO6pwz1WBih7R4A= github.com/linuxsuren/cobra-extension v0.0.10/go.mod h1:nDsXgvm0lSWVV+byAEfwhIGFDoIp0Sq9wkfNUDBp5do= -github.com/linuxsuren/go-cli-alias v0.0.4 h1:+otarDOeSZzzbTCr9CllAOQCQNpaf/HI41iDvWWUo/w= -github.com/linuxsuren/go-cli-alias v0.0.4/go.mod h1:dfwOx8H0iVpdS9gtLC80GCC4cvDvzWejxjxXn7sFWUs= -github.com/linuxsuren/go-cli-plugin v0.0.1/go.mod h1:uyO09KK8otYfDV5LVTfcWX2UbAi3kEz3PrkdfIxnDlg= +github.com/linuxsuren/go-cli-alias v0.0.5 h1:8FuE+z9Gvp6UIxV5ojl+RQ+UazoxGR92OWwrs9z0+RM= +github.com/linuxsuren/go-cli-alias v0.0.5/go.mod h1:Sa7xNUI72BgHTcywDU8MXVSH1q72SLdMALZSSANwuUM= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec h1:N6xw+W4IvgOV0cmhE7iUWKHNZqMUXlT3in+1ORmqIGo= github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI= github.com/linuxsuren/http-downloader v0.0.6 h1:JuCgWVT4/pw8MAhYe+ngE7BwcO575L1aPDc9Fgx+2ig= diff --git a/main.go b/main.go index 9b94862..e99ad3f 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( + "fmt" ext "github.com/linuxsuren/cobra-extension" extver "github.com/linuxsuren/cobra-extension/version" aliasCmd "github.com/linuxsuren/go-cli-alias/pkg/cmd" "github.com/linuxsuren/ks/kubectl-plugin/entrypoint" + "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" "os" ) @@ -14,24 +16,39 @@ const ( TargetCLI = "kubectl" // AliasCLI represents the alias CLI which is ks AliasCLI = "ks" + // KubernetesType is the env name for Kubernetes type + KubernetesType = "kubernetes_type" ) func main() { - cmd := aliasCmd.CreateDefaultCmd(TargetCLI, AliasCLI) + kType := os.Getenv(KubernetesType) + var cmd *cobra.Command + var targetCommand string + switch kType { + case "k3s": + targetCommand = fmt.Sprintf("k3s %s", TargetCLI) + default: + targetCommand = TargetCLI + } + + cmd = aliasCmd.CreateDefaultCmd(TargetCLI, AliasCLI) cmd.AddCommand(extver.NewVersionCmd("linuxsuren", AliasCLI, AliasCLI, nil)) aliasCmd.AddAliasCmd(cmd, getDefault()) cmd.AddCommand(ext.NewCompletionCmd(cmd)) - // add all the sub-commands from kubectl-ks - kubectlPluginCmdRoot := entrypoint.NewCmdKS(genericclioptions.IOStreams{ - In: os.Stdin, - Out: os.Stdout, - ErrOut: os.Stderr, - }) - kubectlPluginCmds := kubectlPluginCmdRoot.Commands() - cmd.AddCommand(kubectlPluginCmds...) + // need to figure out how to connect with k3s before enable below features + if kType != "k3s" { + // add all the sub-commands from kubectl-ks + kubectlPluginCmdRoot := entrypoint.NewCmdKS(genericclioptions.IOStreams{ + In: os.Stdin, + Out: os.Stdout, + ErrOut: os.Stderr, + }) + kubectlPluginCmds := kubectlPluginCmdRoot.Commands() + cmd.AddCommand(kubectlPluginCmds...) + } - aliasCmd.Execute(cmd, TargetCLI, getDefault(), nil) + aliasCmd.Execute(cmd, targetCommand, getDefault(), nil) }