csi: Setup gRPC Clients with a logger

This commit is contained in:
Danielle Lancashire
2020-01-12 15:13:55 +01:00
committed by Tim Gross
parent 48a8f83017
commit cd0c2a6df0
4 changed files with 11 additions and 7 deletions

View File

@@ -294,7 +294,7 @@ func (h *csiPluginSupervisorHook) supervisorLoopOnce(ctx context.Context, socket
return false, fmt.Errorf("failed to stat socket: %v", err)
}
client, err := csi.NewClient(socketPath)
client, err := csi.NewClient(socketPath, h.logger.Named("csi_client").With("plugin.name", h.task.CSIPluginConfig.ID, "plugin.type", h.task.CSIPluginConfig.Type))
defer client.Close()
if err != nil {
return false, fmt.Errorf("failed to create csi client: %v", err)

View File

@@ -343,10 +343,10 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic
serversContactedOnce: sync.Once{},
dynamicRegistry: dynamicplugins.NewRegistry(map[string]dynamicplugins.PluginDispenser{
dynamicplugins.PluginTypeCSIController: func(info *dynamicplugins.PluginInfo) (interface{}, error) {
return csi.NewClient(info.ConnectionInfo.SocketPath)
return csi.NewClient(info.ConnectionInfo.SocketPath, logger.Named("csi_client").With("plugin.name", info.Name, "plugin.type", "controller"))
},
dynamicplugins.PluginTypeCSINode: func(info *dynamicplugins.PluginInfo) (interface{}, error) {
return csi.NewClient(info.ConnectionInfo.SocketPath)
return csi.NewClient(info.ConnectionInfo.SocketPath, logger.Named("csi_client").With("plugin.name", info.Name, "plugin.type", "client"))
},
}),
}

View File

@@ -50,7 +50,7 @@ func newInstanceManager(logger hclog.Logger, updater UpdateNodeCSIInfoFunc, p *d
}
func (i *instanceManager) run() {
c, err := csi.NewClient(i.info.ConnectionInfo.SocketPath)
c, err := csi.NewClient(i.info.ConnectionInfo.SocketPath, i.logger.Named("csi_client").With("plugin.name", i.info.Name, "plugin.type", i.info.Type))
if err != nil {
i.logger.Error("failed to setup instance manager client", "error", err)
close(i.shutdownCh)

View File

@@ -7,7 +7,9 @@ import (
"time"
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/grpc-middleware/logging"
"github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"google.golang.org/grpc"
@@ -81,12 +83,12 @@ func (c *client) Close() error {
return nil
}
func NewClient(addr string) (CSIPlugin, error) {
func NewClient(addr string, logger hclog.Logger) (CSIPlugin, error) {
if addr == "" {
return nil, fmt.Errorf("address is empty")
}
conn, err := newGrpcConn(addr)
conn, err := newGrpcConn(addr, logger)
if err != nil {
return nil, err
}
@@ -99,10 +101,12 @@ func NewClient(addr string) (CSIPlugin, error) {
}, nil
}
func newGrpcConn(addr string) (*grpc.ClientConn, error) {
func newGrpcConn(addr string, logger hclog.Logger) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(
addr,
grpc.WithInsecure(),
grpc.WithUnaryInterceptor(logging.UnaryClientInterceptor(logger)),
grpc.WithStreamInterceptor(logging.StreamClientInterceptor(logger)),
grpc.WithDialer(func(target string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", target, timeout)
}),