@@ -74,14 +74,14 @@ func (c *instanceAttachmentControllerV1) Reconcile(ctx context.Context, ingKey t
7474
7575shouldAttachENIIDs := targetENIIDs .Difference (attachedENIIDs )
7676for 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 {
7878return err
7979}
8080}
8181
8282shouldDetachENIIDs := attachedENIIDs .Difference (targetENIIDs )
8383for 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 {
8585return err
8686}
8787}
@@ -102,8 +102,8 @@ func (c *instanceAttachmentControllerV1) Delete(ctx context.Context, ingKey type
102102if err != nil {
103103return err
104104}
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 {
107107return err
108108}
109109}
@@ -141,7 +141,7 @@ func (c *instanceAttachmentControllerV1) ensureInstanceSG(ctx context.Context, i
141141}
142142
143143// 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 ) {
145145enis , err := c .cloud .DescribeNetworkInterfaces (ctx , & ec2.DescribeNetworkInterfacesInput {
146146Filters : []* ec2.Filter {
147147{
@@ -153,36 +153,34 @@ func (c *instanceAttachmentControllerV1) findENIsAttachedWithInstanceSG(ctx cont
153153if err != nil {
154154return nil , err
155155}
156- result := make (map [string ]* ec2. NetworkInterface , len (enis ))
156+ result := make (map [string ]ENIInfo , len (enis ))
157157for _ , eni := range enis {
158- result [aws .StringValue (eni .NetworkInterfaceId )] = eni
158+ result [aws .StringValue (eni .NetworkInterfaceId )] = NewENIInfoViaENI ( eni )
159159}
160160return result , nil
161161}
162162
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 {
164164desiredGroups := []string {sgID }
165- for _ , group := range eni .Groups {
166- groupID := aws .StringValue (group .GroupId )
165+ for _ , groupID := range eniInfo .SecurityGroups () {
167166if groupID == sgID {
168167return nil
169168}
170169desiredGroups = append (desiredGroups , groupID )
171170}
172171
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 )
174173_ , err := c .cloud .ModifyNetworkInterfaceAttributeWithContext (ctx , & ec2.ModifyNetworkInterfaceAttributeInput {
175- NetworkInterfaceId : eni . NetworkInterfaceId ,
174+ NetworkInterfaceId : aws . String ( eniID ) ,
176175Groups : aws .StringSlice (desiredGroups ),
177176})
178177return err
179178}
180179
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 {
182181sgAttached := false
183182desiredGroups := []string {}
184- for _ , group := range eni .Groups {
185- groupID := aws .StringValue (group .GroupId )
183+ for _ , groupID := range eniInfo .SecurityGroups () {
186184if groupID == sgID {
187185sgAttached = true
188186} else {
@@ -193,9 +191,9 @@ func (c *instanceAttachmentControllerV1) ensureSGDetachedFromENI(ctx context.Con
193191return nil
194192}
195193
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 )
197195_ , err := c .cloud .ModifyNetworkInterfaceAttributeWithContext (ctx , & ec2.ModifyNetworkInterfaceAttributeInput {
198- NetworkInterfaceId : eni . NetworkInterfaceId ,
196+ NetworkInterfaceId : aws . String ( eniID ) ,
199197Groups : aws .StringSlice (desiredGroups ),
200198})
201199return err
0 commit comments