Skip to content

Commit

Permalink
Ensure Volumes and Network Interfaces are also tagged on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
fahlmant committed Jul 16, 2024
1 parent c383c6e commit 7d4d4b7
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 14 deletions.
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

0 comments on commit 7d4d4b7

Please sign in to comment.