Skip to content

Commit

Permalink
feat: add cluster sub-command in vsctl (#71)
Browse files Browse the repository at this point in the history
* add cluster sub-command in vsctl
* add --block and --segment in vsctl eventbus info for more infomation
  • Loading branch information
wenfengwang authored May 26, 2022
1 parent dbd70ae commit 9633ab8
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 12 deletions.
4 changes: 4 additions & 0 deletions deploy/all-in-one.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ spec:
nodePort: 30001
port: 8080
targetPort: 8080
- name: vanus-gateway-http
nodePort: 30002
port: 8081
targetPort: 8081
selector:
app: vanus-gateway
type: NodePort
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/cel-go v0.11.2
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/huandu/skiplist v1.2.0
github.com/jedib0t/go-pretty/v6 v6.3.1
github.com/labstack/echo/v4 v4.7.2
github.com/linkall-labs/embed-etcd v0.0.0
github.com/linkall-labs/eventbus-go v0.0.0
Expand All @@ -32,7 +34,7 @@ require (
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6
google.golang.org/grpc v1.44.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gopkg.in/yaml.v3 v3.0.0
k8s.io/client-go v0.23.4
)

Expand All @@ -56,7 +58,6 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand All @@ -68,12 +69,14 @@ require (
github.com/labstack/gommon v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/scylladb/go-set v1.0.2 // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
Expand Down
11 changes: 10 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jedib0t/go-pretty/v6 v6.3.1 h1:aOXiD9oqiuLH8btPQW6SfgtQN5zwhyfzZls8a6sPJ/I=
github.com/jedib0t/go-pretty/v6 v6.3.1/go.mod h1:FMkOpgGD3EZ91cW8g/96RfxoV7bdeJyzXPYgz1L1ln0=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
Expand Down Expand Up @@ -343,6 +345,8 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down Expand Up @@ -385,6 +389,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -415,6 +420,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -663,6 +670,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -951,8 +959,9 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
11 changes: 8 additions & 3 deletions vsctl/command/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"github.com/golang/protobuf/ptypes/empty"
"github.com/google/uuid"
ctrlpb "github.com/linkall-labs/vsproto/pkg/controller"
"io"
"net/http"
Expand Down Expand Up @@ -77,7 +78,7 @@ func putEventCommand() *cobra.Command {
} else {
target = fmt.Sprintf("%s%s/gateway/%s", httpPrefix, endpoint, args[0])
}
fmt.Printf("endpoint: %s\n", endpoint)

ctx := ce.ContextWithTarget(context.Background(), target)

if dataFile == "" {
Expand All @@ -87,7 +88,7 @@ func putEventCommand() *cobra.Command {
}
},
}
cmd.Flags().StringVar(&eventID, "id", "cmd", "event id of CloudEvent")
cmd.Flags().StringVar(&eventID, "id", "", "event id of CloudEvent")
cmd.Flags().StringVar(&dataFormat, "data-format", "json", "the format of event body, JSON or plain")
cmd.Flags().StringVar(&eventSource, "source", "cmd", "event source of CloudEvent")
cmd.Flags().StringVar(&eventType, "type", "cmd", "event type of CloudEvent")
Expand All @@ -114,14 +115,18 @@ func mustGetGatewayEndpoint(cmd *cobra.Command) string {

func sendOne(ctx context.Context, ceClient ce.Client) {
event := ce.NewEvent()
if eventID == "" {
eventID = uuid.NewString()
}
event.SetID(eventID)
event.SetSource(eventSource)
event.SetType(eventType)
var err error
if strings.ToLower(dataFormat) == "json" {
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(eventBody), &m); err != nil {
cmdFailedf("invalid format of data body: %s", err)
color.White(eventBody)
cmdFailedf("invalid format of data body: %s, err: %s", eventBody, err.Error())
}
err = event.SetData(ce.ApplicationJSON, m)
} else {
Expand Down
117 changes: 111 additions & 6 deletions vsctl/command/eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
"encoding/json"
"github.com/fatih/color"
"github.com/golang/protobuf/ptypes/empty"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
ctrlpb "github.com/linkall-labs/vsproto/pkg/controller"
metapb "github.com/linkall-labs/vsproto/pkg/meta"
"github.com/spf13/cobra"
"os"
)

func NewEventbusCommand() *cobra.Command {
Expand Down Expand Up @@ -108,15 +111,117 @@ func getEventbusInfoCommand() *cobra.Command {
cli := ctrlpb.NewEventBusControllerClient(grpcConn)
res, err := cli.GetEventBus(ctx, &metapb.EventBus{Name: args[0]})
if err != nil {
cmdFailedf("delete eventbus failed: %s", err)
cmdFailedf("get eventbus failed: %s", err)
}
data, _ := json.Marshal(res)
var out bytes.Buffer
_ = json.Indent(&out, data, "", "\t")
color.Green("%s", out.String())

segs := make(map[uint64][]*metapb.Segment)
if showSegment || showBlock {
logCli := ctrlpb.NewEventLogControllerClient(grpcConn)
logs := res.GetLogs()
for idx := range logs {
segRes, err := logCli.ListSegment(ctx, &ctrlpb.ListSegmentRequest{
EventBusId: res.Id,
EventLogId: logs[idx].EventLogId,
})
if err != nil {
cmdFailedf("get segments failed: %s", err)
}
segs[logs[idx].EventLogId] = segRes.Segments
}
}

t := table.NewWriter()

if !showSegment && !showBlock {
t.AppendHeader(table.Row{"Eventbus", "Eventlog", "Segment Number"})
for idx := 0; idx < len(res.Logs); idx++ {
if idx == 0 {
t.AppendRow(table.Row{res.Name, res.Logs[idx].EventLogId, res.Logs[idx].CurrentSegmentNumbers})
} else {
t.AppendRow(table.Row{"", res.Logs[idx].EventLogId, res.Logs[idx].CurrentSegmentNumbers})
}
}
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 2, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 3, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
})
} else {
if !showBlock {
t.AppendHeader(table.Row{"Eventbus", "Eventlog", "Segment", "Start", "End"})
for idx := 0; idx < len(res.Logs); idx++ {
segOfEL := segs[res.Logs[idx].EventLogId]
for sIdx, v := range segOfEL {
if idx == 0 && sIdx == 0 {
t.AppendRow(table.Row{res.Name, res.Logs[idx].EventLogId, v.Id, v.StartOffsetInLog,
v.EndOffsetInLog})
} else if sIdx == 0 {
t.AppendRow(table.Row{"", res.Logs[idx].EventLogId, v.Id, v.StartOffsetInLog,
v.EndOffsetInLog})
} else {
t.AppendRow(table.Row{"", "", v.Id, v.StartOffsetInLog,
v.EndOffsetInLog})
}
}
t.AppendSeparator()
}
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, VAlign: text.VAlignMiddle, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 2, VAlign: text.VAlignMiddle, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 3, VAlign: text.VAlignMiddle, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 4, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 5, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
})
} else {
t.AppendHeader(table.Row{"Eventbus", "Eventlog", "Segment", "Start", "End", "Block", "Leader", "Volume", "Endpoint"})
multiReplica := false
for idx := 0; idx < len(res.Logs); idx++ {
segOfEL := segs[res.Logs[idx].EventLogId]
for sIdx, seg := range segOfEL {
tIdx := 0
if !multiReplica && len(seg.Replicas) > 1 {
multiReplica = true
}
for _, blk := range seg.Replicas {
if idx == 0 && sIdx == 0 && tIdx == 0 {
t.AppendRow(table.Row{res.Name, res.Logs[idx].EventLogId, seg.Id, seg.StartOffsetInLog,
seg.EndOffsetInLog, blk.Id, blk.Id == seg.LeaderBlockId, blk.VolumeID, blk.Endpoint})
} else if sIdx == 0 && tIdx == 0 {
t.AppendRow(table.Row{"", res.Logs[idx].EventLogId, seg.Id, seg.StartOffsetInLog,
seg.EndOffsetInLog, blk.Id, blk.Id == seg.LeaderBlockId, blk.VolumeID, blk.Endpoint})
} else if tIdx == 0 {
t.AppendRow(table.Row{"", "", seg.Id, seg.StartOffsetInLog,
seg.EndOffsetInLog, blk.Id, blk.Id == seg.LeaderBlockId, blk.VolumeID, blk.Endpoint})
} else {
t.AppendRow(table.Row{"", "", "", "", "", blk.Id, blk.Id == seg.LeaderBlockId, blk.VolumeID, blk.Endpoint})
}
tIdx++
}
}
t.AppendSeparator()
}
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, VAlign: text.VAlignMiddle, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 2, VAlign: text.VAlignMiddle, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 3, VAlign: text.VAlignMiddle, AutoMerge: true, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 4, VAlign: text.VAlignMiddle, AutoMerge: multiReplica, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 5, VAlign: text.VAlignMiddle, AutoMerge: multiReplica, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 6, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 7, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 8, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
{Number: 9, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
})
}
}
t.SetStyle(table.StyleLight)
t.Style().Options.SeparateRows = true
t.Style().Box = table.StyleBoxDefault
t.SetOutputMirror(os.Stdout)
t.Render()
},
}
cmd.Flags().StringVar(&eventbus, "name", "", "eventbus name to deleting")
cmd.Flags().BoolVar(&showSegment, "segment", false, "if show all segment of eventlog")
cmd.Flags().BoolVar(&showBlock, "block", false, "if show all block of segment")
return cmd
}

Expand Down
2 changes: 2 additions & 0 deletions vsctl/command/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ var (
filters string
inputTransformer string
subscriptionID uint64
showSegment bool
showBlock bool
)
14 changes: 14 additions & 0 deletions vsctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package command

import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"strings"

"github.com/spf13/cobra"
Expand All @@ -38,3 +40,15 @@ func mustEndpointsFromCmd(cmd *cobra.Command) []string {
}
return eps
}

var styleVanus = table.Style{
Name: "StyleVanus",
Box: table.StyleBoxDefault,
Color: table.ColorOptionsDefault,
Format: table.FormatOptionsDefault,
HTML: table.DefaultHTMLOptions,
Options: table.OptionsDefault,
Title: table.TitleOptions{
Align: text.AlignCenter,
},
}
Loading

0 comments on commit 9633ab8

Please sign in to comment.