Skip to content

Commit

Permalink
plugin/pluginapi/v1: better logging & some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dobarx committed Mar 3, 2024
1 parent 1d819e3 commit 0413e5a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 60 deletions.
38 changes: 8 additions & 30 deletions plugin/pluginapi/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"log/slog"
"os/exec"
"path/filepath"
"strings"

goplugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
Expand All @@ -14,41 +12,21 @@ import (
"github.com/blackstork-io/fabric/plugin"
)

func parsePluginInfo(path string) (name, version string, err error) {
nameVer := filepath.Base(path)
ext := filepath.Ext(path)

parts := strings.SplitN(
nameVer[:len(nameVer)-len(ext)],
"@", 2,
)
if len(parts) != 2 {
err = fmt.Errorf("plugin at '%s' must have a file name '<plugin_name>@<plugin_version>[.exe]'", path)
return
}
name = parts[0]
version = parts[1]
return
}

func NewClient(loc string) (p *plugin.Schema, closefn func() error, err error) {
pluginName, _, err := parsePluginInfo(loc)
if err != nil {
return
}
slog.Info("Loading plugin", "filename", loc)
func NewClient(name, binaryPath string, logger *slog.Logger) (p *plugin.Schema, closefn func() error, err error) {
client := goplugin.NewClient(&goplugin.ClientConfig{
HandshakeConfig: handshake,
Plugins: map[string]goplugin.Plugin{
pluginName: &grpcPlugin{},
name: &grpcPlugin{
logger: logger,
},
},
Cmd: exec.Command(loc),
Cmd: exec.Command(binaryPath),
AllowedProtocols: []goplugin.Protocol{
goplugin.ProtocolGRPC,
},
Logger: sloghclog.Adapt(
slog.Default(),
sloghclog.Name("plugin."+pluginName),
logger,
sloghclog.Name("plugin."+name),
// disable code location reporting, it's always going to be incorrect
// for remote plugin logs
sloghclog.AddSource(false),
Expand All @@ -65,7 +43,7 @@ func NewClient(loc string) (p *plugin.Schema, closefn func() error, err error) {
if err != nil {
return nil, nil, fmt.Errorf("failed to create plugin client: %w", err)
}
raw, err := rpcClient.Dispense(pluginName)
raw, err := rpcClient.Dispense(name)
if err != nil {
rpcClient.Close()
return nil, nil, fmt.Errorf("failed to dispense plugin: %w", err)
Expand Down
6 changes: 1 addition & 5 deletions plugin/pluginapi/v1/cty_type_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ func decodeCtyType(src *CtyType) (cty.Type, error) {
case *CtyType_Tuple:
return decodeCtyTupleType(src.Tuple)
case *CtyType_DynamicPseudo:
return decodeCtyDynamicPseudoType(src.DynamicPseudo)
return cty.DynamicPseudoType, nil
default:
return cty.NilType, fmt.Errorf("unsupported cty type: %T", src)
}
}

func decodeCtyDynamicPseudoType(src *CtyDynamicPseudoType) (cty.Type, error) {
return cty.DynamicPseudoType, nil
}

func decodeCtyPrimitiveType(src *CtyPrimitiveType) (cty.Type, error) {
switch src.GetKind() {
case CtyPrimitiveKind_CTY_PRIMITIVE_KIND_BOOL:
Expand Down
14 changes: 5 additions & 9 deletions plugin/pluginapi/v1/cty_type_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ func encodeCtyType(src cty.Type) (*CtyType, error) {
case src.IsTupleType():
return encodeCtyTupleType(src)
case src.Equals(cty.DynamicPseudoType):
return encodeCtyDynamicPseudoType(src)
return &CtyType{
Data: &CtyType_DynamicPseudo{
DynamicPseudo: &CtyDynamicPseudoType{},
},
}, nil
default:
return nil, fmt.Errorf("unsupported cty type: %s", src.FriendlyName())
}
}

func encodeCtyDynamicPseudoType(src cty.Type) (*CtyType, error) {
return &CtyType{
Data: &CtyType_DynamicPseudo{
DynamicPseudo: &CtyDynamicPseudoType{},
},
}, nil
}

func encodeCtyPrimitiveType(src cty.Type) (*CtyType, error) {
kind := CtyPrimitiveKind_CTY_PRIMITIVE_KIND_UNSPECIFIED
switch src {
Expand Down
24 changes: 12 additions & 12 deletions plugin/pluginapi/v1/cty_value_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ func decodeCtyValue(src *CtyValue) (cty.Value, error) {
}
switch {
case t.IsPrimitiveType() && src.GetPrimitive() != nil:
return decodeCtyPrimitiveValue(t, src.GetPrimitive())
return decodeCtyPrimitiveValue(src.GetPrimitive())
case t.IsListType() && src.GetList() != nil:
return decodeCtyListValue(t, src.GetList())
return decodeCtyListValue(src.GetList())
case t.IsMapType() && src.GetMap() != nil:
return decodeCtyMapValue(t, src.GetMap())
return decodeCtyMapValue(src.GetMap())
case t.IsSetType() && src.GetSet() != nil:
return decodeCtySetValue(t, src.GetSet())
return decodeCtySetValue(src.GetSet())
case t.IsObjectType() && src.GetObject() != nil:
return decodeCtyObjectValue(t, src.GetObject())
return decodeCtyObjectValue(src.GetObject())
case t.IsTupleType() && src.GetTuple() != nil:
return decodeCtyTupleValue(t, src.GetTuple())
return decodeCtyTupleValue(src.GetTuple())
default:
return cty.NullVal(t), nil
}
}

func decodeCtyTupleValue(t cty.Type, src *CtyTupleValue) (cty.Value, error) {
func decodeCtyTupleValue(src *CtyTupleValue) (cty.Value, error) {
elements := make([]cty.Value, len(src.GetElements()))
var err error
for i, elem := range src.GetElements() {
Expand All @@ -44,7 +44,7 @@ func decodeCtyTupleValue(t cty.Type, src *CtyTupleValue) (cty.Value, error) {
return cty.TupleVal(elements), nil
}

func decodeCtyObjectValue(t cty.Type, src *CtyObjectValue) (cty.Value, error) {
func decodeCtyObjectValue(src *CtyObjectValue) (cty.Value, error) {
attrs := make(map[string]cty.Value, len(src.GetAttrs()))
var err error
for k, v := range src.GetAttrs() {
Expand All @@ -56,7 +56,7 @@ func decodeCtyObjectValue(t cty.Type, src *CtyObjectValue) (cty.Value, error) {
return cty.ObjectVal(attrs), nil
}

func decodeCtySetValue(t cty.Type, src *CtySetValue) (cty.Value, error) {
func decodeCtySetValue(src *CtySetValue) (cty.Value, error) {
elements := make([]cty.Value, len(src.GetElements()))
var err error
for i, elem := range src.GetElements() {
Expand All @@ -68,7 +68,7 @@ func decodeCtySetValue(t cty.Type, src *CtySetValue) (cty.Value, error) {
return cty.SetVal(elements), nil
}

func decodeCtyMapValue(t cty.Type, src *CtyMapValue) (cty.Value, error) {
func decodeCtyMapValue(src *CtyMapValue) (cty.Value, error) {
elements := make(map[string]cty.Value, len(src.GetElements()))
var err error
for k, v := range src.GetElements() {
Expand All @@ -80,7 +80,7 @@ func decodeCtyMapValue(t cty.Type, src *CtyMapValue) (cty.Value, error) {
return cty.MapVal(elements), nil
}

func decodeCtyListValue(t cty.Type, src *CtyListValue) (cty.Value, error) {
func decodeCtyListValue(src *CtyListValue) (cty.Value, error) {
elements := make([]cty.Value, len(src.GetElements()))
var err error
for i, elem := range src.GetElements() {
Expand All @@ -92,7 +92,7 @@ func decodeCtyListValue(t cty.Type, src *CtyListValue) (cty.Value, error) {
return cty.ListVal(elements), nil
}

func decodeCtyPrimitiveValue(t cty.Type, src *CtyPrimitiveValue) (cty.Value, error) {
func decodeCtyPrimitiveValue(src *CtyPrimitiveValue) (cty.Value, error) {
switch data := src.GetData().(type) {
case *CtyPrimitiveValue_Bln:
return cty.BoolVal(data.Bln), nil
Expand Down
3 changes: 0 additions & 3 deletions plugin/pluginapi/v1/hclspec_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
)

func decodeHclSpec(src *HclSpec) (hcldec.Spec, error) {
if src == nil {
return nil, nil
}
switch {
case src == nil || src.GetData() == nil:
return nil, nil
Expand Down
12 changes: 11 additions & 1 deletion plugin/pluginapi/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package pluginapiv1
import (
context "context"
"fmt"
"log/slog"
"time"

goplugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/hcl/v2"
Expand All @@ -21,6 +23,7 @@ var handshake = goplugin.HandshakeConfig{

type grpcPlugin struct {
goplugin.Plugin
logger *slog.Logger
schema *plugin.Schema
}

Expand All @@ -33,7 +36,6 @@ func (p *grpcPlugin) GRPCServer(broker *goplugin.GRPCBroker, s *grpc.Server) err

func (p *grpcPlugin) GRPCClient(ctx context.Context, broker *goplugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
client := NewPluginServiceClient(c)

res, err := client.GetSchema(ctx, &GetSchemaRequest{})
if err != nil {
return nil, err
Expand Down Expand Up @@ -66,6 +68,10 @@ func (p *grpcPlugin) callOptions() []grpc.CallOption {

func (p *grpcPlugin) clientGenerateFunc(name string, client PluginServiceClient) plugin.ProvideContentFunc {
return func(ctx context.Context, params *plugin.ProvideContentParams) (*plugin.Content, hcl.Diagnostics) {
p.logger.Debug("Calling content provider", "name", name)
defer func(start time.Time) {
p.logger.Debug("Called content provider", "name", name, "took", time.Since(start))
}(time.Now())
if params == nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Expand Down Expand Up @@ -110,6 +116,10 @@ func (p *grpcPlugin) clientGenerateFunc(name string, client PluginServiceClient)

func (p *grpcPlugin) clientDataFunc(name string, client PluginServiceClient) plugin.RetrieveDataFunc {
return func(ctx context.Context, params *plugin.RetrieveDataParams) (plugin.Data, hcl.Diagnostics) {
p.logger.Debug("Calling data source", "name", name)
defer func(start time.Time) {
p.logger.Debug("Called data source", "name", name, "took", time.Since(start))
}(time.Now())
if params == nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Expand Down

0 comments on commit 0413e5a

Please sign in to comment.