/
vcontainer.go
483 lines (387 loc) · 13.4 KB
/
vcontainer.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
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
package vcontainer
import (
"fmt"
"github.com/vngcloud/vcontainer-sdk/client"
"github.com/vngcloud/vcontainer-sdk/vcontainer/pagination"
lSnap "github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v1/snapshot"
snapV1Obj "github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v1/snapshot/obj"
lVolAct "github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v2/extensions/volume_actions"
lSnapV2 "github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v2/snapshot"
lSnapV2Obj "github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v2/snapshot/obj"
lVol "github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v2/volume"
"github.com/vngcloud/vcontainer-sdk/vcontainer/services/blockstorage/v2/volume/obj"
lVolAtch "github.com/vngcloud/vcontainer-sdk/vcontainer/services/compute/v2/extensions/volume_attach"
lServer "github.com/vngcloud/vcontainer-sdk/vcontainer/services/compute/v2/server"
serverObj "github.com/vngcloud/vcontainer-sdk/vcontainer/services/compute/v2/server/obj"
lPortal "github.com/vngcloud/vcontainer-sdk/vcontainer/services/portal/v1"
"github.com/vngcloud/vcontainer-storage-interface/csi/metrics"
"github.com/vngcloud/vcontainer-storage-interface/csi/utils/metadata"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"strings"
)
type (
vContainer struct {
compute *client.ServiceClient
blockstorage *client.ServiceClient
portal *client.ServiceClient
vBackUp *client.ServiceClient
bsOpts BlockStorageOpts
metadataOpts metadata.Opts
extraInfo *ExtraInfo
}
ExtraInfo struct {
ProjectID string
UserID int64
}
)
func (s *vContainer) GetMetadataOpts() metadata.Opts {
klog.Infof("GetMetadataOpts; metadataOps is %+v", s.metadataOpts)
return s.metadataOpts
}
func (s *vContainer) SetupPortalInfo(metadata metadata.IMetadata) error {
projectID, err := metadata.GetProjectID()
if err != nil {
return err
}
klog.Infof("SetupPortalInfo; projectID is %s", projectID)
portalInfo, err := lPortal.Get(s.portal, projectID)
if err != nil {
return err
}
if portalInfo == nil {
return fmt.Errorf("can not get portal information")
}
s.extraInfo = &ExtraInfo{
ProjectID: portalInfo.ProjectID,
UserID: portalInfo.UserID,
}
return nil
}
func (s *vContainer) ListVolumes(limit int, startingToken string) ([]*obj.Volume, string, error) {
klog.Infof("ListVolumes; called with limit %d and startingToken %s", limit, startingToken)
var nextPage string
var vols []*obj.Volume
//page, size := standardPaging(limit, startingToken)
//opts := lVol.NewListOpts(s.extraInfo.ProjectID, "", page, size)
//mc := metrics.NewMetricContext("volume", "list")
//err := lVol.List(s.blockstorage, opts).EachPage(func(page pagination.IPage) (bool, error) {
// tfPage := page.GetBody().(*lVol.ListResponse)
// nextPage = tfPage.NextPage()
// vols = append(vols, tfPage.ToListVolumeObjects()...)
// return true, nil
//})
mc := metrics.NewMetricContext("volume", "list-all")
vols, err := lVol.ListAll(s.blockstorage, lVol.NewListAllOpts(s.extraInfo.ProjectID))
if mc.ObserveRequest(err) != nil {
return nil, nextPage, err
}
return vols, nextPage, nil
}
func (s *vContainer) GetVolumesByName(name string) ([]*obj.Volume, error) {
klog.Infof("GetVolumesByName; called with name %s", name)
var vols []*obj.Volume
opts := lVol.NewListOpts(s.extraInfo.ProjectID, name, 0, 0)
mc := metrics.NewMetricContext("volume", "list")
err := lVol.List(s.blockstorage, opts).EachPage(func(page pagination.IPage) (bool, error) {
vols = append(vols, page.GetBody().(*lVol.ListResponse).ToListVolumeObjects()...)
return true, nil
})
if mc.ObserveRequest(err) != nil {
return nil, err
}
return vols, nil
}
func (s *vContainer) CreateVolume(name string, size uint64, vtype, availability string, snapshotID string, sourcevolID string, tags *map[string]string) (*obj.Volume, error) {
klog.Infof("CreateVolume; called with name %s, size %d, vtype %s, availability %s, snapshotID %s, sourcevolID %s, tags %+v", name, size, vtype, availability, snapshotID, sourcevolID, tags)
opts := lVol.NewCreateOpts(
s.extraInfo.ProjectID,
&lVol.CreateOpts{
Name: name,
Size: size,
VolumeTypeId: vtype,
CreatedFrom: "NEW",
PersistentVolume: true,
MultiAttach: false,
})
mc := metrics.NewMetricContext("volume", "create")
vol, err := lVol.Create(s.blockstorage, opts)
if mc.ObserveRequest(err) != nil {
return nil, err
}
return vol, nil
}
func (s *vContainer) DeleteVolume(volID string) error {
used, err := s.diskIsUsed(volID)
if err != nil {
return err
}
if used {
return fmt.Errorf("cannot delete the volume %q, it's still attached to a node", volID)
}
mc := metrics.NewMetricContext("volume", "delete")
err = lVol.Delete(s.blockstorage, lVol.NewDeleteOpts(s.extraInfo.ProjectID, volID))
return mc.ObserveRequest(err)
}
func (s *vContainer) GetVolume(volumeID string) (*obj.Volume, error) {
opts := lVol.NewGetOpts(s.extraInfo.ProjectID, volumeID)
mc := metrics.NewMetricContext("volume", "get")
result, err := lVol.Get(s.blockstorage, opts)
if mc.ObserveRequest(err) != nil {
return nil, err
}
return result, nil
}
func (s *vContainer) GetInstanceByID(instanceID string) (*serverObj.Server, error) {
opts := lServer.GetServerOpts{InstanceID: instanceID, ProjectID: s.extraInfo.ProjectID}
mc := metrics.NewMetricContext("server", "get")
result, err := lServer.Get(s.compute, opts)
if mc.ObserveRequest(err) != nil {
return nil, err
}
return result, nil
}
func (s *vContainer) AttachVolume(instanceID, volumeID string) (string, error) {
vol, err := s.GetVolume(volumeID)
if err != nil {
return "", err
}
if vol == nil {
return "", fmt.Errorf("volume %s not found", volumeID)
}
if vol.VmId != nil && *vol.VmId != "-1" {
return "", fmt.Errorf("volume %s already attached to instance %s", volumeID, *vol.VmId)
}
mc := metrics.NewMetricContext("volume", "attach")
opts := lVolAtch.NewCreateOpts(s.extraInfo.ProjectID, instanceID, volumeID)
_, err = lVolAtch.Create(s.compute, opts)
if mc.ObserveRequest(err) != nil {
return "", err
}
return vol.VolumeId, nil
}
func (s *vContainer) GetAttachmentDiskPath(instanceID, volumeID string) (string, error) {
volume, err := s.GetVolume(volumeID)
if err != nil {
return "", err
}
if volume.Status != VolumeInUseStatus {
return "", fmt.Errorf("volume %s not is in use", volumeID)
}
//if volume.VmId == nil || *volume.VmId != instanceID {
// return "", fmt.Errorf("volume %s is not attached to instance %s", volumeID, instanceID)
//}
return "", nil
}
func (s *vContainer) WaitDiskAttached(instanceID string, volumeID string) error {
backoff := wait.Backoff{
Duration: diskAttachInitDelay,
Factor: diskAttachFactor,
Steps: diskAttachSteps,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
attached, err := s.diskIsAttached(instanceID, volumeID)
if err != nil {
return false, err
}
return attached, nil
})
if wait.Interrupted(err) {
err = fmt.Errorf("interrupted while waiting for volume %s to be attached to instance within the alloted time %s", volumeID, instanceID)
}
return err
}
func (s *vContainer) DetachVolume(instanceID, volumeID string) error {
volume, err := s.GetVolume(volumeID)
if err != nil {
return err
}
if volume == nil {
return fmt.Errorf("volume %s not found", volumeID)
}
if volume.PersistentVolume != true {
return fmt.Errorf("volume %s is not persistent volume", volumeID)
}
mc := metrics.NewMetricContext("volume", "detach")
_, err = lVolAtch.Delete(s.compute, lVolAtch.NewDeleteOpts(s.extraInfo.ProjectID, instanceID, volumeID))
if mc.ObserveRequest(err) != nil {
return err
}
// Disk has no attachments or not attached to the provided compute
return nil
}
func (s *vContainer) WaitDiskDetached(instanceID string, volumeID string) error {
backoff := wait.Backoff{
Duration: diskDetachInitDelay,
Factor: diskDetachFactor,
Steps: diskDetachSteps,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
attached, err := s.diskIsAttached(instanceID, volumeID)
if err != nil {
return false, err
}
return !attached, nil
})
if wait.Interrupted(err) {
err = fmt.Errorf("interrupted while waiting for volume %s to be detached from instance within the alloted time %s", volumeID, instanceID)
}
return err
}
func (s *vContainer) ExpandVolume(volumeTypeID, volumeID string, newSize uint64) error {
opts := lVolAct.NewResizeOpts(s.extraInfo.ProjectID, volumeTypeID, volumeID, newSize)
mc := metrics.NewMetricContext("volume", "extend")
_, err := lVolAct.Resize(s.blockstorage, opts)
if mc.ObserveRequest(err) != nil {
return err
}
return nil
}
func (s *vContainer) WaitVolumeTargetStatus(volumeID string, tStatus []string) error {
backoff := wait.Backoff{
Duration: operationFinishInitDelay,
Factor: operationFinishFactor,
Steps: operationFinishSteps,
}
waitErr := wait.ExponentialBackoff(backoff, func() (bool, error) {
vol, err := s.GetVolume(volumeID)
if err != nil {
return false, err
}
if vol == nil {
return false, fmt.Errorf("volume %s not found", volumeID)
}
for _, t := range tStatus {
if vol.Status == t {
return true, nil
}
}
return false, fmt.Errorf("WaitVolumeTargetStatus; volume %s status is %s", volumeID, vol.Status)
})
if wait.Interrupted(waitErr) {
waitErr = fmt.Errorf("timeout on waiting for volume %s status to be in %v", volumeID, tStatus)
}
return waitErr
}
func (s *vContainer) GetMaxVolLimit() int64 {
if s.bsOpts.NodeVolumeAttachLimit > 0 && s.bsOpts.NodeVolumeAttachLimit <= 256 {
return s.bsOpts.NodeVolumeAttachLimit
}
return defaultMaxVolAttachLimit
}
func (s *vContainer) CreateSnapshot(name, volID string) (*lSnapV2Obj.Snapshot, error) {
opts := lSnapV2.NewCreateOpts(s.extraInfo.ProjectID, volID,
fmt.Sprintf("vContainer volume snapshot of volume %s", volID), true, name, 7)
mc := metrics.NewMetricContext("snapshot", "create")
snap, err := lSnapV2.Create(s.blockstorage, opts)
if mc.ObserveRequest(err) != nil {
return nil, err
}
return snap, nil
}
func (s *vContainer) WaitSnapshotReady(snapshotID string) error {
backoff := wait.Backoff{
Duration: snapReadyDuration,
Factor: snapReadyFactor,
Steps: snapReadySteps,
}
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
ready, err := s.snapshotIsReady(snapshotID)
if err != nil {
return false, err
}
return ready, nil
})
if wait.Interrupted(err) {
err = fmt.Errorf("Timeout, Snapshot %s is still not Ready %v", snapshotID, err)
}
return err
}
func (s *vContainer) ListSnapshots(page string, size int, volumeID, status, name string) ([]*snapV1Obj.Snapshot, string, error) {
var nextPage string
var lstSnapshot []*snapV1Obj.Snapshot
p_, s_ := standardPaging(size, page)
opts := lSnap.NewListOpts(p_, s_, volumeID, status, name)
mc := metrics.NewMetricContext("snapshot", "list")
fmt.Printf("The url is: %s\n", s.vBackUp.Endpoint)
err := lSnap.List(s.vBackUp, opts).EachPage(func(page pagination.IPage) (bool, error) {
tfPage := page.GetBody().(*lSnap.ListResponse)
nextPage = tfPage.NextPage()
lstSnapshot = append(lstSnapshot, tfPage.ToListSnapshotObjects()...)
return true, nil
})
if mc.ObserveRequest(err) != nil {
return nil, nextPage, err
}
return lstSnapshot, nextPage, nil
}
func (s *vContainer) GetSnapshotByID(snapshotID string) (*snapV1Obj.Snapshot, error) {
var snapshot *snapV1Obj.Snapshot
opts := lSnap.NewListOpts(1, 10, "", "", "")
mc := metrics.NewMetricContext("snapshot", "get")
err := lSnap.List(s.blockstorage, opts).EachPage(func(page pagination.IPage) (bool, error) {
tfPage := page.GetBody().(*lSnap.ListResponse)
for _, snap := range tfPage.ToListSnapshotObjects() {
if snap.ID == snapshotID {
snapshot = snap
return false, nil
}
}
return true, nil
})
if mc.ObserveRequest(err) != nil {
return nil, err
}
return snapshot, nil
}
func (s *vContainer) DeleteSnapshot(volumeID, snapshotID string) error {
opts := lSnapV2.NewDeleteOpts(s.extraInfo.ProjectID, volumeID, snapshotID)
mc := metrics.NewMetricContext("snapshot", "delete")
err := lSnapV2.Delete(s.blockstorage, opts)
if mc.ObserveRequest(err) != nil {
return err
}
return nil
}
func (s *vContainer) GetBlockStorageOpts() BlockStorageOpts {
return s.bsOpts
}
func (s *vContainer) GetMappingVolume(volumeID string) (string, error) {
opts := lVolAct.NewMappingOpts(s.extraInfo.ProjectID, volumeID)
mc := metrics.NewMetricContext("volume-mapping", "get")
result, err := lVolAct.GetMappingVolume(s.blockstorage, opts)
if mc.ObserveRequest(err) != nil {
return "", err
}
if result.UUID == "" {
return "", fmt.Errorf("volume %s not found", volumeID)
}
return result.UUID, nil
}
func (s *vContainer) diskIsUsed(volumeID string) (bool, error) {
vol, err := s.GetVolume(volumeID)
if err != nil || vol == nil {
return false, err
}
if vol.VmId != nil {
return true, nil
}
return false, nil
}
func (s *vContainer) diskIsAttached(instanceID string, volumeID string) (bool, error) {
vol, err := s.GetVolume(volumeID)
if err != nil || vol == nil {
return false, err
}
if strings.ToUpper(vol.Status) != VolumeInUseStatus {
return false, nil
}
return true, nil
}
func (s *vContainer) snapshotIsReady(snapshotID string) (bool, error) {
snap, err := s.GetSnapshotByID(snapshotID)
if err != nil {
return false, err
}
return strings.ToLower(snap.Status) == snapshotReadyStatus, nil
}