Skip to content

Commit 606b13e

Browse files
committed
Start refactoring the s3 client to aws-sdk-go-v2
Signed-off-by: Michael Shen <[email protected]>
1 parent d020303 commit 606b13e

19 files changed

+481
-5460
lines changed

controllers/awscluster_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
195195
}
196196

197197
// Handle non-deleted clusters
198-
return r.reconcileNormal(clusterScope)
198+
return r.reconcileNormal(ctx, clusterScope)
199199
}
200200

201201
func (r *AWSClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) error {
@@ -229,7 +229,7 @@ func (r *AWSClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
229229
// when external controllers might be using them.
230230
allErrs := []error{}
231231

232-
if err := s3Service.DeleteBucket(); err != nil {
232+
if err := s3Service.DeleteBucket(ctx); err != nil {
233233
allErrs = append(allErrs, errors.Wrapf(err, "error deleting S3 Bucket"))
234234
}
235235

@@ -297,7 +297,7 @@ func (r *AWSClusterReconciler) reconcileLoadBalancer(clusterScope *scope.Cluster
297297
return nil, nil
298298
}
299299

300-
func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope) (reconcile.Result, error) {
300+
func (r *AWSClusterReconciler) reconcileNormal(ctx context.Context, clusterScope *scope.ClusterScope) (reconcile.Result, error) {
301301
clusterScope.Info("Reconciling AWSCluster")
302302

303303
awsCluster := clusterScope.AWSCluster
@@ -346,7 +346,7 @@ func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope)
346346
return reconcile.Result{RequeueAfter: *requeueAfter}, err
347347
}
348348

349-
if err := s3Service.ReconcileBucket(); err != nil {
349+
if err := s3Service.ReconcileBucket(ctx); err != nil {
350350
conditions.MarkFalse(awsCluster, infrav1.S3BucketReadyCondition, infrav1.S3BucketFailedReason, clusterv1.ConditionSeverityError, "%s", err.Error())
351351
return reconcile.Result{}, errors.Wrapf(err, "failed to reconcile S3 Bucket for AWSCluster %s/%s", awsCluster.Namespace, awsCluster.Name)
352352
}

controllers/awscluster_controller_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
146146
},
147147
})
148148

149-
_, err = reconciler.reconcileNormal(cs)
149+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
150150
g.Expect(err).To(BeNil())
151151

152152
cluster := &infrav1.AWSCluster{}
@@ -170,7 +170,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
170170
})).To(Succeed())
171171
// Executing back a second reconciliation:
172172
// the AWS Cluster should be ready with no LoadBalancer false condition.
173-
_, err = reconciler.reconcileNormal(cs)
173+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
174174
g.Expect(err).To(BeNil())
175175
g.Expect(cs.VPC().ID).To(Equal("vpc-exists"))
176176
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
@@ -269,7 +269,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
269269
IsPublic: false,
270270
},
271271
})
272-
_, err = reconciler.reconcileNormal(cs)
272+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
273273
g.Expect(err).To(BeNil())
274274
g.Expect(cs.VPC().ID).To(Equal("vpc-exists"))
275275
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
@@ -376,7 +376,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
376376
IsPublic: false,
377377
},
378378
})
379-
_, err = reconciler.reconcileNormal(cs)
379+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
380380
g.Expect(err).To(BeNil())
381381
g.Expect(cs.VPC().ID).To(Equal("vpc-exists"))
382382
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
@@ -469,7 +469,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
469469
reconciler.elbServiceFactory = func(elbScope scope.ELBScope) services.ELBInterface {
470470
return elbSvc
471471
}
472-
_, err = reconciler.reconcileNormal(cs)
472+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
473473
g.Expect(err).To(BeNil())
474474
g.Expect(cs.VPC().ID).To(Equal("vpc-new"))
475475
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
@@ -565,7 +565,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
565565
return ec2Svc
566566
}
567567

568-
_, err = reconciler.reconcileNormal(cs)
568+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
569569
g.Expect(err.Error()).To(ContainSubstring("The maximum number of VPCs has been reached"))
570570

571571
err = reconciler.reconcileDelete(ctx, cs)

controllers/awscluster_controller_unit_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
244244
IsPublic: false,
245245
},
246246
})
247-
_, err = reconciler.reconcileNormal(cs)
247+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
248248
g.Expect(err).To(BeNil())
249249
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionTrue, "", ""}})
250250
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
@@ -276,7 +276,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
276276
PublicIpv4PoolFallBackOrder: ptr.To(infrav1.PublicIpv4PoolFallbackOrderAmazonPool),
277277
}
278278
g.Expect(err).To(Not(HaveOccurred()))
279-
_, err = reconciler.reconcileNormal(cs)
279+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
280280
g.Expect(err).To(Not(HaveOccurred()))
281281
})
282282
})
@@ -299,7 +299,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
299299
},
300300
)
301301
g.Expect(err).To(BeNil())
302-
_, err = reconciler.reconcileNormal(cs)
302+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
303303
g.Expect(err).Should(Equal(expectedErr))
304304
})
305305
t.Run("Should fail AWSCluster create with ClusterSecurityGroupsReadyCondition status false", func(t *testing.T) {
@@ -320,7 +320,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
320320
},
321321
)
322322
g.Expect(err).To(BeNil())
323-
_, err = reconciler.reconcileNormal(cs)
323+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
324324
g.Expect(err).ToNot(BeNil())
325325
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.ClusterSecurityGroupsReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.ClusterSecurityGroupReconciliationFailedReason}})
326326
})
@@ -343,7 +343,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
343343
},
344344
)
345345
g.Expect(err).To(BeNil())
346-
_, err = reconciler.reconcileNormal(cs)
346+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
347347
g.Expect(err).ToNot(BeNil())
348348
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.BastionHostReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.BastionHostFailedReason}})
349349
})
@@ -367,7 +367,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
367367
},
368368
)
369369
g.Expect(err).To(BeNil())
370-
_, err = reconciler.reconcileNormal(cs)
370+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
371371
g.Expect(err).ToNot(BeNil())
372372
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.LoadBalancerFailedReason}})
373373
})
@@ -391,7 +391,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
391391
},
392392
)
393393
g.Expect(err).To(BeNil())
394-
_, err = reconciler.reconcileNormal(cs)
394+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
395395
g.Expect(err).To(BeNil())
396396
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, infrav1.WaitForDNSNameReason}})
397397
})

controllers/awsmachine_controller.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@ func (r *AWSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
224224
switch infraScope := infraCluster.(type) {
225225
case *scope.ManagedControlPlaneScope:
226226
if !awsMachine.ObjectMeta.DeletionTimestamp.IsZero() {
227-
return r.reconcileDelete(machineScope, infraScope, infraScope, nil, nil)
227+
return r.reconcileDelete(ctx, machineScope, infraScope, infraScope, nil, nil)
228228
}
229229

230230
return r.reconcileNormal(ctx, machineScope, infraScope, infraScope, nil, nil)
231231
case *scope.ClusterScope:
232232
if !awsMachine.ObjectMeta.DeletionTimestamp.IsZero() {
233-
return r.reconcileDelete(machineScope, infraScope, infraScope, infraScope, infraScope)
233+
return r.reconcileDelete(ctx, machineScope, infraScope, infraScope, infraScope, infraScope)
234234
}
235235

236236
return r.reconcileNormal(ctx, machineScope, infraScope, infraScope, infraScope, infraScope)
@@ -298,12 +298,12 @@ func (r *AWSMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
298298
)
299299
}
300300

301-
func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, ec2Scope scope.EC2Scope, elbScope scope.ELBScope, objectStoreScope scope.S3Scope) (ctrl.Result, error) {
301+
func (r *AWSMachineReconciler) reconcileDelete(ctx context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, ec2Scope scope.EC2Scope, elbScope scope.ELBScope, objectStoreScope scope.S3Scope) (ctrl.Result, error) {
302302
machineScope.Info("Handling deleted AWSMachine")
303303

304304
ec2Service := r.getEC2Service(ec2Scope)
305305

306-
if err := r.deleteBootstrapData(machineScope, clusterScope, objectStoreScope); err != nil {
306+
if err := r.deleteBootstrapData(ctx, machineScope, clusterScope, objectStoreScope); err != nil {
307307
machineScope.Error(err, "unable to delete machine")
308308
return ctrl.Result{}, err
309309
}
@@ -459,15 +459,15 @@ func (r *AWSMachineReconciler) findInstance(machineScope *scope.MachineScope, ec
459459
return instance, nil
460460
}
461461

462-
func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, ec2Scope scope.EC2Scope, elbScope scope.ELBScope, objectStoreScope scope.S3Scope) (ctrl.Result, error) {
462+
func (r *AWSMachineReconciler) reconcileNormal(ctx context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, ec2Scope scope.EC2Scope, elbScope scope.ELBScope, objectStoreScope scope.S3Scope) (ctrl.Result, error) {
463463
machineScope.Trace("Reconciling AWSMachine")
464464

465465
// If the AWSMachine is in an error state, return early.
466466
if machineScope.HasFailed() {
467467
machineScope.Info("Error state detected, skipping reconciliation")
468468

469469
// If we are in a failed state, delete the secret regardless of instance state.
470-
if err := r.deleteBootstrapData(machineScope, clusterScope, objectStoreScope); err != nil {
470+
if err := r.deleteBootstrapData(ctx, machineScope, clusterScope, objectStoreScope); err != nil {
471471
machineScope.Error(err, "unable to reconcile machine")
472472
return ctrl.Result{}, err
473473
}
@@ -524,7 +524,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
524524
objectStoreSvc = r.getObjectStoreService(objectStoreScope)
525525
}
526526

527-
instance, err = r.createInstance(ec2svc, machineScope, clusterScope, objectStoreSvc)
527+
instance, err = r.createInstance(ctx, ec2svc, machineScope, clusterScope, objectStoreSvc)
528528
if err != nil {
529529
machineScope.Error(err, "unable to create instance")
530530
conditions.MarkFalse(machineScope.AWSMachine, infrav1.InstanceReadyCondition, infrav1.InstanceProvisionFailedReason, clusterv1.ConditionSeverityError, "%s", err.Error())
@@ -599,7 +599,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
599599
}
600600

601601
// reconcile the deletion of the bootstrap data secret now that we have updated instance state
602-
if deleteSecretErr := r.deleteBootstrapData(machineScope, clusterScope, objectStoreScope); deleteSecretErr != nil {
602+
if deleteSecretErr := r.deleteBootstrapData(ctx, machineScope, clusterScope, objectStoreScope); deleteSecretErr != nil {
603603
r.Log.Error(deleteSecretErr, "unable to delete secrets")
604604
return ctrl.Result{}, deleteSecretErr
605605
}
@@ -709,10 +709,10 @@ func (r *AWSMachineReconciler) deleteEncryptedBootstrapDataSecret(machineScope *
709709
return nil
710710
}
711711

712-
func (r *AWSMachineReconciler) createInstance(ec2svc services.EC2Interface, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreSvc services.ObjectStoreInterface) (*infrav1.Instance, error) {
712+
func (r *AWSMachineReconciler) createInstance(ctx context.Context, ec2svc services.EC2Interface, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreSvc services.ObjectStoreInterface) (*infrav1.Instance, error) {
713713
machineScope.Info("Creating EC2 instance")
714714

715-
userData, userDataFormat, userDataErr := r.resolveUserData(machineScope, clusterScope, objectStoreSvc)
715+
userData, userDataFormat, userDataErr := r.resolveUserData(ctx, machineScope, clusterScope, objectStoreSvc)
716716
if userDataErr != nil {
717717
return nil, errors.Wrapf(userDataErr, "failed to resolve userdata")
718718
}
@@ -725,7 +725,7 @@ func (r *AWSMachineReconciler) createInstance(ec2svc services.EC2Interface, mach
725725
return instance, nil
726726
}
727727

728-
func (r *AWSMachineReconciler) resolveUserData(machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreSvc services.ObjectStoreInterface) ([]byte, string, error) {
728+
func (r *AWSMachineReconciler) resolveUserData(ctx context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreSvc services.ObjectStoreInterface) ([]byte, string, error) {
729729
userData, userDataFormat, err := machineScope.GetRawBootstrapDataWithFormat()
730730
if err != nil {
731731
r.Recorder.Eventf(machineScope.AWSMachine, corev1.EventTypeWarning, "FailedGetBootstrapData", err.Error())
@@ -746,7 +746,7 @@ func (r *AWSMachineReconciler) resolveUserData(machineScope *scope.MachineScope,
746746

747747
switch ignitionStorageType {
748748
case infrav1.IgnitionStorageTypeOptionClusterObjectStore:
749-
userData, err = r.generateIgnitionWithRemoteStorage(machineScope, objectStoreSvc, userData)
749+
userData, err = r.generateIgnitionWithRemoteStorage(ctx, machineScope, objectStoreSvc, userData)
750750
case infrav1.IgnitionStorageTypeOptionUnencryptedUserData:
751751
// No further modifications to userdata are needed for plain storage in UnencryptedUserData.
752752
default:
@@ -793,13 +793,13 @@ func (r *AWSMachineReconciler) cloudInitUserData(machineScope *scope.MachineScop
793793

794794
// generateIgnitionWithRemoteStorage uses a remote object storage (S3 bucket) and stores user data in it,
795795
// then returns the config to instruct ignition on how to pull the user data from the bucket.
796-
func (r *AWSMachineReconciler) generateIgnitionWithRemoteStorage(scope *scope.MachineScope, objectStoreSvc services.ObjectStoreInterface, userData []byte) ([]byte, error) {
796+
func (r *AWSMachineReconciler) generateIgnitionWithRemoteStorage(ctx context.Context, scope *scope.MachineScope, objectStoreSvc services.ObjectStoreInterface, userData []byte) ([]byte, error) {
797797
if objectStoreSvc == nil {
798798
return nil, errors.New("using Ignition by default requires a cluster wide object storage configured at `AWSCluster.Spec.Ignition.S3Bucket`. " +
799799
"You must configure one or instruct Ignition to use EC2 user data instead, by setting `AWSMachine.Spec.Ignition.StorageType` to `UnencryptedUserData`")
800800
}
801801

802-
objectURL, err := objectStoreSvc.Create(scope, userData)
802+
objectURL, err := objectStoreSvc.Create(ctx, scope, userData)
803803
if err != nil {
804804
return nil, errors.Wrap(err, "creating userdata object")
805805
}
@@ -875,7 +875,7 @@ func getIgnitionVersion(scope *scope.MachineScope) string {
875875
return scope.AWSMachine.Spec.Ignition.Version
876876
}
877877

878-
func (r *AWSMachineReconciler) deleteBootstrapData(machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreScope scope.S3Scope) error {
878+
func (r *AWSMachineReconciler) deleteBootstrapData(ctx context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreScope scope.S3Scope) error {
879879
_, userDataFormat, err := machineScope.GetRawBootstrapDataWithFormat()
880880
if client.IgnoreNotFound(err) != nil {
881881
return errors.Wrap(err, "failed to get raw userdata")
@@ -889,15 +889,15 @@ func (r *AWSMachineReconciler) deleteBootstrapData(machineScope *scope.MachineSc
889889

890890
if objectStoreScope != nil {
891891
// Bootstrap data will be removed from S3 if it is already populated.
892-
if err := r.deleteIgnitionBootstrapDataFromS3(machineScope, r.getObjectStoreService(objectStoreScope)); err != nil {
892+
if err := r.deleteIgnitionBootstrapDataFromS3(ctx, machineScope, r.getObjectStoreService(objectStoreScope)); err != nil {
893893
return err
894894
}
895895
}
896896

897897
return nil
898898
}
899899

900-
func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *scope.MachineScope, objectStoreSvc services.ObjectStoreInterface) error {
900+
func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(ctx context.Context, machineScope *scope.MachineScope, objectStoreSvc services.ObjectStoreInterface) error {
901901
// Do nothing if the AWSMachine is not in a failed state, and is operational from an EC2 perspective, but does not have a node reference
902902
if !machineScope.HasFailed() && machineScope.InstanceIsOperational() && machineScope.Machine.Status.NodeRef == nil && !machineScope.AWSMachineIsDeleted() {
903903
return nil
@@ -921,7 +921,7 @@ func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *s
921921
return nil
922922
}
923923

924-
if err := objectStoreSvc.Delete(machineScope); err != nil {
924+
if err := objectStoreSvc.Delete(ctx, machineScope); err != nil {
925925
return errors.Wrap(err, "deleting bootstrap data object")
926926
}
927927

controllers/awsmachine_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func TestAWSMachineReconcilerIntegrationTests(t *testing.T) {
237237
return elbSvc
238238
}
239239

240-
_, err = reconciler.reconcileDelete(ms, cs, cs, cs, cs)
240+
_, err = reconciler.reconcileDelete(context.TODO(), ms, cs, cs, cs, cs)
241241
g.Expect(err).To(BeNil())
242242
expectConditions(g, ms.AWSMachine, []conditionAssertion{
243243
{infrav1.InstanceReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, clusterv1.DeletedReason},
@@ -418,7 +418,7 @@ func TestAWSMachineReconcilerIntegrationTests(t *testing.T) {
418418
return elbSvc
419419
}
420420

421-
_, err = reconciler.reconcileDelete(ms, cs, cs, cs, cs)
421+
_, err = reconciler.reconcileDelete(context.TODO(), ms, cs, cs, cs, cs)
422422
g.Expect(err).Should(HaveOccurred())
423423
expectConditions(g, ms.AWSMachine, []conditionAssertion{
424424
{infrav1.InstanceReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, "DeletingFailed"},

0 commit comments

Comments
 (0)