Skip to content

Commit

Permalink
Merge pull request #805 from oliviassss/release-1.25
Browse files Browse the repository at this point in the history
Cherrypick 1.25: use subnets of ensured NLBs when update worker node SG rules
  • Loading branch information
k8s-ci-robot authored Dec 29, 2023
2 parents 3564286 + 18b861b commit 30384ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
17 changes: 13 additions & 4 deletions pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4100,13 +4100,13 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS

if isNLB(annotations) {
// Find the subnets that the ELB will live in
subnetIDs, err := c.getLoadBalancerSubnets(apiService, internalELB)
discoveredSubnetIDs, err := c.getLoadBalancerSubnets(apiService, internalELB)
if err != nil {
klog.Errorf("Error listing subnets in VPC: %q", err)
return nil, err
}
// Bail out early if there are no subnets
if len(subnetIDs) == 0 {
if len(discoveredSubnetIDs) == 0 {
return nil, fmt.Errorf("could not find any suitable subnets for creating the ELB")
}

Expand All @@ -4123,15 +4123,24 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
loadBalancerName,
v2Mappings,
instanceIDs,
subnetIDs,
discoveredSubnetIDs,
internalELB,
annotations,
)
if err != nil {
return nil, err
}

subnetCidrs, err := c.getSubnetCidrs(subnetIDs)
// try to get the ensured subnets of the LBs from AZs
var ensuredSubnetIDs []string
var subnetCidrs []string
for _, az := range v2LoadBalancer.AvailabilityZones {
ensuredSubnetIDs = append(ensuredSubnetIDs, *az.SubnetId)
}
if len(ensuredSubnetIDs) == 0 {
return nil, fmt.Errorf("did not find ensured subnets on LB %s", loadBalancerName)
}
subnetCidrs, err = c.getSubnetCidrs(ensuredSubnetIDs)
if err != nil {
klog.Errorf("Error getting subnet cidrs: %q", err)
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/providers/v1/aws_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func getKeyValuePropertiesFromAnnotation(annotations map[string]string, annotati
}

// ensureLoadBalancerv2 ensures a v2 load balancer is created
func (c *Cloud) ensureLoadBalancerv2(namespacedName types.NamespacedName, loadBalancerName string, mappings []nlbPortMapping, instanceIDs, subnetIDs []string, internalELB bool, annotations map[string]string) (*elbv2.LoadBalancer, error) {
func (c *Cloud) ensureLoadBalancerv2(namespacedName types.NamespacedName, loadBalancerName string, mappings []nlbPortMapping, instanceIDs, discoveredSubnetIDs []string, internalELB bool, annotations map[string]string) (*elbv2.LoadBalancer, error) {
loadBalancer, err := c.describeLoadBalancerv2(loadBalancerName)
if err != nil {
return nil, err
Expand All @@ -165,14 +165,14 @@ func (c *Cloud) ensureLoadBalancerv2(namespacedName types.NamespacedName, loadBa
var allocationIDs []string
if eipList, present := annotations[ServiceAnnotationLoadBalancerEIPAllocations]; present {
allocationIDs = strings.Split(eipList, ",")
if len(allocationIDs) != len(subnetIDs) {
return nil, fmt.Errorf("error creating load balancer: Must have same number of EIP AllocationIDs (%d) and SubnetIDs (%d)", len(allocationIDs), len(subnetIDs))
if len(allocationIDs) != len(discoveredSubnetIDs) {
return nil, fmt.Errorf("error creating load balancer: Must have same number of EIP AllocationIDs (%d) and SubnetIDs (%d)", len(allocationIDs), len(discoveredSubnetIDs))
}
}

// We are supposed to specify one subnet per AZ.
// TODO: What happens if we have more than one subnet per AZ?
createRequest.SubnetMappings = createSubnetMappings(subnetIDs, allocationIDs)
createRequest.SubnetMappings = createSubnetMappings(discoveredSubnetIDs, allocationIDs)

for k, v := range tags {
createRequest.Tags = append(createRequest.Tags, &elbv2.Tag{
Expand Down
6 changes: 6 additions & 0 deletions pkg/providers/v1/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,12 @@ func (m *MockedFakeELBV2) CreateLoadBalancer(request *elbv2.CreateLoadBalancerIn
LoadBalancerName: request.Name,
Type: aws.String(elbv2.LoadBalancerTypeEnumNetwork),
VpcId: aws.String("vpc-abc123def456abc78"),
AvailabilityZones: []*elbv2.AvailabilityZone{
{
ZoneName: aws.String("us-west-2a"),
SubnetId: aws.String("subnet-abc123de"),
},
},
}
m.LoadBalancers = append(m.LoadBalancers, newLB)

Expand Down

0 comments on commit 30384ad

Please sign in to comment.