forked from hpe-storage/common-host-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.go
182 lines (166 loc) · 5.89 KB
/
remove.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
// Copyright 2019 Hewlett Packard Enterprise Development LP
package handler
import (
"encoding/json"
"errors"
"net/http"
"time"
"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"
log "github.com/hpe-storage/common-host-libs/logger"
)
//@APIVersion 1.0.0
//@Title implement the Nimble Volume Driver Remove for docker
//@Description implement the /VolumeDriver.Remove Docker end point
//@Accept json
//@Resource /VolumeDriver.Remove
//@Success 200 DriverResponse
//@Router /VolumeDriver.Remove [post]
//@BasePath http:/VolumeDriver.Remove
//nolint: gocyclo
// VolumeDriverRemove implement the /VolumeDriver.Remove Docker end point
func VolumeDriverRemove(w http.ResponseWriter, r *http.Request) {
log.Debug("VolumeDriver.Remove called")
volResp := &VolumeResponse{}
dr := &DriverResponse{}
// Populate Host Context to the Plugin Request
pluginReq, err := populateHostContextAndScope(r)
if err != nil {
dr = &DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
// Add user credentials for cloud/simplivity container-provider
user, err := provider.GetProviderAccessKeys()
if err != nil {
dr := DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
if user != nil {
pluginReq.User = user
}
//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
}
//1. container-provider /Nimble.Get called
_, err = providerClient.DoJSON(&connectivity.Request{Action: "POST", Path: provider.VolumeDriverGetURI, Payload: &pluginReq, Response: &volResp, ResponseError: &volResp})
if volResp.Err != "" {
dr = &DriverResponse{Err: volResp.Err}
json.NewEncoder(w).Encode(dr)
return
}
if err != nil {
log.Trace(err.Error())
dr = &DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
volume := volResp.Volume
log.Tracef("Volume found :%+v", volume)
// Check if the volume has ACR before we attempt to delete the volume
log.Tracef("volume status is %+v ", volResp.Volume.Status)
inUseValue, ok := volResp.Volume.Status[inUseKey]
if ok {
switch v := inUseValue.(type) {
case bool:
if v == true {
log.Tracef("value of %s is %v", inUseKey, v)
processDeleteConflictDelay(volume.Name, providerClient, pluginReq, plugin.DeleteConflictDelay)
} else {
log.Infof("%s is false for %s,ignoring processDeleteConflictDelay", plugin.DeleteConflictDelayKey, volume.Name)
}
}
} else {
log.Infof("%s not present for %s,ignoring processDeleteConflictDelay", plugin.DeleteConflictDelayKey, volume.Name)
}
// obtain chapi client
chapiClient, err := chapi.NewChapiClient()
if err != nil {
dr = &DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
//2.Perform host side remove workflow
err = chapiClient.UnmountDevice(volume)
if err != nil {
dr = &DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
// 3 . Offline the device (if present)
device, _ := chapiClient.GetDeviceFromVolume(volume)
if device != nil {
log.Tracef("best effort to ofline device for %+v", volume)
chapiClient.OfflineDevice(device)
}
// 4. Finally call Nimble.Detach (remove acl's). It should not have acl's so don't fail the request but do our best attempt
log.Tracef("best effort to remove acl for %+v", volume)
nimbleDetach(volume, pluginReq)
// 5 . Delete the device (if present)
if device != nil {
log.Tracef("best effort to remove device for %+v", volume)
chapiClient.DeleteDevice(device)
}
// 6. container-provider /VolumeDriver.Remove called
_, err = providerClient.DoJSON(&connectivity.Request{Action: "POST", Path: provider.RemoveURI, Payload: &pluginReq, Response: &dr, ResponseError: nil})
if err != nil {
dr = &DriverResponse{Err: err.Error()}
json.NewEncoder(w).Encode(dr)
return
}
log.Infof("%s: request=(%+v) response=(%+v)", provider.RemoveURI, pluginReq, dr)
json.NewEncoder(w).Encode(dr)
return
}
/* processDeleteConflictDelay
The method checks the volume status to check if it is currently inUse.
If the volume is inUse, we poll every tick ( 5 secs) to recheck if the volume is not inUse.
Eventually after timeout (deleteDelay) we return
*/
func processDeleteConflictDelay(volName string, containerProviderClient *connectivity.Client, pluginReq *PluginRequest, deleteDelay int) {
log.Tracef(">>>>> processDeleteConflictDelay called for %s with a timeout of %d seconds", volName, deleteDelay)
defer log.Tracef("<<<<<< processDeleteConflictDelay")
tick := time.Tick(5 * time.Second)
timeout := time.After(time.Duration(deleteDelay) * time.Second)
// Keep trying until we're timed out or got a result or got an error
try := 0
for {
select {
// Got a timeout! return
case <-timeout:
log.Tracef("timeout occurred after %v seconds for %s. Returning", timeout, volName)
return
// Got a tick, we should check on getVolumeInfo()
case <-tick:
try++
volume, err := getVolumeInfo(containerProviderClient, pluginReq)
if val, ok := volume.Status[inUseKey]; ok {
switch v := val.(type) {
case bool:
if v == false {
// if it is false it is safe to return
log.Tracef("%d volume %s is not currently in use. Returning.", try, volName)
return
}
}
log.Debugf("%d: volume %s is still in use. Continuing.", try, volName)
} else {
// if the inUse field is absent return and not process
log.Debugf("%d: %s absent from volume %s. Returning.", try, inUseKey, volName)
return
}
// Error from getVolumeInfo(), we should bail
if err != nil {
log.Debugf("%d: unable to process deleteConflictDelay for %s, continue %s", try, volName, err.Error())
}
}
}
}