@@ -86,26 +86,6 @@ type Driver interface {
86
86
Unmount (mountDir string ) DriverStatus
87
87
}
88
88
89
- // ExitWithResult outputs the given Result and exits with the appropriate exit
90
- // code.
91
- func ExitWithResult (result DriverStatus ) {
92
- code := 1
93
- if result .Status == StatusSuccess || result .Status == StatusNotSupported {
94
- code = 0
95
- }
96
-
97
- res , err := json .Marshal (result )
98
- if err != nil {
99
- log .Printf ("Error marshaling result: %v" , err )
100
- fmt .Fprintln (out , `{"status":"Failure","message":"Error marshaling result to JSON"}` )
101
- } else {
102
- s := string (res )
103
- log .Printf ("Command result: %s" , s )
104
- fmt .Fprintln (out , s )
105
- }
106
- exit (code )
107
- }
108
-
109
89
// Fail creates a StatusFailure Result with a given message.
110
90
func Fail (a ... interface {}) DriverStatus {
111
91
msg := fmt .Sprint (a ... )
@@ -115,6 +95,15 @@ func Fail(a ...interface{}) DriverStatus {
115
95
}
116
96
}
117
97
98
+ // Failf creates a StatusFailure Result with a given message.
99
+ func Failf (s string , a ... interface {}) DriverStatus {
100
+ msg := fmt .Sprintf (s , a ... )
101
+ return DriverStatus {
102
+ Status : StatusFailure ,
103
+ Message : msg ,
104
+ }
105
+ }
106
+
118
107
// Succeed creates a StatusSuccess Result with a given message.
119
108
func Succeed (a ... interface {}) DriverStatus {
120
109
return DriverStatus {
@@ -123,6 +112,14 @@ func Succeed(a ...interface{}) DriverStatus {
123
112
}
124
113
}
125
114
115
+ // NotSupportedf creates a StatusNotSupported Result with a given message.
116
+ func NotSupportedf (s string , a ... interface {}) DriverStatus {
117
+ return DriverStatus {
118
+ Status : StatusNotSupported ,
119
+ Message : fmt .Sprintf (s , a ... ),
120
+ }
121
+ }
122
+
126
123
// NotSupported creates a StatusNotSupported Result with a given message.
127
124
func NotSupported (a ... interface {}) DriverStatus {
128
125
return DriverStatus {
@@ -147,9 +144,9 @@ func processOpts(optsStr string) (Options, error) {
147
144
148
145
// ExecDriver executes the appropriate FlexvolumeDriver command based on
149
146
// recieved call-out.
150
- func ExecDriver (drivers map [string ]Driver , args []string ) {
147
+ func ExecDriver (drivers map [string ]Driver , args []string ) DriverStatus {
151
148
if len (args ) < 2 {
152
- ExitWithResult ( Fail ( "Expected at least one argument" ) )
149
+ return Failf ( "Expected at least one argument" )
153
150
}
154
151
155
152
log .Printf ("'%s %s' called with %s" , args [0 ], args [1 ], args [2 :])
@@ -161,127 +158,130 @@ func ExecDriver(drivers map[string]Driver, args []string) {
161
158
162
159
if dir != "oci" && dir != DefaultSymlinkDirectory {
163
160
driver = drivers [dir ]
164
- if driver == nil {
165
- ExitWithResult (Fail ("No driver found for " , dir ))
166
- }
161
+ }
162
+
163
+ // Moved outside the above if to catch errors in code.
164
+ if driver == nil {
165
+ Failf ("No driver found for " , dir )
167
166
}
168
167
169
168
log .Printf ("Using %s driver" , dir )
170
169
171
170
switch args [1 ] {
172
171
// <driver executable> init
173
172
case "init" :
174
- ExitWithResult ( driver .Init () )
173
+ return driver .Init ()
175
174
176
175
// <driver executable> getvolumename <json options>
177
176
// Currently broken as of lates kube release (1.6.4). Work around hardcodes
178
177
// exiting with StatusNotSupported.
179
178
// TODO(apryde): Investigate current situation and version support
180
179
// requirements.
181
180
case "getvolumename" :
182
- ExitWithResult ( NotSupported ( "getvolumename is broken as of kube 1.6.4" ) )
181
+ return NotSupportedf ( "getvolumename is broken as of kube 1.6.4" )
183
182
184
183
// <driver executable> attach <json options> <node name>
185
184
case "attach" :
186
185
if len (args ) != 4 {
187
- ExitWithResult ( Fail ( "attach expected exactly 4 arguments; got " , args ) )
186
+ Failf ( "attach expected exactly 4 arguments; got " , args )
188
187
}
189
188
190
189
opts , err := processOpts (args [2 ])
191
190
if err != nil {
192
- ExitWithResult ( Fail ( err ))
191
+ return Failf ( err . Error ( ))
193
192
}
194
193
195
194
nodeName := args [3 ]
196
- ExitWithResult ( driver .Attach (opts , nodeName ) )
195
+ return driver .Attach (opts , nodeName )
197
196
198
197
// <driver executable> detach <mount device> <node name>
199
198
case "detach" :
200
199
if len (args ) != 4 {
201
- ExitWithResult ( Fail ( "detach expected exactly 4 arguments; got " , args ) )
200
+ return Failf ( "detach expected exactly 4 arguments; got " , args )
202
201
}
203
202
204
203
mountDevice := args [2 ]
205
204
nodeName := args [3 ]
206
- ExitWithResult ( driver .Detach (mountDevice , nodeName ) )
205
+ return driver .Detach (mountDevice , nodeName )
207
206
208
207
// <driver executable> waitforattach <mount device> <json options>
209
208
case "waitforattach" :
210
209
if len (args ) != 4 {
211
- ExitWithResult ( Fail ( "waitforattach expected exactly 4 arguments; got " , args ) )
210
+ return Failf ( "waitforattach expected exactly 4 arguments; got " , args )
212
211
}
213
212
214
213
mountDevice := args [2 ]
215
214
opts , err := processOpts (args [3 ])
216
215
if err != nil {
217
- ExitWithResult ( Fail ( err ))
216
+ return Failf ( err . Error ( ))
218
217
}
219
218
220
- ExitWithResult ( driver .WaitForAttach (mountDevice , opts ) )
219
+ return driver .WaitForAttach (mountDevice , opts )
221
220
222
221
// <driver executable> isattached <json options> <node name>
223
222
case "isattached" :
224
223
if len (args ) != 4 {
225
- ExitWithResult ( Fail ( "isattached expected exactly 4 arguments; got " , args ) )
224
+ return Failf ( "isattached expected exactly 4 arguments; got " , args )
226
225
}
227
226
228
227
opts , err := processOpts (args [2 ])
229
228
if err != nil {
230
- ExitWithResult ( Fail ( err ))
229
+ return Failf ( err . Error ( ))
231
230
}
232
231
nodeName := args [3 ]
233
- ExitWithResult ( driver .IsAttached (opts , nodeName ) )
232
+ return driver .IsAttached (opts , nodeName )
234
233
235
234
// <driver executable> mountdevice <mount dir> <mount device> <json options>
236
235
case "mountdevice" :
237
236
if len (args ) != 5 {
238
- ExitWithResult ( Fail ( "mountdevice expected exactly 5 arguments; got " , args ) )
237
+ return Failf ( "mountdevice expected exactly 5 arguments; got " , args )
239
238
}
240
239
241
240
mountDir := args [2 ]
242
241
mountDevice := args [3 ]
243
242
244
243
opts , err := processOpts (args [4 ])
245
244
if err != nil {
246
- ExitWithResult ( Fail ( err ))
245
+ return Failf ( err . Error ( ))
247
246
}
248
247
249
- ExitWithResult ( driver .MountDevice (mountDir , mountDevice , opts ) )
248
+ return driver .MountDevice (mountDir , mountDevice , opts )
250
249
251
250
// <driver executable> unmountdevice <mount dir>
252
251
case "unmountdevice" :
253
252
if len (args ) != 3 {
254
- ExitWithResult ( Fail ( "unmountdevice expected exactly 3 arguments; got " , args ) )
253
+ return Failf ( "unmountdevice expected exactly 3 arguments; got " , args )
255
254
}
256
255
257
256
mountDir := args [2 ]
258
- ExitWithResult ( driver .UnmountDevice (mountDir ) )
257
+ return driver .UnmountDevice (mountDir )
259
258
260
259
// <driver executable> mount <mount dir> <json options>
261
260
case "mount" :
262
261
if len (args ) != 4 {
263
- ExitWithResult ( Fail ( "mount expected exactly 4 arguments; got " , args ) )
262
+ return Failf ( "mount expected exactly 4 arguments; got " , args )
264
263
}
265
264
266
265
mountDir := args [2 ]
267
266
268
267
opts , err := processOpts (args [3 ])
269
268
if err != nil {
270
- ExitWithResult ( Fail ( err ))
269
+ return Failf ( err . Error ( ))
271
270
}
272
271
273
- ExitWithResult ( driver .Mount (mountDir , opts ) )
272
+ return driver .Mount (mountDir , opts )
274
273
275
274
// <driver executable> unmount <mount dir>
276
275
case "unmount" :
277
276
if len (args ) != 3 {
278
- ExitWithResult ( Fail ( "mount expected exactly 3 arguments; got " , args ) )
277
+ return Failf ( "mount expected exactly 3 arguments; got " , args )
279
278
}
280
279
281
280
mountDir := args [2 ]
282
- ExitWithResult ( driver .Unmount (mountDir ) )
281
+ return driver .Unmount (mountDir )
283
282
284
283
default :
285
- ExitWithResult ( Fail ( "Invalid command; got " , args ) )
284
+ return Failf ( "invalid command; got " , args )
286
285
}
286
+ return Failf ("Unexpected condition" )
287
287
}
0 commit comments