From 0413e5ad0fff9201335de73ca03984eae0303a70 Mon Sep 17 00:00:00 2001 From: dobarx Date: Sun, 3 Mar 2024 20:37:38 +0200 Subject: [PATCH] plugin/pluginapi/v1: better logging & some cleanup --- plugin/pluginapi/v1/client.go | 38 +++++------------------- plugin/pluginapi/v1/cty_type_decoder.go | 6 +--- plugin/pluginapi/v1/cty_type_encoder.go | 14 ++++----- plugin/pluginapi/v1/cty_value_decoder.go | 24 +++++++-------- plugin/pluginapi/v1/hclspec_decoder.go | 3 -- plugin/pluginapi/v1/plugin.go | 12 +++++++- 6 files changed, 37 insertions(+), 60 deletions(-) diff --git a/plugin/pluginapi/v1/client.go b/plugin/pluginapi/v1/client.go index 2b4c19a5..83f7a253 100644 --- a/plugin/pluginapi/v1/client.go +++ b/plugin/pluginapi/v1/client.go @@ -4,8 +4,6 @@ import ( "fmt" "log/slog" "os/exec" - "path/filepath" - "strings" goplugin "github.com/hashicorp/go-plugin" "google.golang.org/grpc" @@ -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 '@[.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), @@ -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) diff --git a/plugin/pluginapi/v1/cty_type_decoder.go b/plugin/pluginapi/v1/cty_type_decoder.go index 7f2f2314..d20523e1 100644 --- a/plugin/pluginapi/v1/cty_type_decoder.go +++ b/plugin/pluginapi/v1/cty_type_decoder.go @@ -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: diff --git a/plugin/pluginapi/v1/cty_type_encoder.go b/plugin/pluginapi/v1/cty_type_encoder.go index 0499e1a3..661c465a 100644 --- a/plugin/pluginapi/v1/cty_type_encoder.go +++ b/plugin/pluginapi/v1/cty_type_encoder.go @@ -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 { diff --git a/plugin/pluginapi/v1/cty_value_decoder.go b/plugin/pluginapi/v1/cty_value_decoder.go index 7486bad1..52693074 100644 --- a/plugin/pluginapi/v1/cty_value_decoder.go +++ b/plugin/pluginapi/v1/cty_value_decoder.go @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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 diff --git a/plugin/pluginapi/v1/hclspec_decoder.go b/plugin/pluginapi/v1/hclspec_decoder.go index 14826ad9..f15ca3c0 100644 --- a/plugin/pluginapi/v1/hclspec_decoder.go +++ b/plugin/pluginapi/v1/hclspec_decoder.go @@ -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 diff --git a/plugin/pluginapi/v1/plugin.go b/plugin/pluginapi/v1/plugin.go index cfe49690..cf4a6c4a 100644 --- a/plugin/pluginapi/v1/plugin.go +++ b/plugin/pluginapi/v1/plugin.go @@ -3,6 +3,8 @@ package pluginapiv1 import ( context "context" "fmt" + "log/slog" + "time" goplugin "github.com/hashicorp/go-plugin" "github.com/hashicorp/hcl/v2" @@ -21,6 +23,7 @@ var handshake = goplugin.HandshakeConfig{ type grpcPlugin struct { goplugin.Plugin + logger *slog.Logger schema *plugin.Schema } @@ -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 @@ -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, @@ -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,