Skip to content
This repository was archived by the owner on Jun 23, 2020. It is now read-only.

Commit fbf6928

Browse files
author
Harvey Lowndes
committed
Review changes
1 parent 26e9c2c commit fbf6928

File tree

3 files changed

+91
-38
lines changed

3 files changed

+91
-38
lines changed

cmd/oci/main.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"fmt"
1919
"log"
2020
"os"
21+
"path"
22+
"strings"
23+
"sync"
2124

2225
"github.com/oracle/oci-flexvolume-driver/pkg/flexvolume"
2326
"github.com/oracle/oci-flexvolume-driver/pkg/oci/driver"
@@ -27,6 +30,12 @@ import (
2730
var version string
2831
var build string
2932

33+
// All registered drivers.
34+
var (
35+
driversMutex sync.Mutex
36+
drivers = make(map[string]flexvolume.Driver)
37+
)
38+
3039
// GetLogPath returns the default path to the driver log file.
3140
func GetLogPath() string {
3241
path := os.Getenv("OCI_FLEXD_DRIVER_LOG_DIR")
@@ -51,9 +60,57 @@ func main() {
5160

5261
log.Printf("OCI FlexVolume Driver version: %s (%s)", version, build)
5362

54-
drivers := map[string]flexvolume.Driver{
55-
"oci-bvs": &driver.OCIFlexvolumeDriver{},
63+
RegisterDriver("oci-bvs", &driver.OCIFlexvolumeDriver{})
64+
65+
driver, err := getDriver(drivers)
66+
if err != nil {
67+
log.Fatalf(err.Error())
68+
}
69+
70+
flexvolume.ExitWithResult(flexvolume.ExecDriver(driver, os.Args))
71+
}
72+
73+
func getDriver(drivers map[string]flexvolume.Driver) (flexvolume.Driver, error) {
74+
driver, err := GetRegisteredDriver(flexvolume.DefaultSymlinkDirectory) //Block volume is default
75+
if err != nil {
76+
return nil, err
5677
}
5778

58-
flexvolume.ExecDriver(drivers, os.Args)
79+
dir := path.Base(os.Args[0])
80+
dir = string(strings.TrimPrefix(dir, "oracle~"))
81+
82+
if dir != "oci" && dir != flexvolume.DefaultSymlinkDirectory {
83+
driver, err = GetRegisteredDriver(dir)
84+
if err != nil {
85+
return nil, err
86+
}
87+
}
88+
89+
log.Printf("Using %s driver", dir)
90+
91+
return driver, nil
92+
}
93+
94+
// GetRegisteredDriver returns an instance of the named driver, or nil if
95+
// the name is unknown.
96+
func GetRegisteredDriver(name string) (flexvolume.Driver, error) {
97+
driversMutex.Lock()
98+
defer driversMutex.Unlock()
99+
f, found := drivers[name]
100+
if !found {
101+
return nil, fmt.Errorf("No driver found for %s", name)
102+
}
103+
return f, nil
104+
}
105+
106+
// RegisterDriver registers a flexvolume.Driver by name. This
107+
// is expected to happen during app startup.
108+
func RegisterDriver(name string, driver flexvolume.Driver) {
109+
driversMutex.Lock()
110+
defer driversMutex.Unlock()
111+
if _, found := drivers[name]; found {
112+
log.Fatalf("Driver %q was registered twice", name)
113+
}
114+
log.Printf("Registered driver %q", name)
115+
drivers[name] = driver
59116
}

pkg/flexvolume/flexvolume.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"io"
2121
"log"
2222
"os"
23-
"path"
24-
"strings"
2523
)
2624

2725
// Defined to enable overriding in tests.
@@ -86,6 +84,26 @@ type Driver interface {
8684
Unmount(mountDir string) DriverStatus
8785
}
8886

87+
// ExitWithResult outputs the given Result and exits with the appropriate exit
88+
// code.
89+
func ExitWithResult(result DriverStatus) {
90+
code := 1
91+
if result.Status == StatusSuccess || result.Status == StatusNotSupported {
92+
code = 0
93+
}
94+
95+
res, err := json.Marshal(result)
96+
if err != nil {
97+
log.Printf("Error marshaling result: %v", err)
98+
fmt.Fprintln(out, `{"status":"Failure","message":"Error marshaling result to JSON"}`)
99+
} else {
100+
s := string(res)
101+
log.Printf("Command result: %s", s)
102+
fmt.Fprintln(out, s)
103+
}
104+
exit(code)
105+
}
106+
89107
// Fail creates a StatusFailure Result with a given message.
90108
func Fail(a ...interface{}) DriverStatus {
91109
msg := fmt.Sprint(a...)
@@ -144,29 +162,13 @@ func processOpts(optsStr string) (Options, error) {
144162

145163
// ExecDriver executes the appropriate FlexvolumeDriver command based on
146164
// recieved call-out.
147-
func ExecDriver(drivers map[string]Driver, args []string) DriverStatus {
165+
func ExecDriver(driver Driver, args []string) DriverStatus {
148166
if len(args) < 2 {
149167
return Failf("Expected at least one argument")
150168
}
151169

152170
log.Printf("'%s %s' called with %s", args[0], args[1], args[2:])
153171

154-
driver := drivers[DefaultSymlinkDirectory] //Block volume is default
155-
156-
dir := path.Base(args[0])
157-
dir = string(strings.TrimPrefix(dir, "oracle~"))
158-
159-
if dir != "oci" && dir != DefaultSymlinkDirectory {
160-
driver = drivers[dir]
161-
}
162-
163-
// Moved outside the above if to catch errors in code.
164-
if driver == nil {
165-
return Failf("No driver found for %s", dir)
166-
}
167-
168-
log.Printf("Using %s driver", dir)
169-
170172
switch args[1] {
171173
// <driver executable> init
172174
case "init":

pkg/flexvolume/flexvolume_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func assertFailure(t *testing.T, expected DriverStatus, status DriverStatus) {
3434
}
3535

3636
func TestInit(t *testing.T) {
37-
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
37+
status := ExecDriver(
38+
mockFlexvolumeDriver{},
3839
[]string{"oci", "init"})
3940

4041
expected := DriverStatus{Status: "Success"}
@@ -46,16 +47,19 @@ func TestInit(t *testing.T) {
4647
// StatusNotSupported as the call-out is broken as of the latest stable Kube
4748
// release (1.6.4).
4849
func TestGetVolumeName(t *testing.T) {
49-
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
50-
[]string{"oci", "getvolumename", defaultTestOps})
50+
status := ExecDriver(
51+
mockFlexvolumeDriver{},
52+
[]string{"oci", "getvolumename", defaultTestOps},
53+
)
5154

5255
expected := DriverStatus{Status: "Not supported", Message: "getvolumename is broken as of kube 1.6.4"}
5356

5457
assertFailure(t, expected, status)
5558
}
5659

5760
func TestNoVolumeIDDispatch(t *testing.T) {
58-
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
61+
status := ExecDriver(
62+
mockFlexvolumeDriver{},
5963
[]string{"oci", "attach", noVolIDTestOps, "nodeName"})
6064

6165
expected := DriverStatus{
@@ -65,20 +69,10 @@ func TestNoVolumeIDDispatch(t *testing.T) {
6569
}
6670

6771
func TestAttachUnsuported(t *testing.T) {
68-
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
72+
status := ExecDriver(
73+
mockFlexvolumeDriver{},
6974
[]string{"oci", "attach", defaultTestOps, "nodeName"})
7075

7176
expected := DriverStatus{Status: "Not supported"}
7277
assertFailure(t, expected, status)
7378
}
74-
75-
func TestInvalidSymlink(t *testing.T) {
76-
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
77-
[]string{"oci-abc", "init", defaultTestOps, "nodeName"})
78-
79-
expected := DriverStatus{
80-
Status: "Failure",
81-
Message: "No driver found for oci-abc",
82-
}
83-
assertFailure(t, expected, status)
84-
}

0 commit comments

Comments
 (0)