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

Commit c696bfd

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

File tree

3 files changed

+95
-38
lines changed

3 files changed

+95
-38
lines changed

cmd/oci/main.go

Lines changed: 64 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")
@@ -36,6 +45,10 @@ func GetLogPath() string {
3645
return path + "/oci_flexvolume_driver.log"
3746
}
3847

48+
func loadRegisteredDrivers() {
49+
RegisterDriver("oci-bvs", &driver.OCIFlexvolumeDriver{})
50+
}
51+
3952
func main() {
4053
// TODO: Maybe use sirupsen/logrus?
4154
f, err := os.OpenFile(GetLogPath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
@@ -51,9 +64,57 @@ func main() {
5164

5265
log.Printf("OCI FlexVolume Driver version: %s (%s)", version, build)
5366

54-
drivers := map[string]flexvolume.Driver{
55-
"oci-bvs": &driver.OCIFlexvolumeDriver{},
67+
loadRegisteredDrivers()
68+
69+
driver, err := getDriverFromArgs()
70+
if err != nil {
71+
log.Fatalf(err.Error())
72+
}
73+
74+
flexvolume.ExitWithResult(flexvolume.ExecDriver(driver, os.Args))
75+
}
76+
77+
func getDriverFromArgs() (flexvolume.Driver, error) {
78+
driver, err := GetRegisteredDriver(flexvolume.DefaultSymlinkDirectory) //Block volume is default
79+
if err != nil {
80+
return nil, err
5681
}
5782

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

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)