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

Commit

Permalink
Enable go plugins to send events
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Foo <[email protected]>
Co-authored-by: Liam Rathke <[email protected]>
  • Loading branch information
Sam Foo and liamrathke committed Jul 28, 2021
1 parent 778e278 commit f2b531c
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 82 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/2691-GuessWhoSamFoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `SendEvent` for go plugins
21 changes: 21 additions & 0 deletions pkg/plugin/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package api
import (
"context"

"github.com/vmware-tanzu/octant/pkg/event"

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

Expand Down Expand Up @@ -274,3 +276,22 @@ func (c *Client) CreateLink(ctx context.Context, key store.Key) (LinkResponse, e
Link: *linkComponent,
}, nil
}

// SendEvent sends an event
func (c *Client) SendEvent(ctx context.Context, clientID string, eventName event.EventType, payload action.Payload) error {
client := c.DashboardConnection.Client()

data, err := convertFromPayload(payload)
if err != nil {
return err
}

eventRequest := &proto.EventRequest{
ClientID: clientID,
EventName: string(eventName),
Payload: data,
}

_, err = client.SendEvent(ctx, eventRequest)
return err
}
18 changes: 18 additions & 0 deletions pkg/plugin/api/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ SPDX-License-Identifier: Apache-2.0
package api

import (
"fmt"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/pkg/errors"
Expand Down Expand Up @@ -107,6 +109,22 @@ func convertToLinkComponent(in, name string) (*component.Link, error) {
return link, nil
}

func convertFromPayload(payload action.Payload) ([]byte, error) {
return json.Marshal(payload)
}

func convertToEvent(in *proto.EventRequest) (string, string, action.Payload, error) {
if in == nil {
return "", "", action.Payload{}, fmt.Errorf("event request is nil")
}
payload := action.Payload{}
err := json.Unmarshal(in.Payload, &payload)
if err != nil {
return "", "", action.Payload{}, fmt.Errorf("unmarshal payload: %w", err)
}
return in.ClientID, in.EventName, payload, 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.

Loading

0 comments on commit f2b531c

Please sign in to comment.