Skip to content

Commit 439424a

Browse files
committed
Use net/url library for UDS parsing, now uses more standardised form
1 parent 80f6b7a commit 439424a

File tree

8 files changed

+16
-83
lines changed

8 files changed

+16
-83
lines changed

cmd/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func init() {
3030
}
3131

3232
var (
33-
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
33+
endpoint = flag.String("endpoint", "unix:/tmp/csi.sock", "CSI endpoint")
3434
driverName = flag.String("drivername", "com.google.csi.gcepd", "name of the driver")
3535
nodeID = flag.String("nodeid", "", "node id")
3636
)

deploy/kubernetes/controller.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ spec:
4949
- "--nodeid=$(KUBE_NODE_NAME)"
5050
env:
5151
- name: CSI_ENDPOINT
52-
value: unix:///csi/csi.sock
52+
value: unix:/csi/csi.sock
5353
- name: KUBE_NODE_NAME
5454
valueFrom:
5555
fieldRef:

deploy/kubernetes/demo-pod.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spec:
88
storageClassName: csi-gce-pd
99
resources:
1010
requests:
11-
storage: 1Mi
11+
storage: 6Gi
1212

1313
---
1414

deploy/kubernetes/node.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ spec:
4343
- "--nodeid=$(KUBE_NODE_NAME)"
4444
env:
4545
- name: CSI_ENDPOINT
46-
value: unix:///csi/csi.sock
46+
value: unix:/csi/csi.sock
4747
- name: KUBE_NODE_NAME
4848
valueFrom:
4949
fieldRef:

pkg/gce-pd-csi-driver/server.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package gceGCEDriver
1616

1717
import (
1818
"net"
19+
"net/url"
1920
"os"
2021
"sync"
2122

@@ -70,19 +71,24 @@ func (s *nonBlockingGRPCServer) ForceStop() {
7071

7172
func (s *nonBlockingGRPCServer) serve(endpoint string, ids csi.IdentityServer, cs csi.ControllerServer, ns csi.NodeServer) {
7273

73-
proto, addr, err := ParseEndpoint(endpoint)
74+
u, err := url.Parse(endpoint)
75+
7476
if err != nil {
7577
glog.Fatal(err.Error())
7678
}
7779

78-
if proto == "unix" {
79-
addr = "/" + addr
80+
var addr string
81+
if u.Scheme == "unix" {
82+
addr = u.Path
8083
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
8184
glog.Fatalf("Failed to remove %s, error: %s", addr, err.Error())
8285
}
86+
} else {
87+
glog.Fatalf("%v endpoint scheme not supported", u.Scheme)
8388
}
8489

85-
listener, err := net.Listen(proto, addr)
90+
glog.Infof("Start listening with scheme %v, addr %v", u.Scheme, addr)
91+
listener, err := net.Listen(u.Scheme, addr)
8692
if err != nil {
8793
glog.Fatalf("Failed to listen: %v", err)
8894
}

pkg/gce-pd-csi-driver/utils.go

-13
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,12 @@ limitations under the License.
1717
package gceGCEDriver
1818

1919
import (
20-
"fmt"
21-
"strings"
22-
2320
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
2421
"github.com/golang/glog"
2522
"golang.org/x/net/context"
2623
"google.golang.org/grpc"
2724
)
2825

29-
func ParseEndpoint(ep string) (string, string, error) {
30-
if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
31-
s := strings.SplitN(ep, "://", 2)
32-
if s[1] != "" {
33-
return s[0], s[1], nil
34-
}
35-
}
36-
return "", "", fmt.Errorf("Invalid endpoint: %v", ep)
37-
}
38-
3926
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
4027
return &csi.VolumeCapability_AccessMode{Mode: mode}
4128
}

pkg/gce-pd-csi-driver/utils_test.go

-59
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,3 @@ limitations under the License.
1616
*/
1717

1818
package gceGCEDriver
19-
20-
import (
21-
"testing"
22-
23-
"github.com/stretchr/testify/assert"
24-
)
25-
26-
func TestParseEndpoint(t *testing.T) {
27-
28-
//Valid unix domain socket endpoint
29-
sockType, addr, err := ParseEndpoint("unix://fake.sock")
30-
assert.NoError(t, err)
31-
assert.Equal(t, sockType, "unix")
32-
assert.Equal(t, addr, "fake.sock")
33-
34-
sockType, addr, err = ParseEndpoint("unix:///fakedir/fakedir/fake.sock")
35-
assert.NoError(t, err)
36-
assert.Equal(t, sockType, "unix")
37-
assert.Equal(t, addr, "/fakedir/fakedir/fake.sock")
38-
39-
//Valid unix domain socket with uppercase
40-
sockType, addr, err = ParseEndpoint("UNIX://fake.sock")
41-
assert.NoError(t, err)
42-
assert.Equal(t, sockType, "UNIX")
43-
assert.Equal(t, addr, "fake.sock")
44-
45-
//Valid TCP endpoint with ip
46-
sockType, addr, err = ParseEndpoint("tcp://127.0.0.1:80")
47-
assert.NoError(t, err)
48-
assert.Equal(t, sockType, "tcp")
49-
assert.Equal(t, addr, "127.0.0.1:80")
50-
51-
//Valid TCP endpoint with uppercase
52-
sockType, addr, err = ParseEndpoint("TCP://127.0.0.1:80")
53-
assert.NoError(t, err)
54-
assert.Equal(t, sockType, "TCP")
55-
assert.Equal(t, addr, "127.0.0.1:80")
56-
57-
//Valid TCP endpoint with hostname
58-
sockType, addr, err = ParseEndpoint("tcp://fakehost:80")
59-
assert.NoError(t, err)
60-
assert.Equal(t, sockType, "tcp")
61-
assert.Equal(t, addr, "fakehost:80")
62-
63-
_, _, err = ParseEndpoint("unix:/fake.sock/")
64-
assert.NotNil(t, err)
65-
66-
_, _, err = ParseEndpoint("fake.sock")
67-
assert.NotNil(t, err)
68-
69-
_, _, err = ParseEndpoint("unix://")
70-
assert.NotNil(t, err)
71-
72-
_, _, err = ParseEndpoint("://")
73-
assert.NotNil(t, err)
74-
75-
_, _, err = ParseEndpoint("")
76-
assert.NotNil(t, err)
77-
}

test/sanity/sanity_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func TestSanity(t *testing.T) {
3232
project := "test-project"
3333
zone := "test-zone"
3434
// TODO(dyzz): Only one of these can be correct, the way endpoint is defined in GCE driver is INCORRECT
35-
endpoint := "unix://tmp/csi.sock"
36-
csiSanityEndpoint := "unix:/tmp/csi.sock"
35+
endpoint := "unix:/tmp/csi.sock"
3736
mountPath := "/tmp/csi/mount"
3837
stagePath := "/tmp/csi/stage"
3938
// Set up driver and env
@@ -69,7 +68,7 @@ func TestSanity(t *testing.T) {
6968
config := &sanity.Config{
7069
TargetPath: mountPath,
7170
StagingPath: stagePath,
72-
Address: csiSanityEndpoint,
71+
Address: endpoint,
7372
}
7473
sanity.Test(t, config)
7574

0 commit comments

Comments
 (0)