Skip to content

Commit c7062cb

Browse files
committed
Drop unwanted SGs when calling attachSecurityGroupsToNetworkInterface
Before this, attachSecurityGroupsToNetworkInterface was re-applying existing SGs not specified in user intent. It's up to the caller to choose the right list of SG ids as in https://github.com/kubernetes-sigs/cluster-api-provider-aws/blob/3ebf018bbfc5345fbd9d9598ea9392b2a349ed6c/controllers/awsmachine_security_groups.go#L57-L64 so then attachSecurityGroupsToNetworkInterface just applies what is given.
1 parent eaa3eca commit c7062cb

File tree

1 file changed

+3
-32
lines changed

1 file changed

+3
-32
lines changed

pkg/cloud/services/ec2/instances.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -912,34 +912,15 @@ func (s *Service) getNetworkInterfaceSecurityGroups(interfaceID string) ([]strin
912912
}
913913

914914
func (s *Service) attachSecurityGroupsToNetworkInterface(groups []string, interfaceID string) error {
915-
existingGroups, err := s.getNetworkInterfaceSecurityGroups(interfaceID)
916-
if err != nil {
917-
return errors.Wrapf(err, "failed to look up network interface security groups: %+v", err)
918-
}
919-
920-
totalGroups := make([]string, len(existingGroups))
921-
copy(totalGroups, existingGroups)
922-
923-
for _, group := range groups {
924-
if !containsGroup(existingGroups, group) {
925-
totalGroups = append(totalGroups, group)
926-
}
927-
}
928-
929-
// no new groups to attach
930-
if len(existingGroups) == len(totalGroups) {
931-
return nil
932-
}
933-
934-
s.scope.Info("Updating security groups", "groups", totalGroups)
915+
s.scope.Info("Updating security groups", "groups", groups)
935916

936917
input := &ec2.ModifyNetworkInterfaceAttributeInput{
937918
NetworkInterfaceId: aws.String(interfaceID),
938-
Groups: aws.StringSlice(totalGroups),
919+
Groups: aws.StringSlice(groups),
939920
}
940921

941922
if _, err := s.EC2Client.ModifyNetworkInterfaceAttribute(input); err != nil {
942-
return errors.Wrapf(err, "failed to modify interface %q to have security groups %v", interfaceID, totalGroups)
923+
return errors.Wrapf(err, "failed to modify interface %q to have security groups %v", interfaceID, groups)
943924
}
944925
return nil
945926
}
@@ -1016,16 +997,6 @@ func filterGroups(list []string, strToFilter string) (newList []string) {
1016997
return
1017998
}
1018999

1019-
// containsGroup returns true if a list contains a string.
1020-
func containsGroup(list []string, strToSearch string) bool {
1021-
for _, item := range list {
1022-
if item == strToSearch {
1023-
return true
1024-
}
1025-
}
1026-
return false
1027-
}
1028-
10291000
func getInstanceMarketOptionsRequest(spotMarketOptions *infrav1.SpotMarketOptions) *ec2.InstanceMarketOptionsRequest {
10301001
if spotMarketOptions == nil {
10311002
// Instance is not a Spot instance

0 commit comments

Comments
 (0)