Skip to content

Commit

Permalink
Add a command to manage compoent (#15)
Browse files Browse the repository at this point in the history
* Add a command to manage compoent

* Fix the go build command

* Add component watch command

* Add component to manage all part of ks components

* Add component logs output command
  • Loading branch information
LinuxSuRen committed Dec 21, 2020
1 parent 02d96aa commit 6be9075
Show file tree
Hide file tree
Showing 12 changed files with 793 additions and 33 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ build: pre-build
CGO_ENABLE=0 go build $(BUILDFLAGS) -o bin/ks

build-plugin: pre-build
CGO_ENABLE=0 go build ${BUILDFLAGS} -o bin/kubectl-ks kubectl-plugin/*
CGO_ENABLE=0 go build ${BUILDFLAGS} -o bin/kubectl-ks kubectl-plugin/*.go

pre-build: fmt lint
pre-build: fmt lint mod-tidy

mod-tidy:
go mod tidy

fmt:
go fmt ./...
Expand Down
50 changes: 42 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,52 @@ It's also [a plugin of kubectl](https://github.com/kubernetes-sigs/krew).
All features below work with [Kubesphere](https://github.com/kubsphere/kubesphere) instead of other concept.

* Pipeline management
* Edit a Pipeline without give the fullname (namespace/name)
* User Management
* Component Management
* Enable (disable) a component (TODO)
* Update a component manually or automatically (Working on)
* Enable (disable) a component
* Update a component manually or automatically
* Output the logs of a KubeSphere component
* Edit a KubeSphere component

## Pipeline

You can delete the pipelines from Kubesphere interactively:
```
➜ ~ kubectl ks pipeline delete
? Please select the namespace whose you want to check: rick5rqdt
? Please select the namespace whose you want to check: [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]
> [ ] 123
[ ] abc
➜ ~ kubectl ks pip
Usage:
ks pipeline [flags]
ks pipeline [command]
Aliases:
pipeline, pip
Available Commands:
delete Delete a specific Pipeline of KubeSphere DevOps
edit Edit the target pipeline
view Output the YAML format of a Pipeline
Flags:
-h, --help help for pipeline
Use "ks pipeline [command] --help" for more information about a command.
```

## Component

```
➜ ~ kubectl ks com
Manage the components of KubeSphere
Usage:
ks component [command]
Aliases:
component, com
Available Commands:
edit edit the target component
enable Enable or disable the specific KubeSphere component
log output the log of KubeSphere component
reset reset the component by name
watch Update images of ks-apiserver, ks-controller-manager, ks-console
```
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ go 1.15

require (
github.com/AlecAivazis/survey/v2 v2.2.2
github.com/ghodss/yaml v1.0.0
github.com/imdario/mergo v0.3.9 // indirect
github.com/linuxsuren/cobra-extension v0.0.5
github.com/linuxsuren/go-cli-alias v0.0.4
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.19.4
k8s.io/apimachinery v0.19.4
k8s.io/cli-runtime v0.19.4
k8s.io/client-go v0.19.4
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbt
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
Expand Down Expand Up @@ -66,6 +67,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7 h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -201,6 +203,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -238,6 +241,7 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down
14 changes: 12 additions & 2 deletions kubectl-plugin/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"fmt"
pkg "github.com/linuxsuren/cobra-extension"
extver "github.com/linuxsuren/cobra-extension/version"
"github.com/linuxsuren/ks/kubectl-plugin/component"
kstype "github.com/linuxsuren/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"os"
Expand All @@ -29,6 +32,7 @@ See also https://github.com/kubesphere/kubesphere`,
var config *rest.Config
var err error
var client dynamic.Interface
var clientSet *kubernetes.Clientset

if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil {
panic(err)
Expand All @@ -39,11 +43,17 @@ See also https://github.com/kubesphere/kubesphere`,
return
}

if clientSet, err = kubernetes.NewForConfig(config); err != nil {
panic(err)
return
}

cmd.AddCommand(NewUserCmd(client),
NewPipelineCmd(client),
NewUpdateCmd(client),
extver.NewVersionCmd("linuxsuren", "ks", "kubectl-ks", nil),
pkg.NewCompletionCmd(cmd))
pkg.NewCompletionCmd(cmd),
component.NewComponentCmd(client, clientSet))
return
}

Expand All @@ -56,7 +66,7 @@ func NewUserCmd(client dynamic.Interface) (cmd *cobra.Command) {
RunE: func(cmd *cobra.Command, args []string) (err error) {
name := args[0]

_, err = client.Resource(GetUserSchema()).Patch(context.TODO(),
_, err = client.Resource(kstype.GetUserSchema()).Patch(context.TODO(),
name,
types.MergePatchType,
[]byte(fmt.Sprintf(`{"spec":{"password":"%s"},"metadata":{"annotations":null}}`, name)),
Expand Down
61 changes: 61 additions & 0 deletions kubectl-plugin/common/editor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package common

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/AlecAivazis/survey/v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"os"
"sigs.k8s.io/yaml"
)

// UpdateWithEditor update the k8s resources with a editor
func UpdateWithEditor(resource schema.GroupVersionResource, ns, name string, client dynamic.Interface) (err error) {
var rawPip *unstructured.Unstructured
var data []byte
ctx := context.TODO()

buf := bytes.NewBuffer(data)
if rawPip, err = client.Resource(resource).Namespace(ns).Get(ctx, name, metav1.GetOptions{}); err == nil {
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
if err = enc.Encode(rawPip); err != nil {
return
}
} else {
err = fmt.Errorf("cannot get component, error: %#v", err)
return
}

var yamlData []byte
if yamlData, err = yaml.JSONToYAML(buf.Bytes()); err != nil {
return
}

var fileName = "*.yaml"
var content = string(yamlData)

prompt := &survey.Editor{
Message: fmt.Sprintf("Edit component %s/%s", ns, name),
FileName: fileName,
Default: string(yamlData),
HideDefault: true,
AppendDefault: true,
}

err = survey.AskOne(prompt, &content, survey.WithStdio(os.Stdin, os.Stdout, os.Stderr))
if content == string(yamlData) {
fmt.Println("Edit cancelled, no changes made.")
return
}

if err = yaml.Unmarshal([]byte(content), rawPip); err == nil {
_, err = client.Resource(resource).Namespace(ns).Update(context.TODO(), rawPip, metav1.UpdateOptions{})
}
return
}
Loading

0 comments on commit 6be9075

Please sign in to comment.