Skip to content

Commit

Permalink
Apply tags to resources created by Karpenter
Browse files Browse the repository at this point in the history
  • Loading branch information
dewjam committed Mar 14, 2022
1 parent 3266cdc commit 4839c40
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
8 changes: 7 additions & 1 deletion pkg/cloudprovider/aws/apis/v1alpha1/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ import (

func MergeTags(ctx context.Context, custom ...map[string]string) (result []*ec2.Tag) {
tags := map[string]string{
// karpenter.sh/provisioner-name: <provisioner-name>
v1alpha5.ProvisionerNameLabelKey: injection.GetNamespacedName(ctx).Name,
"Name": fmt.Sprintf("%s/%s", v1alpha5.ProvisionerNameLabelKey, injection.GetNamespacedName(ctx).Name),
// karpenter.sh/cluster/<cluster-name>: owned
fmt.Sprintf("%s/cluster/%s", v1alpha5.Group, injection.GetOptions(ctx).ClusterName): "owned",
// kubernetes.io/cluster/<cluster-name>: owned
fmt.Sprintf("kubernetes.io/cluster/%s", injection.GetOptions(ctx).ClusterName): "owned",
// Name: karpenter.sh/cluster/<cluster-name>/provisioner/<provisioner-name>
"Name": fmt.Sprintf("%s/cluster/%s/provisioner/%s", v1alpha5.Group, injection.GetOptions(ctx).ClusterName, injection.GetNamespacedName(ctx).Name),
}
// Custom tags may override defaults (e.g. Name)
for _, t := range custom {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cloudprovider/aws/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (p *InstanceProvider) launchInstances(ctx context.Context, constraints *v1a
return nil, fmt.Errorf("getting launch template configs, %w", err)
}
// Create fleet
tags := v1alpha1.MergeTags(ctx, constraints.Tags, map[string]string{fmt.Sprintf("kubernetes.io/cluster/%s", injection.GetOptions(ctx).ClusterName): "owned"})
tags := v1alpha1.MergeTags(ctx, constraints.Tags)
createFleetInput := &ec2.CreateFleetInput{
Type: aws.String(ec2.FleetTypeInstant),
LaunchTemplateConfigs: launchTemplateConfigs,
Expand All @@ -148,6 +148,7 @@ func (p *InstanceProvider) launchInstances(ctx context.Context, constraints *v1a
TagSpecifications: []*ec2.TagSpecification{
{ResourceType: aws.String(ec2.ResourceTypeInstance), Tags: tags},
{ResourceType: aws.String(ec2.ResourceTypeVolume), Tags: tags},
{ResourceType: aws.String(ec2.ResourceTypeFleet), Tags: tags},
},
}
if capacityType == v1alpha1.CapacityTypeSpot {
Expand Down
59 changes: 57 additions & 2 deletions pkg/cloudprovider/aws/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ var _ = Describe("Allocation", func() {
ExpectScheduled(ctx, env.Client, pod)
Expect(fakeEC2API.CalledWithCreateFleetInput.Cardinality()).To(Equal(1))
createFleetInput := fakeEC2API.CalledWithCreateFleetInput.Pop().(*ec2.CreateFleetInput)
Expect(createFleetInput.TagSpecifications).To(HaveLen(2))
Expect(createFleetInput.TagSpecifications).To(HaveLen(3))

// tags should be included in both the instance and volume tag specification
Expect(*createFleetInput.TagSpecifications[0].ResourceType).To(Equal(ec2.ResourceTypeInstance))
Expand All @@ -497,14 +497,49 @@ var _ = Describe("Allocation", func() {
ExpectScheduled(ctx, env.Client, pod)
Expect(fakeEC2API.CalledWithCreateFleetInput.Cardinality()).To(Equal(1))
createFleetInput := fakeEC2API.CalledWithCreateFleetInput.Pop().(*ec2.CreateFleetInput)
Expect(createFleetInput.TagSpecifications).To(HaveLen(2))
Expect(createFleetInput.TagSpecifications).To(HaveLen(3))

// tags should be included in both the instance and volume tag specification
Expect(*createFleetInput.TagSpecifications[0].ResourceType).To(Equal(ec2.ResourceTypeInstance))
ExpectTags(createFleetInput.TagSpecifications[0].Tags, provider.Tags)

Expect(*createFleetInput.TagSpecifications[1].ResourceType).To(Equal(ec2.ResourceTypeVolume))
ExpectTags(createFleetInput.TagSpecifications[1].Tags, provider.Tags)

Expect(fakeEC2API.CalledWithCreateLaunchTemplateInput.Cardinality()).To(Equal(1))
createLaunchTemplateInput := fakeEC2API.CalledWithCreateLaunchTemplateInput.Pop().(*ec2.CreateLaunchTemplateInput)
// default tags should be included in the LaunchTemplate specification
Expect(*createLaunchTemplateInput.TagSpecifications[0].ResourceType).To(Equal(ec2.ResourceTypeLaunchTemplate))
ExpectTags(createLaunchTemplateInput.TagSpecifications[0].Tags, provider.Tags)
})

It("should apply default tags if not overriden", func() {
// default tags applied to all created resources
defaultTags := map[string]string{
v1alpha5.ProvisionerNameLabelKey: provisioner.Name,
fmt.Sprintf("karpenter.sh/cluster/%s", opts.ClusterName): "owned",
fmt.Sprintf("kubernetes.io/cluster/%s", opts.ClusterName): "owned",
"Name": fmt.Sprintf("karpenter.sh/cluster/%s/provisioner/%s", opts.ClusterName, provisioner.Name),
}

pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, ProvisionerWithProvider(provisioner, provider), test.UnschedulablePod())[0]
ExpectScheduled(ctx, env.Client, pod)
Expect(fakeEC2API.CalledWithCreateFleetInput.Cardinality()).To(Equal(1))
createFleetInput := fakeEC2API.CalledWithCreateFleetInput.Pop().(*ec2.CreateFleetInput)
Expect(createFleetInput.TagSpecifications).To(HaveLen(3))

// default tags should be included in the Instance specification
Expect(*createFleetInput.TagSpecifications[0].ResourceType).To(Equal(ec2.ResourceTypeInstance))
ExpectTags(createFleetInput.TagSpecifications[0].Tags, defaultTags)
// default tags should be included in the Volume specification
Expect(*createFleetInput.TagSpecifications[1].ResourceType).To(Equal(ec2.ResourceTypeVolume))
ExpectTags(createFleetInput.TagSpecifications[1].Tags, defaultTags)

Expect(fakeEC2API.CalledWithCreateLaunchTemplateInput.Cardinality()).To(Equal(1))
createLaunchTemplateInput := fakeEC2API.CalledWithCreateLaunchTemplateInput.Pop().(*ec2.CreateLaunchTemplateInput)
// default tags should be included in the LaunchTemplate specification
Expect(*createLaunchTemplateInput.TagSpecifications[0].ResourceType).To(Equal(ec2.ResourceTypeLaunchTemplate))
ExpectTags(createLaunchTemplateInput.TagSpecifications[0].Tags, defaultTags)
})

It("should default to a generated launch template", func() {
Expand Down Expand Up @@ -533,6 +568,26 @@ var _ = Describe("Allocation", func() {
Expect(*launchTemplate.Version).To(Equal("$Latest"))
})
})
Context("Fleets", func() {
It("should have default tags applied", func() {
// default tags applied to all created resources
defaultTags := map[string]string{
v1alpha5.ProvisionerNameLabelKey: provisioner.Name,
fmt.Sprintf("karpenter.sh/cluster/%s", opts.ClusterName): "owned",
fmt.Sprintf("kubernetes.io/cluster/%s", opts.ClusterName): "owned",
"Name": fmt.Sprintf("karpenter.sh/cluster/%s/provisioner/%s", opts.ClusterName, provisioner.Name),
}

pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, ProvisionerWithProvider(provisioner, provider), test.UnschedulablePod())[0]
ExpectScheduled(ctx, env.Client, pod)
Expect(fakeEC2API.CalledWithCreateFleetInput.Cardinality()).To(Equal(1))
createFleetInput := fakeEC2API.CalledWithCreateFleetInput.Pop().(*ec2.CreateFleetInput)

// default tags should be included in the fleet tag specifications
Expect(*createFleetInput.TagSpecifications[2].ResourceType).To(Equal(ec2.ResourceTypeFleet))
ExpectTags(createFleetInput.TagSpecifications[2].Tags, defaultTags)
})
})
Context("Subnets", func() {
It("should default to the cluster's subnets", func() {
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, ProvisionerWithProvider(provisioner, provider), test.UnschedulablePod())[0]
Expand Down

0 comments on commit 4839c40

Please sign in to comment.