Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2165 from GuessWhoSamFoo/issue-1440
Browse files Browse the repository at this point in the history
Allow plugins to add multiple tabs
  • Loading branch information
Sam Foo committed Mar 18, 2021
2 parents 09de758 + 244ba31 commit 8055778
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 255 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/2165-GuessWhoSamFoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow plugins to add multiple tabs
1 change: 0 additions & 1 deletion pkg/dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ func (r *Runner) initAPI(ctx context.Context, logger log.Logger, opts ...RunnerO
}
}

fmt.Println(options.EnableMemStats)
if options.EnableMemStats {
if err := memStats(); err != nil {
logger.Infof("Enable MemStat")
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Metadata struct {
type Service interface {
Register(ctx context.Context, dashboardAPIAddress string) (Metadata, error)
Print(ctx context.Context, object runtime.Object) (PrintResponse, error)
PrintTab(ctx context.Context, object runtime.Object) (TabResponse, error)
PrintTabs(ctx context.Context, object runtime.Object) ([]TabResponse, error)
ObjectStatus(ctx context.Context, object runtime.Object) (ObjectStatusResponse, error)
HandleAction(ctx context.Context, actionName string, payload action.Payload) error
}
Expand Down
327 changes: 196 additions & 131 deletions pkg/plugin/dashboard/dashboard.pb.go

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion pkg/plugin/dashboard/dashboard.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ message PrintResponse {
}

message PrintTabResponse {
repeated PrintTab tabs = 1;
}

message PrintTab {
string name = 1;
bytes layout = 2;
}
Expand All @@ -100,7 +104,7 @@ service Plugin {
rpc Register(RegisterRequest) returns (RegisterResponse);
rpc Print(ObjectRequest) returns (PrintResponse);
rpc ObjectStatus(ObjectRequest) returns (ObjectStatusResponse);
rpc PrintTab(ObjectRequest) returns (PrintTabResponse);
rpc PrintTabs(ObjectRequest) returns (PrintTabResponse);
rpc WatchAdd(WatchRequest) returns (Empty);
rpc WatchUpdate(WatchRequest) returns (Empty);
rpc WatchDelete(WatchRequest) returns (Empty);
Expand Down
32 changes: 16 additions & 16 deletions pkg/plugin/fake/fakes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions pkg/plugin/fake/mock_plugin_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 43 additions & 35 deletions pkg/plugin/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,9 @@ func createObjectRequest(object runtime.Object, clientID string) (*dashboard.Obj
return or, err
}

// PrintTab creates a tab for an object.
func (c *GRPCClient) PrintTab(ctx context.Context, object runtime.Object) (TabResponse, error) {
var tab component.Tab

// PrintTabs creates one or more tabs for an object.
func (c *GRPCClient) PrintTabs(ctx context.Context, object runtime.Object) ([]TabResponse, error) {
var responses []TabResponse
clientID := ocontext.WebsocketClientIDFrom(ctx)

err := c.run(func() error {
Expand All @@ -283,37 +282,40 @@ func (c *GRPCClient) PrintTab(ctx context.Context, object runtime.Object) (TabRe
return err
}

resp, err := c.client.PrintTab(ctx, in, grpc.WaitForReady(true))
resp, err := c.client.PrintTabs(ctx, in, grpc.WaitForReady(true))
if err != nil {
return errors.Wrap(err, "grpc client print tab")
}

var to component.TypedObject
if err := json.Unmarshal(resp.Layout, &to); err != nil {
return err
}

c, err := to.ToComponent()
if err != nil {
return err
}
for _, t := range resp.Tabs {
var tab component.Tab
var to component.TypedObject
if err := json.Unmarshal(t.Layout, &to); err != nil {
return err
}

layout, ok := c.(*component.FlexLayout)
if !ok {
return errors.Errorf("expected to be flex layout was: %T", c)
}
c, err := to.ToComponent()
if err != nil {
return err
}

tab.Name = resp.Name
tab.Contents = *layout
layout, ok := c.(*component.FlexLayout)
if !ok {
return errors.Errorf("expected to be flex layout was: %T", c)
}

tab.Name = t.Name
tab.Contents = *layout
responses = append(responses, TabResponse{&tab})
}
return nil
})

if err != nil {
return TabResponse{}, err
return []TabResponse{}, err
}

return TabResponse{Tab: &tab}, nil
return responses, nil
}

// GRPCServer is the grpc server the dashboard will use to communicate with the
Expand Down Expand Up @@ -474,34 +476,40 @@ func decodeObjectRequest(req *dashboard.ObjectRequest) (*unstructured.Unstructur
return u, nil
}

// PrintTab prints a tab for an object.
func (s *GRPCServer) PrintTab(ctx context.Context, objectRequest *dashboard.ObjectRequest) (*dashboard.PrintTabResponse, error) {
// PrintTabs prints one or more tabs for an object.
func (s *GRPCServer) PrintTabs(ctx context.Context, objectRequest *dashboard.ObjectRequest) (*dashboard.PrintTabResponse, error) {
u, err := decodeObjectRequest(objectRequest)
if err != nil {
return nil, err
}

ctx = ocontext.WithWebsocketClientID(ctx, objectRequest.ClientID)
tabResponse, err := s.Impl.PrintTab(ctx, u)
tabResponses, err := s.Impl.PrintTabs(ctx, u)
if err != nil {
return nil, errors.Wrap(err, "grpc server print tab")
}

if tabResponse.Tab == nil {
return nil, errors.New("tab is nil")
out := dashboard.PrintTabResponse{
Tabs: []*dashboard.PrintTab{},
}
for _, tabResponse := range tabResponses {
if tabResponse.Tab == nil {
return nil, errors.New("tab is nil")
}

layoutBytes, err := json.Marshal(tabResponse.Tab.Contents)
if err != nil {
return nil, err
}
layoutBytes, err := json.Marshal(tabResponse.Tab.Contents)
if err != nil {
return nil, err
}

out := &dashboard.PrintTabResponse{
Name: tabResponse.Tab.Name,
Layout: layoutBytes,
tab := &dashboard.PrintTab{
Name: tabResponse.Tab.Name,
Layout: layoutBytes,
}
out.Tabs = append(out.Tabs, tab)
}

return out, nil
return &out, nil
}

// WatchAdd is called when a watched GVK has a new object added.
Expand Down
24 changes: 15 additions & 9 deletions pkg/plugin/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func Test_GRPCClient_Print(t *testing.T) {
})
}

func Test_GRPCClient_PrintTab(t *testing.T) {
func Test_GRPCClient_PrintTabs(t *testing.T) {
testWithGRPCClient(t, func(mocks *grpcClientMocks) {
object := testutil.CreateDeployment("deployment")

Expand All @@ -244,17 +244,21 @@ func Test_GRPCClient_PrintTab(t *testing.T) {
require.NoError(t, err)

tabResponse := &dashboard.PrintTabResponse{
Name: "tab name",
Layout: encodeComponent(t, layout.ToComponent("component title")),
Tabs: []*dashboard.PrintTab{
{
Name: "tab name",
Layout: encodeComponent(t, layout.ToComponent("component title")),
},
},
}

mocks.protoClient.EXPECT().
PrintTab(gomock.Any(), gomock.Eq(objectRequest), grpc.WaitForReady(true)).
PrintTabs(gomock.Any(), gomock.Eq(objectRequest), grpc.WaitForReady(true)).
Return(tabResponse, nil)

client := mocks.genClient()
ctx := context.Background()
got, err := client.PrintTab(ctx, object)
got, err := client.PrintTabs(ctx, object)
require.NoError(t, err)

expectedLayout := component.NewFlexLayout("component title")
Expand All @@ -265,10 +269,12 @@ func Test_GRPCClient_PrintTab(t *testing.T) {
View: component.NewText("text")},
},
)
expected := plugin.TabResponse{
Tab: &component.Tab{
Name: "tab name",
Contents: *expectedLayout,
expected := []plugin.TabResponse{
{
Tab: &component.Tab{
Name: "tab name",
Contents: *expectedLayout,
},
},
}

Expand Down

0 comments on commit 8055778

Please sign in to comment.