Skip to content

Commit 0da03b3

Browse files
committed
external-snapshotter should use CSI connection lib
1 parent 7536845 commit 0da03b3

File tree

3 files changed

+10
-42
lines changed

3 files changed

+10
-42
lines changed

cmd/csi-snapshotter/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const (
5252
var (
5353
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.")
5454
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
55-
connectionTimeout = flag.Duration("connection-timeout", 1*time.Minute, "Timeout for waiting for CSI driver socket.")
55+
connectionTimeout = flag.Duration("connection-timeout", 0, "The --connection-timeout flag is deprecated")
5656
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
5757
createSnapshotContentRetryCount = flag.Int("create-snapshotcontent-retrycount", 5, "Number of retries when we create a snapshot content object for a snapshot.")
5858
createSnapshotContentInterval = flag.Duration("create-snapshotcontent-interval", 10*time.Second, "Interval between retries when we create a snapshot content object for a snapshot.")
@@ -76,6 +76,10 @@ func main() {
7676
}
7777
glog.Infof("Version: %s", version)
7878

79+
if *connectionTimeout != 0 {
80+
glog.Warning("--connection-timeout is deprecated and will have no effect")
81+
}
82+
7983
// Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
8084
config, err := buildConfig(*kubeconfig)
8185
if err != nil {
@@ -116,7 +120,7 @@ func main() {
116120
snapshotscheme.AddToScheme(scheme.Scheme)
117121

118122
// Connect to CSI.
119-
csiConn, err := connection.New(*csiAddress, *connectionTimeout)
123+
csiConn, err := connection.New(*csiAddress)
120124
if err != nil {
121125
glog.Error(err.Error())
122126
os.Exit(1)

pkg/connection/connection.go

+3-39
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ package connection
1919
import (
2020
"context"
2121
"fmt"
22-
"net"
23-
"strings"
24-
"time"
2522

2623
"github.com/container-storage-interface/spec/lib/go/csi"
2724
"github.com/golang/glog"
2825
"github.com/golang/protobuf/ptypes"
2926
"github.com/golang/protobuf/ptypes/timestamp"
27+
"github.com/kubernetes-csi/csi-lib-utils/connection"
3028
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
3129
"google.golang.org/grpc"
32-
"google.golang.org/grpc/connectivity"
3330
"k8s.io/api/core/v1"
3431
)
3532

@@ -73,8 +70,8 @@ var (
7370
)
7471

7572
// New returns a CSI connection object.
76-
func New(address string, timeout time.Duration) (CSIConnection, error) {
77-
conn, err := connect(address, timeout)
73+
func New(address string) (CSIConnection, error) {
74+
conn, err := connection.Connect(address)
7875
if err != nil {
7976
return nil, err
8077
}
@@ -83,39 +80,6 @@ func New(address string, timeout time.Duration) (CSIConnection, error) {
8380
}, nil
8481
}
8582

86-
func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
87-
glog.V(2).Infof("Connecting to %s", address)
88-
dialOptions := []grpc.DialOption{
89-
grpc.WithInsecure(),
90-
grpc.WithBackoffMaxDelay(time.Second),
91-
grpc.WithUnaryInterceptor(logGRPC),
92-
}
93-
if strings.HasPrefix(address, "/") {
94-
dialOptions = append(dialOptions, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
95-
return net.DialTimeout("unix", addr, timeout)
96-
}))
97-
}
98-
conn, err := grpc.Dial(address, dialOptions...)
99-
100-
if err != nil {
101-
return nil, err
102-
}
103-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
104-
defer cancel()
105-
for {
106-
if !conn.WaitForStateChange(ctx, conn.GetState()) {
107-
glog.V(4).Infof("Connection timed out")
108-
// subsequent GetPluginInfo will show the real connection error
109-
return conn, nil
110-
}
111-
if conn.GetState() == connectivity.Ready {
112-
glog.V(3).Infof("Connected")
113-
return conn, nil
114-
}
115-
glog.V(4).Infof("Still trying, connection is %s", conn.GetState())
116-
}
117-
}
118-
11983
func (c *csiConnection) GetDriverName(ctx context.Context) (string, error) {
12084
client := csi.NewIdentityClient(c.conn)
12185

pkg/connection/connection_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func createMockServer(t *testing.T) (*gomock.Controller, *driver.MockCSIDriver,
5050

5151
// Create a client connection to it
5252
addr := drv.Address()
53-
csiConn, err := New(addr, 10)
53+
csiConn, err := New(addr)
5454
if err != nil {
5555
return nil, nil, nil, nil, nil, err
5656
}

0 commit comments

Comments
 (0)