Skip to content

Commit 7b09356

Browse files
authored
Merge pull request #5389 from punkwalker/aws-sdk-go-v2
✨ Migrate S3, Sessions and Endpoints packages to AWS SDK v2
2 parents 310ad99 + 2fce746 commit 7b09356

32 files changed

+1475
-5512
lines changed

controllers/awscluster_controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
196196
}
197197

198198
// Handle non-deleted clusters
199-
return r.reconcileNormal(clusterScope)
199+
return r.reconcileNormal(ctx, clusterScope)
200200
}
201201

202202
func (r *AWSClusterReconciler) reconcileDelete(ctx context.Context, clusterScope *scope.ClusterScope) (ctrl.Result, error) {
@@ -243,7 +243,7 @@ func (r *AWSClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
243243
// when external controllers might be using them.
244244
allErrs := []error{}
245245

246-
if err := s3Service.DeleteBucket(); err != nil {
246+
if err := s3Service.DeleteBucket(ctx); err != nil {
247247
allErrs = append(allErrs, errors.Wrapf(err, "error deleting S3 Bucket"))
248248
}
249249

@@ -311,7 +311,7 @@ func (r *AWSClusterReconciler) reconcileLoadBalancer(clusterScope *scope.Cluster
311311
return nil, nil
312312
}
313313

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

317317
awsCluster := clusterScope.AWSCluster
@@ -360,7 +360,7 @@ func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope)
360360
return reconcile.Result{RequeueAfter: *requeueAfter}, err
361361
}
362362

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

controllers/awscluster_controller_test.go

+6-6
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

+7-7
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
242242
IsPublic: false,
243243
},
244244
})
245-
_, err = reconciler.reconcileNormal(cs)
245+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
246246
g.Expect(err).To(BeNil())
247247
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionTrue, "", ""}})
248248
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
@@ -274,7 +274,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
274274
PublicIpv4PoolFallBackOrder: ptr.To(infrav1.PublicIpv4PoolFallbackOrderAmazonPool),
275275
}
276276
g.Expect(err).To(Not(HaveOccurred()))
277-
_, err = reconciler.reconcileNormal(cs)
277+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
278278
g.Expect(err).To(Not(HaveOccurred()))
279279
})
280280
})
@@ -297,7 +297,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
297297
},
298298
)
299299
g.Expect(err).To(BeNil())
300-
_, err = reconciler.reconcileNormal(cs)
300+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
301301
g.Expect(err).Should(Equal(expectedErr))
302302
})
303303
t.Run("Should fail AWSCluster create with ClusterSecurityGroupsReadyCondition status false", func(t *testing.T) {
@@ -318,7 +318,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
318318
},
319319
)
320320
g.Expect(err).To(BeNil())
321-
_, err = reconciler.reconcileNormal(cs)
321+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
322322
g.Expect(err).ToNot(BeNil())
323323
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.ClusterSecurityGroupsReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.ClusterSecurityGroupReconciliationFailedReason}})
324324
})
@@ -341,7 +341,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
341341
},
342342
)
343343
g.Expect(err).To(BeNil())
344-
_, err = reconciler.reconcileNormal(cs)
344+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
345345
g.Expect(err).ToNot(BeNil())
346346
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.BastionHostReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.BastionHostFailedReason}})
347347
})
@@ -365,7 +365,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
365365
},
366366
)
367367
g.Expect(err).To(BeNil())
368-
_, err = reconciler.reconcileNormal(cs)
368+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
369369
g.Expect(err).ToNot(BeNil())
370370
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.LoadBalancerFailedReason}})
371371
})
@@ -389,7 +389,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
389389
},
390390
)
391391
g.Expect(err).To(BeNil())
392-
_, err = reconciler.reconcileNormal(cs)
392+
_, err = reconciler.reconcileNormal(context.TODO(), cs)
393393
g.Expect(err).To(BeNil())
394394
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, infrav1.WaitForDNSNameReason}})
395395
})

controllers/awsmachine_controller.go

+18-18
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
}
@@ -460,15 +460,15 @@ func (r *AWSMachineReconciler) findInstance(machineScope *scope.MachineScope, ec
460460
}
461461

462462
//nolint:gocyclo
463-
func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, ec2Scope scope.EC2Scope, elbScope scope.ELBScope, objectStoreScope scope.S3Scope) (ctrl.Result, error) {
463+
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) {
464464
machineScope.Trace("Reconciling AWSMachine")
465465

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

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

534-
instance, err = r.createInstance(ec2svc, machineScope, clusterScope, objectStoreSvc)
534+
instance, err = r.createInstance(ctx, ec2svc, machineScope, clusterScope, objectStoreSvc)
535535
if err != nil {
536536
machineScope.Error(err, "unable to create instance")
537537
conditions.MarkFalse(machineScope.AWSMachine, infrav1.InstanceReadyCondition, infrav1.InstanceProvisionFailedReason, clusterv1.ConditionSeverityError, "%s", err.Error())
@@ -616,7 +616,7 @@ func (r *AWSMachineReconciler) reconcileNormal(_ context.Context, machineScope *
616616

617617
// reconcile the deletion of the bootstrap data secret now that we have updated instance state
618618
if !machineScope.IsMachinePoolMachine() {
619-
if deleteSecretErr := r.deleteBootstrapData(machineScope, clusterScope, objectStoreScope); deleteSecretErr != nil {
619+
if deleteSecretErr := r.deleteBootstrapData(ctx, machineScope, clusterScope, objectStoreScope); deleteSecretErr != nil {
620620
r.Log.Error(deleteSecretErr, "unable to delete secrets")
621621
return ctrl.Result{}, deleteSecretErr
622622
}
@@ -729,10 +729,10 @@ func (r *AWSMachineReconciler) deleteEncryptedBootstrapDataSecret(machineScope *
729729
return nil
730730
}
731731

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

735-
userData, userDataFormat, userDataErr := r.resolveUserData(machineScope, clusterScope, objectStoreSvc)
735+
userData, userDataFormat, userDataErr := r.resolveUserData(ctx, machineScope, clusterScope, objectStoreSvc)
736736
if userDataErr != nil {
737737
return nil, errors.Wrapf(userDataErr, "failed to resolve userdata")
738738
}
@@ -745,7 +745,7 @@ func (r *AWSMachineReconciler) createInstance(ec2svc services.EC2Interface, mach
745745
return instance, nil
746746
}
747747

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

767767
switch ignitionStorageType {
768768
case infrav1.IgnitionStorageTypeOptionClusterObjectStore:
769-
userData, err = r.generateIgnitionWithRemoteStorage(machineScope, objectStoreSvc, userData)
769+
userData, err = r.generateIgnitionWithRemoteStorage(ctx, machineScope, objectStoreSvc, userData)
770770
case infrav1.IgnitionStorageTypeOptionUnencryptedUserData:
771771
// No further modifications to userdata are needed for plain storage in UnencryptedUserData.
772772
default:
@@ -813,13 +813,13 @@ func (r *AWSMachineReconciler) cloudInitUserData(machineScope *scope.MachineScop
813813

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

822-
objectURL, err := objectStoreSvc.Create(scope, userData)
822+
objectURL, err := objectStoreSvc.Create(ctx, scope, userData)
823823
if err != nil {
824824
return nil, errors.Wrap(err, "creating userdata object")
825825
}
@@ -895,7 +895,7 @@ func getIgnitionVersion(scope *scope.MachineScope) string {
895895
return scope.AWSMachine.Spec.Ignition.Version
896896
}
897897

898-
func (r *AWSMachineReconciler) deleteBootstrapData(machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreScope scope.S3Scope) error {
898+
func (r *AWSMachineReconciler) deleteBootstrapData(ctx context.Context, machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, objectStoreScope scope.S3Scope) error {
899899
var userDataFormat string
900900
var err error
901901
if machineScope.Machine.Spec.Bootstrap.DataSecretName != nil {
@@ -913,15 +913,15 @@ func (r *AWSMachineReconciler) deleteBootstrapData(machineScope *scope.MachineSc
913913

914914
if objectStoreScope != nil {
915915
// Bootstrap data will be removed from S3 if it is already populated.
916-
if err := r.deleteIgnitionBootstrapDataFromS3(machineScope, r.getObjectStoreService(objectStoreScope)); err != nil {
916+
if err := r.deleteIgnitionBootstrapDataFromS3(ctx, machineScope, r.getObjectStoreService(objectStoreScope)); err != nil {
917917
return err
918918
}
919919
}
920920

921921
return nil
922922
}
923923

924-
func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *scope.MachineScope, objectStoreSvc services.ObjectStoreInterface) error {
924+
func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(ctx context.Context, machineScope *scope.MachineScope, objectStoreSvc services.ObjectStoreInterface) error {
925925
// 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
926926
if !machineScope.HasFailed() && machineScope.InstanceIsOperational() && machineScope.Machine.Status.NodeRef == nil && !machineScope.AWSMachineIsDeleted() {
927927
return nil
@@ -945,7 +945,7 @@ func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *s
945945
return nil
946946
}
947947

948-
if err := objectStoreSvc.Delete(machineScope); err != nil {
948+
if err := objectStoreSvc.Delete(ctx, machineScope); err != nil {
949949
return errors.Wrap(err, "deleting bootstrap data object")
950950
}
951951

controllers/awsmachine_controller_test.go

+2-2
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)