@@ -3603,6 +3603,37 @@ func (c *Cloud) buildELBSecurityGroupList(serviceName types.NamespacedName, load
3603
3603
return sgList , setupSg , nil
3604
3604
}
3605
3605
3606
+ // sortELBSecurityGroupList returns a list of sorted securityGroupIDs based on the original order
3607
+ // from buildELBSecurityGroupList. The logic is:
3608
+ // * securityGroups specified by ServiceAnnotationLoadBalancerSecurityGroups appears first in order
3609
+ // * securityGroups specified by ServiceAnnotationLoadBalancerExtraSecurityGroups appears last in order
3610
+ func (c * Cloud ) sortELBSecurityGroupList (securityGroupIDs []string , annotations map [string ]string ) {
3611
+ annotatedSGList := getSGListFromAnnotation (annotations [ServiceAnnotationLoadBalancerSecurityGroups ])
3612
+ annotatedExtraSGList := getSGListFromAnnotation (annotations [ServiceAnnotationLoadBalancerExtraSecurityGroups ])
3613
+ annotatedSGIndex := make (map [string ]int , len (annotatedSGList ))
3614
+ annotatedExtraSGIndex := make (map [string ]int , len (annotatedExtraSGList ))
3615
+
3616
+ for i , sgID := range annotatedSGList {
3617
+ annotatedSGIndex [sgID ] = i
3618
+ }
3619
+ for i , sgID := range annotatedExtraSGList {
3620
+ annotatedExtraSGIndex [sgID ] = i
3621
+ }
3622
+ sgOrderMapping := make (map [string ]int , len (securityGroupIDs ))
3623
+ for _ , sgID := range securityGroupIDs {
3624
+ if i , ok := annotatedSGIndex [sgID ]; ok {
3625
+ sgOrderMapping [sgID ] = i
3626
+ } else if j , ok := annotatedExtraSGIndex [sgID ]; ok {
3627
+ sgOrderMapping [sgID ] = len (annotatedSGIndex ) + 1 + j
3628
+ } else {
3629
+ sgOrderMapping [sgID ] = len (annotatedSGIndex )
3630
+ }
3631
+ }
3632
+ sort .Slice (securityGroupIDs , func (i , j int ) bool {
3633
+ return sgOrderMapping [securityGroupIDs [i ]] < sgOrderMapping [securityGroupIDs [j ]]
3634
+ })
3635
+ }
3636
+
3606
3637
// buildListener creates a new listener from the given port, adding an SSL certificate
3607
3638
// if indicated by the appropriate annotations.
3608
3639
func buildListener (port v1.ServicePort , annotations map [string ]string , sslPorts * portSets ) (* elb.Listener , error ) {
@@ -4015,7 +4046,7 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
4015
4046
}
4016
4047
}
4017
4048
4018
- err = c .updateInstanceSecurityGroupsForLoadBalancer (loadBalancer , instances )
4049
+ err = c .updateInstanceSecurityGroupsForLoadBalancer (loadBalancer , instances , annotations )
4019
4050
if err != nil {
4020
4051
klog .Warningf ("Error opening ingress rules for the load balancer to the instances: %q" , err )
4021
4052
return nil , err
@@ -4173,26 +4204,18 @@ func (c *Cloud) getTaggedSecurityGroups() (map[string]*ec2.SecurityGroup, error)
4173
4204
4174
4205
// Open security group ingress rules on the instances so that the load balancer can talk to them
4175
4206
// Will also remove any security groups ingress rules for the load balancer that are _not_ needed for allInstances
4176
- func (c * Cloud ) updateInstanceSecurityGroupsForLoadBalancer (lb * elb.LoadBalancerDescription , instances map [InstanceID ]* ec2.Instance ) error {
4207
+ func (c * Cloud ) updateInstanceSecurityGroupsForLoadBalancer (lb * elb.LoadBalancerDescription , instances map [InstanceID ]* ec2.Instance , annotations map [ string ] string ) error {
4177
4208
if c .cfg .Global .DisableSecurityGroupIngress {
4178
4209
return nil
4179
4210
}
4180
4211
4181
4212
// Determine the load balancer security group id
4182
- loadBalancerSecurityGroupID := ""
4183
- for _ , securityGroup := range lb .SecurityGroups {
4184
- if aws .StringValue (securityGroup ) == "" {
4185
- continue
4186
- }
4187
- if loadBalancerSecurityGroupID != "" {
4188
- // We create LBs with one SG
4189
- klog .Warningf ("Multiple security groups for load balancer: %q" , aws .StringValue (lb .LoadBalancerName ))
4190
- }
4191
- loadBalancerSecurityGroupID = * securityGroup
4192
- }
4193
- if loadBalancerSecurityGroupID == "" {
4213
+ lbSecurityGroupIDs := aws .StringValueSlice (lb .SecurityGroups )
4214
+ if len (lbSecurityGroupIDs ) == 0 {
4194
4215
return fmt .Errorf ("could not determine security group for load balancer: %s" , aws .StringValue (lb .LoadBalancerName ))
4195
4216
}
4217
+ c .sortELBSecurityGroupList (lbSecurityGroupIDs , annotations )
4218
+ loadBalancerSecurityGroupID := lbSecurityGroupIDs [0 ]
4196
4219
4197
4220
// Get the actual list of groups that allow ingress from the load-balancer
4198
4221
var actualGroups []* ec2.SecurityGroup
@@ -4368,7 +4391,7 @@ func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin
4368
4391
4369
4392
{
4370
4393
// De-authorize the load balancer security group from the instances security group
4371
- err = c .updateInstanceSecurityGroupsForLoadBalancer (lb , nil )
4394
+ err = c .updateInstanceSecurityGroupsForLoadBalancer (lb , nil , service . Annotations )
4372
4395
if err != nil {
4373
4396
klog .Errorf ("Error deregistering load balancer from instance security groups: %q" , err )
4374
4397
return err
@@ -4533,7 +4556,7 @@ func (c *Cloud) UpdateLoadBalancer(ctx context.Context, clusterName string, serv
4533
4556
return nil
4534
4557
}
4535
4558
4536
- err = c .updateInstanceSecurityGroupsForLoadBalancer (lb , instances )
4559
+ err = c .updateInstanceSecurityGroupsForLoadBalancer (lb , instances , service . Annotations )
4537
4560
if err != nil {
4538
4561
return err
4539
4562
}
0 commit comments