Skip to content

Commit

Permalink
Support to pass context flag into componenets command (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Sep 24, 2021
1 parent 893e70a commit 4d78233
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 75 deletions.
9 changes: 8 additions & 1 deletion kubectl-plugin/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions kubectl-plugin/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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",
Expand All @@ -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]
}
Expand Down
14 changes: 5 additions & 9 deletions kubectl-plugin/component/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]
}
Expand Down
14 changes: 6 additions & 8 deletions kubectl-plugin/component/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down
37 changes: 19 additions & 18 deletions kubectl-plugin/component/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,41 @@ 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.",
Long: `Execute a command in a container.
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
}

Expand Down
9 changes: 5 additions & 4 deletions kubectl-plugin/component/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
11 changes: 2 additions & 9 deletions kubectl-plugin/component/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
13 changes: 6 additions & 7 deletions kubectl-plugin/component/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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]
}
Expand Down
13 changes: 6 additions & 7 deletions kubectl-plugin/component/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 4d78233

Please sign in to comment.