Skip to content

Commit a2bd59b

Browse files
committed
Use NLB Subnet CIDRs instead of VPC CIDRs in updateInstanceSecurityGroupsForNLB
Signed-off-by: Pedro Tôrres <[email protected]>
1 parent 498b1e2 commit a2bd59b

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

staging/src/k8s.io/legacy-cloud-providers/aws/aws.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,6 +3675,27 @@ func buildListener(port v1.ServicePort, annotations map[string]string, sslPorts
36753675
return listener, nil
36763676
}
36773677

3678+
func (c *Cloud) getSubnetCidrs(subnetIDs []string) ([]string, error) {
3679+
request := &ec2.DescribeSubnetsInput{}
3680+
for _, subnetID := range subnetIDs {
3681+
request.SubnetIds = append(request.SubnetIds, aws.String(subnetID))
3682+
}
3683+
3684+
subnets, err := c.ec2.DescribeSubnets(request)
3685+
if err != nil {
3686+
return nil, fmt.Errorf("error querying Subnet for ELB: %q", err)
3687+
}
3688+
if len(subnets) != len(subnetIDs) {
3689+
return nil, fmt.Errorf("error querying Subnet for ELB, got %d subnets for %v", len(subnets), subnetIDs)
3690+
}
3691+
3692+
cidrs := make([]string, 0, len(subnets))
3693+
for _, subnet := range subnets {
3694+
cidrs = append(cidrs, aws.StringValue(subnet.CidrBlock))
3695+
}
3696+
return cidrs, nil
3697+
}
3698+
36783699
// EnsureLoadBalancer implements LoadBalancer.EnsureLoadBalancer
36793700
func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiService *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) {
36803701
annotations := apiService.Annotations
@@ -3804,6 +3825,12 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
38043825
return nil, err
38053826
}
38063827

3828+
subnetCidrs, err := c.getSubnetCidrs(subnetIDs)
3829+
if err != nil {
3830+
klog.Errorf("Error getting subnet cidrs: %q", err)
3831+
return nil, err
3832+
}
3833+
38073834
sourceRangeCidrs := []string{}
38083835
for cidr := range sourceRanges {
38093836
sourceRangeCidrs = append(sourceRangeCidrs, cidr)
@@ -3812,7 +3839,7 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
38123839
sourceRangeCidrs = append(sourceRangeCidrs, "0.0.0.0/0")
38133840
}
38143841

3815-
err = c.updateInstanceSecurityGroupsForNLB(loadBalancerName, instances, sourceRangeCidrs, v2Mappings)
3842+
err = c.updateInstanceSecurityGroupsForNLB(loadBalancerName, instances, subnetCidrs, sourceRangeCidrs, v2Mappings)
38163843
if err != nil {
38173844
klog.Warningf("Error opening ingress rules for the load balancer to the instances: %q", err)
38183845
return nil, err
@@ -4383,7 +4410,7 @@ func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin
43834410
}
43844411
}
43854412

4386-
return c.updateInstanceSecurityGroupsForNLB(loadBalancerName, nil, nil, nil)
4413+
return c.updateInstanceSecurityGroupsForNLB(loadBalancerName, nil, nil, nil, nil)
43874414
}
43884415

43894416
lb, err := c.describeLoadBalancer(loadBalancerName)

staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -716,30 +716,9 @@ func (c *Cloud) ensureTargetGroup(targetGroup *elbv2.TargetGroup, serviceName ty
716716
return targetGroup, nil
717717
}
718718

719-
func (c *Cloud) getVpcCidrBlocks() ([]string, error) {
720-
vpcs, err := c.ec2.DescribeVpcs(&ec2.DescribeVpcsInput{
721-
VpcIds: []*string{aws.String(c.vpcID)},
722-
})
723-
if err != nil {
724-
return nil, fmt.Errorf("error querying VPC for ELB: %q", err)
725-
}
726-
if len(vpcs.Vpcs) != 1 {
727-
return nil, fmt.Errorf("error querying VPC for ELB, got %d vpcs for %s", len(vpcs.Vpcs), c.vpcID)
728-
}
729-
730-
cidrBlocks := make([]string, 0, len(vpcs.Vpcs[0].CidrBlockAssociationSet))
731-
for _, cidr := range vpcs.Vpcs[0].CidrBlockAssociationSet {
732-
if aws.StringValue(cidr.CidrBlockState.State) != ec2.VpcCidrBlockStateCodeAssociated {
733-
continue
734-
}
735-
cidrBlocks = append(cidrBlocks, aws.StringValue(cidr.CidrBlock))
736-
}
737-
return cidrBlocks, nil
738-
}
739-
740719
// updateInstanceSecurityGroupsForNLB will adjust securityGroup's settings to allow inbound traffic into instances from clientCIDRs and portMappings.
741720
// TIP: if either instances or clientCIDRs or portMappings are nil, then the securityGroup rules for lbName are cleared.
742-
func (c *Cloud) updateInstanceSecurityGroupsForNLB(lbName string, instances map[InstanceID]*ec2.Instance, clientCIDRs []string, portMappings []nlbPortMapping) error {
721+
func (c *Cloud) updateInstanceSecurityGroupsForNLB(lbName string, instances map[InstanceID]*ec2.Instance, subnetCIDRs []string, clientCIDRs []string, portMappings []nlbPortMapping) error {
743722
if c.cfg.Global.DisableSecurityGroupIngress {
744723
return nil
745724
}
@@ -787,14 +766,10 @@ func (c *Cloud) updateInstanceSecurityGroupsForNLB(lbName string, instances map[
787766
}
788767
clientRuleAnnotation := fmt.Sprintf("%s=%s", NLBClientRuleDescription, lbName)
789768
healthRuleAnnotation := fmt.Sprintf("%s=%s", NLBHealthCheckRuleDescription, lbName)
790-
vpcCIDRs, err := c.getVpcCidrBlocks()
791-
if err != nil {
792-
return err
793-
}
794769
for sgID, sg := range clusterSGs {
795770
sgPerms := NewIPPermissionSet(sg.IpPermissions...).Ungroup()
796771
if desiredSGIDs.Has(sgID) {
797-
if err := c.updateInstanceSecurityGroupForNLBTraffic(sgID, sgPerms, healthRuleAnnotation, "tcp", healthCheckPorts, vpcCIDRs); err != nil {
772+
if err := c.updateInstanceSecurityGroupForNLBTraffic(sgID, sgPerms, healthRuleAnnotation, "tcp", healthCheckPorts, subnetCIDRs); err != nil {
798773
return err
799774
}
800775
if err := c.updateInstanceSecurityGroupForNLBTraffic(sgID, sgPerms, clientRuleAnnotation, clientProtocol, clientPorts, clientCIDRs); err != nil {

0 commit comments

Comments
 (0)