Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: ⚠️ aws-sdk-go-v2 refactor starting with s3 #5272

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions controllers/awscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

// Handle non-deleted clusters
return r.reconcileNormal(clusterScope)
return r.reconcileNormal(ctx, clusterScope)
}

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

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

Expand Down Expand Up @@ -297,7 +297,7 @@ func (r *AWSClusterReconciler) reconcileLoadBalancer(clusterScope *scope.Cluster
return nil, nil
}

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

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

if err := s3Service.ReconcileBucket(); err != nil {
if err := s3Service.ReconcileBucket(ctx); err != nil {
conditions.MarkFalse(awsCluster, infrav1.S3BucketReadyCondition, infrav1.S3BucketFailedReason, clusterv1.ConditionSeverityError, "%s", err.Error())
return reconcile.Result{}, errors.Wrapf(err, "failed to reconcile S3 Bucket for AWSCluster %s/%s", awsCluster.Namespace, awsCluster.Name)
}
Expand Down
12 changes: 6 additions & 6 deletions controllers/awscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
},
})

_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())

cluster := &infrav1.AWSCluster{}
Expand All @@ -170,7 +170,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
})).To(Succeed())
// Executing back a second reconciliation:
// the AWS Cluster should be ready with no LoadBalancer false condition.
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())
g.Expect(cs.VPC().ID).To(Equal("vpc-exists"))
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
IsPublic: false,
},
})
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())
g.Expect(cs.VPC().ID).To(Equal("vpc-exists"))
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
IsPublic: false,
},
})
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())
g.Expect(cs.VPC().ID).To(Equal("vpc-exists"))
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
Expand Down Expand Up @@ -469,7 +469,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
reconciler.elbServiceFactory = func(elbScope scope.ELBScope) services.ELBInterface {
return elbSvc
}
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())
g.Expect(cs.VPC().ID).To(Equal("vpc-new"))
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
return ec2Svc
}

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

err = reconciler.reconcileDelete(ctx, cs)
Expand Down
14 changes: 7 additions & 7 deletions controllers/awscluster_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
IsPublic: false,
},
})
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionTrue, "", ""}})
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
PublicIpv4PoolFallBackOrder: ptr.To(infrav1.PublicIpv4PoolFallbackOrderAmazonPool),
}
g.Expect(err).To(Not(HaveOccurred()))
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(Not(HaveOccurred()))
})
})
Expand All @@ -299,7 +299,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
},
)
g.Expect(err).To(BeNil())
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).Should(Equal(expectedErr))
})
t.Run("Should fail AWSCluster create with ClusterSecurityGroupsReadyCondition status false", func(t *testing.T) {
Expand All @@ -320,7 +320,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
},
)
g.Expect(err).To(BeNil())
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).ToNot(BeNil())
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.ClusterSecurityGroupsReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.ClusterSecurityGroupReconciliationFailedReason}})
})
Expand All @@ -343,7 +343,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
},
)
g.Expect(err).To(BeNil())
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).ToNot(BeNil())
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.BastionHostReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.BastionHostFailedReason}})
})
Expand All @@ -367,7 +367,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
},
)
g.Expect(err).To(BeNil())
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).ToNot(BeNil())
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, infrav1.LoadBalancerFailedReason}})
})
Expand All @@ -391,7 +391,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
},
)
g.Expect(err).To(BeNil())
_, err = reconciler.reconcileNormal(cs)
_, err = reconciler.reconcileNormal(context.TODO(), cs)
g.Expect(err).To(BeNil())
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, infrav1.WaitForDNSNameReason}})
})
Expand Down
36 changes: 18 additions & 18 deletions controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ func (r *AWSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
switch infraScope := infraCluster.(type) {
case *scope.ManagedControlPlaneScope:
if !awsMachine.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(machineScope, infraScope, infraScope, nil, nil)
return r.reconcileDelete(ctx, machineScope, infraScope, infraScope, nil, nil)
}

return r.reconcileNormal(ctx, machineScope, infraScope, infraScope, nil, nil)
case *scope.ClusterScope:
if !awsMachine.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(machineScope, infraScope, infraScope, infraScope, infraScope)
return r.reconcileDelete(ctx, machineScope, infraScope, infraScope, infraScope, infraScope)
}

return r.reconcileNormal(ctx, machineScope, infraScope, infraScope, infraScope, infraScope)
Expand Down Expand Up @@ -298,12 +298,12 @@ func (r *AWSMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
)
}

func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope, clusterScope cloud.ClusterScoper, ec2Scope scope.EC2Scope, elbScope scope.ELBScope, objectStoreScope scope.S3Scope) (ctrl.Result, error) {
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) {
machineScope.Info("Handling deleted AWSMachine")

ec2Service := r.getEC2Service(ec2Scope)

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

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

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

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

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

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

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

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

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

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

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

objectURL, err := objectStoreSvc.Create(scope, userData)
objectURL, err := objectStoreSvc.Create(ctx, scope, userData)
if err != nil {
return nil, errors.Wrap(err, "creating userdata object")
}
Expand Down Expand Up @@ -875,7 +875,7 @@ func getIgnitionVersion(scope *scope.MachineScope) string {
return scope.AWSMachine.Spec.Ignition.Version
}

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

if objectStoreScope != nil {
// Bootstrap data will be removed from S3 if it is already populated.
if err := r.deleteIgnitionBootstrapDataFromS3(machineScope, r.getObjectStoreService(objectStoreScope)); err != nil {
if err := r.deleteIgnitionBootstrapDataFromS3(ctx, machineScope, r.getObjectStoreService(objectStoreScope)); err != nil {
return err
}
}

return nil
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions controllers/awsmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestAWSMachineReconcilerIntegrationTests(t *testing.T) {
return elbSvc
}

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

_, err = reconciler.reconcileDelete(ms, cs, cs, cs, cs)
_, err = reconciler.reconcileDelete(context.TODO(), ms, cs, cs, cs, cs)
g.Expect(err).Should(HaveOccurred())
expectConditions(g, ms.AWSMachine, []conditionAssertion{
{infrav1.InstanceReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityWarning, "DeletingFailed"},
Expand Down
Loading