Skip to content

Commit

Permalink
Merge pull request #92 from andrewsykim/conn-lib
Browse files Browse the repository at this point in the history
external-snapshotter should use CSI connection lib
  • Loading branch information
k8s-ci-robot authored Feb 14, 2019
2 parents 9a34e4a + 0da03b3 commit f59b58d
Show file tree
Hide file tree
Showing 27 changed files with 11,934 additions and 160 deletions.
271 changes: 247 additions & 24 deletions Gopkg.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ required = [

[[constraint]]
name = "github.com/kubernetes-csi/csi-lib-utils"
version = "0.1.0"
version = "0.3.0"
8 changes: 6 additions & 2 deletions cmd/csi-snapshotter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
var (
snapshotter = flag.String("snapshotter", "", "Name of the snapshotter. The snapshotter will only create snapshot content for snapshot that requests a VolumeSnapshotClass with a snapshotter field set equal to this name.")
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
connectionTimeout = flag.Duration("connection-timeout", 1*time.Minute, "Timeout for waiting for CSI driver socket.")
connectionTimeout = flag.Duration("connection-timeout", 0, "The --connection-timeout flag is deprecated")
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
createSnapshotContentRetryCount = flag.Int("create-snapshotcontent-retrycount", 5, "Number of retries when we create a snapshot content object for a snapshot.")
createSnapshotContentInterval = flag.Duration("create-snapshotcontent-interval", 10*time.Second, "Interval between retries when we create a snapshot content object for a snapshot.")
Expand All @@ -76,6 +76,10 @@ func main() {
}
glog.Infof("Version: %s", version)

if *connectionTimeout != 0 {
glog.Warning("--connection-timeout is deprecated and will have no effect")
}

// Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
config, err := buildConfig(*kubeconfig)
if err != nil {
Expand Down Expand Up @@ -116,7 +120,7 @@ func main() {
snapshotscheme.AddToScheme(scheme.Scheme)

// Connect to CSI.
csiConn, err := connection.New(*csiAddress, *connectionTimeout)
csiConn, err := connection.New(*csiAddress)
if err != nil {
glog.Error(err.Error())
os.Exit(1)
Expand Down
42 changes: 3 additions & 39 deletions pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ package connection
import (
"context"
"fmt"
"net"
"strings"
"time"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/kubernetes-csi/csi-lib-utils/connection"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -73,8 +70,8 @@ var (
)

// New returns a CSI connection object.
func New(address string, timeout time.Duration) (CSIConnection, error) {
conn, err := connect(address, timeout)
func New(address string) (CSIConnection, error) {
conn, err := connection.Connect(address)
if err != nil {
return nil, err
}
Expand All @@ -83,39 +80,6 @@ func New(address string, timeout time.Duration) (CSIConnection, error) {
}, nil
}

func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
glog.V(2).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.V(4).Infof("Connection timed out")
// subsequent GetPluginInfo will show the real connection error
return conn, nil
}
if conn.GetState() == connectivity.Ready {
glog.V(3).Infof("Connected")
return conn, nil
}
glog.V(4).Infof("Still trying, connection is %s", conn.GetState())
}
}

func (c *csiConnection) GetDriverName(ctx context.Context) (string, error) {
client := csi.NewIdentityClient(c.conn)

Expand Down
2 changes: 1 addition & 1 deletion pkg/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func createMockServer(t *testing.T) (*gomock.Controller, *driver.MockCSIDriver,

// Create a client connection to it
addr := drv.Address()
csiConn, err := New(addr, 10)
csiConn, err := New(addr)
if err != nil {
return nil, nil, nil, nil, nil, err
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f59b58d

Please sign in to comment.