Skip to content

Commit

Permalink
use aws arn package to extract account, drop requeue
Browse files Browse the repository at this point in the history
  • Loading branch information
Berk Dehrioglu committed Sep 21, 2023
1 parent 2f4b4e1 commit 59bdcdd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
11 changes: 2 additions & 9 deletions controllers/awsmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -148,10 +147,7 @@ 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) {
Expand All @@ -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.
Expand Down
11 changes: 2 additions & 9 deletions controllers/awsmachinetemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 5 additions & 14 deletions pkg/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 59bdcdd

Please sign in to comment.