/
bv_controller.go
1345 lines (1192 loc) · 62.1 KB
/
bv_controller.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Oracle and/or its affiliates. All rights reserved.
//
// 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 driver
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/container-storage-interface/spec/lib/go/csi"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
kubeAPI "k8s.io/api/core/v1"
k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/oracle/oci-cloud-controller-manager/pkg/cloudprovider/providers/oci/config"
csi_util "github.com/oracle/oci-cloud-controller-manager/pkg/csi-util"
"github.com/oracle/oci-cloud-controller-manager/pkg/metrics"
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
"github.com/oracle/oci-cloud-controller-manager/pkg/util"
"github.com/oracle/oci-cloud-controller-manager/pkg/util/disk"
"github.com/oracle/oci-go-sdk/v65/core"
)
const (
// Prefix to apply to the name of a created volume. This should be the same as the option '--volume-name-prefix' of csi-provisioner.
pvcPrefix = "csi"
csiDriver = "csi"
fsTypeKey = "csi.storage.k8s.io/fstype"
fsTypeKeyDeprecated = "fstype"
timeout = time.Minute * 3
kmsKey = "kms-key-id"
attachmentType = "attachment-type"
attachmentTypeISCSI = "iscsi"
attachmentTypeParavirtualized = "paravirtualized"
initialFreeformTagsOverride = "oci.oraclecloud.com/initial-freeform-tags-override"
initialDefinedTagsOverride = "oci.oraclecloud.com/initial-defined-tags-override"
backupType = "backupType"
backupTypeFull = "full"
backupTypeIncremental = "incremental"
backupDefinedTags = "oci.oraclecloud.com/defined-tags"
backupFreeformTags = "oci.oraclecloud.com/freeform-tags"
newBackupAvailableTimeout = 45 * time.Second
needResize = "needResize"
newSize = "newSize"
multipathEnabled = "multipathEnabled"
multipathDevices = "multipathDevices"
//device is the consistent device path that would be used for paravirtualized attachment
device = "device"
)
var (
// OCI currently only support a single node to be attached to a single node
// in read/write mode. This corresponds to `accessModes.ReadWriteOnce` in a
// PVC resource on Kubernetes
supportedAccessMode = &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
}
)
// VolumeParameters holds configuration
type VolumeParameters struct {
//kmsKey is the KMS key that would be used as CMEK key for BV attachment
diskEncryptionKey string
attachmentParameter map[string]string
// freeform tags to add for BVs
freeformTags map[string]string
// defined tags to add for BVs
definedTags map[string]map[string]interface{}
//volume performance units per gb describes the block volume performance level
vpusPerGB int64
}
// VolumeAttachmentOption holds config for attachments
type VolumeAttachmentOption struct {
//whether the attachment type is paravirtualized
useParavirtualizedAttachment bool
//whether to encrypt the compute to BV attachment as in-transit encryption.
enableInTransitEncryption bool
}
type SnapshotParameters struct {
//backupType is the parameter which is used to decide if the backup created will be FULL or INCREMENTAL
backupType core.CreateVolumeBackupDetailsTypeEnum
// freeform tags to add for backups
freeformTags map[string]string
// defined tags to add for backups
definedTags map[string]map[string]interface{}
}
func extractVolumeParameters(log *zap.SugaredLogger, parameters map[string]string) (VolumeParameters, error) {
p := VolumeParameters{
diskEncryptionKey: "",
attachmentParameter: make(map[string]string),
vpusPerGB: 10, // default value is 10 -> Balanced
}
for k, v := range parameters {
switch k {
case fsTypeKeyDeprecated:
log.Warnf("%s is deprecated, please use %s instead", fsTypeKeyDeprecated, fsTypeKey)
case kmsKey:
if v != "" {
p.diskEncryptionKey = v
}
case attachmentType:
attachmentTypeLower := strings.ToLower(v)
if attachmentTypeLower != attachmentTypeISCSI && attachmentTypeLower != attachmentTypeParavirtualized {
return p, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid attachment-type: %s provided "+
"for storageclass. supported attachment-types are %s and %s", v, attachmentTypeISCSI, attachmentTypeParavirtualized))
}
p.attachmentParameter[attachmentType] = attachmentTypeLower
case initialFreeformTagsOverride:
if v == "" {
continue
}
freeformTags := make(map[string]string)
err := json.Unmarshal([]byte(v), &freeformTags)
if err != nil {
return p, status.Errorf(codes.InvalidArgument, "failed to parse freeform tags provided "+
"for storageclass. please check the parameters block on the storage class")
}
p.freeformTags = freeformTags
case initialDefinedTagsOverride:
if v == "" {
continue
}
definedTags := make(map[string]map[string]interface{})
err := json.Unmarshal([]byte(v), &definedTags)
if err != nil {
return p, status.Errorf(codes.InvalidArgument, "failed to parse defined tags provided "+
"for storageclass. please check the parameters block on the storage class")
}
p.definedTags = definedTags
case csi_util.VpusPerGB:
vpusPerGB, err := csi_util.ExtractBlockVolumePerformanceLevel(v)
if err != nil {
return p, status.Error(codes.InvalidArgument, err.Error())
}
p.vpusPerGB = vpusPerGB
}
}
return p, nil
}
func extractSnapshotParameters(parameters map[string]string) (SnapshotParameters, error) {
p := SnapshotParameters{
backupType: core.CreateVolumeBackupDetailsTypeIncremental, //Default backupType is incremental
}
for k, v := range parameters {
switch k {
case backupType:
backupTypeLower := strings.ToLower(v)
if backupTypeLower == backupTypeIncremental {
p.backupType = core.CreateVolumeBackupDetailsTypeIncremental
} else if backupTypeLower == backupTypeFull {
p.backupType = core.CreateVolumeBackupDetailsTypeFull
} else {
return p, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid backupType: %s provided "+
"in volumesnapshotclass. supported backupType are %s and %s", v, backupTypeIncremental, backupTypeFull))
}
case backupFreeformTags:
if v == "" {
continue
}
freeformTags := make(map[string]string)
err := json.Unmarshal([]byte(v), &freeformTags)
if err != nil {
return p, status.Errorf(codes.InvalidArgument, "failed to parse freeform tags provided "+
"in volumesnapshotclass. please check the parameters block on the volume snapshot class")
}
p.freeformTags = freeformTags
case backupDefinedTags:
if v == "" {
continue
}
definedTags := make(map[string]map[string]interface{})
err := json.Unmarshal([]byte(v), &definedTags)
if err != nil {
return p, status.Errorf(codes.InvalidArgument, "failed to parse defined tags provided "+
"in volumesnapshotclass. please check the parameters block on the volume snapshot class")
}
p.definedTags = definedTags
}
}
return p, nil
}
// CreateVolume creates a new volume from the given request. The function is
// idempotent.
func (d *BlockVolumeControllerDriver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
startTime := time.Now()
log := d.logger.With("volumeName", req.Name, "csiOperation", "create")
var errorType string
var metricDimension string
volumeContext := map[string]string{
needResize: "false",
newSize: "",
}
if req.Name == "" {
return nil, status.Error(codes.InvalidArgument, "CreateVolume Name must be provided")
}
if req.VolumeCapabilities == nil || len(req.VolumeCapabilities) == 0 {
return nil, status.Error(codes.InvalidArgument, "VolumeCapabilities must be provided in CreateVolumeRequest")
}
if err := d.validateCapabilities(req.VolumeCapabilities); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
size, err := csi_util.ExtractStorage(req.CapacityRange)
if err != nil {
return nil, status.Errorf(codes.OutOfRange, "invalid capacity range: %v", err)
}
availableDomainShortName := ""
volumeName := req.Name
dimensionsMap := make(map[string]string)
volumeParams, err := extractVolumeParameters(log, req.GetParameters())
if err != nil {
log.With(zap.Error(err)).Error("Failed to parse storageclass parameters.")
metricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVProvision, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.InvalidArgument, "failed to parse storageclass parameters %v", err)
}
dimensionsMap[metrics.ResourceOCIDDimension] = volumeName
dimensionsMap[metrics.VolumeVpusPerGBDimension] = strconv.Itoa(int(volumeParams.vpusPerGB))
srcSnapshotId := ""
srcVolumeId := ""
volumeContentSource := req.GetVolumeContentSource()
if volumeContentSource != nil {
_, isVolumeContentSource_Snapshot := volumeContentSource.GetType().(*csi.VolumeContentSource_Snapshot)
_, isVolumeContentSource_Volume := volumeContentSource.GetType().(*csi.VolumeContentSource_Volume)
if !isVolumeContentSource_Snapshot && !isVolumeContentSource_Volume {
log.Error("Unsupported volumeContentSource")
return nil, status.Error(codes.InvalidArgument, "Unsupported volumeContentSource")
}
if isVolumeContentSource_Snapshot {
srcSnapshot := volumeContentSource.GetSnapshot()
if srcSnapshot == nil {
log.With("volumeSourceType", "snapshot").Error("Error fetching snapshot from the volumeContentSource")
return nil, status.Error(codes.InvalidArgument, "Error fetching snapshot from the volumeContentSource")
}
id := srcSnapshot.GetSnapshotId()
volumeBackup, err := d.client.BlockStorage().GetVolumeBackup(ctx, id)
if err != nil {
if k8sapierrors.IsNotFound(err) {
log.With("service", "blockstorage", "verb", "get", "resource", "volumeBackup", "statusCode", util.GetHttpStatusCode(err)).Errorf("Failed to get snapshot with ID %v", id)
return nil, status.Errorf(codes.NotFound, "Failed to get snapshot with ID %v", id)
}
log.With("service", "blockstorage", "verb", "get", "resource", "volumeBackup", "statusCode", util.GetHttpStatusCode(err)).Errorf("Failed to fetch snapshot with ID %v with error %v", id, err)
return nil, status.Errorf(codes.Internal, "Failed to fetch snapshot with ID %v with error %v", id, err)
}
volumeBackupSize := *volumeBackup.SizeInMBs * client.MiB
if volumeBackupSize < size {
volumeContext[needResize] = "true"
volumeContext[newSize] = strconv.FormatInt(size, 10)
}
srcSnapshotId = id
} else {
srcVolume := volumeContentSource.GetVolume()
if srcVolume == nil {
log.With("volumeSourceType", "pvc").Error("Error fetching volume from the volumeContentSource")
return nil, status.Error(codes.InvalidArgument, "Error fetching volume from the volumeContentSource")
}
id := srcVolume.GetVolumeId()
srcBlockVolume, err := d.client.BlockStorage().GetVolume(ctx, id)
if err != nil {
if client.IsNotFound(err) {
log.With("service", "blockstorage", "verb", "get", "resource", "blockVolume", "statusCode", util.GetHttpStatusCode(err)).Errorf("Failed to get volume with ID %v", id)
return nil, status.Errorf(codes.NotFound, "Failed to get volume with ID %v", id)
}
log.With("service", "blockstorage", "verb", "get", "resource", "blockVolume", "statusCode", util.GetHttpStatusCode(err)).Errorf("Failed to fetch volume with ID %v with error %v", id, err)
return nil, status.Errorf(codes.Internal, "Failed to fetch volume with ID %v with error %v", id, err)
}
availableDomainShortName = *srcBlockVolume.AvailabilityDomain
log.With("AD", availableDomainShortName).Info("Using availability domain of source volume to provision clone volume.")
srcBlockVolumeSize := *srcBlockVolume.SizeInMBs * client.MiB
if srcBlockVolumeSize < size {
volumeContext["needResize"] = "true"
volumeContext["newSize"] = strconv.FormatInt(size, 10)
}
srcVolumeId = id
}
}
if req.AccessibilityRequirements != nil && req.AccessibilityRequirements.Preferred != nil && availableDomainShortName == "" {
for _, t := range req.AccessibilityRequirements.Preferred {
availableDomainShortName, _ = t.Segments[kubeAPI.LabelZoneFailureDomain]
log.With("AD", availableDomainShortName).Info("Using preferred topology for AD.")
if len(availableDomainShortName) > 0 {
break
}
}
}
if availableDomainShortName == "" {
if req.AccessibilityRequirements != nil && req.AccessibilityRequirements.Requisite != nil {
for _, t := range req.AccessibilityRequirements.Requisite {
availableDomainShortName, _ = t.Segments[kubeAPI.LabelZoneFailureDomain]
log.With("AD", availableDomainShortName).Info("Using requisite topology for AD.")
if len(availableDomainShortName) > 0 {
break
}
}
}
}
metric := metrics.PVProvision
metricType := util.CSIStorageType
if srcSnapshotId != "" {
metric = metrics.BlockSnapshotRestore
metricType = util.CSIStorageType
}
if srcVolumeId != "" {
metric = metrics.PVClone
metricType = util.CSIStorageType
}
if availableDomainShortName == "" {
metricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
log.Error("Available domain short name is not found")
return nil, status.Errorf(codes.InvalidArgument, "%s is required in PreferredTopologies or allowedTopologies", kubeAPI.LabelZoneFailureDomain)
}
//make sure this method is idempotent by checking existence of volume with same name.
volumes, err := d.client.BlockStorage().GetVolumesByName(ctx, volumeName, d.config.CompartmentID)
if err != nil {
log.With("service", "blockstorage", "verb", "get", "resource", "volume", "statusCode", util.GetHttpStatusCode(err)).
With(zap.Error(err)).Error("Failed to find existence of volume.")
errorType = util.GetError(err)
metricDimension = util.GetMetricDimensionForComponent(errorType, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "failed to check existence of volume %v", err)
}
if len(volumes) > 1 {
log.Error("Duplicate volume exists")
metricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
return nil, fmt.Errorf("duplicate volume %q exists", volumeName)
}
provisionedVolume := core.Volume{}
if len(volumes) > 0 {
//Volume already exists so checking state of the volume and returning the same.
log.Info("Volume already created!")
//Assigning existing volume
provisionedVolume = volumes[0]
} else {
// Creating new volume
ad, err := d.client.Identity().GetAvailabilityDomainByName(ctx, d.config.CompartmentID, availableDomainShortName)
if err != nil {
log.With("Compartment Id", d.config.CompartmentID, "service", "identity", "verb", "get", "resource", "AD", "statusCode", util.GetHttpStatusCode(err)).
With(zap.Error(err)).Error("Failed to get available domain.")
errorType = util.GetError(err)
metricDimension = util.GetMetricDimensionForComponent(errorType, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.InvalidArgument, "invalid available domain: %s or compartment ID: %s", availableDomainShortName, d.config.CompartmentID)
}
// use initial tags for all BVs
bvTags := &config.TagConfig{}
if d.config.Tags != nil && d.config.Tags.BlockVolume != nil {
bvTags = d.config.Tags.BlockVolume
}
// use storage class level tags if provided
scTags := &config.TagConfig{
FreeformTags: volumeParams.freeformTags,
DefinedTags: volumeParams.definedTags,
}
// storage class tags overwrite initial BV Tags
if scTags.FreeformTags != nil || scTags.DefinedTags != nil {
bvTags = scTags
}
provisionedVolume, err = provision(ctx, log, d.client, volumeName, size, *ad.Name, d.config.CompartmentID, srcSnapshotId, srcVolumeId,
volumeParams.diskEncryptionKey, volumeParams.vpusPerGB, bvTags)
if err != nil {
log.With("Ad name", *ad.Name, "Compartment Id", d.config.CompartmentID).With(zap.Error(err)).Error("New volume creation failed.")
errorType = util.GetError(err)
metricDimension = util.GetMetricDimensionForComponent(errorType, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "New volume creation failed %v", err.Error())
}
}
log.Info("Waiting for volume to become available.")
if srcVolumeId != "" {
_, err = d.client.BlockStorage().AwaitVolumeCloneAvailableOrTimeout(ctx, *provisionedVolume.Id)
} else {
_, err = d.client.BlockStorage().AwaitVolumeAvailableORTimeout(ctx, *provisionedVolume.Id)
}
if err != nil {
log.With("service", "blockstorage", "verb", "get", "resource", "volume", "statusCode", util.GetHttpStatusCode(err)).
With("volumeName", volumeName).Error("Create volume failed with time out")
errorType = util.GetError(err)
metricDimension = util.GetMetricDimensionForComponent(errorType, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.DeadlineExceeded, "Create volume failed with time out %v", err.Error())
}
volumeOCID := volumeName
if provisionedVolume.Id != nil {
volumeOCID = *provisionedVolume.Id
}
log.With("volumeID", volumeOCID).Info("Volume is created.")
metricDimension = util.GetMetricDimensionForComponent(util.Success, metricType)
dimensionsMap[metrics.ComponentDimension] = metricDimension
dimensionsMap[metrics.ResourceOCIDDimension] = volumeOCID
metrics.SendMetricData(d.metricPusher, metric, time.Since(startTime).Seconds(), dimensionsMap)
volumeContext[attachmentType] = volumeParams.attachmentParameter[attachmentType]
volumeContext[csi_util.VpusPerGB] = strconv.FormatInt(volumeParams.vpusPerGB, 10)
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: *provisionedVolume.Id,
CapacityBytes: *provisionedVolume.SizeInMBs * client.MiB,
AccessibleTopology: []*csi.Topology{
{
Segments: map[string]string{
kubeAPI.LabelZoneFailureDomain: d.util.GetAvailableDomainInNodeLabel(*provisionedVolume.AvailabilityDomain),
},
},
},
VolumeContext: volumeContext,
ContentSource: volumeContentSource,
},
}, nil
}
// DeleteVolume deletes the given volume. The function is idempotent.
func (d *BlockVolumeControllerDriver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
startTime := time.Now()
log := d.logger.With("volumeID", req.VolumeId, "csiOperation", "delete")
var errorType string
var csiMetricDimension string
dimensionsMap := make(map[string]string)
dimensionsMap[metrics.ResourceOCIDDimension] = req.VolumeId
if req.VolumeId == "" {
log.Info("Unable to get Volume Id")
csiMetricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDelete, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Error(codes.InvalidArgument, "DeleteVolume Volume ID must be provided")
}
log.Info("Deleting Volume")
err := d.client.BlockStorage().DeleteVolume(ctx, req.VolumeId)
if err != nil {
if !client.IsNotFound(err) {
log.With("service", "blockstorage", "verb", "delete", "resource", "volume", "statusCode", util.GetHttpStatusCode(err)).With(zap.Error(err)).Error("Failed to delete volume.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDelete, time.Since(startTime).Seconds(), dimensionsMap)
return nil, fmt.Errorf("failed to delete volume, volumeId: %s, error: %v", req.VolumeId, err)
}
log.With("service", "blockstorage", "verb", "delete", "resource", "volume", "statusCode", util.GetHttpStatusCode(err)).With(zap.Error(err)).
Error("Unable to find volume to delete. Volume is possibly already deleted. No Delete Operation required.")
} else {
log.Info("Volume is deleted.")
}
csiMetricDimension = util.GetMetricDimensionForComponent(util.Success, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDelete, time.Since(startTime).Seconds(), dimensionsMap)
return &csi.DeleteVolumeResponse{}, nil
}
// ControllerPublishVolume attaches the given volume to the node
func (d *BlockVolumeControllerDriver) ControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
startTime := time.Now()
var errorType string
var csiMetricDimension string
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID must be provided")
}
if req.NodeId == "" {
return nil, status.Error(codes.InvalidArgument, "Node ID must be provided")
}
if req.VolumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capability must be provided")
}
dimensionsMap := make(map[string]string)
dimensionsMap[metrics.ResourceOCIDDimension] = req.VolumeId
log := d.logger.With("volumeID", req.VolumeId, "nodeId", req.NodeId, "csiOperation", "attach")
id, err := d.util.LookupNodeID(d.KubeClient, req.NodeId)
if err != nil {
log.With(zap.Error(err)).Error("Failed to lookup node")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.InvalidArgument, "failed to get ProviderID by nodeName. error : %s", err)
}
id = client.MapProviderIDToResourceID(id)
//if the attachmentType is missing, default is iscsi
attachType, ok := req.VolumeContext[attachmentType]
if !ok {
attachType = attachmentTypeISCSI
}
volumeAttachmentOptions, err := getAttachmentOptions(ctx, d.client.Compute(), attachType, id)
if err != nil {
log.With("service", "compute", "verb", "get", "resource", "instance", "statusCode", util.GetHttpStatusCode(err)).
With(zap.Error(err)).With("attachmentType", attachType, "instanceID", id).Error("failed to get the attachment options")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Unknown, "failed to get the attachment options. error : %s", err)
}
//in transit encryption is not supported for other attachment type than paravirtualized
if volumeAttachmentOptions.enableInTransitEncryption && !volumeAttachmentOptions.useParavirtualizedAttachment {
log.Errorf("node %s has in transit encryption enabled, but attachment type is not paravirtualized. invalid input", id)
csiMetricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.InvalidArgument, "node %s has in transit encryption enabled, but attachment type is not paravirtualized. invalid input", id)
}
compartmentID, err := util.LookupNodeCompartment(d.KubeClient, req.NodeId)
if err != nil {
log.With(zap.Error(err)).With("instanceID", id).Errorf("failed to get compartmentID from node annotation: %s", util.CompartmentIDAnnotation)
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Unknown, "failed to get compartmentID from node annotation:. error : %s", err)
}
volumeAttached, err := d.client.Compute().FindActiveVolumeAttachment(ctx, compartmentID, req.VolumeId)
if err != nil && !client.IsNotFound(err) {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With(zap.Error(err)).Error("Got error in finding volume attachment.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, err
}
vpusPerGB, ok := req.VolumeContext[csi_util.VpusPerGB]
if !ok || vpusPerGB == "" {
log.Warnf("No vpusPerGB found in Volume Context falling back to balanced performance")
vpusPerGB = "10"
}
// volume already attached to an instance
if err == nil {
log = log.With("volumeAttachedId", *volumeAttached.GetId())
if volumeAttached.GetLifecycleState() == core.VolumeAttachmentLifecycleStateDetaching {
log.With("instanceID", *volumeAttached.GetInstanceId()).Info("Waiting for volume to get detached before attaching.")
err = d.client.Compute().WaitForVolumeDetached(ctx, *volumeAttached.GetId())
if err != nil {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", *volumeAttached.GetInstanceId()).With(zap.Error(err)).Error("Error while waiting for volume to get detached before attaching.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "Error while waiting for volume to get detached before attaching: %s", err)
}
} else {
if id != *volumeAttached.GetInstanceId() {
log.Errorf("Volume is already attached to another node: %s", *volumeAttached.GetInstanceId())
csiMetricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "Failed to attach volume to node. "+
"The volume is already attached to another node.")
}
if volumeAttached.GetLifecycleState() == core.VolumeAttachmentLifecycleStateAttaching {
log.With("instanceID", id).Info("Volume is in ATTACHING state. Waiting for Volume to attach to the Node.")
volumeAttached, err = d.client.Compute().WaitForVolumeAttached(ctx, *volumeAttached.GetId())
if err != nil {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", id).With(zap.Error(err)).Error("Error while waiting: failed to attach volume to the node.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "Failed to attach volume to the node: %s", err)
}
}
//Checking if Volume state is already Attached or Attachment (from above condition) is completed
if volumeAttached.GetLifecycleState() == core.VolumeAttachmentLifecycleStateAttached {
log.With("instanceID", id).Info("Volume is already ATTACHED to the Node.")
resp, err := generatePublishContext(volumeAttachmentOptions, log, volumeAttached, vpusPerGB, req.VolumeContext[needResize], req.VolumeContext[newSize])
if err != nil {
log.With(zap.Error(err)).Error("Failed to generate publish context")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "Failed to generate publish context: %s", err)
}
return resp, nil
}
}
}
log.Info("Attaching volume to instance")
if volumeAttachmentOptions.useParavirtualizedAttachment {
volumeAttached, err = d.client.Compute().AttachParavirtualizedVolume(ctx, id, req.VolumeId, volumeAttachmentOptions.enableInTransitEncryption)
if err != nil {
log.With("service", "compute", "verb", "create", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", id).With(zap.Error(err)).Info("failed paravirtualized attachment instance to volume.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "failed paravirtualized attachment instance to volume. error : %s", err)
}
} else {
volumeAttached, err = d.client.Compute().AttachVolume(ctx, id, req.VolumeId)
if err != nil {
log.With("service", "compute", "verb", "create", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", id).With(zap.Error(err)).Info("failed iscsi attachment instance to volume.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "failed iscsi attachment instance to volume : %s", err)
}
}
volumeAttached, err = d.client.Compute().WaitForVolumeAttached(ctx, *volumeAttached.GetId())
if err != nil {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", id).With(zap.Error(err)).Error("Failed to attach volume to the node.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "Failed to attach volume to the node %s", err)
}
log.Info("Volume is ATTACHED to Node.")
csiMetricDimension = util.GetMetricDimensionForComponent(util.Success, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
resp, err := generatePublishContext(volumeAttachmentOptions, log, volumeAttached, vpusPerGB, req.VolumeContext[needResize], req.VolumeContext[newSize])
if err != nil {
log.With(zap.Error(err)).Error("Failed to generate publish context")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVAttach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Internal, "Failed to generate publish context: %s", err)
}
return resp, nil
}
func generatePublishContext(volumeAttachmentOptions VolumeAttachmentOption, log *zap.SugaredLogger, volumeAttached core.VolumeAttachment, vpusPerGB string, needsResize string, expectedSize string) (*csi.ControllerPublishVolumeResponse, error) {
multipath := "false"
if volumeAttached.GetIsMultipath() != nil {
multipath = strconv.FormatBool(*volumeAttached.GetIsMultipath())
}
if volumeAttachmentOptions.useParavirtualizedAttachment {
log.With("volumeAttachedId", *volumeAttached.GetId()).Info("Publishing paravirtualized Volume Completed.")
return &csi.ControllerPublishVolumeResponse{
PublishContext: map[string]string{
attachmentType: attachmentTypeParavirtualized,
device: *volumeAttached.GetDevice(),
csi_util.VpusPerGB: vpusPerGB,
needResize: needsResize,
newSize: expectedSize,
multipathEnabled: multipath,
},
}, nil
}
iSCSIVolumeAttached := volumeAttached.(core.IScsiVolumeAttachment)
multiPathDevicesJson := []byte{}
if len(iSCSIVolumeAttached.MultipathDevices) > 0 {
var err error
multiPathDevicesJson, err = json.Marshal(iSCSIVolumeAttached.MultipathDevices)
if err != nil {
return nil, err
}
}
log.With("volumeAttachedId", *volumeAttached.GetId()).Info("Publishing iSCSI Volume Completed.")
return &csi.ControllerPublishVolumeResponse{
PublishContext: map[string]string{
attachmentType: attachmentTypeISCSI,
device: *volumeAttached.GetDevice(),
disk.ISCSIIQN: *iSCSIVolumeAttached.Iqn,
disk.ISCSIIP: *iSCSIVolumeAttached.Ipv4,
disk.ISCSIPORT: strconv.Itoa(*iSCSIVolumeAttached.Port),
csi_util.VpusPerGB: vpusPerGB,
needResize: needsResize,
newSize: expectedSize,
multipathEnabled: multipath,
multipathDevices: string(multiPathDevicesJson),
},
}, nil
}
// ControllerUnpublishVolume detaches the given volume from the node
func (d *BlockVolumeControllerDriver) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
startTime := time.Now()
log := d.logger.With("volumeID", req.VolumeId, "nodeId", req.NodeId, "csiOperation", "detach")
var errorType string
var csiMetricDimension string
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID must be provided")
}
dimensionsMap := make(map[string]string)
dimensionsMap[metrics.ResourceOCIDDimension] = req.VolumeId
compartmentID, err := util.LookupNodeCompartment(d.KubeClient, req.NodeId)
if err != nil {
if k8sapierrors.IsNotFound(err) {
log.Infof("Node with nodeID %s is not found, volume is likely already detached", req.NodeId)
csiMetricDimension = util.GetMetricDimensionForComponent(util.Success, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDetach, time.Since(startTime).Seconds(), dimensionsMap)
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
log.With(zap.Error(err)).Errorf("failed to get compartmentID from node annotation: %s", util.CompartmentIDAnnotation)
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDetach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Unknown, "failed to get compartmentID from node annotation:: error : %s", err)
}
log = log.With("compartmentID", compartmentID)
attachedVolume, err := d.client.Compute().FindVolumeAttachment(ctx, compartmentID, req.VolumeId)
if attachedVolume != nil && attachedVolume.GetId() != nil {
log = log.With("volumeAttachedId", *attachedVolume.GetId())
}
if err != nil {
if !client.IsNotFound(err) {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With(zap.Error(err)).Error("Error while fetching the Volume details. Unable to detach Volume from the node.")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDetach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, err
}
if attachedVolume == nil {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).With(zap.Error(err)).
Error("Unable to find volume attachment for volume to detach. Volume is possibly already detached. Nothing to do in Un-publish Volume.")
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
log.Info("Attached Volume is still in Detaching state")
}
if attachedVolume.GetLifecycleState() != core.VolumeAttachmentLifecycleStateDetaching {
log.With("instanceID", *attachedVolume.GetInstanceId()).Info("Detaching Volume")
err = d.client.Compute().DetachVolume(ctx, *attachedVolume.GetId())
if err != nil {
log.With("service", "compute", "verb", "delete", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", *attachedVolume.GetInstanceId()).With(zap.Error(err)).Error("Volume can not be detached")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDetach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Unknown, "volume can not be detached %s", err)
}
}
log.With("instanceID", *attachedVolume.GetInstanceId()).Info("Waiting for Volume to Detach")
err = d.client.Compute().WaitForVolumeDetached(ctx, *attachedVolume.GetId())
if err != nil {
log.With("service", "compute", "verb", "get", "resource", "volumeAttachment", "statusCode", util.GetHttpStatusCode(err)).
With("instanceID", *attachedVolume.GetInstanceId()).With(zap.Error(err)).Error("timed out waiting for volume to be detached")
errorType = util.GetError(err)
csiMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDetach, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Errorf(codes.Unknown, "timed out waiting for volume to be detached %s", err)
}
multipath := false
if attachedVolume.GetIsMultipath() != nil {
multipath = *attachedVolume.GetIsMultipath()
}
// sleeping to ensure block volume plugin logs out of iscsi connections on nodes before delete
if multipath {
log.Info("Waiting for 90 seconds to ensure block volume plugin logs out of iscsi connections on nodes")
time.Sleep(90 * time.Second)
}
log.Info("Un-publishing Volume Completed")
csiMetricDimension = util.GetMetricDimensionForComponent(util.Success, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = csiMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.PVDetach, time.Since(startTime).Seconds(), dimensionsMap)
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
// ValidateVolumeCapabilities checks whether the volume capabilities requested
// are supported.
func (d *BlockVolumeControllerDriver) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
log := d.logger.With("volumeID", req.VolumeId, "csiOperation", "validateVolumeCapabilities")
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "Volume ID must be provided")
}
if req.VolumeCapabilities == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capabilities must be provided")
}
// check if volume exist before trying to validate it
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
volume, err := d.client.BlockStorage().GetVolume(ctx, req.VolumeId)
if err != nil {
log.With("service", "blockstorage", "verb", "get", "resource", "volume", "statusCode", util.GetHttpStatusCode(err)).
With(zap.Error(err)).Error("Volume ID not found.")
return nil, status.Errorf(codes.NotFound, "Volume ID not found.")
}
if *volume.Id == req.VolumeId {
return &csi.ValidateVolumeCapabilitiesResponse{
Confirmed: &csi.ValidateVolumeCapabilitiesResponse_Confirmed{
VolumeCapabilities: []*csi.VolumeCapability{
{
AccessMode: supportedAccessMode,
},
},
},
}, nil
}
return nil, status.Errorf(codes.NotFound, "VolumeId mis-match.")
}
// ListVolumes returns a list of all requested volumes
func (d *BlockVolumeControllerDriver) ListVolumes(ctx context.Context, req *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// GetCapacity returns the capacity of the storage pool
func (d *BlockVolumeControllerDriver) GetCapacity(ctx context.Context, req *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// ControllerGetCapabilities returns the capabilities of the controller service.
func (d *BlockVolumeControllerDriver) ControllerGetCapabilities(ctx context.Context, req *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
newCap := func(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
}
}
var caps []*csi.ControllerServiceCapability
for _, cap := range []csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
} {
caps = append(caps, newCap(cap))
}
resp := &csi.ControllerGetCapabilitiesResponse{
Capabilities: caps,
}
return resp, nil
}
// validateCapabilities validates the requested capabilities. It returns an error
// if it doesn't satisfy the currently supported modes of OCI Block Volume
func (d *BlockVolumeControllerDriver) validateCapabilities(caps []*csi.VolumeCapability) error {
vcaps := []*csi.VolumeCapability_AccessMode{supportedAccessMode}
hasSupport := func(mode csi.VolumeCapability_AccessMode_Mode) bool {
for _, m := range vcaps {
if mode == m.Mode {
return true
}
}
return false
}
for _, cap := range caps {
if blk := cap.GetBlock(); blk != nil {
d.logger.Error("volumeMode is set to Block which is not supported.")
return fmt.Errorf("driver does not support Block volumeMode. Please use Filesystem mode")
}
if hasSupport(cap.AccessMode.Mode) {
continue
} else {
// we need to make sure all capabilities are supported. Revert back
// in case we have a cap that is supported, but is invalidated now
d.logger.Errorf("The VolumeCapability isn't supported: %s", cap.AccessMode)
return fmt.Errorf("invalid volume capabilities requested. Only SINGLE_NODE_WRITER is supported ('accessModes.ReadWriteOnce' on Kubernetes)")
}
}
return nil
}
// CreateSnapshot will be called by the CO to create a new snapshot from a
// source volume on behalf of a user.
func (d *BlockVolumeControllerDriver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
startTime := time.Now()
var snapshotMetricDimension string
var errorType string
dimensionsMap := make(map[string]string)
dimensionsMap[metrics.ResourceOCIDDimension] = req.Name
log := d.logger.With("snapshotName", req.Name, "sourceVolumeId", req.SourceVolumeId, "csiOperation", "createSnapshot")
if req.Name == "" {
log.Error("Volume Snapshot name must be provided.")
snapshotMetricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = snapshotMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.BlockSnapshotProvision, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Error(codes.InvalidArgument, "Volume snapshot name must be provided")
}
sourceVolumeId := req.SourceVolumeId
if sourceVolumeId == "" {
log.Error("Volume snapshot source ID must be provided")
snapshotMetricDimension = util.GetMetricDimensionForComponent(util.ErrValidation, util.CSIStorageType)
dimensionsMap[metrics.ComponentDimension] = snapshotMetricDimension
metrics.SendMetricData(d.metricPusher, metrics.BlockSnapshotProvision, time.Since(startTime).Seconds(), dimensionsMap)
return nil, status.Error(codes.InvalidArgument, "Volume snapshot source ID must be provided")
}
snapshots, err := d.client.BlockStorage().GetVolumeBackupsByName(ctx, req.Name, d.config.CompartmentID)
if err != nil {
errorType = util.GetError(err)
snapshotMetricDimension = util.GetMetricDimensionForComponent(errorType, util.CSIStorageType)