blob: f91c5232994f42fad9d614e8c9fba24bf89e5d1e [file]
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package connection
import (
"context"
"fmt"
"net"
"strings"
"time"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
// CSIConnection is gRPC connection to a remote CSI driver and abstracts all
// CSI calls.
type CSIConnection interface {
// GetDriverName returns driver name as discovered by GetPluginInfo() gRPC
// call.
GetDriverName(ctx context.Context) (string, error)
// NodeGetId returns node ID of the current according to the CSI driver.
NodeGetId(ctx context.Context) (string, error)
// Liveness Probe
LivenessProbe(ctx context.Context) error
// Close the connection
Close() error
}
type csiConnection struct {
conn *grpc.ClientConn
}
var (
_ CSIConnection = &csiConnection{}
)
// NewConnection establishes new connection to CSI driver
func NewConnection(
address string, timeout time.Duration) (CSIConnection, error) {
conn, err := connect(address, timeout)
if err != nil {
return nil, err
}
return &csiConnection{
conn: conn,
}, nil
}
func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
glog.Infof("Connecting to %s", address)
dialOptions := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBackoffMaxDelay(time.Second),
grpc.WithUnaryInterceptor(logGRPC),
}
if strings.HasPrefix(address, "/") {
dialOptions = append(dialOptions, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}))
}
conn, err := grpc.Dial(address, dialOptions...)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
if !conn.WaitForStateChange(ctx, conn.GetState()) {
glog.Infof("Connection timed out")
return conn, nil // return nil, subsequent GetPluginInfo will show the real connection error
}
if conn.GetState() == connectivity.Ready {
glog.Infof("Connected")
return conn, nil
}
glog.Infof("Still trying, connection is %s", conn.GetState())
}
}
func (c *csiConnection) GetDriverName(ctx context.Context) (string, error) {
client := csi.NewIdentityClient(c.conn)
req := csi.GetPluginInfoRequest{}
rsp, err := client.GetPluginInfo(ctx, &req)
if err != nil {
return "", err
}
name := rsp.GetName()
if name == "" {
return "", fmt.Errorf("name is empty")
}
return name, nil
}
func (c *csiConnection) LivenessProbe(ctx context.Context) error {
client := csi.NewIdentityClient(c.conn)
req := csi.ProbeRequest{}
_, err := client.Probe(ctx, &req)
if err != nil {
return err
}
return nil
}
func (c *csiConnection) NodeGetId(ctx context.Context) (string, error) {
client := csi.NewNodeClient(c.conn)
req := csi.NodeGetIdRequest{}
rsp, err := client.NodeGetId(ctx, &req)
if err != nil {
return "", err
}
nodeID := rsp.GetNodeId()
if nodeID == "" {
return "", fmt.Errorf("node ID is empty")
}
return nodeID, nil
}
func (c *csiConnection) Close() error {
return c.conn.Close()
}
func logGRPC(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
glog.Infof("GRPC call: %s", method)
glog.Infof("GRPC request: %+v", req)
err := invoker(ctx, method, req, reply, cc, opts...)
glog.Infof("GRPC response: %+v", reply)
glog.Infof("GRPC error: %v", err)
return err
}