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

Commit 19346b2

Browse files
author
Harvey Lowndes
committed
Support using symlinks in storage class to find correct driver
1 parent 35c8b1a commit 19346b2

File tree

4 files changed

+159
-76
lines changed

4 files changed

+159
-76
lines changed

cmd/oci/main.go

Lines changed: 67 additions & 1 deletion
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)
@@ -50,5 +63,58 @@ func main() {
5063
log.SetOutput(f)
5164

5265
log.Printf("OCI FlexVolume Driver version: %s (%s)", version, build)
53-
flexvolume.ExecDriver(&driver.OCIFlexvolumeDriver{}, os.Args)
66+
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
81+
}
82+
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
54120
}

deploy.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ set -o pipefail
2020
VENDOR=oracle
2121
DRIVER=oci
2222

23-
driver_dir="/flexmnt/$VENDOR${VENDOR:+"~"}${DRIVER}"
23+
ORACLE_OCI_FOLDER=$VENDOR${VENDOR:+"~"}${DRIVER}
24+
25+
driver_dir="/flexmnt/$ORACLE_OCI_FOLDER"
2426

2527
LOG_FILE="$driver_dir/oci_flexvolume_driver.log"
2628

@@ -33,14 +35,20 @@ if [ ! -d "$driver_dir" ]; then
3335
mkdir "$driver_dir"
3436
fi
3537

38+
if [ ! -d "$driver_dir-bvs" ]; then
39+
mkdir "$driver_dir-bvs"
40+
fi
41+
3642
cp "/$DRIVER" "$driver_dir/.$DRIVER"
3743
mv -f "$driver_dir/.$DRIVER" "$driver_dir/$DRIVER"
3844

45+
ln -sf "../$ORACLE_OCI_FOLDER/$DRIVER" "$driver_dir-bvs/$DRIVER-bvs"
46+
3947
if [ -f "$CONFIG_FILE" ]; then
4048
cp "$CONFIG_FILE" "$driver_dir/$config_file_name"
4149
fi
4250

4351
while : ; do
4452
touch $LOG_FILE
4553
tail -f $LOG_FILE
46-
done
54+
done

pkg/flexvolume/flexvolume.go

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const (
6767
OptionKeyPodUID = "kubernetes.io/pod.uid"
6868

6969
OptionKeyServiceAccountName = "kubernetes.io/serviceAccount.name"
70+
71+
DefaultSymlinkDirectory = "oci-bvs"
7072
)
7173

7274
// Driver is the main Flexvolume interface.
@@ -111,6 +113,15 @@ func Fail(a ...interface{}) DriverStatus {
111113
}
112114
}
113115

116+
// Failf creates a StatusFailure Result with a given message.
117+
func Failf(s string, a ...interface{}) DriverStatus {
118+
msg := fmt.Sprintf(s, a...)
119+
return DriverStatus{
120+
Status: StatusFailure,
121+
Message: msg,
122+
}
123+
}
124+
114125
// Succeed creates a StatusSuccess Result with a given message.
115126
func Succeed(a ...interface{}) DriverStatus {
116127
return DriverStatus{
@@ -119,6 +130,14 @@ func Succeed(a ...interface{}) DriverStatus {
119130
}
120131
}
121132

133+
// NotSupportedf creates a StatusNotSupported Result with a given message.
134+
func NotSupportedf(s string, a ...interface{}) DriverStatus {
135+
return DriverStatus{
136+
Status: StatusNotSupported,
137+
Message: fmt.Sprintf(s, a...),
138+
}
139+
}
140+
122141
// NotSupported creates a StatusNotSupported Result with a given message.
123142
func NotSupported(a ...interface{}) DriverStatus {
124143
return DriverStatus{
@@ -143,127 +162,127 @@ func processOpts(optsStr string) (Options, error) {
143162

144163
// ExecDriver executes the appropriate FlexvolumeDriver command based on
145164
// recieved call-out.
146-
func ExecDriver(driver Driver, args []string) {
165+
func ExecDriver(driver Driver, args []string) DriverStatus {
147166
if len(args) < 2 {
148-
ExitWithResult(Fail("Expected at least one argument"))
167+
return Failf("Expected at least one argument")
149168
}
150169

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

153172
switch args[1] {
154173
// <driver executable> init
155174
case "init":
156-
ExitWithResult(driver.Init())
175+
return driver.Init()
157176

158177
// <driver executable> getvolumename <json options>
159178
// Currently broken as of lates kube release (1.6.4). Work around hardcodes
160179
// exiting with StatusNotSupported.
161180
// TODO(apryde): Investigate current situation and version support
162181
// requirements.
163182
case "getvolumename":
164-
ExitWithResult(NotSupported("getvolumename is broken as of kube 1.6.4"))
183+
return NotSupportedf("getvolumename is broken as of kube 1.6.4")
165184

166185
// <driver executable> attach <json options> <node name>
167186
case "attach":
168187
if len(args) != 4 {
169-
ExitWithResult(Fail("attach expected exactly 4 arguments; got ", args))
188+
Failf("attach expected exactly 4 arguments; got ", args)
170189
}
171190

172191
opts, err := processOpts(args[2])
173192
if err != nil {
174-
ExitWithResult(Fail(err))
193+
return Failf(err.Error())
175194
}
176195

177196
nodeName := args[3]
178-
ExitWithResult(driver.Attach(opts, nodeName))
197+
return driver.Attach(opts, nodeName)
179198

180199
// <driver executable> detach <mount device> <node name>
181200
case "detach":
182201
if len(args) != 4 {
183-
ExitWithResult(Fail("detach expected exactly 4 arguments; got ", args))
202+
return Failf("detach expected exactly 4 arguments; got ", args)
184203
}
185204

186205
mountDevice := args[2]
187206
nodeName := args[3]
188-
ExitWithResult(driver.Detach(mountDevice, nodeName))
207+
return driver.Detach(mountDevice, nodeName)
189208

190209
// <driver executable> waitforattach <mount device> <json options>
191210
case "waitforattach":
192211
if len(args) != 4 {
193-
ExitWithResult(Fail("waitforattach expected exactly 4 arguments; got ", args))
212+
return Failf("waitforattach expected exactly 4 arguments; got ", args)
194213
}
195214

196215
mountDevice := args[2]
197216
opts, err := processOpts(args[3])
198217
if err != nil {
199-
ExitWithResult(Fail(err))
218+
return Failf(err.Error())
200219
}
201220

202-
ExitWithResult(driver.WaitForAttach(mountDevice, opts))
221+
return driver.WaitForAttach(mountDevice, opts)
203222

204223
// <driver executable> isattached <json options> <node name>
205224
case "isattached":
206225
if len(args) != 4 {
207-
ExitWithResult(Fail("isattached expected exactly 4 arguments; got ", args))
226+
return Failf("isattached expected exactly 4 arguments; got ", args)
208227
}
209228

210229
opts, err := processOpts(args[2])
211230
if err != nil {
212-
ExitWithResult(Fail(err))
231+
return Failf(err.Error())
213232
}
214233
nodeName := args[3]
215-
ExitWithResult(driver.IsAttached(opts, nodeName))
234+
return driver.IsAttached(opts, nodeName)
216235

217236
// <driver executable> mountdevice <mount dir> <mount device> <json options>
218237
case "mountdevice":
219238
if len(args) != 5 {
220-
ExitWithResult(Fail("mountdevice expected exactly 5 arguments; got ", args))
239+
return Failf("mountdevice expected exactly 5 arguments; got ", args)
221240
}
222241

223242
mountDir := args[2]
224243
mountDevice := args[3]
225244

226245
opts, err := processOpts(args[4])
227246
if err != nil {
228-
ExitWithResult(Fail(err))
247+
return Failf(err.Error())
229248
}
230249

231-
ExitWithResult(driver.MountDevice(mountDir, mountDevice, opts))
250+
return driver.MountDevice(mountDir, mountDevice, opts)
232251

233252
// <driver executable> unmountdevice <mount dir>
234253
case "unmountdevice":
235254
if len(args) != 3 {
236-
ExitWithResult(Fail("unmountdevice expected exactly 3 arguments; got ", args))
255+
return Failf("unmountdevice expected exactly 3 arguments; got ", args)
237256
}
238257

239258
mountDir := args[2]
240-
ExitWithResult(driver.UnmountDevice(mountDir))
259+
return driver.UnmountDevice(mountDir)
241260

242261
// <driver executable> mount <mount dir> <json options>
243262
case "mount":
244263
if len(args) != 4 {
245-
ExitWithResult(Fail("mount expected exactly 4 arguments; got ", args))
264+
return Failf("mount expected exactly 4 arguments; got ", args)
246265
}
247266

248267
mountDir := args[2]
249268

250269
opts, err := processOpts(args[3])
251270
if err != nil {
252-
ExitWithResult(Fail(err))
271+
return Failf(err.Error())
253272
}
254273

255-
ExitWithResult(driver.Mount(mountDir, opts))
274+
return driver.Mount(mountDir, opts)
256275

257276
// <driver executable> unmount <mount dir>
258277
case "unmount":
259278
if len(args) != 3 {
260-
ExitWithResult(Fail("mount expected exactly 3 arguments; got ", args))
279+
return Failf("mount expected exactly 3 arguments; got ", args)
261280
}
262281

263282
mountDir := args[2]
264-
ExitWithResult(driver.Unmount(mountDir))
283+
return driver.Unmount(mountDir)
265284

266285
default:
267-
ExitWithResult(Fail("Invalid command; got ", args))
286+
return Failf("invalid command; got ", args)
268287
}
269288
}

0 commit comments

Comments
 (0)