@@ -74,14 +74,14 @@ func (c *instanceAttachmentControllerV1) Reconcile(ctx context.Context, ingKey t
74
74
75
75
shouldAttachENIIDs := targetENIIDs .Difference (attachedENIIDs )
76
76
for eniID := range shouldAttachENIIDs {
77
- if err := c .ensureSGAttachedToENI (ctx , instanceSGID , targetENIs [eniID ]); err != nil {
77
+ if err := c .ensureSGAttachedToENI (ctx , instanceSGID , eniID , targetENIs [eniID ]); err != nil {
78
78
return err
79
79
}
80
80
}
81
81
82
82
shouldDetachENIIDs := attachedENIIDs .Difference (targetENIIDs )
83
83
for eniID := range shouldDetachENIIDs {
84
- if err := c .ensureSGDetachedFromENI (ctx , instanceSGID , attachedENIs [eniID ]); err != nil {
84
+ if err := c .ensureSGDetachedFromENI (ctx , instanceSGID , eniID , attachedENIs [eniID ]); err != nil {
85
85
return err
86
86
}
87
87
}
@@ -102,8 +102,8 @@ func (c *instanceAttachmentControllerV1) Delete(ctx context.Context, ingKey type
102
102
if err != nil {
103
103
return err
104
104
}
105
- for _ , eni := range attachedENIs {
106
- if err := c .ensureSGDetachedFromENI (ctx , instanceSGID , eni ); err != nil {
105
+ for eniID , eniInfo := range attachedENIs {
106
+ if err := c .ensureSGDetachedFromENI (ctx , instanceSGID , eniID , eniInfo ); err != nil {
107
107
return err
108
108
}
109
109
}
@@ -141,7 +141,7 @@ func (c *instanceAttachmentControllerV1) ensureInstanceSG(ctx context.Context, i
141
141
}
142
142
143
143
// findENIsAttachedWithInstanceSG finds all ENIs attached with instance SG.
144
- func (c * instanceAttachmentControllerV1 ) findENIsAttachedWithInstanceSG (ctx context.Context , instanceSGID string ) (map [string ]* ec2. NetworkInterface , error ) {
144
+ func (c * instanceAttachmentControllerV1 ) findENIsAttachedWithInstanceSG (ctx context.Context , instanceSGID string ) (map [string ]ENIInfo , error ) {
145
145
enis , err := c .cloud .DescribeNetworkInterfaces (ctx , & ec2.DescribeNetworkInterfacesInput {
146
146
Filters : []* ec2.Filter {
147
147
{
@@ -153,36 +153,34 @@ func (c *instanceAttachmentControllerV1) findENIsAttachedWithInstanceSG(ctx cont
153
153
if err != nil {
154
154
return nil , err
155
155
}
156
- result := make (map [string ]* ec2. NetworkInterface , len (enis ))
156
+ result := make (map [string ]ENIInfo , len (enis ))
157
157
for _ , eni := range enis {
158
- result [aws .StringValue (eni .NetworkInterfaceId )] = eni
158
+ result [aws .StringValue (eni .NetworkInterfaceId )] = NewENIInfoViaENI ( eni )
159
159
}
160
160
return result , nil
161
161
}
162
162
163
- func (c * instanceAttachmentControllerV1 ) ensureSGAttachedToENI (ctx context.Context , sgID string , eni * ec2. InstanceNetworkInterface ) error {
163
+ func (c * instanceAttachmentControllerV1 ) ensureSGAttachedToENI (ctx context.Context , sgID string , eniID string , eniInfo ENIInfo ) error {
164
164
desiredGroups := []string {sgID }
165
- for _ , group := range eni .Groups {
166
- groupID := aws .StringValue (group .GroupId )
165
+ for _ , groupID := range eniInfo .SecurityGroups () {
167
166
if groupID == sgID {
168
167
return nil
169
168
}
170
169
desiredGroups = append (desiredGroups , groupID )
171
170
}
172
171
173
- albctx .GetLogger (ctx ).Infof ("attaching securityGroup %s to ENI %s" , sgID , * eni . NetworkInterfaceId )
172
+ albctx .GetLogger (ctx ).Infof ("attaching securityGroup %s to ENI %s" , sgID , eniID )
174
173
_ , err := c .cloud .ModifyNetworkInterfaceAttributeWithContext (ctx , & ec2.ModifyNetworkInterfaceAttributeInput {
175
- NetworkInterfaceId : eni . NetworkInterfaceId ,
174
+ NetworkInterfaceId : aws . String ( eniID ) ,
176
175
Groups : aws .StringSlice (desiredGroups ),
177
176
})
178
177
return err
179
178
}
180
179
181
- func (c * instanceAttachmentControllerV1 ) ensureSGDetachedFromENI (ctx context.Context , sgID string , eni * ec2. NetworkInterface ) error {
180
+ func (c * instanceAttachmentControllerV1 ) ensureSGDetachedFromENI (ctx context.Context , sgID string , eniID string , eniInfo ENIInfo ) error {
182
181
sgAttached := false
183
182
desiredGroups := []string {}
184
- for _ , group := range eni .Groups {
185
- groupID := aws .StringValue (group .GroupId )
183
+ for _ , groupID := range eniInfo .SecurityGroups () {
186
184
if groupID == sgID {
187
185
sgAttached = true
188
186
} else {
@@ -193,9 +191,9 @@ func (c *instanceAttachmentControllerV1) ensureSGDetachedFromENI(ctx context.Con
193
191
return nil
194
192
}
195
193
196
- albctx .GetLogger (ctx ).Infof ("detaching securityGroup %s from ENI %s" , sgID , * eni . NetworkInterfaceId )
194
+ albctx .GetLogger (ctx ).Infof ("detaching securityGroup %s from ENI %s" , sgID , eniID )
197
195
_ , err := c .cloud .ModifyNetworkInterfaceAttributeWithContext (ctx , & ec2.ModifyNetworkInterfaceAttributeInput {
198
- NetworkInterfaceId : eni . NetworkInterfaceId ,
196
+ NetworkInterfaceId : aws . String ( eniID ) ,
199
197
Groups : aws .StringSlice (desiredGroups ),
200
198
})
201
199
return err
0 commit comments