Skip to content

Commit

Permalink
Add describe commponent support (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Apr 14, 2021
1 parent 2b08608 commit c8595ee
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fmt:
go fmt ./...

lint:
~/go/bin/golint -set_exit_status ./...
golint -set_exit_status ./...

copy: build
sudo cp bin/ks /usr/local/bin/ks
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ 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
* Describe a KubeSphere component (wrapper of kubectl describe)
* Pipeline management
* Create a Pipeline with java, go template
* Edit a Pipeline without give the fullname (namespace/name)
* User Management
* Working with [k3s](https://github.com/k3s-io/k3s) via setting environment `kubernetes_type=k3s`

* KubeSphere installation
* Install KubeSphere via [ks-installer](https://github.com/kubesphere/ks-installer)
* Install KubeSphere via [k3d](https://github.com/rancher/k3d)
* Install KubeSphere via [kubekey](https://github.com/kubesphere/kubekey)
* Install KubeSphere via [kind](https://github.com/kubernetes-sigs/kind)
## Component

```
Expand Down
12 changes: 10 additions & 2 deletions kubectl-plugin/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package common

import "github.com/spf13/cobra"

// CompletionFunc is the function for command completion
type CompletionFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

// NoFileCompletion avoid completion with files
func NoFileCompletion(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}

// ArrayCompletion return a completion which base on an array
func ArrayCompletion(array ...string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func ArrayCompletion(array ...string) CompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return array, cobra.ShellCompDirectiveNoFileComp
}
}

// PluginAbleComponentsCompletion returns a completion function for pluginAble components
func PluginAbleComponentsCompletion() func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func PluginAbleComponentsCompletion() CompletionFunc {
return ArrayCompletion(GetPluginAbleComponents()...)
}

// KubeSphereDeploymentCompletion returns a completion function for KuebSphere deployments
func KubeSphereDeploymentCompletion() CompletionFunc {
return ArrayCompletion(GetKubeShpereDeployment()...)
}
7 changes: 7 additions & 0 deletions kubectl-plugin/common/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ func GetPluginAbleComponents() []string {
"notification", "openpitrix", "servicemesh",
}
}

//GetKubeShpereDeployment returns the deployment of KubeSphere
func GetKubeShpereDeployment() []string {
return []string{
"apiserver", "controller", "console", "jenkins", "installer",
}
}
5 changes: 3 additions & 2 deletions kubectl-plugin/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ func NewComponentCmd(client dynamic.Interface, clientset *kubernetes.Clientset)
NewComponentEditCmd(client),
NewComponentResetCmd(client),
NewComponentWatchCmd(client),
NewComponentLogCmd(client, clientset),
newComponentLogCmd(client, clientset),
newComponentsExecCmd(client),
newComponentsKillCmd(client),
newScaleCmd())
newScaleCmd(),
newComponentDescribeCmd(client, clientset))
return
}

Expand Down
52 changes: 52 additions & 0 deletions kubectl-plugin/component/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package component

import (
"fmt"
"github.com/linuxsuren/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,
},
}

cmd = &cobra.Command{
Use: "describe",
Short: "Wrapper of kubectl describe",
Aliases: []string{"desc", "inspect"},
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: common.KubeSphereDeploymentCompletion(),
PreRunE: opt.preRunE,
RunE: opt.runE,
}
return
}

type describeOption struct {
Option
}

func (o *describeOption) preRunE(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
o.Name = args[0]
}

if o.Name == "" {
err = fmt.Errorf("please provide the name of component")
}
return
}

func (o *describeOption) runE(_ *cobra.Command, args []string) (err error) {
var podName string
var ns string
if ns, podName, err = o.getPod(args[0]); err == nil {
err = common.ExecCommand("kubectl", "describe", "-n", ns, "pod", podName)
}
return
}
71 changes: 28 additions & 43 deletions kubectl-plugin/component/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,51 @@ import (
)

func newComponentsExecCmd(client dynamic.Interface) (cmd *cobra.Command) {
availableComs := common.ArrayCompletion("jenkins", "apiserver")
opt := &Option{
Client: client,
}

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: availableComs,
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
}

switch args[0] {
case "jenkins":
var jenkinsPodName string
var list *unstructured.UnstructuredList
if list, err = client.Resource(kstypes.GetPodSchema()).Namespace("kubesphere-devops-system").List(
context.TODO(), metav1.ListOptions{}); err == nil {
for _, item := range list.Items {
if strings.HasPrefix(item.GetName(), "ks-jenkins") {
jenkinsPodName = item.GetName()
}
}
} else {
fmt.Println(err)
return
}

if jenkinsPodName == "" {
err = fmt.Errorf("cannot found ks-jenkins pod")
} else {
err = syscall.Exec(kubectl, []string{"kubectl", "-n", "kubesphere-devops-system", "exec", "-it", jenkinsPodName, "bash"}, os.Environ())
}
case "apiserver":
var apiserverPodName string
var list *unstructured.UnstructuredList
if list, err = client.Resource(kstypes.GetPodSchema()).Namespace("kubesphere-system").List(
context.TODO(), metav1.ListOptions{}); err == nil {
for _, item := range list.Items {
if strings.HasPrefix(item.GetName(), "ks-apiserver") {
apiserverPodName = item.GetName()
}
}
} else {
fmt.Println(err)
return
}

if apiserverPodName == "" {
err = fmt.Errorf("cannot found ks-jenkins pod")
} else {
err = syscall.Exec(kubectl, []string{"kubectl", "-n", "kubesphere-system", "exec", "-it", apiserverPodName, "sh"}, os.Environ())
}
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
},
}

return
}

func (o *Option) getPod(name string) (ns, podName string, err error) {
var deployName string
var list *unstructured.UnstructuredList
ns, deployName = o.getNsAndName(name)
if list, err = o.Client.Resource(kstypes.GetPodSchema()).Namespace(ns).List(
context.TODO(), metav1.ListOptions{}); err == nil {
for _, item := range list.Items {
if strings.HasPrefix(item.GetName(), deployName) {
podName = item.GetName()
break
}
}
}

if podName == "" {
err = fmt.Errorf("cannot found %s pod", deployName)
}
return
}
6 changes: 3 additions & 3 deletions kubectl-plugin/component/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type LogOption struct {
Tail int64
}

// NewComponentLogCmd returns a command to enable (or disable) a component by name
func NewComponentLogCmd(client dynamic.Interface, clientset *kubernetes.Clientset) (cmd *cobra.Command) {
// 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,
Expand All @@ -36,7 +36,7 @@ func NewComponentLogCmd(client dynamic.Interface, clientset *kubernetes.Clientse
cmd = &cobra.Command{
Use: "log",
Short: "Output the log of KubeSphere component",
ValidArgsFunction: common.ArrayCompletion("apiserver", "controller", "console", "jenkins", "installer"),
ValidArgsFunction: common.KubeSphereDeploymentCompletion(),
PreRunE: opt.componentNameCheck,
RunE: opt.logRunE,
}
Expand Down

0 comments on commit c8595ee

Please sign in to comment.