Skip to content

Commit

Permalink
chore: Always disable instance metadata tags (#7012)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Sep 16, 2024
1 parent 3193170 commit b19c64b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions pkg/providers/launchtemplate/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ func (p *DefaultProvider) createLaunchTemplate(ctx context.Context, options *ami
HttpProtocolIpv6: options.MetadataOptions.HTTPProtocolIPv6,
HttpPutResponseHopLimit: options.MetadataOptions.HTTPPutResponseHopLimit,
HttpTokens: options.MetadataOptions.HTTPTokens,
// We statically set the InstanceMetadataTags to "disabled" for all new instances since
// account-wide defaults can override instance defaults on metadata settings
// This can cause instance failure on accounts that default to instance tags since Karpenter
// can't support instance tags with its current tags (e.g. kubernetes.io/cluster/*, karpenter.k8s.aws/ec2nodeclass)
// See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-options.html#instance-metadata-options-order-of-precedence
InstanceMetadataTags: lo.ToPtr(ec2.InstanceMetadataTagsStateDisabled),
},
NetworkInterfaces: networkInterfaces,
TagSpecifications: launchTemplateDataTags,
Expand Down
30 changes: 25 additions & 5 deletions pkg/providers/launchtemplate/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ var _ = Describe("LaunchTemplate Provider", func() {
})
})
It("should use custom block device mapping", func() {
nodeClass.Spec.AMISelectorTerms = []v1.AMISelectorTerm{{Alias: "al2@latest"}}
nodeClass.Spec.BlockDeviceMappings = []*v1.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/xvda"),
Expand Down Expand Up @@ -676,7 +675,6 @@ var _ = Describe("LaunchTemplate Provider", func() {
})
})
It("should round up for custom block device mappings when specified in gigabytes", func() {
nodeClass.Spec.AMISelectorTerms = []v1.AMISelectorTerm{{Alias: "al2@latest"}}
nodeClass.Spec.BlockDeviceMappings = []*v1.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/xvda"),
Expand Down Expand Up @@ -1362,7 +1360,6 @@ var _ = Describe("LaunchTemplate Provider", func() {
ExpectLaunchTemplatesCreatedWithUserDataNotContaining(corev1.LabelNamespaceNodeRestriction)
})
It("should specify --local-disks raid0 when instance-store policy is set on AL2", func() {
nodeClass.Spec.AMISelectorTerms = []v1.AMISelectorTerm{{Alias: "al2@latest"}}
nodeClass.Spec.InstanceStorePolicy = lo.ToPtr(v1.InstanceStorePolicyRAID0)
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod()
Expand Down Expand Up @@ -2122,7 +2119,6 @@ var _ = Describe("LaunchTemplate Provider", func() {
})
Context("Detailed Monitoring", func() {
It("should default detailed monitoring to off", func() {
nodeClass.Spec.AMISelectorTerms = []v1.AMISelectorTerm{{Alias: "al2@latest"}}
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod()
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
Expand All @@ -2133,7 +2129,6 @@ var _ = Describe("LaunchTemplate Provider", func() {
})
})
It("should pass detailed monitoring setting to the launch template at creation", func() {
nodeClass.Spec.AMISelectorTerms = []v1.AMISelectorTerm{{Alias: "al2@latest"}}
nodeClass.Spec.DetailedMonitoring = aws.Bool(true)
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod()
Expand All @@ -2145,6 +2140,31 @@ var _ = Describe("LaunchTemplate Provider", func() {
})
})
})
Context("Instance Metadata", func() {
It("should set the default instance metadata settings on instances", func() {
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod()
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
ExpectScheduled(ctx, env.Client, pod)
Expect(awsEnv.EC2API.CalledWithCreateLaunchTemplateInput.Len()).To(BeNumerically(">=", 1))
awsEnv.EC2API.CalledWithCreateLaunchTemplateInput.ForEach(func(ltInput *ec2.CreateLaunchTemplateInput) {
Expect(lo.FromPtr(ltInput.LaunchTemplateData.MetadataOptions.HttpEndpoint)).To(Equal(ec2.InstanceMetadataEndpointStateEnabled))
Expect(lo.FromPtr(ltInput.LaunchTemplateData.MetadataOptions.HttpProtocolIpv6)).To(Equal(ec2.InstanceMetadataEndpointStateDisabled))
Expect(lo.FromPtr(ltInput.LaunchTemplateData.MetadataOptions.HttpPutResponseHopLimit)).To(BeNumerically("==", 1))
Expect(lo.FromPtr(ltInput.LaunchTemplateData.MetadataOptions.HttpTokens)).To(Equal(ec2.HttpTokensStateRequired))
})
})
It("should set instance metadata tags to disabled", func() {
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod()
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
ExpectScheduled(ctx, env.Client, pod)
Expect(awsEnv.EC2API.CalledWithCreateLaunchTemplateInput.Len()).To(BeNumerically(">=", 1))
awsEnv.EC2API.CalledWithCreateLaunchTemplateInput.ForEach(func(ltInput *ec2.CreateLaunchTemplateInput) {
Expect(lo.FromPtr(ltInput.LaunchTemplateData.MetadataOptions.InstanceMetadataTags)).To(Equal(ec2.InstanceMetadataTagsStateDisabled))
})
})
})
})

// ExpectTags verifies that the expected tags are a subset of the tags found
Expand Down

0 comments on commit b19c64b

Please sign in to comment.