Skip to content

Commit 69ac905

Browse files
committed
chore: allow paths to be conditionally disabled
1 parent ed7846a commit 69ac905

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

charts/karpenter-crd/templates/karpenter.k8s.aws_ec2nodeclasses.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
{{- with .Values.additionalAnnotations }}
77
{{- toYaml . | nindent 4 }}
88
{{- end }}
9-
controller-gen.kubebuilder.io/version: v0.18.0
9+
controller-gen.kubebuilder.io/version: v0.19.0
1010
name: ec2nodeclasses.karpenter.k8s.aws
1111
spec:
1212
group: karpenter.k8s.aws

pkg/apis/crds/karpenter.k8s.aws_ec2nodeclasses.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.18.0
6+
controller-gen.kubebuilder.io/version: v0.19.0
77
name: ec2nodeclasses.karpenter.k8s.aws
88
spec:
99
group: karpenter.k8s.aws

pkg/controllers/nodeclass/instanceprofile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (ip *InstanceProfile) Reconcile(ctx context.Context, nodeClass *v1.EC2NodeC
8585
nodeClass.InstanceProfileRole(),
8686
nodeClass.InstanceProfileTags(options.FromContext(ctx).ClusterName, ip.region),
8787
string(nodeClass.UID),
88+
true,
8889
); err != nil {
8990
// If we failed Create, we may have successfully created the instance profile but failed to either attach the new
9091
// role or remove the existing role. To prevent runaway instance profile creation, we'll attempt to delete the

pkg/providers/instanceprofile/instanceprofile.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636

3737
type Provider interface {
3838
Get(context.Context, string) (*iamtypes.InstanceProfile, error)
39-
Create(context.Context, string, string, map[string]string, string) error
39+
Create(context.Context, string, string, map[string]string, string, bool) error
4040
Delete(context.Context, string) error
4141
ListClusterProfiles(context.Context) ([]*iamtypes.InstanceProfile, error)
4242
ListNodeClassProfiles(context.Context, *v1.EC2NodeClass) ([]*iamtypes.InstanceProfile, error)
@@ -87,7 +87,14 @@ func (p *DefaultProvider) Get(ctx context.Context, instanceProfileName string) (
8787
return out.InstanceProfile, nil
8888
}
8989

90-
func (p *DefaultProvider) Create(ctx context.Context, instanceProfileName string, roleName string, tags map[string]string, nodeClassUID string) error {
90+
func (p *DefaultProvider) Create(
91+
ctx context.Context,
92+
instanceProfileName string,
93+
roleName string,
94+
tags map[string]string,
95+
nodeClassUID string,
96+
usePath bool,
97+
) error {
9198
// Don't attempt to create an instance profile if the role hasn't been found. This prevents runaway instance profile
9299
// creation by the NodeClass controller when there's a missing role.
93100
if err, ok := p.roleNotFoundErrorCache.HasError(roleName); ok {
@@ -100,11 +107,14 @@ func (p *DefaultProvider) Create(ctx context.Context, instanceProfileName string
100107
if !awserrors.IsNotFound(err) {
101108
return serrors.Wrap(fmt.Errorf("getting instance profile, %w", err), "instance-profile", instanceProfileName)
102109
}
103-
o, err := p.iamapi.CreateInstanceProfile(ctx, &iam.CreateInstanceProfileInput{
110+
input := &iam.CreateInstanceProfileInput{
104111
InstanceProfileName: lo.ToPtr(instanceProfileName),
105112
Tags: utils.IAMMergeTags(tags),
106-
Path: lo.ToPtr(fmt.Sprintf("/karpenter/%s/%s/%s/", p.region, options.FromContext(ctx).ClusterName, nodeClassUID)),
107-
})
113+
}
114+
if usePath {
115+
input.Path = lo.ToPtr(fmt.Sprintf("/karpenter/%s/%s/%s/", p.region, options.FromContext(ctx).ClusterName, nodeClassUID))
116+
}
117+
o, err := p.iamapi.CreateInstanceProfile(ctx, input)
108118
if err != nil {
109119
return serrors.Wrap(err, "instance-profile", instanceProfileName)
110120
}

pkg/providers/instanceprofile/suite_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ var _ = Describe("InstanceProfileProvider", func() {
112112
func(roleWithPath, role string) {
113113
const profileName = "profile-A"
114114
nodeClass.Spec.Role = roleWithPath
115-
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, role, nil, string(nodeClass.UID))).To(Succeed())
115+
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, role, nil, string(nodeClass.UID), true)).To(Succeed())
116116
Expect(profileName).ToNot(BeNil())
117117
Expect(awsEnv.IAMAPI.InstanceProfiles[profileName].Roles).To(HaveLen(1))
118118
Expect(aws.ToString(awsEnv.IAMAPI.InstanceProfiles[profileName].Roles[0].RoleName)).To(Equal(role))
@@ -269,7 +269,7 @@ var _ = Describe("InstanceProfileProvider", func() {
269269
nodeClassUID := "test-uid"
270270
expectedPath := fmt.Sprintf("/karpenter/%s/%s/%s/", fake.DefaultRegion, options.FromContext(ctx).ClusterName, nodeClassUID)
271271

272-
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, nodeRole, nil, nodeClassUID)).To(Succeed())
272+
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, nodeRole, nil, nodeClassUID, true)).To(Succeed())
273273

274274
// Get the created profile
275275
profile, err := awsEnv.InstanceProfileProvider.Get(ctx, profileName)
@@ -287,7 +287,7 @@ var _ = Describe("InstanceProfileProvider", func() {
287287
profileName := "profile-A"
288288
nodeClassUID := "test-uid"
289289

290-
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, nodeRole, nil, nodeClassUID)).To(Succeed())
290+
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, nodeRole, nil, nodeClassUID, true)).To(Succeed())
291291

292292
// Verify profile exists
293293
Expect(awsEnv.IAMAPI.InstanceProfiles).To(HaveKey(profileName))
@@ -308,7 +308,7 @@ var _ = Describe("InstanceProfileProvider", func() {
308308
It("should reflect IsProtected updates", func() {
309309
// Create a profile
310310
profileName := "profile-A"
311-
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, nodeRole, nil, "test-uid")).To(Succeed())
311+
Expect(awsEnv.InstanceProfileProvider.Create(ctx, profileName, nodeRole, nil, "test-uid", true)).To(Succeed())
312312

313313
// Initially should not be protected (protection is set in instance profile reconciler)
314314
Expect(awsEnv.InstanceProfileProvider.IsProtected(profileName)).To(BeFalse())
@@ -331,14 +331,14 @@ var _ = Describe("InstanceProfileProvider", func() {
331331
}
332332
})
333333
It("should not cache role not found errors when the role exists", func() {
334-
err := awsEnv.InstanceProfileProvider.Create(ctx, "test-profile", roleName, nil, "test-uid")
334+
err := awsEnv.InstanceProfileProvider.Create(ctx, "test-profile", roleName, nil, "test-uid", true)
335335
Expect(err).ToNot(HaveOccurred())
336336
_, ok := awsEnv.RoleCache.Get(roleName)
337337
Expect(ok).To(BeFalse())
338338
})
339339
It("should cache role not found errors when the role does not", func() {
340340
missingRoleName := "non-existent-role"
341-
err := awsEnv.InstanceProfileProvider.Create(ctx, "test-profile", missingRoleName, nil, "test-uid")
341+
err := awsEnv.InstanceProfileProvider.Create(ctx, "test-profile", missingRoleName, nil, "test-uid", true)
342342
Expect(err).To(HaveOccurred())
343343
_, ok := awsEnv.RoleCache.Get(missingRoleName)
344344
Expect(ok).To(BeTrue())
@@ -347,7 +347,7 @@ var _ = Describe("InstanceProfileProvider", func() {
347347
missingRoleName := "non-existent-role"
348348
awsEnv.RoleCache.SetDefault(missingRoleName, errors.New("role not found"))
349349

350-
err := awsEnv.InstanceProfileProvider.Create(ctx, "test-profile", missingRoleName, nil, "test-uid")
350+
err := awsEnv.InstanceProfileProvider.Create(ctx, "test-profile", missingRoleName, nil, "test-uid", true)
351351
Expect(err).To(HaveOccurred())
352352

353353
Expect(awsEnv.IAMAPI.InstanceProfiles).To(HaveLen(0))

0 commit comments

Comments
 (0)