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

Add link generator to DashboardClient #2276

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/unreleased/2276-GuessWhoSamFoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add CreateLink method with objects for go plugin
1 change: 1 addition & 0 deletions pkg/dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ func (r *Runner) initAPI(ctx context.Context, logger log.Logger, opts ...RunnerO

r.moduleManager = moduleManager
pluginDashboardService := &pluginAPI.GRPCService{
LinkGenerator: moduleManager,
ObjectStore: appObjectStore,
PortForwarder: portForwarder,
NamespaceInterface: nsClient,
Expand Down
23 changes: 23 additions & 0 deletions pkg/plugin/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,26 @@ func (c *Client) SendAlert(ctx context.Context, clientID string, alert action.Al
_, err = client.SendAlert(ctx, alertRequest)
return err
}

func (c *Client) CreateLink(ctx context.Context, key store.Key) (LinkResponse, error) {
client := c.DashboardConnection.Client()

req, err := convertFromKey(key)
if err != nil {
return LinkResponse{}, err
}

resp, err := client.CreateLink(ctx, req)
if err != nil {
return LinkResponse{}, nil
}

linkComponent, err := convertToLinkComponent(resp.Ref, key.Name)
if err != nil {
return LinkResponse{}, err
}

return LinkResponse{
Link: *linkComponent,
}, nil
}
13 changes: 11 additions & 2 deletions pkg/plugin/api/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ SPDX-License-Identifier: Apache-2.0
package api

import (
"github.com/vmware-tanzu/octant/internal/util/json"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"

"github.com/vmware-tanzu/octant/internal/util/json"
"github.com/vmware-tanzu/octant/pkg/view/component"

"github.com/vmware-tanzu/octant/pkg/action"
"github.com/vmware-tanzu/octant/pkg/plugin/api/proto"
"github.com/vmware-tanzu/octant/pkg/store"
Expand Down Expand Up @@ -98,6 +99,14 @@ func convertToAlert(in *proto.AlertRequest) (action.Alert, error) {
return alert, nil
}

func convertToLinkComponent(in, name string) (*component.Link, error) {
if in == "" {
return &component.Link{}, nil
}
link := component.NewLink("", name, in)
return link, nil
}

func convertFromObjects(in *unstructured.UnstructuredList) ([][]byte, error) {
var out [][]byte

Expand Down
15 changes: 15 additions & 0 deletions pkg/plugin/api/fake/mock_dash_service.go

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

20 changes: 20 additions & 0 deletions pkg/plugin/api/fake/mock_dashboard_client.go

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

216 changes: 160 additions & 56 deletions pkg/plugin/api/proto/dashboard_api.pb.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pkg/plugin/api/proto/dashboard_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ message AlertRequest {
string clientID = 4;
}

message LinkResponse {
string ref = 1;
}

service Dashboard {
rpc List(KeyRequest) returns (ListResponse);
rpc Get(KeyRequest) returns (GetResponse);
Expand All @@ -82,4 +86,5 @@ service Dashboard {
rpc ListNamespaces(Empty) returns (NamespacesResponse);
rpc ForceFrontendUpdate(Empty) returns(Empty);
rpc SendAlert(AlertRequest) returns(Empty);
rpc CreateLink(KeyRequest) returns(LinkResponse);
}
42 changes: 42 additions & 0 deletions pkg/plugin/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"fmt"
"strings"

"github.com/vmware-tanzu/octant/internal/octant"
"github.com/vmware-tanzu/octant/pkg/view/component"

"google.golang.org/grpc/metadata"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

Expand Down Expand Up @@ -46,6 +49,10 @@ type NamespacesResponse struct {
Namespaces []string
}

type LinkResponse struct {
Link component.Link
}

// Service is the dashboard service.
type Service interface {
List(ctx context.Context, key store.Key) (*unstructured.UnstructuredList, error)
Expand All @@ -58,6 +65,7 @@ type Service interface {
Delete(ctx context.Context, key store.Key) error
ForceFrontendUpdate(ctx context.Context) error
SendAlert(ctx context.Context, clientID string, alert action.Alert) error
CreateLink(ctx context.Context, key store.Key) (LinkResponse, error)
}

// FrontendUpdateController can control the frontend. ie. the web gui
Expand Down Expand Up @@ -86,6 +94,7 @@ type GRPCService struct {
FrontendProxy FrontendProxy
NamespaceInterface cluster.NamespaceInterface
WebsocketClientManager event.WSClientGetter
LinkGenerator octant.LinkGenerator
}

var _ Service = (*GRPCService)(nil)
Expand Down Expand Up @@ -188,6 +197,18 @@ func (s *GRPCService) SendAlert(ctx context.Context, clientID string, alert acti
return nil
}

func (s *GRPCService) CreateLink(_ context.Context, key store.Key) (LinkResponse, error) {
ref, err := s.LinkGenerator.ObjectPath(key.Namespace, key.APIVersion, key.Kind, key.Name)
if err != nil {
return LinkResponse{}, err
}

linkComponent := component.NewLink("", "", ref)
return LinkResponse{
Link: *linkComponent,
}, nil
}

func NewGRPCServer(service Service) *grpcServer {
return &grpcServer{
service: service,
Expand Down Expand Up @@ -368,6 +389,27 @@ func (c *grpcServer) SendAlert(ctx context.Context, in *proto.AlertRequest) (*pr
return &proto.Empty{}, nil
}

func (c *grpcServer) CreateLink(ctx context.Context, in *proto.KeyRequest) (*proto.LinkResponse, error) {
key, err := convertToKey(in)
if err != nil {
return nil, err
}

resp, err := c.service.CreateLink(ctx, key)
if err != nil {
return nil, err
}

var ref string
if &resp.Link != nil && &resp.Link.Config != nil {
ref = resp.Link.Config.Ref
}

return &proto.LinkResponse{
Ref: ref,
}, nil
}

func extractObjectStoreMetadata(ctx context.Context) context.Context {
// Second value is ignored as metadata is always set by grpc.
md, _ := metadata.FromIncomingContext(ctx)
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Dashboard interface {
ListNamespaces(ctx context.Context) (api.NamespacesResponse, error)
ForceFrontendUpdate(ctx context.Context) error
SendAlert(ctx context.Context, clientID string, alert action.Alert) error
CreateLink(ctx context.Context, key store.Key) (api.LinkResponse, error)
}

// NewDashboardClient creates a dashboard client.
Expand Down
15 changes: 15 additions & 0 deletions pkg/plugin/service/fake/mock_dashboard.go

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