-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvolumes.go
More file actions
344 lines (298 loc) · 10.5 KB
/
volumes.go
File metadata and controls
344 lines (298 loc) · 10.5 KB
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
package stackit
import (
"context"
"fmt"
"net/http"
"slices"
"time"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/stackiterrors"
"github.com/stackitcloud/stackit-sdk-go/core/runtime"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
sdkWait "github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
)
const (
VolumeAvailableStatus = "AVAILABLE"
VolumeAttachedStatus = "ATTACHED"
operationFinishInitDelay = 1 * time.Second
operationFinishFactor = 1.1
operationFinishSteps = 10
diskAttachInitDelay = 1 * time.Second
diskAttachFactor = 1.2
diskAttachSteps = 15
diskDetachInitDelay = 1 * time.Second
diskDetachFactor = 1.2
diskDetachSteps = 13
VolumeDescription = "Created by STACKIT CSI driver"
)
var volumeErrorStates = [...]string{"ERROR", "ERROR_RESIZING", "ERROR_DELETING"}
func (os *iaasClient) CreateVolume(ctx context.Context, payload *iaas.CreateVolumePayload) (*iaas.Volume, error) {
payload.Description = new(VolumeDescription)
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
req, err := os.iaas.CreateVolume(ctxWithHTTPResp, os.projectID, os.region).CreateVolumePayload(*payload).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return nil, stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, err
}
return req, nil
}
func (os *iaasClient) DeleteVolume(ctx context.Context, volumeID string) error {
used, err := os.diskIsUsed(ctx, volumeID)
if err != nil {
return err
}
if used {
return fmt.Errorf("cannot delete the volume %q, it's still attached to a node", volumeID)
}
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
err = os.iaas.DeleteVolume(ctxWithHTTPResp, os.projectID, os.region, volumeID).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return err
}
return err
}
func (os *iaasClient) AttachVolume(ctx context.Context, instanceID, volumeID string) (string, error) {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return "", err
}
if volume.ServerId != nil && instanceID == *volume.ServerId {
klog.V(4).Infof("Disk %s is already attached to instance %s", volumeID, instanceID)
return *volume.Id, nil
}
payload := iaas.AddVolumeToServerPayload{
DeleteOnTermination: new(false),
}
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
_, err = os.iaas.AddVolumeToServer(ctxWithHTTPResp, os.projectID, os.region, instanceID, volumeID).AddVolumeToServerPayload(payload).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return "", stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return "", err
}
return *volume.Id, err
}
// WaitVolumeTargetStatusWithCustomBackoff waits for volume to be in target state with custom backoff
func (os *iaasClient) WaitVolumeTargetStatusWithCustomBackoff(ctx context.Context, volumeID string, tStatus []string, backoff *wait.Backoff) error {
waitErr := wait.ExponentialBackoff(*backoff, func() (bool, error) {
vol, err := os.GetVolume(ctx, volumeID)
if err != nil {
return false, err
}
if slices.Contains(tStatus, *vol.Status) {
return true, nil
}
for _, eState := range volumeErrorStates {
if *vol.Status == eState {
return false, fmt.Errorf("volume is in error state: %s", *vol.Status)
}
}
return false, nil
})
if wait.Interrupted(waitErr) {
waitErr = fmt.Errorf("timeout on waiting for volume %s status to be in %v", volumeID, tStatus)
}
return waitErr
}
func (os *iaasClient) ListVolumes(ctx context.Context, _ int, _ string) ([]iaas.Volume, string, error) {
// TODO: Add support for pagination when IaaS adds it
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
volumes, err := os.iaas.ListVolumes(ctxWithHTTPResp, os.projectID, os.region).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return nil, "", stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, "", err
}
return *volumes.Items, "", err
}
func (os *iaasClient) WaitDiskAttached(ctx context.Context, instanceID, volumeID string) error {
backoff := wait.Backoff{
Duration: diskAttachInitDelay,
Factor: diskAttachFactor,
Steps: diskAttachSteps,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
attached, err := os.diskIsAttached(ctx, instanceID, volumeID)
if err != nil && !stackiterrors.IsNotFound(err) {
// if this is a race condition indicate the volume is deleted
// during sleep phase, ignore the error and return attach=false
return false, err
}
return attached, nil
})
if wait.Interrupted(err) {
err = fmt.Errorf("volume %q failed to be attached within the allowed time", volumeID)
}
return err
}
func (os *iaasClient) DetachVolume(ctx context.Context, instanceID, volumeID string) error {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return err
}
if *volume.Status == VolumeAvailableStatus {
klog.V(2).Infof("Volume: %s has been detached from compute: %s ", *volume.Id, instanceID)
return nil
}
if *volume.Status != VolumeAttachedStatus {
return fmt.Errorf("can not detach volume %s, its status is %s", *volume.Name, *volume.Status)
}
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
if volume.ServerId != nil && *volume.ServerId == instanceID {
err = os.iaas.RemoveVolumeFromServer(ctxWithHTTPResp, os.projectID, os.region, instanceID, volumeID).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return stackiterrors.WrapErrorWithResponseID(fmt.Errorf("failed to detach volume %s from compute %s : %v", *volume.Id, instanceID, err), reqID)
}
return err
}
klog.V(2).Infof("Successfully detached volume: %s from compute: %s", *volume.Id, instanceID)
return nil
}
// Disk has no attachments or not attached to provided compute
return nil
}
func (os *iaasClient) WaitDiskDetached(ctx context.Context, instanceID, volumeID string) error {
backoff := wait.Backoff{
Duration: diskDetachInitDelay,
Factor: diskDetachFactor,
Steps: diskDetachSteps,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
attached, err := os.diskIsAttached(ctx, instanceID, volumeID)
if err != nil {
return false, err
}
return !attached, nil
})
if wait.Interrupted(err) {
err = fmt.Errorf("volume %q failed to detach within the allowed time", volumeID)
}
return err
}
// diskIsUsed returns true whether a disk is attached to any node
func (os *iaasClient) diskIsUsed(ctx context.Context, volumeID string) (bool, error) {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return false, err
}
diskUsed := volume.ServerId != nil && *volume.ServerId != ""
return diskUsed, nil
}
// diskIsAttached queries if a volume is attached to a compute instance
func (os *iaasClient) diskIsAttached(ctx context.Context, instanceID, volumeID string) (bool, error) {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return false, err
}
if volume.ServerId != nil && *volume.ServerId == instanceID {
return true, nil
}
return false, nil
}
func (os *iaasClient) GetVolume(ctx context.Context, volumeID string) (*iaas.Volume, error) {
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
vol, err := os.iaas.GetVolume(ctxWithHTTPResp, os.projectID, os.region, volumeID).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return nil, stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, err
}
return vol, nil
}
func (os *iaasClient) GetVolumesByName(ctx context.Context, volName string) ([]iaas.Volume, error) {
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
// TODO: Add API filter once available.
volumes, err := os.iaas.ListVolumes(ctxWithHTTPResp, os.projectID, os.region).Execute()
if err != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return nil, stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return nil, err
}
filterMap := map[string]string{"Name": volName}
filteredVolumes := filterVolumes(*volumes.Items, filterMap)
return filteredVolumes, nil
}
func (os *iaasClient) GetVolumeByName(ctx context.Context, name string) (*iaas.Volume, error) {
vols, err := os.GetVolumesByName(ctx, name)
if err != nil {
return nil, err
}
if len(vols) == 0 {
return nil, stackiterrors.ErrNotFound
}
if len(vols) > 1 {
return nil, fmt.Errorf("found %d volumes with name %q", len(vols), name)
}
return &vols[0], nil
}
func (os *iaasClient) WaitVolumeTargetStatus(ctx context.Context, volumeID string, tStatus []string) error {
backoff := wait.Backoff{
Duration: operationFinishInitDelay,
Factor: operationFinishFactor,
Steps: operationFinishSteps,
}
waitErr := wait.ExponentialBackoff(backoff, func() (bool, error) {
vol, err := os.GetVolume(ctx, volumeID)
if err != nil {
return false, err
}
if slices.Contains(tStatus, *vol.Status) {
return true, nil
}
for _, eState := range volumeErrorStates {
if *vol.Status == eState {
return false, fmt.Errorf("volume is in Error State : %s", ptr.Deref(vol.Status, ""))
}
}
return false, nil
})
if wait.Interrupted(waitErr) {
waitErr = fmt.Errorf("timeout on waiting for volume %s status to be in %v", volumeID, tStatus)
}
return waitErr
}
func (os *iaasClient) ExpandVolume(ctx context.Context, volumeID, volumeStatus string, newSize int64) error {
extendOpts := iaas.ResizeVolumePayload{Size: new(newSize)}
var httpResp *http.Response
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp)
switch volumeStatus {
case VolumeAttachedStatus, VolumeAvailableStatus:
resizeErr := os.iaas.ResizeVolume(ctxWithHTTPResp, os.projectID, os.region, volumeID).ResizeVolumePayload(extendOpts).Execute()
if resizeErr != nil {
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return stackiterrors.WrapErrorWithResponseID(resizeErr, reqID)
}
return resizeErr
}
return nil
default:
return fmt.Errorf("volume cannot be resized, when status is %s", volumeStatus)
}
}