Skip to content

Commit

Permalink
chore: Support GetProfileName() through Instance profile interface (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Apr 9, 2024
1 parent 2cb186c commit 3fbaf66
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
5 changes: 2 additions & 3 deletions pkg/controllers/nodeclass/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclass"
"github.com/aws/karpenter-provider-aws/pkg/fake"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/instanceprofile"
"github.com/aws/karpenter-provider-aws/pkg/test"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -1016,7 +1015,7 @@ var _ = Describe("NodeClassController", func() {
Context("NodeClass Termination", func() {
var profileName string
BeforeEach(func() {
profileName = instanceprofile.GetProfileName(ctx, fake.DefaultRegion, nodeClass)
profileName = awsEnv.InstanceProfileProvider.GetProfileName(ctx, fake.DefaultRegion, nodeClass.Name)
})
It("should not delete the NodeClass if launch template deletion fails", func() {
launchTemplateName := aws.String(fake.LaunchTemplateName())
Expand Down Expand Up @@ -1189,7 +1188,7 @@ var _ = Describe("NodeClassController", func() {
Context("Instance Profile Status", func() {
var profileName string
BeforeEach(func() {
profileName = instanceprofile.GetProfileName(ctx, fake.DefaultRegion, nodeClass)
profileName = awsEnv.InstanceProfileProvider.GetProfileName(ctx, fake.DefaultRegion, nodeClass.Name)
})
It("should create the instance profile when it doesn't exist", func() {
nodeClass.Spec.Role = "test-role"
Expand Down
9 changes: 5 additions & 4 deletions pkg/providers/instanceprofile/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
type Provider interface {
Create(context.Context, *v1beta1.EC2NodeClass) (string, error)
Delete(context.Context, *v1beta1.EC2NodeClass) error
GetProfileName(ctx context.Context, region, nodeClassName string) string
}

type DefaultProvider struct {
Expand All @@ -59,7 +60,7 @@ func (p *DefaultProvider) Create(ctx context.Context, nodeClass *v1beta1.EC2Node
v1beta1.LabelNodeClass: nodeClass.Name,
v1.LabelTopologyRegion: p.region,
})
profileName := GetProfileName(ctx, p.region, nodeClass)
profileName := p.GetProfileName(ctx, p.region, nodeClass.Name)

// An instance profile exists for this NodeClass
if _, ok := p.cache.Get(string(nodeClass.UID)); ok {
Expand Down Expand Up @@ -107,7 +108,7 @@ func (p *DefaultProvider) Create(ctx context.Context, nodeClass *v1beta1.EC2Node
}

func (p *DefaultProvider) Delete(ctx context.Context, nodeClass *v1beta1.EC2NodeClass) error {
profileName := GetProfileName(ctx, p.region, nodeClass)
profileName := p.GetProfileName(ctx, p.region, nodeClass.Name)
out, err := p.iamapi.GetInstanceProfileWithContext(ctx, &iam.GetInstanceProfileInput{
InstanceProfileName: aws.String(profileName),
})
Expand All @@ -134,6 +135,6 @@ func (p *DefaultProvider) Delete(ctx context.Context, nodeClass *v1beta1.EC2Node

// GetProfileName gets the string for the profile name based on the cluster name and the NodeClass UUID.
// The length of this string can never exceed the maximum instance profile name limit of 128 characters.
func GetProfileName(ctx context.Context, region string, nodeClass *v1beta1.EC2NodeClass) string {
return fmt.Sprintf("%s_%d", options.FromContext(ctx).ClusterName, lo.Must(hashstructure.Hash(fmt.Sprintf("%s%s", region, nodeClass.Name), hashstructure.FormatV2, nil)))
func (p *DefaultProvider) GetProfileName(ctx context.Context, region, nodeClassName string) string {
return fmt.Sprintf("%s_%d", options.FromContext(ctx).ClusterName, lo.Must(hashstructure.Hash(fmt.Sprintf("%s%s", region, nodeClassName), hashstructure.FormatV2, nil)))
}

0 comments on commit 3fbaf66

Please sign in to comment.