cluster-api-provider-aws
cluster-api-provider-aws copied to clipboard
🐛 Fix subnet sorting with multiple vpcs when launching an instance
What type of PR is this? /kind bug
What this PR does / why we need it:
https://github.com/kubernetes-sigs/cluster-api-provider-aws/pull/4541 I introduced a change to allow subnets from other subnets to be returned from the AWS describe subnets query. However, the sorting is subtly bugged. func(i, j int) bool { is supposed to return true when i is less than j, or should be ordered first. We want any subnets with the cluster VPC to be "less" than to sort the strings.
The existing implementation (which I wrote :disappointed: ) does not do that. strings.Compare(*filtered[i].VpcId, clusterVPC) returns 0 (string match) or 1 if the clusterVPC > VPCID, or -1 if VPCID < clusterVIPC. The second comparison strings.Compare(*filtered[j].VpcId, clusterVPC) is the same. However, it does not make sense to compare the result of these two string comparisons to sort the strings, the goal is to ensure subnets with the cluster VPC is ordered earlier.
Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes # N/A
Special notes for your reviewer:
The first commit updates only the unit tests to demonstrate the bug exists in the main branch. The second commit fixes the bug and makes the tests pass.
Checklist:
- [ ] squashed commits
- [ ] includes documentation
- [X] includes emojis
- [X] adds unit tests
- [ ] adds or updates e2e tests
Release note:
Fix subnet sorting with multiple vpcs when launching an instance