@@ -49,6 +49,7 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
49
49
kpRootKeyCrn string
50
50
pvcName string
51
51
pvcNamespace string
52
+ bucketVersioning string
52
53
)
53
54
secretMapCustom := make (map [string ]string )
54
55
@@ -85,6 +86,7 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
85
86
secretMap := req .GetSecrets ()
86
87
klog .Info ("req.GetSecrets() length:\t " , len (secretMap ))
87
88
89
+ var customSecretName string
88
90
if len (secretMap ) == 0 {
89
91
klog .Info ("Did not find the secret that matches pvc name. Fetching custom secret from PVC annotations" )
90
92
@@ -108,10 +110,10 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
108
110
109
111
pvcAnnotations := pvcRes .Annotations
110
112
111
- secretName : = pvcAnnotations ["cos.csi.driver/secret" ]
113
+ customSecretName = pvcAnnotations ["cos.csi.driver/secret" ]
112
114
secretNamespace := pvcAnnotations ["cos.csi.driver/secret-namespace" ]
113
115
114
- if secretName == "" {
116
+ if customSecretName == "" {
115
117
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("secretName annotation 'cos.csi.driver/secret' not specified in the PVC annotations, could not fetch the secret %v" , err ))
116
118
}
117
119
@@ -120,7 +122,7 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
120
122
secretNamespace = constants .DefaultNamespace
121
123
}
122
124
123
- secret , err := utils .GetSecret (secretName , secretNamespace )
125
+ secret , err := utils .GetSecret (customSecretName , secretNamespace )
124
126
if err != nil {
125
127
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("Secret resource not found %v" , err ))
126
128
}
@@ -165,6 +167,24 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
165
167
bucketName = secretMapCustom ["bucketName" ]
166
168
}
167
169
170
+ // Check for bucketVersioning parameter
171
+ if val , ok := secretMap ["bucketVersioning" ]; ok && val != "" {
172
+ enable := strings .ToLower (strings .TrimSpace (val ))
173
+ if enable != "true" && enable != "false" {
174
+ return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("Invalid BucketVersioning value in secret: %s. Value set %s. Must be 'true' or 'false'" , customSecretName , val ))
175
+ }
176
+ bucketVersioning = enable
177
+ klog .Infof ("BucketVersioning value that will be set via secret: %s" , bucketVersioning )
178
+ } else if val , ok := params ["bucketVersioning" ]; ok && val != "" {
179
+ enable := strings .ToLower (strings .TrimSpace (val ))
180
+ if enable != "true" && enable != "false" {
181
+ return nil , status .Error (codes .InvalidArgument ,
182
+ fmt .Sprintf ("Invalid bucketVersioning value in storage class: %s. Must be 'true' or 'false'" , val ))
183
+ }
184
+ bucketVersioning = enable
185
+ klog .Infof ("BucketVersioning value that will be set via storage class params: %s" , bucketVersioning )
186
+ }
187
+
168
188
creds , err := getCredentials (secretMap )
169
189
if err != nil {
170
190
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("Error in getting credentials %v" , err ))
@@ -185,6 +205,24 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
185
205
params ["userProvidedBucket" ] = "false"
186
206
klog .Infof ("Created bucket: %s" , bucketName )
187
207
}
208
+
209
+ if bucketVersioning != "" {
210
+ enable := strings .ToLower (strings .TrimSpace (bucketVersioning )) == "true"
211
+ klog .Infof ("Bucket versioning value evaluated to: %t" , enable )
212
+
213
+ err := sess .SetBucketVersioning (bucketName , enable )
214
+ if err != nil {
215
+ if params ["userProvidedBucket" ] == "false" {
216
+ err1 := sess .DeleteBucket (bucketName )
217
+ if err1 != nil {
218
+ return nil , status .Error (codes .Internal , fmt .Sprintf ("cannot set versioning: %v and cannot delete bucket %s: %v" , err , bucketName , err1 ))
219
+ }
220
+ }
221
+ return nil , status .Error (codes .Internal , fmt .Sprintf ("failed to set versioning %t for bucket %s: %v" , enable , bucketName , err ))
222
+ }
223
+ klog .Infof ("Bucket versioning set to %t for bucket %s" , enable , bucketName )
224
+ }
225
+
188
226
params ["bucketName" ] = bucketName
189
227
} else {
190
228
// Generate random temp bucket name based on volume id
@@ -198,6 +236,21 @@ func (cs *controllerServer) CreateVolume(_ context.Context, req *csi.CreateVolum
198
236
if err != nil {
199
237
return nil , status .Error (codes .PermissionDenied , fmt .Sprintf ("%v: %v" , err , tempBucketName ))
200
238
}
239
+
240
+ if bucketVersioning != "" {
241
+ enable := strings .ToLower (strings .TrimSpace (bucketVersioning )) == "true"
242
+ klog .Infof ("Temp bucket versioning value evaluated to: %t" , enable )
243
+
244
+ err := sess .SetBucketVersioning (tempBucketName , enable )
245
+ if err != nil {
246
+ err1 := sess .DeleteBucket (tempBucketName )
247
+ if err1 != nil {
248
+ return nil , status .Error (codes .Internal , fmt .Sprintf ("cannot set versioning: %v and cannot delete temp bucket %s: %v" , err , tempBucketName , err1 ))
249
+ }
250
+ return nil , status .Error (codes .Internal , fmt .Sprintf ("failed to set versioning %t for temp bucket %s: %v" , enable , tempBucketName , err ))
251
+ }
252
+ klog .Infof ("Bucket versioning set to %t for temp bucket %s" , enable , tempBucketName )
253
+ }
201
254
klog .Infof ("Created temp bucket: %s" , tempBucketName )
202
255
params ["userProvidedBucket" ] = "false"
203
256
params ["bucketName" ] = tempBucketName
@@ -442,6 +495,7 @@ func parseCustomSecret(secret *v1.Secret) map[string]string {
442
495
iamEndpoint string
443
496
cosEndpoint string
444
497
locationConstraint string
498
+ bucketVersioning string
445
499
)
446
500
447
501
if bytesVal , ok := secret .Data ["accessKey" ]; ok {
@@ -480,6 +534,10 @@ func parseCustomSecret(secret *v1.Secret) map[string]string {
480
534
locationConstraint = string (bytesVal )
481
535
}
482
536
537
+ if bytesVal , ok := secret .Data ["bucketVersioning" ]; ok {
538
+ bucketVersioning = string (bytesVal )
539
+ }
540
+
483
541
secretMapCustom ["accessKey" ] = accessKey
484
542
secretMapCustom ["secretKey" ] = secretKey
485
543
secretMapCustom ["apiKey" ] = apiKey
@@ -489,6 +547,7 @@ func parseCustomSecret(secret *v1.Secret) map[string]string {
489
547
secretMapCustom ["iamEndpoint" ] = iamEndpoint
490
548
secretMapCustom ["cosEndpoint" ] = cosEndpoint
491
549
secretMapCustom ["locationConstraint" ] = locationConstraint
550
+ secretMapCustom ["bucketVersioning" ] = bucketVersioning
492
551
493
552
return secretMapCustom
494
553
}
0 commit comments