@@ -2255,6 +2255,22 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
2255
2255
instanceIDs = append (instanceIDs , string (id ))
2256
2256
}
2257
2257
2258
+ // TODO: get the list of SGs when it requires to add it (only if NLB was created with one)
2259
+ // Check if SG annotation has been added and SG exists, then build the required permissions.
2260
+ if _ , present := annotations [ServiceAnnotationLoadBalancerSecurityGroups ]; present {
2261
+ securityGroups , _ , err := c .buildELBSecurityGroupList (serviceName , loadBalancerName , annotations )
2262
+ if err != nil {
2263
+ return nil , err
2264
+ }
2265
+ if len (securityGroups ) == 0 {
2266
+ return nil , fmt .Errorf ("NLB must be created with an Security Group to allow syncronization, please recrease the service with a security group" )
2267
+ }
2268
+ permissions := buildSecuritySecurityGroupPermissions (apiService , sourceRanges )
2269
+ if _ , err = c .setSecurityGroupIngress (securityGroups [0 ], permissions ); err != nil {
2270
+ return nil , err
2271
+ }
2272
+ }
2273
+
2258
2274
v2LoadBalancer , err := c .ensureLoadBalancerv2 (
2259
2275
serviceName ,
2260
2276
loadBalancerName ,
@@ -2435,36 +2451,7 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
2435
2451
}
2436
2452
2437
2453
if setupSg {
2438
- ec2SourceRanges := []* ec2.IpRange {}
2439
- for _ , sourceRange := range sourceRanges .StringSlice () {
2440
- ec2SourceRanges = append (ec2SourceRanges , & ec2.IpRange {CidrIp : aws .String (sourceRange )})
2441
- }
2442
-
2443
- permissions := NewIPPermissionSet ()
2444
- for _ , port := range apiService .Spec .Ports {
2445
- portInt64 := int64 (port .Port )
2446
- protocol := strings .ToLower (string (port .Protocol ))
2447
-
2448
- permission := & ec2.IpPermission {}
2449
- permission .FromPort = & portInt64
2450
- permission .ToPort = & portInt64
2451
- permission .IpRanges = ec2SourceRanges
2452
- permission .IpProtocol = & protocol
2453
-
2454
- permissions .Insert (permission )
2455
- }
2456
-
2457
- // Allow ICMP fragmentation packets, important for MTU discovery
2458
- {
2459
- permission := & ec2.IpPermission {
2460
- IpProtocol : aws .String ("icmp" ),
2461
- FromPort : aws .Int64 (3 ),
2462
- ToPort : aws .Int64 (4 ),
2463
- IpRanges : ec2SourceRanges ,
2464
- }
2465
-
2466
- permissions .Insert (permission )
2467
- }
2454
+ permissions := buildSecuritySecurityGroupPermissions (apiService , sourceRanges )
2468
2455
_ , err = c .setSecurityGroupIngress (securityGroupIDs [0 ], permissions )
2469
2456
if err != nil {
2470
2457
return nil , err
@@ -2573,6 +2560,41 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
2573
2560
return status , nil
2574
2561
}
2575
2562
2563
+ func buildSecuritySecurityGroupPermissions (apiService * v1.Service , sourceRanges netutils.IPNetSet ) IPPermissionSet {
2564
+ ec2SourceRanges := []* ec2.IpRange {}
2565
+ for _ , sourceRange := range sourceRanges .StringSlice () {
2566
+ ec2SourceRanges = append (ec2SourceRanges , & ec2.IpRange {CidrIp : aws .String (sourceRange )})
2567
+ }
2568
+
2569
+ permissions := NewIPPermissionSet ()
2570
+ for _ , port := range apiService .Spec .Ports {
2571
+ portInt64 := int64 (port .Port )
2572
+ protocol := strings .ToLower (string (port .Protocol ))
2573
+
2574
+ permission := & ec2.IpPermission {}
2575
+ permission .FromPort = & portInt64
2576
+ permission .ToPort = & portInt64
2577
+ permission .IpRanges = ec2SourceRanges
2578
+ permission .IpProtocol = & protocol
2579
+
2580
+ permissions .Insert (permission )
2581
+ }
2582
+
2583
+ // Allow ICMP fragmentation packets, important for MTU discovery
2584
+ {
2585
+ permission := & ec2.IpPermission {
2586
+ IpProtocol : aws .String ("icmp" ),
2587
+ FromPort : aws .Int64 (3 ),
2588
+ ToPort : aws .Int64 (4 ),
2589
+ IpRanges : ec2SourceRanges ,
2590
+ }
2591
+
2592
+ permissions .Insert (permission )
2593
+ }
2594
+
2595
+ return permissions
2596
+ }
2597
+
2576
2598
// GetLoadBalancer is an implementation of LoadBalancer.GetLoadBalancer
2577
2599
func (c * Cloud ) GetLoadBalancer (ctx context.Context , clusterName string , service * v1.Service ) (* v1.LoadBalancerStatus , bool , error ) {
2578
2600
if isLBExternal (service .Annotations ) {
@@ -2898,6 +2920,8 @@ func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin
2898
2920
}
2899
2921
}
2900
2922
2923
+ // TODO add delete SG
2924
+
2901
2925
return c .updateInstanceSecurityGroupsForNLB (loadBalancerName , nil , nil , nil , nil )
2902
2926
}
2903
2927
@@ -3068,6 +3092,11 @@ func (c *Cloud) UpdateLoadBalancer(ctx context.Context, clusterName string, serv
3068
3092
return fmt .Errorf ("Load balancer not found" )
3069
3093
}
3070
3094
_ , err = c .EnsureLoadBalancer (ctx , clusterName , service , nodes )
3095
+ if err != nil {
3096
+ return err
3097
+ }
3098
+
3099
+ // TODO ensure security groups when defined
3071
3100
return err
3072
3101
}
3073
3102
lb , err := c .describeLoadBalancer (loadBalancerName )
0 commit comments