@@ -135,6 +135,11 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
135
135
d .Config .Location = infra .Status .PlatformStatus .IBMCloud .Location
136
136
d .Config .ResourceGroupName = infra .Status .PlatformStatus .IBMCloud .ResourceGroupName
137
137
138
+ // Initialize IBMCOS status
139
+ if cr .Status .Storage .IBMCOS == nil {
140
+ cr .Status .Storage .IBMCOS = & imageregistryv1.ImageRegistryConfigStorageIBMCOS {}
141
+ }
142
+
138
143
// Get resource controller service
139
144
rc , err := d .getResouceControllerService ()
140
145
if err != nil {
@@ -176,9 +181,8 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
176
181
// Set resource group name
177
182
d .Config .ResourceGroupName = * rg .Name
178
183
}
179
- cr .Status .Storage = imageregistryv1.ImageRegistryConfigStorage {
180
- IBMCOS : d .Config .DeepCopy (),
181
- }
184
+ cr .Status .Storage .IBMCOS .ServiceInstanceCRN = d .Config .ServiceInstanceCRN
185
+ cr .Status .Storage .IBMCOS .ResourceGroupName = d .Config .ResourceGroupName
182
186
cr .Spec .Storage .IBMCOS = d .Config .DeepCopy ()
183
187
util .UpdateCondition (cr , defaults .StorageExists , operatorapi .ConditionFalse , "IBM COS Instance Active" , "IBM COS service instance is active" )
184
188
case resourcecontrollerv2 .ListResourceInstancesOptionsStateProvisioningConst :
@@ -261,15 +265,15 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
261
265
}
262
266
263
267
d .Config .ServiceInstanceCRN = * instance .CRN
264
- cr .Status .Storage = imageregistryv1. ImageRegistryConfigStorage {
265
- IBMCOS : d .Config .DeepCopy (),
266
- }
268
+ cr .Status .Storage . IBMCOS . ServiceInstanceCRN = d . Config . ServiceInstanceCRN
269
+ cr . Status . Storage . IBMCOS . ResourceGroupName = d .Config .ResourceGroupName
270
+ cr . Status . Storage . ManagementState = cr . Spec . Storage . ManagementState
267
271
cr .Spec .Storage .IBMCOS = d .Config .DeepCopy ()
268
272
util .UpdateCondition (cr , defaults .StorageExists , operatorapi .ConditionFalse , "IBM COS Instance Creation Successful" , "IBM COS service instance was successfully created" )
269
273
}
270
274
271
- // Create resource key
272
275
if len (d .Config .ResourceKeyCRN ) == 0 {
276
+ // Create resource key
273
277
keyName := fmt .Sprintf ("%s-%s" , infra .Status .InfrastructureName , defaults .ImageRegistryName )
274
278
roleCRN := "crn:v1:bluemix:public:iam::::serviceRole:Writer"
275
279
params := & resourcecontrollerv2.ResourceKeyPostParameters {}
@@ -289,11 +293,43 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
289
293
}
290
294
291
295
d .Config .ResourceKeyCRN = * key .CRN
292
- cr .Status .Storage = imageregistryv1.ImageRegistryConfigStorage {
293
- IBMCOS : d .Config .DeepCopy (),
294
- }
296
+ cr .Status .Storage .IBMCOS .ResourceKeyCRN = d .Config .ResourceKeyCRN
295
297
cr .Spec .Storage .IBMCOS = d .Config .DeepCopy ()
296
298
util .UpdateCondition (cr , defaults .StorageExists , operatorapi .ConditionFalse , "IBM COS Resource Key Creation Successful" , "IBM COS resource key was successfully created" )
299
+ } else {
300
+ // Get resource key
301
+ key , resp , err := rc .GetResourceKeyWithContext (
302
+ d .Context ,
303
+ & resourcecontrollerv2.GetResourceKeyOptions {
304
+ ID : & d .Config .ResourceKeyCRN ,
305
+ },
306
+ )
307
+ if err != nil {
308
+ return fmt .Errorf ("unable to get resource key for service instance: %s with resp code: %d" , err .Error (), resp .StatusCode )
309
+ }
310
+
311
+ // Check if resource key is for service instance
312
+ if * key .SourceCRN != d .Config .ServiceInstanceCRN {
313
+ return fmt .Errorf ("specified resource key is not valid for service instance" )
314
+ }
315
+
316
+ if key .Credentials != nil {
317
+ // Check if resource key is HMAC enabled
318
+ if key .Credentials .GetProperty ("cos_hmac_keys" ) == nil {
319
+ return fmt .Errorf ("specified resource key credentials does not contain HMAC keys" )
320
+ }
321
+ // Check if resource key has a valid IAM role
322
+ if * key .Credentials .IamRoleCRN != "crn:v1:bluemix:public:iam::::serviceRole:Writer" && * key .Credentials .IamRoleCRN != "crn:v1:bluemix:public:iam::::serviceRole:Manager" {
323
+ return fmt .Errorf ("specified resource key's IAM role is not valid" )
324
+ }
325
+ // Valid resource key
326
+ d .Config .ResourceKeyCRN = * key .CRN
327
+ cr .Status .Storage .IBMCOS .ResourceKeyCRN = d .Config .ResourceKeyCRN
328
+ cr .Spec .Storage .IBMCOS = d .Config .DeepCopy ()
329
+ util .UpdateCondition (cr , defaults .StorageExists , operatorapi .ConditionFalse , "IBM COS Resource Key Valid" , "IBM COS resource key exists and is valid" )
330
+ } else {
331
+ return fmt .Errorf ("specified resource key does not have any attached credentials" )
332
+ }
297
333
}
298
334
299
335
// Check if bucket already exists
@@ -360,15 +396,6 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
360
396
return err
361
397
}
362
398
363
- if cr .Spec .Storage .ManagementState == "" {
364
- cr .Spec .Storage .ManagementState = imageregistryv1 .StorageManagementStateManaged
365
- }
366
- cr .Status .Storage = imageregistryv1.ImageRegistryConfigStorage {
367
- IBMCOS : d .Config .DeepCopy (),
368
- }
369
- cr .Spec .Storage .IBMCOS = d .Config .DeepCopy ()
370
- util .UpdateCondition (cr , defaults .StorageExists , operatorapi .ConditionTrue , "Creation Successful" , "IBM COS bucket was successfully created" )
371
-
372
399
// Wait until the bucket exists
373
400
if err := client .WaitUntilBucketExistsWithContext (
374
401
d .Context ,
@@ -381,6 +408,15 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
381
408
}
382
409
return err
383
410
}
411
+
412
+ if cr .Spec .Storage .ManagementState == "" {
413
+ cr .Spec .Storage .ManagementState = imageregistryv1 .StorageManagementStateManaged
414
+ }
415
+ cr .Status .Storage = imageregistryv1.ImageRegistryConfigStorage {
416
+ IBMCOS : d .Config .DeepCopy (),
417
+ }
418
+ cr .Spec .Storage .IBMCOS = d .Config .DeepCopy ()
419
+ util .UpdateCondition (cr , defaults .StorageExists , operatorapi .ConditionTrue , "Creation Successful" , "IBM COS bucket was successfully created" )
384
420
}
385
421
386
422
return nil
0 commit comments