Plugins: Add state logs for plugin client retrieval (#93630)
* add state to grpc plugin * tidy * fix lint * fix issues * return true * use defer * update err message
This commit is contained in:
@@ -3,7 +3,6 @@ package grpcplugin
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
@@ -15,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errClientNotStarted = errors.New("plugin client has not been started")
|
||||
errClientNotAvailable = errors.New("plugin client not available")
|
||||
)
|
||||
|
||||
var _ ProtoClient = (*protoClient)(nil)
|
||||
@@ -31,20 +30,12 @@ type ProtoClient interface {
|
||||
PID(context.Context) (string, error)
|
||||
PluginID() string
|
||||
PluginVersion() string
|
||||
PluginJSON() plugins.JSONData
|
||||
Backend() backendplugin.Plugin
|
||||
Logger() log.Logger
|
||||
Start(context.Context) error
|
||||
Stop(context.Context) error
|
||||
Running(context.Context) bool
|
||||
}
|
||||
|
||||
type protoClient struct {
|
||||
plugin *grpcPlugin
|
||||
pluginVersion string
|
||||
pluginJSON plugins.JSONData
|
||||
|
||||
mu sync.RWMutex
|
||||
plugin *grpcPlugin
|
||||
pluginJSON plugins.JSONData
|
||||
}
|
||||
|
||||
type ProtoClientOpts struct {
|
||||
@@ -68,12 +59,12 @@ func NewProtoClient(opts ProtoClientOpts) (ProtoClient, error) {
|
||||
func() []string { return opts.Env },
|
||||
)
|
||||
|
||||
return &protoClient{plugin: p, pluginVersion: opts.PluginJSON.Info.Version, pluginJSON: opts.PluginJSON}, nil
|
||||
return &protoClient{plugin: p, pluginJSON: opts.PluginJSON}, nil
|
||||
}
|
||||
|
||||
func (r *protoClient) PID(ctx context.Context) (string, error) {
|
||||
if _, exists := r.client(ctx); !exists {
|
||||
return "", errClientNotStarted
|
||||
return "", errClientNotAvailable
|
||||
}
|
||||
return r.plugin.client.ID(), nil
|
||||
}
|
||||
@@ -83,11 +74,7 @@ func (r *protoClient) PluginID() string {
|
||||
}
|
||||
|
||||
func (r *protoClient) PluginVersion() string {
|
||||
return r.pluginVersion
|
||||
}
|
||||
|
||||
func (r *protoClient) PluginJSON() plugins.JSONData {
|
||||
return r.pluginJSON
|
||||
return r.pluginJSON.Info.Version
|
||||
}
|
||||
|
||||
func (r *protoClient) Backend() backendplugin.Plugin {
|
||||
@@ -98,43 +85,14 @@ func (r *protoClient) Logger() log.Logger {
|
||||
return r.plugin.logger
|
||||
}
|
||||
|
||||
func (r *protoClient) Start(ctx context.Context) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.plugin.Start(ctx)
|
||||
}
|
||||
|
||||
func (r *protoClient) Stop(ctx context.Context) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.plugin.Stop(ctx)
|
||||
}
|
||||
|
||||
func (r *protoClient) Running(_ context.Context) bool {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
return !r.plugin.Exited()
|
||||
}
|
||||
|
||||
func (r *protoClient) client(ctx context.Context) (*ClientV2, bool) {
|
||||
if !r.Running(ctx) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
r.mu.RLock()
|
||||
if r.plugin.pluginClient == nil {
|
||||
r.mu.RUnlock()
|
||||
return nil, false
|
||||
}
|
||||
pc := r.plugin.pluginClient
|
||||
r.mu.RUnlock()
|
||||
return pc, true
|
||||
return r.plugin.getPluginClient(ctx)
|
||||
}
|
||||
|
||||
func (r *protoClient) QueryData(ctx context.Context, in *pluginv2.QueryDataRequest, opts ...grpc.CallOption) (*pluginv2.QueryDataResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.DataClient.QueryData(ctx, in, opts...)
|
||||
}
|
||||
@@ -142,7 +100,7 @@ func (r *protoClient) QueryData(ctx context.Context, in *pluginv2.QueryDataReque
|
||||
func (r *protoClient) CallResource(ctx context.Context, in *pluginv2.CallResourceRequest, opts ...grpc.CallOption) (pluginv2.Resource_CallResourceClient, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.ResourceClient.CallResource(ctx, in, opts...)
|
||||
}
|
||||
@@ -150,7 +108,7 @@ func (r *protoClient) CallResource(ctx context.Context, in *pluginv2.CallResourc
|
||||
func (r *protoClient) CheckHealth(ctx context.Context, in *pluginv2.CheckHealthRequest, opts ...grpc.CallOption) (*pluginv2.CheckHealthResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.DiagnosticsClient.CheckHealth(ctx, in, opts...)
|
||||
}
|
||||
@@ -158,7 +116,7 @@ func (r *protoClient) CheckHealth(ctx context.Context, in *pluginv2.CheckHealthR
|
||||
func (r *protoClient) CollectMetrics(ctx context.Context, in *pluginv2.CollectMetricsRequest, opts ...grpc.CallOption) (*pluginv2.CollectMetricsResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.DiagnosticsClient.CollectMetrics(ctx, in, opts...)
|
||||
}
|
||||
@@ -166,7 +124,7 @@ func (r *protoClient) CollectMetrics(ctx context.Context, in *pluginv2.CollectMe
|
||||
func (r *protoClient) SubscribeStream(ctx context.Context, in *pluginv2.SubscribeStreamRequest, opts ...grpc.CallOption) (*pluginv2.SubscribeStreamResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.StreamClient.SubscribeStream(ctx, in, opts...)
|
||||
}
|
||||
@@ -174,7 +132,7 @@ func (r *protoClient) SubscribeStream(ctx context.Context, in *pluginv2.Subscrib
|
||||
func (r *protoClient) RunStream(ctx context.Context, in *pluginv2.RunStreamRequest, opts ...grpc.CallOption) (pluginv2.Stream_RunStreamClient, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.StreamClient.RunStream(ctx, in, opts...)
|
||||
}
|
||||
@@ -182,7 +140,7 @@ func (r *protoClient) RunStream(ctx context.Context, in *pluginv2.RunStreamReque
|
||||
func (r *protoClient) PublishStream(ctx context.Context, in *pluginv2.PublishStreamRequest, opts ...grpc.CallOption) (*pluginv2.PublishStreamResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.StreamClient.PublishStream(ctx, in, opts...)
|
||||
}
|
||||
@@ -190,7 +148,7 @@ func (r *protoClient) PublishStream(ctx context.Context, in *pluginv2.PublishStr
|
||||
func (r *protoClient) ValidateAdmission(ctx context.Context, in *pluginv2.AdmissionRequest, opts ...grpc.CallOption) (*pluginv2.ValidationResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.AdmissionClient.ValidateAdmission(ctx, in, opts...)
|
||||
}
|
||||
@@ -198,7 +156,7 @@ func (r *protoClient) ValidateAdmission(ctx context.Context, in *pluginv2.Admiss
|
||||
func (r *protoClient) MutateAdmission(ctx context.Context, in *pluginv2.AdmissionRequest, opts ...grpc.CallOption) (*pluginv2.MutationResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.AdmissionClient.MutateAdmission(ctx, in, opts...)
|
||||
}
|
||||
@@ -206,7 +164,7 @@ func (r *protoClient) MutateAdmission(ctx context.Context, in *pluginv2.Admissio
|
||||
func (r *protoClient) ConvertObjects(ctx context.Context, in *pluginv2.ConversionRequest, opts ...grpc.CallOption) (*pluginv2.ConversionResponse, error) {
|
||||
c, exists := r.client(ctx)
|
||||
if !exists {
|
||||
return nil, errClientNotStarted
|
||||
return nil, errClientNotAvailable
|
||||
}
|
||||
return c.ConversionClient.ConvertObjects(ctx, in, opts...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user