@@ -3,6 +3,7 @@ package sg
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "k8s.io/apimachinery/pkg/util/sets"
6
7
7
8
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/albctx"
8
9
@@ -40,10 +41,9 @@ func (controller *lbAttachmentController) Reconcile(ctx context.Context, attachm
40
41
return fmt .Errorf ("loadBalancer %s doesn't exists" , attachment .LbArn )
41
42
}
42
43
43
- groupsInLb := aws .StringValueSlice (loadBalancer .SecurityGroups )
44
- groupsToAdd := diffStringSet (attachment .GroupIDs , groupsInLb )
45
- groupsToDelete := diffStringSet (groupsInLb , attachment .GroupIDs )
46
- if len (groupsToAdd ) != 0 || len (groupsToDelete ) != 0 {
44
+ desiredGroups := sets .NewString (attachment .GroupIDs ... )
45
+ currentGroups := sets .NewString (aws .StringValueSlice (loadBalancer .SecurityGroups )... )
46
+ if ! desiredGroups .Equal (currentGroups ) {
47
47
albctx .GetLogger (ctx ).Infof ("modify securityGroup on LoadBalancer %s to be %v" , attachment .LbArn , attachment .GroupIDs )
48
48
_ , err := controller .cloud .SetSecurityGroupsWithContext (ctx , & elbv2.SetSecurityGroupsInput {
49
49
LoadBalancerArn : aws .String (attachment .LbArn ),
@@ -65,21 +65,22 @@ func (controller *lbAttachmentController) Delete(ctx context.Context, attachment
65
65
return fmt .Errorf ("loadBalancer %s doesn't exist" , attachment .LbArn )
66
66
}
67
67
68
- groupsInLb := aws .StringValueSlice (loadBalancer .SecurityGroups )
69
- groupsShouldRemain := diffStringSet (groupsInLb , attachment .GroupIDs )
70
- if len (groupsShouldRemain ) != len (groupsInLb ) {
71
- if len (groupsShouldRemain ) == 0 {
68
+ undesiredGroups := sets .NewString (attachment .GroupIDs ... )
69
+ currentGroups := sets .NewString (aws .StringValueSlice (loadBalancer .SecurityGroups )... )
70
+ groupsToKeep := currentGroups .Difference (undesiredGroups )
71
+ if len (groupsToKeep ) != len (currentGroups ) {
72
+ if len (groupsToKeep ) == 0 {
72
73
defaultSGID , err := controller .getDefaultSecurityGroupID ()
73
74
if err != nil {
74
75
return fmt .Errorf ("failed to get default securityGroup for current vpc due to %v" , err )
75
76
}
76
- groupsShouldRemain = append ( groupsShouldRemain , * defaultSGID )
77
+ groupsToKeep . Insert ( defaultSGID )
77
78
}
78
-
79
- albctx .GetLogger (ctx ).Infof ("modify securityGroup on LoadBalancer %s to be %v" , attachment .LbArn , groupsShouldRemain )
79
+ desiredGroups := groupsToKeep . List ()
80
+ albctx .GetLogger (ctx ).Infof ("modify securityGroup on LoadBalancer %s to be %v" , attachment .LbArn , desiredGroups )
80
81
_ , err := controller .cloud .SetSecurityGroupsWithContext (ctx , & elbv2.SetSecurityGroupsInput {
81
82
LoadBalancerArn : aws .String (attachment .LbArn ),
82
- SecurityGroups : aws .StringSlice (groupsShouldRemain ),
83
+ SecurityGroups : aws .StringSlice (desiredGroups ),
83
84
})
84
85
if err != nil {
85
86
return err
@@ -88,29 +89,15 @@ func (controller *lbAttachmentController) Delete(ctx context.Context, attachment
88
89
return nil
89
90
}
90
91
91
- func (controller * lbAttachmentController ) getDefaultSecurityGroupID () (* string , error ) {
92
+ func (controller * lbAttachmentController ) getDefaultSecurityGroupID () (string , error ) {
92
93
vpcID , err := controller .cloud .GetVPCID ()
93
94
if err != nil {
94
- return nil , err
95
+ return "" , err
95
96
}
96
97
97
98
defaultSG , err := controller .cloud .GetSecurityGroupByName (* vpcID , "default" )
98
99
if err != nil {
99
- return nil , err
100
- }
101
- return defaultSG .GroupId , nil
102
- }
103
-
104
- // diffStringSet calcuates the set_difference as source - target
105
- func diffStringSet (source []string , target []string ) (diffs []string ) {
106
- targetSet := make (map [string ]bool )
107
- for _ , t := range target {
108
- targetSet [t ] = true
109
- }
110
- for _ , s := range source {
111
- if _ , ok := targetSet [s ]; ! ok {
112
- diffs = append (diffs , s )
113
- }
100
+ return "" , err
114
101
}
115
- return diffs
102
+ return aws . StringValue ( defaultSG . GroupId ), nil
116
103
}
0 commit comments