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 f58f74f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 34 deletions.
15 changes: 4 additions & 11 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 @@ -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)
Expand Down Expand Up @@ -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)
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 f58f74f

Please sign in to comment.