Skip to content

Commit d2d91c9

Browse files
authored
Merge pull request #3946 from spectrocloud/add-eni-tags
Adding tags to AWS Network Interfaces
2 parents c599452 + 0fb0e7a commit d2d91c9

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

controllers/awsmachine_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func mockedCreateInstanceCalls(m *mocks.MockEC2APIMockRecorder) {
598598
},
599599
},
600600
},
601-
}}, nil).MaxTimes(2)
601+
}}, nil).MaxTimes(3)
602602
m.DescribeNetworkInterfaceAttribute(gomock.Eq(&ec2.DescribeNetworkInterfaceAttributeInput{
603603
NetworkInterfaceId: aws.String("eni-1"),
604604
Attribute: aws.String("groupSet"),

pkg/cloud/services/ec2/instances.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func (s *Service) InstanceIfExists(id *string) (*infrav1.Instance, error) {
110110
}
111111

112112
// CreateInstance runs an ec2 instance.
113+
//
114+
//nolint:gocyclo // this function has multiple processes to perform
113115
func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, userDataFormat string) (*infrav1.Instance, error) {
114116
s.scope.Debug("Creating an instance for a machine")
115117

@@ -249,6 +251,27 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use
249251
}
250252
}
251253

254+
s.scope.Debug("Adding tags on each network interface from resource", "resource-id", out.ID)
255+
256+
// Fetching the network interfaces attached to the specific instanace
257+
networkInterfaces, err := s.getInstanceENIs(out.ID)
258+
if err != nil {
259+
return nil, err
260+
}
261+
262+
s.scope.Debug("Fetched the network interfaces")
263+
264+
// Once all the network interfaces attached to the specific instanace are found, the similar tags of instance are created for network interfaces too
265+
if len(networkInterfaces) > 0 {
266+
s.scope.Debug("Attempting to create tags from resource", "resource-id", out.ID)
267+
for _, networkInterface := range networkInterfaces {
268+
// Create/Update tags in AWS.
269+
if err := s.UpdateResourceTags(networkInterface.NetworkInterfaceId, out.Tags, nil); err != nil {
270+
return nil, errors.Wrapf(err, "failed to create tags for resource %q: ", *networkInterface.NetworkInterfaceId)
271+
}
272+
}
273+
}
274+
252275
record.Eventf(scope.AWSMachine, "SuccessfulCreate", "Created new %s instance with id %q", scope.Role(), out.ID)
253276
return out, nil
254277
}

pkg/cloud/services/ec2/instances_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ func TestCreateInstance(t *testing.T) {
387387
}, nil)
388388
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
389389
Return(nil)
390+
m.
391+
DescribeNetworkInterfaces(gomock.Any()).
392+
Return(&ec2.DescribeNetworkInterfacesOutput{
393+
NetworkInterfaces: []*ec2.NetworkInterface{},
394+
NextToken: nil,
395+
}, nil)
390396
},
391397
check: func(instance *infrav1.Instance, err error) {
392398
if err != nil {
@@ -494,6 +500,12 @@ func TestCreateInstance(t *testing.T) {
494500

495501
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
496502
Return(nil)
503+
m.
504+
DescribeNetworkInterfaces(gomock.Any()).
505+
Return(&ec2.DescribeNetworkInterfacesOutput{
506+
NetworkInterfaces: []*ec2.NetworkInterface{},
507+
NextToken: nil,
508+
}, nil)
497509
},
498510
check: func(instance *infrav1.Instance, err error) {
499511
if err != nil {
@@ -628,6 +640,12 @@ func TestCreateInstance(t *testing.T) {
628640

629641
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
630642
Return(nil)
643+
m.
644+
DescribeNetworkInterfaces(gomock.Any()).
645+
Return(&ec2.DescribeNetworkInterfacesOutput{
646+
NetworkInterfaces: []*ec2.NetworkInterface{},
647+
NextToken: nil,
648+
}, nil)
631649
},
632650
check: func(instance *infrav1.Instance, err error) {
633651
if err != nil {
@@ -758,6 +776,12 @@ func TestCreateInstance(t *testing.T) {
758776

759777
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
760778
Return(nil)
779+
m.
780+
DescribeNetworkInterfaces(gomock.Any()).
781+
Return(&ec2.DescribeNetworkInterfacesOutput{
782+
NetworkInterfaces: []*ec2.NetworkInterface{},
783+
NextToken: nil,
784+
}, nil)
761785
},
762786
check: func(instance *infrav1.Instance, err error) {
763787
if err != nil {
@@ -889,6 +913,12 @@ func TestCreateInstance(t *testing.T) {
889913

890914
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
891915
Return(nil)
916+
m.
917+
DescribeNetworkInterfaces(gomock.Any()).
918+
Return(&ec2.DescribeNetworkInterfacesOutput{
919+
NetworkInterfaces: []*ec2.NetworkInterface{},
920+
NextToken: nil,
921+
}, nil)
892922
},
893923
check: func(instance *infrav1.Instance, err error) {
894924
if err != nil {
@@ -996,6 +1026,12 @@ func TestCreateInstance(t *testing.T) {
9961026
}, nil)
9971027
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
9981028
Return(nil)
1029+
m.
1030+
DescribeNetworkInterfaces(gomock.Any()).
1031+
Return(&ec2.DescribeNetworkInterfacesOutput{
1032+
NetworkInterfaces: []*ec2.NetworkInterface{},
1033+
NextToken: nil,
1034+
}, nil)
9991035
},
10001036
check: func(instance *infrav1.Instance, err error) {
10011037
if err != nil {
@@ -1102,6 +1138,12 @@ func TestCreateInstance(t *testing.T) {
11021138
}, nil)
11031139
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
11041140
Return(nil)
1141+
m.
1142+
DescribeNetworkInterfaces(gomock.Any()).
1143+
Return(&ec2.DescribeNetworkInterfacesOutput{
1144+
NetworkInterfaces: []*ec2.NetworkInterface{},
1145+
NextToken: nil,
1146+
}, nil)
11051147
},
11061148
check: func(instance *infrav1.Instance, err error) {
11071149
if err != nil {
@@ -1283,6 +1325,12 @@ func TestCreateInstance(t *testing.T) {
12831325
}, nil)
12841326
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
12851327
Return(nil)
1328+
m.
1329+
DescribeNetworkInterfaces(gomock.Any()).
1330+
Return(&ec2.DescribeNetworkInterfacesOutput{
1331+
NetworkInterfaces: []*ec2.NetworkInterface{},
1332+
NextToken: nil,
1333+
}, nil)
12861334
},
12871335
check: func(instance *infrav1.Instance, err error) {
12881336
if err != nil {
@@ -1539,6 +1587,12 @@ func TestCreateInstance(t *testing.T) {
15391587
}, nil)
15401588
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
15411589
Return(nil)
1590+
m.
1591+
DescribeNetworkInterfaces(gomock.Any()).
1592+
Return(&ec2.DescribeNetworkInterfacesOutput{
1593+
NetworkInterfaces: []*ec2.NetworkInterface{},
1594+
NextToken: nil,
1595+
}, nil)
15421596
},
15431597
check: func(instance *infrav1.Instance, err error) {
15441598
if err != nil {
@@ -1738,6 +1792,12 @@ func TestCreateInstance(t *testing.T) {
17381792
}, nil)
17391793
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
17401794
Return(nil)
1795+
m.
1796+
DescribeNetworkInterfaces(gomock.Any()).
1797+
Return(&ec2.DescribeNetworkInterfacesOutput{
1798+
NetworkInterfaces: []*ec2.NetworkInterface{},
1799+
NextToken: nil,
1800+
}, nil)
17411801
},
17421802
check: func(instance *infrav1.Instance, err error) {
17431803
if err != nil {
@@ -1835,6 +1895,12 @@ func TestCreateInstance(t *testing.T) {
18351895
}, nil)
18361896
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
18371897
Return(nil)
1898+
m.
1899+
DescribeNetworkInterfaces(gomock.Any()).
1900+
Return(&ec2.DescribeNetworkInterfacesOutput{
1901+
NetworkInterfaces: []*ec2.NetworkInterface{},
1902+
NextToken: nil,
1903+
}, nil)
18381904
},
18391905
check: func(instance *infrav1.Instance, err error) {
18401906
if err != nil {
@@ -2003,6 +2069,12 @@ func TestCreateInstance(t *testing.T) {
20032069
}, nil)
20042070
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
20052071
Return(nil)
2072+
m.
2073+
DescribeNetworkInterfaces(gomock.Any()).
2074+
Return(&ec2.DescribeNetworkInterfacesOutput{
2075+
NetworkInterfaces: []*ec2.NetworkInterface{},
2076+
NextToken: nil,
2077+
}, nil)
20062078
},
20072079
check: func(instance *infrav1.Instance, err error) {
20082080
if err != nil {
@@ -2138,6 +2210,12 @@ func TestCreateInstance(t *testing.T) {
21382210
}, nil)
21392211
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
21402212
Return(nil)
2213+
m.
2214+
DescribeNetworkInterfaces(gomock.Any()).
2215+
Return(&ec2.DescribeNetworkInterfacesOutput{
2216+
NetworkInterfaces: []*ec2.NetworkInterface{},
2217+
NextToken: nil,
2218+
}, nil)
21412219
},
21422220
check: func(instance *infrav1.Instance, err error) {
21432221
if err != nil {
@@ -2275,6 +2353,12 @@ func TestCreateInstance(t *testing.T) {
22752353
}, nil)
22762354
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
22772355
Return(nil)
2356+
m.
2357+
DescribeNetworkInterfaces(gomock.Any()).
2358+
Return(&ec2.DescribeNetworkInterfacesOutput{
2359+
NetworkInterfaces: []*ec2.NetworkInterface{},
2360+
NextToken: nil,
2361+
}, nil)
22782362
},
22792363
check: func(instance *infrav1.Instance, err error) {
22802364
if err != nil {
@@ -2383,6 +2467,12 @@ func TestCreateInstance(t *testing.T) {
23832467
})
23842468
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
23852469
Return(nil)
2470+
m.
2471+
DescribeNetworkInterfaces(gomock.Any()).
2472+
Return(&ec2.DescribeNetworkInterfacesOutput{
2473+
NetworkInterfaces: []*ec2.NetworkInterface{},
2474+
NextToken: nil,
2475+
}, nil)
23862476
},
23872477
check: func(instance *infrav1.Instance, err error) {
23882478
if err != nil {
@@ -2492,6 +2582,12 @@ func TestCreateInstance(t *testing.T) {
24922582
})
24932583
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
24942584
Return(nil)
2585+
m.
2586+
DescribeNetworkInterfaces(gomock.Any()).
2587+
Return(&ec2.DescribeNetworkInterfacesOutput{
2588+
NetworkInterfaces: []*ec2.NetworkInterface{},
2589+
NextToken: nil,
2590+
}, nil)
24952591
},
24962592
check: func(instance *infrav1.Instance, err error) {
24972593
if err != nil {
@@ -2602,6 +2698,12 @@ func TestCreateInstance(t *testing.T) {
26022698
})
26032699
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
26042700
Return(nil)
2701+
m.
2702+
DescribeNetworkInterfaces(gomock.Any()).
2703+
Return(&ec2.DescribeNetworkInterfacesOutput{
2704+
NetworkInterfaces: []*ec2.NetworkInterface{},
2705+
NextToken: nil,
2706+
}, nil)
26052707
},
26062708
check: func(instance *infrav1.Instance, err error) {
26072709
if err != nil {
@@ -2709,6 +2811,12 @@ func TestCreateInstance(t *testing.T) {
27092811
})
27102812
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
27112813
Return(nil)
2814+
m.
2815+
DescribeNetworkInterfaces(gomock.Any()).
2816+
Return(&ec2.DescribeNetworkInterfacesOutput{
2817+
NetworkInterfaces: []*ec2.NetworkInterface{},
2818+
NextToken: nil,
2819+
}, nil)
27122820
},
27132821
check: func(instance *infrav1.Instance, err error) {
27142822
if err != nil {
@@ -2816,6 +2924,12 @@ func TestCreateInstance(t *testing.T) {
28162924
})
28172925
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
28182926
Return(nil)
2927+
m.
2928+
DescribeNetworkInterfaces(gomock.Any()).
2929+
Return(&ec2.DescribeNetworkInterfacesOutput{
2930+
NetworkInterfaces: []*ec2.NetworkInterface{},
2931+
NextToken: nil,
2932+
}, nil)
28192933
},
28202934
check: func(instance *infrav1.Instance, err error) {
28212935
if err != nil {
@@ -2923,6 +3037,12 @@ func TestCreateInstance(t *testing.T) {
29233037
})
29243038
m.WaitUntilInstanceRunningWithContext(gomock.Any(), gomock.Any(), gomock.Any()).
29253039
Return(nil)
3040+
m.
3041+
DescribeNetworkInterfaces(gomock.Any()).
3042+
Return(&ec2.DescribeNetworkInterfacesOutput{
3043+
NetworkInterfaces: []*ec2.NetworkInterface{},
3044+
NextToken: nil,
3045+
}, nil)
29263046
},
29273047
check: func(instance *infrav1.Instance, err error) {
29283048
if err != nil {

0 commit comments

Comments
 (0)