5
5
"fmt"
6
6
"sync"
7
7
8
+ "k8s.io/apimachinery/pkg/util/sets"
9
+
8
10
"github.com/aws/aws-sdk-go/service/ec2"
9
11
"github.com/aws/aws-sdk-go/service/elbv2"
10
12
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/alb/tg"
@@ -37,38 +39,46 @@ func (controller *instanceAttachmentController) Reconcile(ctx context.Context, g
37
39
if err != nil {
38
40
return fmt .Errorf ("failed to get cluster ENIs due to %v" , err )
39
41
}
42
+
43
+ // sgAttachedENIs is not always an subnet of instanceENIs :(, e.g. this CNI bug: https://github.com/aws/amazon-vpc-cni-k8s/issues/69
44
+ sgAttachedENIs , err := controller .getSGAttachedENIs (ctx , groupID )
45
+ if err != nil {
46
+ return fmt .Errorf ("failed to get ENIs attached to %s due to %v" , groupID , err )
47
+ }
48
+
40
49
supportingENIs := controller .findENIsSupportingTargets (instanceENIs , tgGroup )
41
50
for _ , enis := range instanceENIs {
42
51
for _ , eni := range enis {
43
- if _ , ok := supportingENIs [aws .StringValue (eni .NetworkInterfaceId )]; ok {
44
- err := controller .ensureSGAttachedToENI (ctx , groupID , eni )
45
- if err != nil {
46
- return err
47
- }
48
- } else {
49
- err := controller .ensureSGDetachedFromENI (ctx , groupID , eni )
50
- if err != nil {
52
+ if supportingENIs .Has (aws .StringValue (eni .NetworkInterfaceId )) {
53
+ if err := controller .ensureSGAttachedToENI (ctx , groupID , eni ); err != nil {
51
54
return err
52
55
}
53
56
}
54
57
}
55
58
}
59
+
60
+ for _ , eni := range sgAttachedENIs {
61
+ if ! supportingENIs .Has (aws .StringValue (eni .NetworkInterfaceId )) {
62
+ if err := controller .ensureSGDetachedFromENI (ctx , groupID , eni ); err != nil {
63
+ return err
64
+ }
65
+ }
66
+ }
67
+
56
68
return nil
57
69
}
58
70
59
71
func (controller * instanceAttachmentController ) Delete (ctx context.Context , groupID string ) error {
60
72
clusterInstanceENILock .Lock ()
61
73
defer clusterInstanceENILock .Unlock ()
62
- instanceENIs , err := controller .getClusterInstanceENIs ()
74
+
75
+ sgAttachedENIs , err := controller .getSGAttachedENIs (ctx , groupID )
63
76
if err != nil {
64
- return fmt .Errorf ("failed to get cluster enis due to %v" , err )
77
+ return fmt .Errorf ("failed to get ENIs attached to %s due to %v" , groupID , err )
65
78
}
66
- for _ , enis := range instanceENIs {
67
- for _ , eni := range enis {
68
- err := controller .ensureSGDetachedFromENI (ctx , groupID , eni )
69
- if err != nil {
70
- return err
71
- }
79
+ for _ , eni := range sgAttachedENIs {
80
+ if err := controller .ensureSGDetachedFromENI (ctx , groupID , eni ); err != nil {
81
+ return err
72
82
}
73
83
}
74
84
return nil
@@ -92,7 +102,7 @@ func (controller *instanceAttachmentController) ensureSGAttachedToENI(ctx contex
92
102
return err
93
103
}
94
104
95
- func (controller * instanceAttachmentController ) ensureSGDetachedFromENI (ctx context.Context , sgID string , eni * ec2.InstanceNetworkInterface ) error {
105
+ func (controller * instanceAttachmentController ) ensureSGDetachedFromENI (ctx context.Context , sgID string , eni * ec2.NetworkInterface ) error {
96
106
sgAttached := false
97
107
desiredGroups := []string {}
98
108
for _ , group := range eni .Groups {
@@ -116,17 +126,15 @@ func (controller *instanceAttachmentController) ensureSGDetachedFromENI(ctx cont
116
126
}
117
127
118
128
// findENIsSupportingTargets find the ID of ENIs that are used to supporting ingress traffic to targets
119
- func (controller * instanceAttachmentController ) findENIsSupportingTargets (instanceENIs map [string ][]* ec2.InstanceNetworkInterface , tgGroup tg.TargetGroupGroup ) map [ string ] bool {
120
- result := make ( map [ string ] bool )
129
+ func (controller * instanceAttachmentController ) findENIsSupportingTargets (instanceENIs map [string ][]* ec2.InstanceNetworkInterface , tgGroup tg.TargetGroupGroup ) sets. String {
130
+ result := sets . NewString ( )
121
131
for _ , tg := range tgGroup .TGByBackend {
122
132
if tg .TargetType == elbv2 .TargetTypeEnumInstance {
123
- for _ , eniID := range controller .findENIsSupportingTargetGroupOfTypeInstance (instanceENIs , tg ) {
124
- result [eniID ] = true
125
- }
133
+ eniIDs := controller .findENIsSupportingTargetGroupOfTypeInstance (instanceENIs , tg )
134
+ result .Insert (eniIDs ... )
126
135
} else {
127
- for _ , eniID := range controller .findENIsSupportingTargetGroupOfTypeIP (instanceENIs , tg ) {
128
- result [eniID ] = true
129
- }
136
+ eniIDs := controller .findENIsSupportingTargetGroupOfTypeIP (instanceENIs , tg )
137
+ result .Insert (eniIDs ... )
130
138
}
131
139
}
132
140
return result
@@ -173,7 +181,7 @@ func (controller *instanceAttachmentController) findENIsSupportingTargetGroupOfT
173
181
return result
174
182
}
175
183
176
- // getClusterInstanceENIs retrives all ENIs attached to instances indexed by instanceID
184
+ // getClusterInstanceENIs retrieves all ENIs attached to instances indexed by instanceID
177
185
func (controller * instanceAttachmentController ) getClusterInstanceENIs () (map [string ][]* ec2.InstanceNetworkInterface , error ) {
178
186
instanceIDs , err := controller .store .GetClusterInstanceIDs ()
179
187
if err != nil {
@@ -189,3 +197,15 @@ func (controller *instanceAttachmentController) getClusterInstanceENIs() (map[st
189
197
}
190
198
return result , nil
191
199
}
200
+
201
+ // getSGAttachedENIs retrieves all ENIs attached with specified securityGroup.
202
+ func (controller * instanceAttachmentController ) getSGAttachedENIs (ctx context.Context , sgID string ) ([]* ec2.NetworkInterface , error ) {
203
+ return controller .cloud .DescribeNetworkInterfaces (ctx , & ec2.DescribeNetworkInterfacesInput {
204
+ Filters : []* ec2.Filter {
205
+ {
206
+ Name : aws .String ("group-id" ),
207
+ Values : []* string {aws .String (sgID )},
208
+ },
209
+ },
210
+ })
211
+ }
0 commit comments