Skip to content

Commit 02b5b60

Browse files
authored
Merge pull request kubernetes#93515 from t0rr3sp3dr0/master
Use NLB Subnet CIDRs instead of VPC CIDRs in Health Check SG Rules
2 parents c182a59 + a2bd59b commit 02b5b60

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
@@ -3665,6 +3665,27 @@ func buildListener(port v1.ServicePort, annotations map[string]string, sslPorts
36653665
return listener, nil
36663666
}
36673667

3668+
func (c *Cloud) getSubnetCidrs(subnetIDs []string) ([]string, error) {
3669+
request := &ec2.DescribeSubnetsInput{}
3670+
for _, subnetID := range subnetIDs {
3671+
request.SubnetIds = append(request.SubnetIds, aws.String(subnetID))
3672+
}
3673+
3674+
subnets, err := c.ec2.DescribeSubnets(request)
3675+
if err != nil {
3676+
return nil, fmt.Errorf("error querying Subnet for ELB: %q", err)
3677+
}
3678+
if len(subnets) != len(subnetIDs) {
3679+
return nil, fmt.Errorf("error querying Subnet for ELB, got %d subnets for %v", len(subnets), subnetIDs)
3680+
}
3681+
3682+
cidrs := make([]string, 0, len(subnets))
3683+
for _, subnet := range subnets {
3684+
cidrs = append(cidrs, aws.StringValue(subnet.CidrBlock))
3685+
}
3686+
return cidrs, nil
3687+
}
3688+
36683689
// EnsureLoadBalancer implements LoadBalancer.EnsureLoadBalancer
36693690
func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiService *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) {
36703691
annotations := apiService.Annotations
@@ -3796,6 +3817,12 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
37963817
return nil, err
37973818
}
37983819

3820+
subnetCidrs, err := c.getSubnetCidrs(subnetIDs)
3821+
if err != nil {
3822+
klog.Errorf("Error getting subnet cidrs: %q", err)
3823+
return nil, err
3824+
}
3825+
37993826
sourceRangeCidrs := []string{}
38003827
for cidr := range sourceRanges {
38013828
sourceRangeCidrs = append(sourceRangeCidrs, cidr)
@@ -3804,7 +3831,7 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
38043831
sourceRangeCidrs = append(sourceRangeCidrs, "0.0.0.0/0")
38053832
}
38063833

3807-
err = c.updateInstanceSecurityGroupsForNLB(loadBalancerName, instances, sourceRangeCidrs, v2Mappings)
3834+
err = c.updateInstanceSecurityGroupsForNLB(loadBalancerName, instances, subnetCidrs, sourceRangeCidrs, v2Mappings)
38083835
if err != nil {
38093836
klog.Warningf("Error opening ingress rules for the load balancer to the instances: %q", err)
38103837
return nil, err
@@ -4381,7 +4408,7 @@ func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin
43814408
}
43824409
}
43834410

4384-
return c.updateInstanceSecurityGroupsForNLB(loadBalancerName, nil, nil, nil)
4411+
return c.updateInstanceSecurityGroupsForNLB(loadBalancerName, nil, nil, nil, nil)
43854412
}
43864413

43874414
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
@@ -723,30 +723,9 @@ func (c *Cloud) ensureTargetGroup(targetGroup *elbv2.TargetGroup, serviceName ty
723723
return targetGroup, nil
724724
}
725725

726-
func (c *Cloud) getVpcCidrBlocks() ([]string, error) {
727-
vpcs, err := c.ec2.DescribeVpcs(&ec2.DescribeVpcsInput{
728-
VpcIds: []*string{aws.String(c.vpcID)},
729-
})
730-
if err != nil {
731-
return nil, fmt.Errorf("error querying VPC for ELB: %q", err)
732-
}
733-
if len(vpcs.Vpcs) != 1 {
734-
return nil, fmt.Errorf("error querying VPC for ELB, got %d vpcs for %s", len(vpcs.Vpcs), c.vpcID)
735-
}
736-
737-
cidrBlocks := make([]string, 0, len(vpcs.Vpcs[0].CidrBlockAssociationSet))
738-
for _, cidr := range vpcs.Vpcs[0].CidrBlockAssociationSet {
739-
if aws.StringValue(cidr.CidrBlockState.State) != ec2.VpcCidrBlockStateCodeAssociated {
740-
continue
741-
}
742-
cidrBlocks = append(cidrBlocks, aws.StringValue(cidr.CidrBlock))
743-
}
744-
return cidrBlocks, nil
745-
}
746-
747726
// updateInstanceSecurityGroupsForNLB will adjust securityGroup's settings to allow inbound traffic into instances from clientCIDRs and portMappings.
748727
// TIP: if either instances or clientCIDRs or portMappings are nil, then the securityGroup rules for lbName are cleared.
749-
func (c *Cloud) updateInstanceSecurityGroupsForNLB(lbName string, instances map[InstanceID]*ec2.Instance, clientCIDRs []string, portMappings []nlbPortMapping) error {
728+
func (c *Cloud) updateInstanceSecurityGroupsForNLB(lbName string, instances map[InstanceID]*ec2.Instance, subnetCIDRs []string, clientCIDRs []string, portMappings []nlbPortMapping) error {
750729
if c.cfg.Global.DisableSecurityGroupIngress {
751730
return nil
752731
}
@@ -794,14 +773,10 @@ func (c *Cloud) updateInstanceSecurityGroupsForNLB(lbName string, instances map[
794773
}
795774
clientRuleAnnotation := fmt.Sprintf("%s=%s", NLBClientRuleDescription, lbName)
796775
healthRuleAnnotation := fmt.Sprintf("%s=%s", NLBHealthCheckRuleDescription, lbName)
797-
vpcCIDRs, err := c.getVpcCidrBlocks()
798-
if err != nil {
799-
return err
800-
}
801776
for sgID, sg := range clusterSGs {
802777
sgPerms := NewIPPermissionSet(sg.IpPermissions...).Ungroup()
803778
if desiredSGIDs.Has(sgID) {
804-
if err := c.updateInstanceSecurityGroupForNLBTraffic(sgID, sgPerms, healthRuleAnnotation, "tcp", healthCheckPorts, vpcCIDRs); err != nil {
779+
if err := c.updateInstanceSecurityGroupForNLBTraffic(sgID, sgPerms, healthRuleAnnotation, "tcp", healthCheckPorts, subnetCIDRs); err != nil {
805780
return err
806781
}
807782
if err := c.updateInstanceSecurityGroupForNLBTraffic(sgID, sgPerms, clientRuleAnnotation, clientProtocol, clientPorts, clientCIDRs); err != nil {

0 commit comments

Comments
 (0)