forked from hpe-storage/common-host-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
347 lines (315 loc) · 11.3 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2019 Hewlett Packard Enterprise Development LP
package handler
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
log "github.com/hpe-storage/common-host-libs/logger"
"github.com/hpe-storage/common-host-libs/chapi"
"github.com/hpe-storage/common-host-libs/connectivity"
"github.com/hpe-storage/common-host-libs/dockerplugin/plugin"
"github.com/hpe-storage/common-host-libs/dockerplugin/provider"
"github.com/hpe-storage/common-host-libs/model"
"github.com/hpe-storage/common-host-libs/util"
)
var (
defaultCreationTimeout = time.Duration(300) * time.Second
listOfCreateKeysToRemove = []string{"logLevel", volumeDirKey, plugin.DeleteConflictDelayKey, plugin.MountConflictDelayKey}
)
//@APIVersion 1.0.0
//@Title implement the Nimble Volume Driver Create for docker
//@Description implement the /VolumeDriver.Create Docker end point
//@Accept json
//@Resource /VolumeDriver.Create
//@Success 200 CreateResponse
//@Router /VolumeDriver.Create [post]
//@BasePath http:/VolumeDriver.Create
// nolint : gocyclo exceeded
// VolumeDriverCreate implement the /VolumeDriver.Create Docker end point
func VolumeDriverCreate(w http.ResponseWriter, r *http.Request) {
log.Debug("volumeCreate called")
cr := &CreateResponse{}
// Populate Host Context to the Plugin Request
pluginReq, err := preparePluginRequest(r)
if err != nil {
dr := DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
//get containerProviderClient
providerClient, err := provider.GetProviderClient()
if err != nil {
err = errors.New("unable to setup the container-provider client " + err.Error())
resp := &DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(resp)
return
}
// populate defaut create options
err = populateVolCreateOptions(pluginReq)
if err != nil {
log.Errorf("%s failed to add mount options from config file using defaults", err.Error())
}
// validate fsMode and fsOwner if specified in the request
fsMode, fsOwner, err := getFileSystemModeAndOwnerFromRequest(pluginReq)
if err != nil {
dr := DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
fsOpts := &model.FilesystemOpts{Mode: fsMode, Owner: fsOwner}
// populate delayed create option to pluginReq except for import and clone workflows
if !isValidDelayedCreateOpt(pluginReq) {
log.Tracef("valid delayedCreate opts (%v) setting delayedCreate to true", pluginReq.Opts)
pluginReq.Opts[delayedCreateOpt] = true
}
// remove global options from create request
removeGlobalOptionsFromCreateRequest(pluginReq)
// check if valid fileystem was present in the request
if !isValidFilesystem(pluginReq) {
dr := DriverResponse{Err: fmt.Sprintf("invalid filesystem type(%s), please enter one of the following options (%s)", pluginReq.Opts["filesystem"], strings.Join(plugin.SupportedFileSystems, " "))}
json.NewEncoder(w).Encode(dr)
return
}
mapMutex.Lock(pluginReq.Name)
log.Debugf("taken lock on %s in create", pluginReq.Name)
defer mapMutex.Unlock(pluginReq.Name)
//1. container-provider /VolumeDriver.Create called
var dr DriverResponse
_, err = providerClient.DoJSON(&connectivity.Request{Action: "POST", Path: provider.CreateURI, Payload: &pluginReq, Response: &cr, ResponseError: &dr})
if err != nil {
if cr.Err != "" {
dr := DriverResponse{Err: fmt.Errorf("unable to create the volume %s %s", pluginReq.Name, cr.Err).Error()}
json.NewEncoder(w).Encode(dr)
return
}
log.Tracef("err: %s", err.Error())
dr := DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
if cr.Err != "" {
if strings.Contains(strings.ToLower(cr.Err), "unable to find any logged-in fc sessions") {
//TODO issue rescan on the fc host
} else if strings.Contains(strings.ToLower(cr.Err), "unrecognized field") {
errMsg := fmt.Sprintf("unknown options for create volume %v", pluginReq.Opts)
dr := DriverResponse{Err: errMsg}
json.NewEncoder(w).Encode(dr)
return
}
dr := DriverResponse{Err: cr.Err}
json.NewEncoder(w).Encode(dr)
return
} else if cr.Help != "" {
// create called with Help option
log.Trace("Help Message")
cr.Err = cr.Help
json.NewEncoder(w).Encode(cr)
} else if len(cr.Volumes) > 0 {
// check if it is delayedCreate Response else continue with older create
log.Tracef("response from volume create (%+v)", cr.Volumes[0])
if val, ok := cr.Volumes[0].Status[delayedCreateOpt]; ok {
log.Tracef("delayedCreate response %s", val)
json.NewEncoder(w).Encode(cr)
return
}
// obtain chapi client
chapiClient, err := chapi.NewChapiClient()
if err != nil {
log.Trace("err: ", err.Error())
cr = &CreateResponse{Err: "Unable to get chapi client" + err.Error()}
json.NewEncoder(w).Encode(cr)
return
}
// Creation of new volume
log.Debug("Volume creation initiated for ", cr.Volumes[0].Name)
discoveryIP := cr.Volumes[0].DiscoveryIP
iqn := cr.Volumes[0].Iqn
log.Tracef("SNO :%s DiscoveryIP :%s IQN %s", cr.Volumes[0].SerialNumber, discoveryIP, iqn)
// change the connection mode to manual for docker
cr.Volumes[0].ConnectionMode = manualMode
//2. attach the device, create file system
device, err := createFileSystemOnVolume(cr.Volumes, pluginReq, fsOpts)
if err != nil {
// since device creation failed. Cleanup the cache
invalidateHostContextCache()
// cleanup host side device if device exist
if device != nil {
// detach device and cleanup host side
log.Debugf("initiating device detach for %+v", device)
// set the target scope
device.TargetScope = cr.Volumes[0].TargetScope
// offline the device
err = chapiClient.OfflineDevice(device)
if err != nil {
log.Debug("OfflineDevice err: ", err.Error())
}
}
// call Nimble.Detach (remove acl's)
err = nimbleDetach(cr.Volumes[0], pluginReq)
if err != nil {
log.Trace("err: ", err.Error())
}
if device != nil {
// delete the device
err = chapiClient.DeleteDevice(device)
if err != nil {
log.Trace("err: ", err.Error())
}
}
var dr DriverResponse
//force delete the volume on create failures else it will lie around in offline state
pluginReq.Opts["destroyOnRm"] = true
providerClient.DoJSON(&connectivity.Request{Action: "POST", Path: provider.RemoveURI, Payload: &pluginReq, Response: &dr, ResponseError: nil})
dr = DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
// final return after all the cleanup
return
}
//3. Now offline the device
log.Debugf("device %+v is unmounted for volume %+v, offline the device", device, cr.Volumes[0])
// set the target scope
device.TargetScope = cr.Volumes[0].TargetScope
err = chapiClient.OfflineDevice(device)
if err != nil {
dr := DriverResponse{Err: "unable to detach volume from host " + err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
//4. invoke Nimble.Detach (remove acl's)
err = nimbleDetach(cr.Volumes[0], pluginReq)
if err != nil {
dr := DriverResponse{Err: "unable to detach volume from array " + err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
//5. Finally delete the device
err = chapiClient.DeleteDevice(device)
if err != nil {
dr := DriverResponse{Err: "unable to detach volume from host " + err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
}
log.Infof("%s: request=(%+v) response=(%+v)", provider.CreateURI, pluginReq, cr.Volumes)
json.NewEncoder(w).Encode(cr)
return
}
// Attach the device, Create filesystem on the device
// nolint : gocyclo
func createFileSystemOnVolume(vols []*model.Volume, pluginReq *PluginRequest, fsOpts *model.FilesystemOpts) (*model.Device, error) {
log.Tracef("createFileSystemOnVolume called for %+v", vols)
log.Traceln("Vol :", vols, "Host :", pluginReq.Host)
// obtain chapi client with large timeout of 5 minutes max for creation
chapiClient, err := chapi.NewChapiClientWithTimeout(defaultCreationTimeout)
if err != nil {
return nil, err
}
//1. Create and attach the device
log.Tracef("calling attach device with vols %+v", vols)
devices, err := chapiClient.AttachDevice(vols)
if err != nil {
if devices != nil {
return devices[0], err
}
return nil, err
}
if len(devices) == 0 || len(vols) == 0 {
return nil, errors.New("unable to retrieve volume / device ")
}
log.Trace("Device found ", devices[0].Pathname)
device := devices[0]
vol := vols[0]
//2. Make a put request to put a partition / filesystem on the device
fileSystemType := getFileSystemTypeFromRequest(pluginReq)
//make sure volume.Mountpoint is populated
vol.MountPoint = plugin.MountDir + vol.Name
err = chapiClient.SetupFilesystemAndPermissions(device, vol, fileSystemType)
if err != nil {
return nil, fmt.Errorf("unable to setup filesystem for device %s, err(%s)", device.AltFullPathName, err.Error())
}
err = chapiClient.UnmountDevice(vol)
if err == nil {
// delete the mountPoint
os.RemoveAll(vol.MountPoint)
if err != nil {
return nil, err
}
} else {
return nil, err
}
return device, err
}
func populateVolCreateOptions(req *PluginRequest) (err error) {
log.Trace(">>>>> populateVolCreateOptions")
defer log.Trace("<<<<< populateVolCreateOptions")
// populate options based on correct priority order
// check for short form or size and add complete key
if _, present := req.Opts["size"]; present {
req.Opts["sizeInGiB"] = req.Opts["size"]
}
// check if config file exist and load config
volumeDriverConfFile := plugin.PluginConfigDir + plugin.DriverConfigFile
// check if volumeDriverConfig is initialized or not
if plugin.VolumeDriverConfig == nil {
err = plugin.LoadHPEVolConfig()
if err != nil {
return err
}
_, err = plugin.VolumeDriverConfig.GetCache().GetMap(plugin.Section.String(plugin.Global))
if err != nil {
// volumeDriverConfig is not initialized yet.
log.Tracef("error %s to retrieve data from existing config, load %s", err.Error(), volumeDriverConfFile)
exists, _, _ := util.FileExists(volumeDriverConfFile)
if !exists {
return fmt.Errorf("unable to populate volume create options, driver config file %s doesn't exist", volumeDriverConfFile)
}
log.Tracef("volumeDriver Config file exists at %s", volumeDriverConfFile)
err = plugin.LoadHPEVolConfig()
if err != nil {
return err
}
}
}
// validate if the volumeDriverConfFile in the cache is current with respect to modification time of the config file. Check if the cache is dirty
plugin.UpdateVolumeDriverConfigCache(volumeDriverConfFile)
var updatedOpts map[string]interface{}
updatedOpts, err = plugin.GetUpdatedOptsFromConfig(req.Opts)
if err != nil {
return err
}
// update original options in the request
req.Opts = updatedOpts
log.Tracef("updated opts %+v", req.Opts)
return nil
}
func isValidDelayedCreateOpt(pluginReq *PluginRequest) bool {
log.Trace(">>>> isValidDelayedCreateOpt called")
defer log.Tracef("<<<< isValidDelayedCreateOpt")
_, ok := pluginReq.Opts["cloneOf"]
if ok {
return true
}
_, ok = pluginReq.Opts["importVolAsClone"]
if ok {
return true
}
_, ok = pluginReq.Opts["importVol"]
if ok {
return true
}
return false
}
func removeGlobalOptionsFromCreateRequest(pluginReq *PluginRequest) error {
log.Trace(">>>>> removeGlobalOptionsFromCreateRequest called")
defer log.Trace("<<<<< removeGlobalOptionsFromCreateRequest")
for _, keyToRemove := range listOfCreateKeysToRemove {
if _, ok := pluginReq.Opts[keyToRemove]; ok {
delete(pluginReq.Opts, keyToRemove)
}
}
return nil
}