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

Commit 1ce1c73

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

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

cmd/oci/main.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"fmt"
1919
"log"
2020
"os"
21+
"path"
22+
"strings"
2123

2224
"github.com/oracle/oci-flexvolume-driver/pkg/flexvolume"
2325
"github.com/oracle/oci-flexvolume-driver/pkg/oci/driver"
@@ -55,5 +57,30 @@ func main() {
5557
"oci-bvs": &driver.OCIFlexvolumeDriver{},
5658
}
5759

58-
flexvolume.ExecDriver(drivers, os.Args)
60+
driver, err := getDriver(drivers)
61+
if err != nil {
62+
log.Fatalf(err.Error())
63+
}
64+
65+
flexvolume.ExitWithResult(flexvolume.ExecDriver(driver, os.Args))
66+
}
67+
68+
func getDriver(drivers map[string]flexvolume.Driver) (flexvolume.Driver, error) {
69+
driver := drivers[flexvolume.DefaultSymlinkDirectory] //Block volume is default
70+
71+
dir := path.Base(os.Args[0])
72+
dir = string(strings.TrimPrefix(dir, "oracle~"))
73+
74+
if dir != "oci" && dir != flexvolume.DefaultSymlinkDirectory {
75+
driver = drivers[dir]
76+
}
77+
78+
// Moved outside the above if to catch errors in code.
79+
if driver == nil {
80+
return nil, fmt.Errorf("No driver found for %s", dir)
81+
}
82+
83+
log.Printf("Using %s driver", dir)
84+
85+
return driver, nil
5986
}

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)