-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathopenstack_volumes.go
More file actions
497 lines (419 loc) · 15.3 KB
/
openstack_volumes.go
File metadata and controls
497 lines (419 loc) · 15.3 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openstack
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/volumeattach"
"github.com/gophercloud/gophercloud/v2/pagination"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cloud-provider-openstack/pkg/metrics"
"k8s.io/cloud-provider-openstack/pkg/util"
cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
"k8s.io/klog/v2"
)
const (
VolumeAvailableStatus = "available"
VolumeInUseStatus = "in-use"
VolumeDetachingStatus = "detaching"
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 OpenStack Cinder CSI driver"
)
var volumeErrorStates = [...]string{"error", "error_extending", "error_deleting"}
// CreateVolume creates a volume of given size
func (os *OpenStack) CreateVolume(ctx context.Context, opts *volumes.CreateOpts, schedulerHints volumes.SchedulerHintOptsBuilder) (*volumes.Volume, error) {
blockstorageClient, err := openstack.NewBlockStorageV3(os.blockstorage.ProviderClient, os.epOpts)
if err != nil {
return nil, err
}
// creating volumes from backups and backups cross-az is available since 3.51 microversion
// https://docs.openstack.org/cinder/latest/contributor/api_microversion_history.html#id47
if !os.bsOpts.IgnoreVolumeMicroversion && opts.BackupID != "" {
blockstorageClient.Microversion = "3.51"
}
mc := metrics.NewMetricContext("volume", "create")
opts.Description = volumeDescription
vol, err := volumes.Create(ctx, blockstorageClient, opts, schedulerHints).Extract()
if mc.ObserveRequest(err) != nil {
if gophercloud.ResponseCodeIs(err, http.StatusRequestEntityTooLarge) {
return nil, errors.Join(err, cpoerrors.ErrQuotaExceeded)
}
return nil, err
}
return vol, nil
}
// ListVolumes list all the volumes
func (os *OpenStack) ListVolumes(ctx context.Context, limit int, startingToken string) ([]volumes.Volume, string, error) {
var nextPageToken string
var vols []volumes.Volume
mc := metrics.NewMetricContext("volume", "list")
if limit == 0 {
page, err := volumes.List(os.blockstorage, nil).AllPages(ctx)
if err != nil {
return nil, "", err
}
vols, err = volumes.ExtractVolumes(page)
return vols, "", mc.ObserveRequest(err)
}
opts := volumes.ListOpts{Limit: limit, Marker: startingToken}
err := volumes.List(os.blockstorage, opts).EachPage(ctx, func(_ context.Context, page pagination.Page) (bool, error) {
var err error
vols, err = volumes.ExtractVolumes(page)
if err != nil {
return false, err
}
nextPageURL, err := page.NextPageURL()
if err != nil {
return false, err
}
if nextPageURL != "" {
pageURL, err := url.Parse(nextPageURL)
if err != nil {
return false, err
}
nextPageToken = pageURL.Query().Get("marker")
}
return false, nil
})
return vols, nextPageToken, mc.ObserveRequest(err)
}
// GetVolumesByName is a wrapper around ListVolumes that creates a Name filter to act as a GetByName
// Returns a list of Volume references with the specified name
func (os *OpenStack) GetVolumesByName(ctx context.Context, n string) ([]volumes.Volume, error) {
// Init a local thread safe copy of the Cinder ServiceClient
blockstorageClient, err := openstack.NewBlockStorageV3(os.blockstorage.ProviderClient, os.epOpts)
if err != nil {
return nil, err
}
// cinder filtering in volumes list is available since 3.34 microversion
// https://docs.openstack.org/cinder/latest/contributor/api_microversion_history.html#id32
if !os.bsOpts.IgnoreVolumeMicroversion {
blockstorageClient.Microversion = "3.34"
}
opts := volumes.ListOpts{Name: n}
mc := metrics.NewMetricContext("volume", "list")
pages, err := volumes.List(blockstorageClient, opts).AllPages(ctx)
if mc.ObserveRequest(err) != nil {
return nil, err
}
vols, err := volumes.ExtractVolumes(pages)
if err != nil {
return nil, err
}
return vols, nil
}
// GetVolumeByName is a wrapper around GetVolumesByName that returns a single Volume reference
// with the specified name
func (os *OpenStack) GetVolumeByName(ctx context.Context, n string) (*volumes.Volume, error) {
vols, err := os.GetVolumesByName(ctx, n)
if err != nil {
return nil, err
}
if len(vols) == 0 {
return nil, cpoerrors.ErrNotFound
}
if len(vols) > 1 {
return nil, fmt.Errorf("found %d volumes with name %q", len(vols), n)
}
return &vols[0], nil
}
// DeleteVolume delete a volume
func (os *OpenStack) 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)
}
mc := metrics.NewMetricContext("volume", "delete")
err = volumes.Delete(ctx, os.blockstorage, volumeID, nil).ExtractErr()
return mc.ObserveRequest(err)
}
// GetVolume retrieves Volume by its ID.
func (os *OpenStack) GetVolume(ctx context.Context, volumeID string) (*volumes.Volume, error) {
mc := metrics.NewMetricContext("volume", "get")
vol, err := volumes.Get(ctx, os.blockstorage, volumeID).Extract()
if mc.ObserveRequest(err) != nil {
return nil, err
}
return vol, nil
}
// AttachVolume attaches given cinder volume to the compute
func (os *OpenStack) AttachVolume(ctx context.Context, instanceID, volumeID string) (string, error) {
computeServiceClient := os.compute
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return "", err
}
for _, att := range volume.Attachments {
if instanceID == att.ServerID {
klog.V(4).Infof("Disk %s is already attached to instance %s", volumeID, instanceID)
return volume.ID, nil
}
}
if len(volume.Attachments) > 0 && !volume.Multiattach {
return "", status.Errorf(codes.FailedPrecondition, "volume %s is already attached to node %s", volumeID, volume.Attachments[0].ServerID)
}
if volume.Multiattach {
// For multiattach volumes, supported compute api version is 2.60
// Init a local thread safe copy of the compute ServiceClient
computeServiceClient, err = openstack.NewComputeV2(os.compute.ProviderClient, os.epOpts)
if err != nil {
return "", err
}
computeServiceClient.Microversion = "2.60"
}
mc := metrics.NewMetricContext("volume", "attach")
_, err = volumeattach.Create(ctx, computeServiceClient, instanceID, &volumeattach.CreateOpts{
VolumeID: volume.ID,
}).Extract()
if mc.ObserveRequest(err) != nil {
return "", fmt.Errorf("failed to attach %s volume to %s compute: %v", volumeID, instanceID, err)
}
return volume.ID, nil
}
// WaitDiskAttached waits for attached
func (os *OpenStack) WaitDiskAttached(ctx context.Context, instanceID string, 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 && !cpoerrors.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 allotted time", volumeID)
}
return err
}
// WaitVolumeTargetStatus waits for volume to be in target state
func (os *OpenStack) 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
}
for _, t := range tStatus {
if vol.Status == t {
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
}
// DetachVolume detaches given cinder volume from the compute
func (os *OpenStack) 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 the volume is already in detaching state, we can return nil
if volume.Status == VolumeDetachingStatus {
klog.V(2).Infof("volume: %s is already in detaching state from compute %s ", volume.ID, instanceID)
return nil
}
if volume.Status != VolumeInUseStatus {
return fmt.Errorf("can not detach volume %s, its status is %s", volume.Name, volume.Status)
}
// Incase volume is of type multiattach, it could be attached to more than one instance
for _, att := range volume.Attachments {
if att.ServerID == instanceID {
mc := metrics.NewMetricContext("volume", "detach")
err = volumeattach.Delete(ctx, os.compute, instanceID, volume.ID).ExtractErr()
if mc.ObserveRequest(err) != nil {
return fmt.Errorf("failed to detach volume %s from compute %s : %v", volume.ID, instanceID, 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
}
// WaitDiskDetached waits for detached
func (os *OpenStack) WaitDiskDetached(ctx context.Context, instanceID string, 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 allotted time", volumeID)
}
return err
}
// GetAttachmentDiskPath gets device path of attached volume to the compute
func (os *OpenStack) GetAttachmentDiskPath(ctx context.Context, instanceID, volumeID string) (string, error) {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return "", err
}
if volume.Status != VolumeInUseStatus {
return "", fmt.Errorf("can not get device path of volume %s, its status is %s ", volume.Name, volume.Status)
}
if len(volume.Attachments) > 0 {
for _, att := range volume.Attachments {
if att.ServerID == instanceID {
return att.Device, nil
}
}
return "", fmt.Errorf("disk %q is not attached to compute: %q", volumeID, instanceID)
}
return "", fmt.Errorf("volume %s has no Attachments", volumeID)
}
// ExpandVolume expands the volume to new size
func (os *OpenStack) ExpandVolume(ctx context.Context, volumeID string, status string, newSize int) error {
extendOpts := volumes.ExtendSizeOpts{
NewSize: newSize,
}
switch status {
case VolumeInUseStatus:
// If the user has disabled the use of microversion to be compatible with
// older clouds, we should fail early
if os.bsOpts.IgnoreVolumeMicroversion {
return fmt.Errorf("volume online resize is not available with ignore-volume-microversion, requires microversion 3.42 or newer")
}
// Init a local thread safe copy of the Cinder ServiceClient
blockstorageClient, err := openstack.NewBlockStorageV3(os.blockstorage.ProviderClient, os.epOpts)
if err != nil {
return err
}
// cinder online resize is available since 3.42 microversion
// https://docs.openstack.org/cinder/latest/contributor/api_microversion_history.html#id40
blockstorageClient.Microversion = "3.42"
mc := metrics.NewMetricContext("volume", "expand")
return mc.ObserveRequest(volumes.ExtendSize(ctx, blockstorageClient, volumeID, extendOpts).ExtractErr())
case VolumeAvailableStatus:
mc := metrics.NewMetricContext("volume", "expand")
return mc.ObserveRequest(volumes.ExtendSize(ctx, os.blockstorage, volumeID, extendOpts).ExtractErr())
}
// cinder volume can not be expanded when volume status is not volumeInUseStatus or not volumeAvailableStatus
return fmt.Errorf("volume cannot be resized, when status is %s", status)
}
// GetMaxVolLimit returns max vol limit
func (os *OpenStack) GetMaxVolLimit() int64 {
if os.bsOpts.NodeVolumeAttachLimit > 0 && os.bsOpts.NodeVolumeAttachLimit <= 256 {
return os.bsOpts.NodeVolumeAttachLimit
}
return defaultMaxVolAttachLimit
}
// diskIsAttached queries if a volume is attached to a compute instance
func (os *OpenStack) diskIsAttached(ctx context.Context, instanceID, volumeID string) (bool, error) {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return false, err
}
for _, att := range volume.Attachments {
if att.ServerID == instanceID {
return true, nil
}
}
return false, nil
}
// diskIsUsed returns true a disk is attached to any node.
func (os *OpenStack) diskIsUsed(ctx context.Context, volumeID string) (bool, error) {
volume, err := os.GetVolume(ctx, volumeID)
if err != nil {
return false, err
}
if len(volume.Attachments) > 0 {
return volume.Attachments[0].ServerID != "", nil
}
return false, nil
}
// GetBlockStorageOpts returns OpenStack block storage options
func (os *OpenStack) GetBlockStorageOpts() BlockStorageOpts {
return os.bsOpts
}
// ResolveVolumeListToUUIDs resolves a list of volume names or UUIDs to a
// string of UUIDs
func (os *OpenStack) ResolveVolumeListToUUIDs(ctx context.Context, affinityList string) (string, error) {
list := util.SplitTrim(affinityList, ',')
if len(list) == 0 {
return "", nil
}
affinityUUIDs := make([]string, 0, len(list))
for _, v := range list {
var volume *volumes.Volume
var err error
if id, e := util.UUID(v); e == nil {
// First try to get volume by ID
volume, err = os.GetVolume(ctx, id)
if err != nil && cpoerrors.IsNotFound(err) {
volume, err = os.GetVolumeByName(ctx, v)
}
} else {
// If not a UUID, try to get volume by name
volume, err = os.GetVolumeByName(ctx, v)
}
if err != nil {
if cpoerrors.IsNotFound(err) {
return "", status.Errorf(codes.NotFound, "referenced volume %s not found: %v", v, err)
}
return "", status.Errorf(codes.Internal, "failed to resolve volume %s: %v", v, err)
}
affinityUUIDs = append(affinityUUIDs, volume.ID)
}
return strings.Join(affinityUUIDs, ","), nil
}