Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Ensure Volumes and Network Interfaces are also tagged when created as part of instance creation #5057

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pkg/cloud/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,21 +616,25 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
}

if len(i.Tags) > 0 {
spec := &ec2.TagSpecification{ResourceType: aws.String(ec2.ResourceTypeInstance)}
// We need to sort keys for tests to work
keys := make([]string, 0, len(i.Tags))
for k := range i.Tags {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
spec.Tags = append(spec.Tags, &ec2.Tag{
Key: aws.String(key),
Value: aws.String(i.Tags[key]),
})
}
resources := []string{ec2.ResourceTypeInstance, ec2.ResourceTypeVolume, ec2.ResourceTypeNetworkInterface}
for _, r := range resources {
spec := &ec2.TagSpecification{ResourceType: aws.String(r)}

// We need to sort keys for tests to work
keys := make([]string, 0, len(i.Tags))
for k := range i.Tags {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
spec.Tags = append(spec.Tags, &ec2.Tag{
Key: aws.String(key),
Value: aws.String(i.Tags[key]),
})
}

input.TagSpecifications = append(input.TagSpecifications, spec)
input.TagSpecifications = append(input.TagSpecifications, spec)
}
}

input.InstanceMarketOptions = getInstanceMarketOptionsRequest(i.SpotMarketOptions)
Expand Down
300 changes: 300 additions & 0 deletions pkg/cloud/services/ec2/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,56 @@ func TestCreateInstance(t *testing.T) {
},
},
},
{
ResourceType: aws.String("volume"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("/"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
{
ResourceType: aws.String("network-interface"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("/"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
},
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
MaxCount: aws.Int64(1),
Expand Down Expand Up @@ -901,6 +951,56 @@ func TestCreateInstance(t *testing.T) {
},
},
},
{
ResourceType: aws.String("volume"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("/"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
{
ResourceType: aws.String("network-interface"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("/"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
},
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
MaxCount: aws.Int64(1),
Expand Down Expand Up @@ -3274,6 +3374,56 @@ func TestCreateInstance(t *testing.T) {
},
},
},
{
ResourceType: aws.String("volume"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
{
ResourceType: aws.String("network-interface"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
},
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
})).
Expand Down Expand Up @@ -3433,6 +3583,56 @@ func TestCreateInstance(t *testing.T) {
},
},
},
{
ResourceType: aws.String("volume"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
{
ResourceType: aws.String("network-interface"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
},
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
})).
Expand Down Expand Up @@ -3613,6 +3813,56 @@ func TestCreateInstance(t *testing.T) {
},
},
},
{
ResourceType: aws.String("volume"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
{
ResourceType: aws.String("network-interface"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
},
UserData: aws.String(base64.StdEncoding.EncodeToString(data)),
})).
Expand Down Expand Up @@ -3754,6 +4004,56 @@ func TestCreateInstance(t *testing.T) {
},
},
},
{
ResourceType: aws.String("volume"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
{
ResourceType: aws.String("network-interface"),
Tags: []*ec2.Tag{
{
Key: aws.String("MachineName"),
Value: aws.String("default/machine-aws-test1"),
},
{
Key: aws.String("Name"),
Value: aws.String("aws-test1"),
},
{
Key: aws.String("kubernetes.io/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
Value: aws.String("owned"),
},
{
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
Value: aws.String("node"),
},
},
},
},
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
})).
Expand Down