diff --git a/controllers/awsmachinepool_controller.go b/controllers/awsmachinepool_controller.go index 1f498b19..377b612c 100644 --- a/controllers/awsmachinepool_controller.go +++ b/controllers/awsmachinepool_controller.go @@ -19,7 +19,6 @@ package controllers import ( "context" "fmt" - "time" awsclientgo "github.com/aws/aws-sdk-go/aws/client" "github.com/aws/aws-sdk-go/service/iam/iamiface" @@ -120,7 +119,7 @@ func (r *AWSMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Reque return r.reconcileNormal(ctx, awsMachinePool, iamService, logger) } -func (r *AWSMachinePoolReconciler) reconcileDelete(ctx context.Context, awsMachinePool *expcapa.AWSMachinePool, iamService *iam.IAMService, logger logr.Logger) (ctrl.Result, error) { +func (r *AWSMachinePoolReconciler) reconcileDelete(ctx context.Context, awsMachinePool *expcapa.AWSMachinePool, iamService *iam.IAMService, logger logr.Logger, clusterName string) (ctrl.Result, error) { roleUsed, err := isRoleUsedElsewhere(ctx, r.Client, awsMachinePool.Spec.AWSLaunchTemplate.IamInstanceProfile) if err != nil { return ctrl.Result{}, errors.WithStack(err) @@ -148,13 +147,10 @@ func (r *AWSMachinePoolReconciler) reconcileDelete(ctx context.Context, awsMachi logger.Info("successfully removed finalizer from AWSMachinePool", "finalizer_name", iam.NodesRole) } - return ctrl.Result{ - Requeue: true, - RequeueAfter: time.Minute * 5, - }, nil + return ctrl.Result{}, nil } -func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, awsMachinePool *expcapa.AWSMachinePool, iamService *iam.IAMService, logger logr.Logger) (ctrl.Result, error) { +func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, awsMachinePool *expcapa.AWSMachinePool, iamService *iam.IAMService, logger logr.Logger, clusterName string) (ctrl.Result, error) { // add finalizer to AWSMachinePool if !controllerutil.ContainsFinalizer(awsMachinePool, key.FinalizerName(iam.NodesRole)) { patchHelper, err := patch.NewHelper(awsMachinePool, r.Client) @@ -175,10 +171,7 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, awsMachi return ctrl.Result{}, errors.WithStack(err) } - return ctrl.Result{ - Requeue: true, - RequeueAfter: time.Minute * 5, - }, nil + return ctrl.Result{}, nil } // SetupWithManager sets up the controller with the Manager. diff --git a/controllers/awsmachinetemplate_controller.go b/controllers/awsmachinetemplate_controller.go index 73e0967e..3c5b6bb8 100644 --- a/controllers/awsmachinetemplate_controller.go +++ b/controllers/awsmachinetemplate_controller.go @@ -19,7 +19,6 @@ package controllers import ( "context" "fmt" - "time" awsclientgo "github.com/aws/aws-sdk-go/aws/client" "github.com/aws/aws-sdk-go/service/iam/iamiface" @@ -232,10 +231,7 @@ func (r *AWSMachineTemplateReconciler) reconcileDelete(ctx context.Context, iamS logger.Info("successfully removed finalizer from configmap", "finalizer_name", iam.ControlPlaneRole) } - return ctrl.Result{ - Requeue: true, - RequeueAfter: time.Minute * 5, - }, nil + return ctrl.Result{}, nil } func (r *AWSMachineTemplateReconciler) reconcileNormal(ctx context.Context, iamService *iam.IAMService, awsMachineTemplate *capa.AWSMachineTemplate, logger logr.Logger, clusterName, namespace, role string) (ctrl.Result, error) { @@ -349,10 +345,7 @@ func (r *AWSMachineTemplateReconciler) reconcileNormal(ctx context.Context, iamS } } - return ctrl.Result{ - Requeue: true, - RequeueAfter: time.Minute * 5, - }, nil + return ctrl.Result{}, nil } // SetupWithManager sets up the controller with the Manager. diff --git a/pkg/key/key.go b/pkg/key/key.go index ef46fe6b..2b94d939 100644 --- a/pkg/key/key.go +++ b/pkg/key/key.go @@ -3,9 +3,8 @@ package key import ( "context" "fmt" - "regexp" - "strings" + awsarn "github.com/aws/aws-sdk-go/aws/arn" corev1 "k8s.io/api/core/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -152,18 +151,10 @@ func GetBaseDomain(ctx context.Context, ctrlClient client.Client, clusterName, n func GetAWSAccountID(awsClusterRoleIdentity *capa.AWSClusterRoleIdentity) (string, error) { arn := awsClusterRoleIdentity.Spec.RoleArn - if arn == "" || len(strings.TrimSpace(arn)) < 1 { - err := fmt.Errorf("unable to extract ARN from AWSClusterRoleIdentity %s", awsClusterRoleIdentity.Name) - return "", err - } - - re := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`) - accountID := re.FindAllString(arn, 1)[0] - - if accountID == "" || len(strings.TrimSpace(accountID)) < 1 { - err := fmt.Errorf("unable to extract AWS account ID from ARN %s", arn) - return "", err + a, err := awsarn.Parse(arn) + if err != nil { + return "", microerror.Mask(err) } - return accountID, nil + return a.AccountID, nil }