|
6 | 6 | "encoding/hex"
|
7 | 7 | "fmt"
|
8 | 8 | "regexp"
|
| 9 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/deploy/tracking" |
9 | 10 | "strings"
|
10 | 11 |
|
11 | 12 | awssdk "github.com/aws/aws-sdk-go/aws"
|
@@ -189,32 +190,48 @@ func (t *defaultModelBuildTask) buildLoadBalancerSubnetMappings(ctx context.Cont
|
189 | 190 | explicitSubnetNameOrIDsList = append(explicitSubnetNameOrIDsList, rawSubnetNameOrIDs)
|
190 | 191 | }
|
191 | 192 |
|
192 |
| - if len(explicitSubnetNameOrIDsList) == 0 { |
193 |
| - chosenSubnets, err := t.subnetsResolver.ResolveViaDiscovery(ctx, |
| 193 | + if len(explicitSubnetNameOrIDsList) != 0 { |
| 194 | + chosenSubnetNameOrIDs := explicitSubnetNameOrIDsList[0] |
| 195 | + for _, subnetNameOrIDs := range explicitSubnetNameOrIDsList[1:] { |
| 196 | + // subnetNameOrIDs order doesn't matter |
| 197 | + if !cmp.Equal(chosenSubnetNameOrIDs, subnetNameOrIDs, equality.IgnoreStringSliceOrder()) { |
| 198 | + return nil, errors.Errorf("conflicting subnets: %v | %v", chosenSubnetNameOrIDs, subnetNameOrIDs) |
| 199 | + } |
| 200 | + } |
| 201 | + chosenSubnets, err := t.subnetsResolver.ResolveViaNameOrIDSlice(ctx, chosenSubnetNameOrIDs, |
194 | 202 | networking.WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeApplication),
|
195 | 203 | networking.WithSubnetsResolveLBScheme(scheme),
|
196 | 204 | )
|
197 | 205 | if err != nil {
|
198 |
| - return nil, errors.Wrap(err, "couldn't auto-discover subnets") |
| 206 | + return nil, err |
199 | 207 | }
|
200 | 208 | return buildLoadBalancerSubnetMappingsWithSubnets(chosenSubnets), nil
|
201 | 209 | }
|
| 210 | + stackTags := t.trackingProvider.StackTags(t.stack) |
202 | 211 |
|
203 |
| - chosenSubnetNameOrIDs := explicitSubnetNameOrIDsList[0] |
204 |
| - for _, subnetNameOrIDs := range explicitSubnetNameOrIDsList[1:] { |
205 |
| - // subnetNameOrIDs orders doesn't matter. |
206 |
| - if !cmp.Equal(chosenSubnetNameOrIDs, subnetNameOrIDs, equality.IgnoreStringSliceOrder()) { |
207 |
| - return nil, errors.Errorf("conflicting subnets: %v | %v", chosenSubnetNameOrIDs, subnetNameOrIDs) |
208 |
| - } |
209 |
| - } |
210 |
| - chosenSubnets, err := t.subnetsResolver.ResolveViaNameOrIDSlice(ctx, chosenSubnetNameOrIDs, |
211 |
| - networking.WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeApplication), |
212 |
| - networking.WithSubnetsResolveLBScheme(scheme), |
213 |
| - ) |
| 212 | + sdkLBs, err := t.elbv2TaggingManager.ListLoadBalancers(ctx, tracking.TagsAsTagFilter(stackTags)) |
214 | 213 | if err != nil {
|
215 | 214 | return nil, err
|
216 | 215 | }
|
217 |
| - return buildLoadBalancerSubnetMappingsWithSubnets(chosenSubnets), nil |
| 216 | + |
| 217 | + if len(sdkLBs) == 0 { |
| 218 | + chosenSubnets, err := t.subnetsResolver.ResolveViaDiscovery(ctx, |
| 219 | + networking.WithSubnetsResolveLBType(elbv2model.LoadBalancerTypeApplication), |
| 220 | + networking.WithSubnetsResolveLBScheme(scheme), |
| 221 | + ) |
| 222 | + if err != nil { |
| 223 | + return nil, errors.Wrap(err, "couldn't auto-discover subnets") |
| 224 | + } |
| 225 | + return buildLoadBalancerSubnetMappingsWithSubnets(chosenSubnets), nil |
| 226 | + } |
| 227 | + |
| 228 | + availabilityZones := sdkLBs[0].LoadBalancer.AvailabilityZones |
| 229 | + subnetIDs := make([]string, 0, len(availabilityZones)) |
| 230 | + for _, availabilityZone := range availabilityZones { |
| 231 | + subnetID := awssdk.StringValue(availabilityZone.SubnetId) |
| 232 | + subnetIDs = append(subnetIDs, subnetID) |
| 233 | + } |
| 234 | + return buildLoadBalancerSubnetMappingsWithSubnetIDs(subnetIDs), nil |
218 | 235 | }
|
219 | 236 |
|
220 | 237 | func (t *defaultModelBuildTask) buildLoadBalancerSecurityGroups(ctx context.Context, listenPortConfigByPort map[int64]listenPortConfig, ipAddressType elbv2model.IPAddressType) ([]core.StringToken, error) {
|
@@ -368,3 +385,13 @@ func buildLoadBalancerSubnetMappingsWithSubnets(subnets []*ec2sdk.Subnet) []elbv
|
368 | 385 | }
|
369 | 386 | return subnetMappings
|
370 | 387 | }
|
| 388 | + |
| 389 | +func buildLoadBalancerSubnetMappingsWithSubnetIDs(subnetIDs []string) []elbv2model.SubnetMapping { |
| 390 | + subnetMappings := make([]elbv2model.SubnetMapping, 0, len(subnetIDs)) |
| 391 | + for _, subnetID := range subnetIDs { |
| 392 | + subnetMappings = append(subnetMappings, elbv2model.SubnetMapping{ |
| 393 | + SubnetID: subnetID, |
| 394 | + }) |
| 395 | + } |
| 396 | + return subnetMappings |
| 397 | +} |
0 commit comments