Skip to content

Commit a0b47c5

Browse files
otaviomacedomrgrain
authored andcommitted
fix(cli): the LoadBalancerProvider doesn't match LBs when querying by a subset of tags (#32164)
There was a regression in the load balancer lookup, in which we started requiring that the set of tags in the query is strictly the same as the set of tags in the load balancer (rather than merely a subset of it). Remove the length equality constraint and also simplify the code to make the intent clearer. Fixes #32161. ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* Co-authored-by: Momo Kornher <[email protected]>
1 parent 3fd9699 commit a0b47c5

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

packages/aws-cdk/lib/context-providers/load-balancers.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,12 @@ class LoadBalancerProvider {
160160
}
161161
return (await this.describeTags(loadBalancers.map((lb) => lb.LoadBalancerArn!)))
162162
.filter((tagDescription) => {
163-
return (
164-
tagDescription.Tags?.length === this.filter.loadBalancerTags?.length &&
165-
tagDescription.Tags?.filter(
166-
(tag) =>
167-
!this.filter.loadBalancerTags!.some((filter) => {
168-
return filter.key === tag.Key && filter.value === tag.Value;
169-
}),
170-
).length === 0
171-
);
163+
// For every tag in the filter, there is some tag in the LB that matches it.
164+
// In other words, the set of tags in the filter is a subset of the set of tags in the LB.
165+
return this.filter.loadBalancerTags!.every((filter) => {
166+
return tagDescription.Tags?.some((tag) =>
167+
filter.key === tag.Key && filter.value === tag.Value);
168+
});
172169
})
173170
.flatMap((tag) => loadBalancers.filter((loadBalancer) => tag.ResourceArn === loadBalancer.LoadBalancerArn));
174171
}

packages/aws-cdk/test/context-providers/load-balancers.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,52 @@ describe('load balancer context provider plugin', () => {
199199
});
200200
});
201201

202+
test('looks up by tags - query by subset', async () => {
203+
// GIVEN
204+
mockElasticLoadBalancingV2Client
205+
.on(DescribeLoadBalancersCommand)
206+
.resolves({
207+
LoadBalancers: [
208+
{
209+
IpAddressType: 'ipv4',
210+
LoadBalancerArn: 'arn:load-balancer2',
211+
DNSName: 'dns2.example.com',
212+
CanonicalHostedZoneId: 'Z1234',
213+
SecurityGroups: ['sg-1234'],
214+
VpcId: 'vpc-1234',
215+
Type: 'application',
216+
},
217+
],
218+
})
219+
.on(DescribeTagsCommand)
220+
.resolves({
221+
TagDescriptions: [
222+
{
223+
ResourceArn: 'arn:load-balancer2',
224+
Tags: [
225+
// Load balancer has two tags...
226+
{ Key: 'some', Value: 'tag' },
227+
{ Key: 'second', Value: 'tag2' },
228+
],
229+
},
230+
],
231+
});
232+
const provider = new LoadBalancerContextProviderPlugin(mockSDK);
233+
234+
// WHEN
235+
const result = await provider.getValue({
236+
account: '1234',
237+
region: 'us-east-1',
238+
loadBalancerType: LoadBalancerType.APPLICATION,
239+
loadBalancerTags: [
240+
// ...but we are querying for only one of them
241+
{ key: 'second', value: 'tag2' },
242+
],
243+
});
244+
245+
expect(result.loadBalancerArn).toEqual('arn:load-balancer2');
246+
});
247+
202248
test('filters by type', async () => {
203249
// GIVEN
204250
mockElasticLoadBalancingV2Client

0 commit comments

Comments
 (0)