Skip to content

Commit 7b37581

Browse files
authored
Merge pull request #5057 from fahlmant/create-instance-tag-vol-eni
✨ Ensure Volumes and Network Interfaces are also tagged when created as part of instance creation
2 parents 6e43273 + 7d4d4b7 commit 7b37581

File tree

2 files changed

+318
-14
lines changed

2 files changed

+318
-14
lines changed

pkg/cloud/services/ec2/instances.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -616,21 +616,25 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
616616
}
617617

618618
if len(i.Tags) > 0 {
619-
spec := &ec2.TagSpecification{ResourceType: aws.String(ec2.ResourceTypeInstance)}
620-
// We need to sort keys for tests to work
621-
keys := make([]string, 0, len(i.Tags))
622-
for k := range i.Tags {
623-
keys = append(keys, k)
624-
}
625-
sort.Strings(keys)
626-
for _, key := range keys {
627-
spec.Tags = append(spec.Tags, &ec2.Tag{
628-
Key: aws.String(key),
629-
Value: aws.String(i.Tags[key]),
630-
})
631-
}
619+
resources := []string{ec2.ResourceTypeInstance, ec2.ResourceTypeVolume, ec2.ResourceTypeNetworkInterface}
620+
for _, r := range resources {
621+
spec := &ec2.TagSpecification{ResourceType: aws.String(r)}
622+
623+
// We need to sort keys for tests to work
624+
keys := make([]string, 0, len(i.Tags))
625+
for k := range i.Tags {
626+
keys = append(keys, k)
627+
}
628+
sort.Strings(keys)
629+
for _, key := range keys {
630+
spec.Tags = append(spec.Tags, &ec2.Tag{
631+
Key: aws.String(key),
632+
Value: aws.String(i.Tags[key]),
633+
})
634+
}
632635

633-
input.TagSpecifications = append(input.TagSpecifications, spec)
636+
input.TagSpecifications = append(input.TagSpecifications, spec)
637+
}
634638
}
635639

636640
input.InstanceMarketOptions = getInstanceMarketOptionsRequest(i.SpotMarketOptions)

pkg/cloud/services/ec2/instances_test.go

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,56 @@ func TestCreateInstance(t *testing.T) {
703703
},
704704
},
705705
},
706+
{
707+
ResourceType: aws.String("volume"),
708+
Tags: []*ec2.Tag{
709+
{
710+
Key: aws.String("MachineName"),
711+
Value: aws.String("/"),
712+
},
713+
{
714+
Key: aws.String("Name"),
715+
Value: aws.String("aws-test1"),
716+
},
717+
{
718+
Key: aws.String("kubernetes.io/cluster/test1"),
719+
Value: aws.String("owned"),
720+
},
721+
{
722+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
723+
Value: aws.String("owned"),
724+
},
725+
{
726+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
727+
Value: aws.String("node"),
728+
},
729+
},
730+
},
731+
{
732+
ResourceType: aws.String("network-interface"),
733+
Tags: []*ec2.Tag{
734+
{
735+
Key: aws.String("MachineName"),
736+
Value: aws.String("/"),
737+
},
738+
{
739+
Key: aws.String("Name"),
740+
Value: aws.String("aws-test1"),
741+
},
742+
{
743+
Key: aws.String("kubernetes.io/cluster/test1"),
744+
Value: aws.String("owned"),
745+
},
746+
{
747+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
748+
Value: aws.String("owned"),
749+
},
750+
{
751+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
752+
Value: aws.String("node"),
753+
},
754+
},
755+
},
706756
},
707757
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
708758
MaxCount: aws.Int64(1),
@@ -901,6 +951,56 @@ func TestCreateInstance(t *testing.T) {
901951
},
902952
},
903953
},
954+
{
955+
ResourceType: aws.String("volume"),
956+
Tags: []*ec2.Tag{
957+
{
958+
Key: aws.String("MachineName"),
959+
Value: aws.String("/"),
960+
},
961+
{
962+
Key: aws.String("Name"),
963+
Value: aws.String("aws-test1"),
964+
},
965+
{
966+
Key: aws.String("kubernetes.io/cluster/test1"),
967+
Value: aws.String("owned"),
968+
},
969+
{
970+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
971+
Value: aws.String("owned"),
972+
},
973+
{
974+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
975+
Value: aws.String("node"),
976+
},
977+
},
978+
},
979+
{
980+
ResourceType: aws.String("network-interface"),
981+
Tags: []*ec2.Tag{
982+
{
983+
Key: aws.String("MachineName"),
984+
Value: aws.String("/"),
985+
},
986+
{
987+
Key: aws.String("Name"),
988+
Value: aws.String("aws-test1"),
989+
},
990+
{
991+
Key: aws.String("kubernetes.io/cluster/test1"),
992+
Value: aws.String("owned"),
993+
},
994+
{
995+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
996+
Value: aws.String("owned"),
997+
},
998+
{
999+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
1000+
Value: aws.String("node"),
1001+
},
1002+
},
1003+
},
9041004
},
9051005
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
9061006
MaxCount: aws.Int64(1),
@@ -3274,6 +3374,56 @@ func TestCreateInstance(t *testing.T) {
32743374
},
32753375
},
32763376
},
3377+
{
3378+
ResourceType: aws.String("volume"),
3379+
Tags: []*ec2.Tag{
3380+
{
3381+
Key: aws.String("MachineName"),
3382+
Value: aws.String("default/machine-aws-test1"),
3383+
},
3384+
{
3385+
Key: aws.String("Name"),
3386+
Value: aws.String("aws-test1"),
3387+
},
3388+
{
3389+
Key: aws.String("kubernetes.io/cluster/test1"),
3390+
Value: aws.String("owned"),
3391+
},
3392+
{
3393+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
3394+
Value: aws.String("owned"),
3395+
},
3396+
{
3397+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
3398+
Value: aws.String("node"),
3399+
},
3400+
},
3401+
},
3402+
{
3403+
ResourceType: aws.String("network-interface"),
3404+
Tags: []*ec2.Tag{
3405+
{
3406+
Key: aws.String("MachineName"),
3407+
Value: aws.String("default/machine-aws-test1"),
3408+
},
3409+
{
3410+
Key: aws.String("Name"),
3411+
Value: aws.String("aws-test1"),
3412+
},
3413+
{
3414+
Key: aws.String("kubernetes.io/cluster/test1"),
3415+
Value: aws.String("owned"),
3416+
},
3417+
{
3418+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
3419+
Value: aws.String("owned"),
3420+
},
3421+
{
3422+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
3423+
Value: aws.String("node"),
3424+
},
3425+
},
3426+
},
32773427
},
32783428
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
32793429
})).
@@ -3433,6 +3583,56 @@ func TestCreateInstance(t *testing.T) {
34333583
},
34343584
},
34353585
},
3586+
{
3587+
ResourceType: aws.String("volume"),
3588+
Tags: []*ec2.Tag{
3589+
{
3590+
Key: aws.String("MachineName"),
3591+
Value: aws.String("default/machine-aws-test1"),
3592+
},
3593+
{
3594+
Key: aws.String("Name"),
3595+
Value: aws.String("aws-test1"),
3596+
},
3597+
{
3598+
Key: aws.String("kubernetes.io/cluster/test1"),
3599+
Value: aws.String("owned"),
3600+
},
3601+
{
3602+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
3603+
Value: aws.String("owned"),
3604+
},
3605+
{
3606+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
3607+
Value: aws.String("node"),
3608+
},
3609+
},
3610+
},
3611+
{
3612+
ResourceType: aws.String("network-interface"),
3613+
Tags: []*ec2.Tag{
3614+
{
3615+
Key: aws.String("MachineName"),
3616+
Value: aws.String("default/machine-aws-test1"),
3617+
},
3618+
{
3619+
Key: aws.String("Name"),
3620+
Value: aws.String("aws-test1"),
3621+
},
3622+
{
3623+
Key: aws.String("kubernetes.io/cluster/test1"),
3624+
Value: aws.String("owned"),
3625+
},
3626+
{
3627+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
3628+
Value: aws.String("owned"),
3629+
},
3630+
{
3631+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
3632+
Value: aws.String("node"),
3633+
},
3634+
},
3635+
},
34363636
},
34373637
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
34383638
})).
@@ -3613,6 +3813,56 @@ func TestCreateInstance(t *testing.T) {
36133813
},
36143814
},
36153815
},
3816+
{
3817+
ResourceType: aws.String("volume"),
3818+
Tags: []*ec2.Tag{
3819+
{
3820+
Key: aws.String("MachineName"),
3821+
Value: aws.String("default/machine-aws-test1"),
3822+
},
3823+
{
3824+
Key: aws.String("Name"),
3825+
Value: aws.String("aws-test1"),
3826+
},
3827+
{
3828+
Key: aws.String("kubernetes.io/cluster/test1"),
3829+
Value: aws.String("owned"),
3830+
},
3831+
{
3832+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
3833+
Value: aws.String("owned"),
3834+
},
3835+
{
3836+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
3837+
Value: aws.String("node"),
3838+
},
3839+
},
3840+
},
3841+
{
3842+
ResourceType: aws.String("network-interface"),
3843+
Tags: []*ec2.Tag{
3844+
{
3845+
Key: aws.String("MachineName"),
3846+
Value: aws.String("default/machine-aws-test1"),
3847+
},
3848+
{
3849+
Key: aws.String("Name"),
3850+
Value: aws.String("aws-test1"),
3851+
},
3852+
{
3853+
Key: aws.String("kubernetes.io/cluster/test1"),
3854+
Value: aws.String("owned"),
3855+
},
3856+
{
3857+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
3858+
Value: aws.String("owned"),
3859+
},
3860+
{
3861+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
3862+
Value: aws.String("node"),
3863+
},
3864+
},
3865+
},
36163866
},
36173867
UserData: aws.String(base64.StdEncoding.EncodeToString(data)),
36183868
})).
@@ -3754,6 +4004,56 @@ func TestCreateInstance(t *testing.T) {
37544004
},
37554005
},
37564006
},
4007+
{
4008+
ResourceType: aws.String("volume"),
4009+
Tags: []*ec2.Tag{
4010+
{
4011+
Key: aws.String("MachineName"),
4012+
Value: aws.String("default/machine-aws-test1"),
4013+
},
4014+
{
4015+
Key: aws.String("Name"),
4016+
Value: aws.String("aws-test1"),
4017+
},
4018+
{
4019+
Key: aws.String("kubernetes.io/cluster/test1"),
4020+
Value: aws.String("owned"),
4021+
},
4022+
{
4023+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
4024+
Value: aws.String("owned"),
4025+
},
4026+
{
4027+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
4028+
Value: aws.String("node"),
4029+
},
4030+
},
4031+
},
4032+
{
4033+
ResourceType: aws.String("network-interface"),
4034+
Tags: []*ec2.Tag{
4035+
{
4036+
Key: aws.String("MachineName"),
4037+
Value: aws.String("default/machine-aws-test1"),
4038+
},
4039+
{
4040+
Key: aws.String("Name"),
4041+
Value: aws.String("aws-test1"),
4042+
},
4043+
{
4044+
Key: aws.String("kubernetes.io/cluster/test1"),
4045+
Value: aws.String("owned"),
4046+
},
4047+
{
4048+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test1"),
4049+
Value: aws.String("owned"),
4050+
},
4051+
{
4052+
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
4053+
Value: aws.String("node"),
4054+
},
4055+
},
4056+
},
37574057
},
37584058
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
37594059
})).

0 commit comments

Comments
 (0)