Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherrypick 1.24: use subnets of ensured NLBs when update worker node SG rules #804

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4096,13 +4096,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 @@ -4119,15 +4119,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
Loading