Skip to content

Commit

Permalink
tag volumes with the provider tags (#1455)
Browse files Browse the repository at this point in the history
Pass provider tags to CreateFleet for volumes as well.

Fixes #1430
  • Loading branch information
tzneal committed Mar 2, 2022
1 parent db7ed9c commit 761554c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
7 changes: 3 additions & 4 deletions pkg/cloudprovider/aws/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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"})
createFleetInput := &ec2.CreateFleetInput{
Type: aws.String(ec2.FleetTypeInstant),
LaunchTemplateConfigs: launchTemplateConfigs,
Expand All @@ -123,10 +124,8 @@ func (p *InstanceProvider) launchInstances(ctx context.Context, constraints *v1a
TotalTargetCapacity: aws.Int64(int64(quantity)),
},
TagSpecifications: []*ec2.TagSpecification{
{
ResourceType: aws.String(ec2.ResourceTypeInstance),
Tags: v1alpha1.MergeTags(ctx, constraints.Tags, map[string]string{fmt.Sprintf("kubernetes.io/cluster/%s", injection.GetOptions(ctx).ClusterName): "owned"}),
},
{ResourceType: aws.String(ec2.ResourceTypeInstance), Tags: tags},
{ResourceType: aws.String(ec2.ResourceTypeVolume), Tags: tags},
},
}
if capacityType == v1alpha1.CapacityTypeSpot {
Expand Down
33 changes: 33 additions & 0 deletions pkg/cloudprovider/aws/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"math"
"strings"
"testing"
Expand Down Expand Up @@ -416,6 +417,25 @@ var _ = Describe("Allocation", func() {
Expect(name1).To(Equal(name2))
})

It("should request that tags be applied to both instances and volumes", func() {
provider.Tags = map[string]string{
"tag1": "tag1value",
"tag2": "tag2value",
}
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(2))

// 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)
})

It("should default to a generated launch template", func() {
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod())[0]
ExpectScheduled(ctx, env.Client, pod)
Expand Down Expand Up @@ -754,6 +774,19 @@ var _ = Describe("Allocation", func() {
})
})

// ExpectTags verifies that the expected tags are a subset of the tags found
func ExpectTags(tags []*ec2.Tag, expected map[string]string) {
existingTags := map[string]string{}
for _, tag := range tags {
existingTags[*tag.Key] = *tag.Value
}
for expKey, expValue := range expected {
foundValue, ok := existingTags[expKey]
Expect(ok).To(BeTrue(), fmt.Sprintf("expected to find tag %s in %s", expKey, existingTags))
Expect(foundValue).To(Equal(expValue))
}
}

func ProvisionerWithProvider(provisioner *v1alpha5.Provisioner, provider *v1alpha1.AWS) *v1alpha5.Provisioner {
raw, err := json.Marshal(provider)
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 761554c

Please sign in to comment.