From 4d7823321f83fda45eb33482eff798040802f403 Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Fri, 24 Sep 2021 09:11:28 +0800 Subject: [PATCH] Support to pass context flag into componenets command (#206) --- kubectl-plugin/common/client.go | 9 ++++++- kubectl-plugin/component/component.go | 26 ++++++++++--------- kubectl-plugin/component/describe.go | 14 ++++------ kubectl-plugin/component/enable.go | 14 +++++----- kubectl-plugin/component/exec.go | 37 ++++++++++++++------------- kubectl-plugin/component/kill.go | 9 ++++--- kubectl-plugin/component/log.go | 11 ++------ kubectl-plugin/component/reset.go | 13 +++++----- kubectl-plugin/component/watch.go | 13 +++++----- 9 files changed, 71 insertions(+), 75 deletions(-) diff --git a/kubectl-plugin/common/client.go b/kubectl-plugin/common/client.go index 44e18cf..9a66987 100644 --- a/kubectl-plugin/common/client.go +++ b/kubectl-plugin/common/client.go @@ -20,13 +20,20 @@ func GetClient() (client dynamic.Interface, clientSet *kubernetes.Clientset, err return } -// GetDynamicClient get the dynamic k8s client from context +// GetDynamicClient gets the dynamic k8s client from context func GetDynamicClient(ctx context.Context) (client dynamic.Interface) { factory := ctx.Value(ClientFactory{}) client, _ = factory.(*ClientFactory).GetClient() return } +// GetClientset gets the clientset of k8s +func GetClientset(ctx context.Context) (clientset *kubernetes.Clientset) { + factory := ctx.Value(ClientFactory{}) + _, clientset = factory.(*ClientFactory).GetClient() + return +} + // ClientFactory is for getting k8s client type ClientFactory struct { //client dynamic.Interface diff --git a/kubectl-plugin/component/component.go b/kubectl-plugin/component/component.go index 21608ac..c6ae662 100644 --- a/kubectl-plugin/component/component.go +++ b/kubectl-plugin/component/component.go @@ -21,15 +21,15 @@ func NewComponentCmd(client dynamic.Interface, clientset *kubernetes.Clientset) Short: "Manage the components of KubeSphere", } - cmd.AddCommand(newComponentEnableCmd(client), - NewComponentEditCmd(client), - NewComponentResetCmd(client), - NewComponentWatchCmd(client), - newComponentLogCmd(client, clientset), - newComponentsExecCmd(client), - newComponentsKillCmd(client), + cmd.AddCommand(newComponentEnableCmd(), + NewComponentEditCmd(), + NewComponentResetCmd(), + NewComponentWatchCmd(), + newComponentLogCmd(), + newComponentsExecCmd(), + newComponentsKillCmd(), newScaleCmd(), - newComponentDescribeCmd(client, clientset)) + newComponentDescribeCmd()) return } @@ -141,10 +141,8 @@ type simpleDeploy struct { } // NewComponentEditCmd returns a command to enable (or disable) a component by name -func NewComponentEditCmd(client dynamic.Interface) (cmd *cobra.Command) { - opt := &Option{ - Client: client, - } +func NewComponentEditCmd() (cmd *cobra.Command) { + opt := &Option{} cmd = &cobra.Command{ Use: "edit", Short: "Edit the target component", @@ -159,6 +157,10 @@ func NewComponentEditCmd(client dynamic.Interface) (cmd *cobra.Command) { } func (o *Option) componentNameCheck(cmd *cobra.Command, args []string) (err error) { + ctx := cmd.Root().Context() + o.Client = common.GetDynamicClient(ctx) + o.Clientset = common.GetClientset(ctx) + if len(args) > 0 { o.Name = args[0] } diff --git a/kubectl-plugin/component/describe.go b/kubectl-plugin/component/describe.go index dcaf402..9c74470 100644 --- a/kubectl-plugin/component/describe.go +++ b/kubectl-plugin/component/describe.go @@ -4,17 +4,10 @@ import ( "fmt" "github.com/kubesphere-sigs/ks/kubectl-plugin/common" "github.com/spf13/cobra" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/kubernetes" ) -func newComponentDescribeCmd(client dynamic.Interface, _ *kubernetes.Clientset) (cmd *cobra.Command) { - opt := &describeOption{ - Option{ - Client: client, - }, - } - +func newComponentDescribeCmd() (cmd *cobra.Command) { + opt := &describeOption{} cmd = &cobra.Command{ Use: "describe", Short: "Wrapper of kubectl describe", @@ -32,6 +25,9 @@ type describeOption struct { } func (o *describeOption) preRunE(cmd *cobra.Command, args []string) (err error) { + ctx := cmd.Root().Context() + o.Client = common.GetDynamicClient(ctx) + if len(args) > 0 { o.Name = args[0] } diff --git a/kubectl-plugin/component/enable.go b/kubectl-plugin/component/enable.go index 5d636c8..ff337e4 100644 --- a/kubectl-plugin/component/enable.go +++ b/kubectl-plugin/component/enable.go @@ -9,7 +9,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/dynamic" "strconv" ) @@ -22,13 +21,8 @@ type EnableOption struct { } // newComponentEnableCmd returns a command to enable (or disable) a component by name -func newComponentEnableCmd(client dynamic.Interface) (cmd *cobra.Command) { - opt := &EnableOption{ - Option: Option{ - Client: client, - }, - } - +func newComponentEnableCmd() (cmd *cobra.Command) { + opt := &EnableOption{} cmd = &cobra.Command{ Use: "enable", Short: "Enable or disable the specific KubeSphere component", @@ -61,6 +55,10 @@ Or it's possible to enable all components via: ks com enable all'`, } func (o *EnableOption) enablePreRunE(cmd *cobra.Command, args []string) (err error) { + ctx := cmd.Root().Context() + o.Client = common.GetDynamicClient(ctx) + o.Clientset = common.GetClientset(ctx) + if o.Edit { return } diff --git a/kubectl-plugin/component/exec.go b/kubectl-plugin/component/exec.go index 4eb33b0..0aaf725 100644 --- a/kubectl-plugin/component/exec.go +++ b/kubectl-plugin/component/exec.go @@ -8,18 +8,14 @@ import ( "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/client-go/dynamic" "os" "os/exec" "strings" "syscall" ) -func newComponentsExecCmd(client dynamic.Interface) (cmd *cobra.Command) { - opt := &Option{ - Client: client, - } - +func newComponentsExecCmd() (cmd *cobra.Command) { + opt := &Option{} cmd = &cobra.Command{ Use: "exec", Short: "Execute a command in a container.", @@ -27,21 +23,26 @@ func newComponentsExecCmd(client dynamic.Interface) (cmd *cobra.Command) { This command is similar with kubectl exec, the only difference is that you don't need to type the fullname'`, ValidArgsFunction: common.KubeSphereDeploymentCompletion(), Args: cobra.MinimumNArgs(1), - RunE: func(cmd *cobra.Command, args []string) (err error) { - var kubectl string - if kubectl, err = exec.LookPath("kubectl"); err != nil { - return - } + RunE: opt.runE, + } + return +} - var podName string - var ns string - if ns, podName, err = opt.getPod(args[0]); err == nil { - err = syscall.Exec(kubectl, []string{"kubectl", "-n", ns, "exec", "-it", podName, "bash"}, os.Environ()) - } - return - }, +func (o *Option) runE(cmd *cobra.Command, args []string) (err error) { + ctx := cmd.Root().Context() + o.Client = common.GetDynamicClient(ctx) + o.Clientset = common.GetClientset(ctx) + + var kubectl string + if kubectl, err = exec.LookPath("kubectl"); err != nil { + return } + var podName string + var ns string + if ns, podName, err = o.getPod(args[0]); err == nil { + err = syscall.Exec(kubectl, []string{"kubectl", "-n", ns, "exec", "-it", podName, "bash"}, os.Environ()) + } return } diff --git a/kubectl-plugin/component/kill.go b/kubectl-plugin/component/kill.go index 7c5bdba..60202cf 100644 --- a/kubectl-plugin/component/kill.go +++ b/kubectl-plugin/component/kill.go @@ -17,10 +17,8 @@ type killOption struct { name string } -func newComponentsKillCmd(client dynamic.Interface) (cmd *cobra.Command) { - opt := killOption{ - client: client, - } +func newComponentsKillCmd() (cmd *cobra.Command) { + opt := killOption{} cmd = &cobra.Command{ Use: "kill", Short: "Kill the pods of the components", @@ -43,6 +41,9 @@ func (o *killOption) preRunE(cmd *cobra.Command, args []string) (err error) { o.name = args[0] } + ctx := cmd.Root().Context() + o.client = common.GetDynamicClient(ctx) + switch o.name { case "apiserver": o.name = "ks-apiserver" diff --git a/kubectl-plugin/component/log.go b/kubectl-plugin/component/log.go index ce164ad..ba7322f 100644 --- a/kubectl-plugin/component/log.go +++ b/kubectl-plugin/component/log.go @@ -13,8 +13,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/kubernetes" ) // LogOption is the option for component log command @@ -26,13 +24,8 @@ type LogOption struct { } // newComponentLogCmd returns a command to enable (or disable) a component by name -func newComponentLogCmd(client dynamic.Interface, clientset *kubernetes.Clientset) (cmd *cobra.Command) { - opt := &LogOption{ - Option: Option{ - Clientset: clientset, - Client: client, - }, - } +func newComponentLogCmd() (cmd *cobra.Command) { + opt := &LogOption{} cmd = &cobra.Command{ Use: "log", Short: "Output the log of KubeSphere component", diff --git a/kubectl-plugin/component/reset.go b/kubectl-plugin/component/reset.go index 61d284a..0e1a3c9 100644 --- a/kubectl-plugin/component/reset.go +++ b/kubectl-plugin/component/reset.go @@ -6,16 +6,11 @@ import ( "github.com/kubesphere-sigs/ks/kubectl-plugin/common" kstypes "github.com/kubesphere-sigs/ks/kubectl-plugin/types" "github.com/spf13/cobra" - "k8s.io/client-go/dynamic" ) // NewComponentResetCmd returns a command to enable (or disable) a component by name -func NewComponentResetCmd(client dynamic.Interface) (cmd *cobra.Command) { - opt := &ResetOption{ - Option: Option{ - Client: client, - }, - } +func NewComponentResetCmd() (cmd *cobra.Command) { + opt := &ResetOption{} cmd = &cobra.Command{ Use: "reset", Short: "Reset the component by name", @@ -42,6 +37,10 @@ func NewComponentResetCmd(client dynamic.Interface) (cmd *cobra.Command) { } func (o *ResetOption) preRunE(cmd *cobra.Command, args []string) (err error) { + ctx := cmd.Root().Context() + o.Client = common.GetDynamicClient(ctx) + o.Clientset = common.GetClientset(ctx) + if o.Name == "" && len(args) > 0 { o.Name = args[0] } diff --git a/kubectl-plugin/component/watch.go b/kubectl-plugin/component/watch.go index 2ca5382..76eec6d 100644 --- a/kubectl-plugin/component/watch.go +++ b/kubectl-plugin/component/watch.go @@ -8,7 +8,6 @@ import ( "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/dynamic" "os" "os/exec" "os/signal" @@ -17,12 +16,8 @@ import ( ) // NewComponentWatchCmd returns a command to enable (or disable) a component by name -func NewComponentWatchCmd(client dynamic.Interface) (cmd *cobra.Command) { - opt := &WatchOption{ - Option: Option{ - Client: client, - }, - } +func NewComponentWatchCmd() (cmd *cobra.Command) { + opt := &WatchOption{} cmd = &cobra.Command{ Use: "watch", Short: "Update images of ks-apiserver, ks-controller-manager, ks-console", @@ -73,6 +68,10 @@ func (o *WatchOption) getDigest(image, tag string) string { } func (o *WatchOption) watchPreRunE(cmd *cobra.Command, args []string) (err error) { + ctx := cmd.Root().Context() + o.Client = common.GetDynamicClient(ctx) + o.Clientset = common.GetClientset(ctx) + if o.PrivateRegistry == "" { o.PrivateRegistry = os.Getenv("kS_PRIVATE_REG") }