Skip to content

Commit 54f91c8

Browse files
authored
feat(app-mesh): support port property on weighted targets (#26114)
As described in the related issue, `WeightedTarget` L2 construct was missing `port` property which is already present in the L1 construct `CfnRoute`. This PR adds the missing `port` property to `WeightedTarget` L2 construct. The PR includes unit tests expansion to cover this new property appearance or absence. Closes #26083. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7926560 commit 54f91c8

File tree

7 files changed

+134
-7
lines changed

7 files changed

+134
-7
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-appmesh/test/integ.mesh-port-match.js.snapshot/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/22240821b1bd2dffca97a9e0b581bf2d1a2dc32765da9872c066c10f315cd391.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/7eca470b64972db3f33a642114983901fa5e45a5c40d071fe328451294b6f3db.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [

packages/@aws-cdk-testing/framework-integ/test/aws-appmesh/test/integ.mesh-port-match.js.snapshot/mesh-stack.assets.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "32.0.0",
33
"files": {
4-
"22240821b1bd2dffca97a9e0b581bf2d1a2dc32765da9872c066c10f315cd391": {
4+
"7eca470b64972db3f33a642114983901fa5e45a5c40d071fe328451294b6f3db": {
55
"source": {
66
"path": "mesh-stack.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "22240821b1bd2dffca97a9e0b581bf2d1a2dc32765da9872c066c10f315cd391.json",
12+
"objectKey": "7eca470b64972db3f33a642114983901fa5e45a5c40d071fe328451294b6f3db.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk-testing/framework-integ/test/aws-appmesh/test/integ.mesh-port-match.js.snapshot/mesh-stack.template.json

+1
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@
468468
"Action": {
469469
"WeightedTargets": [
470470
{
471+
"Port": 1234,
471472
"VirtualNode": {
472473
"Fn::GetAtt": [
473474
"meshgrpcnode5DE90B75",

packages/@aws-cdk-testing/framework-integ/test/aws-appmesh/test/integ.mesh-port-match.js.snapshot/tree.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,8 @@
807807
"VirtualNodeName"
808808
]
809809
},
810-
"weight": 1
810+
"weight": 1,
811+
"port": 1234
811812
}
812813
]
813814
},
@@ -1264,7 +1265,7 @@
12641265
"path": "appmesh-routes-port-matchers/DefaultTest/Default",
12651266
"constructInfo": {
12661267
"fqn": "constructs.Construct",
1267-
"version": "10.2.26"
1268+
"version": "10.2.55"
12681269
}
12691270
},
12701271
"DeployAssert": {
@@ -1310,7 +1311,7 @@
13101311
"path": "Tree",
13111312
"constructInfo": {
13121313
"fqn": "constructs.Construct",
1313-
"version": "10.2.26"
1314+
"version": "10.2.55"
13141315
}
13151316
}
13161317
},

packages/@aws-cdk-testing/framework-integ/test/aws-appmesh/test/integ.mesh-port-match.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ httpRouter.addRoute('http-route', {
6565

6666
grpcRouter.addRoute('grpc-route', {
6767
routeSpec: appmesh.RouteSpec.grpc({
68-
weightedTargets: [{ virtualNode: grpcNode }],
68+
weightedTargets: [{ virtualNode: grpcNode, port: 1234 }],
6969
match: {
7070
port: 1234,
7171
},

packages/aws-cdk-lib/aws-appmesh/lib/route-spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export interface WeightedTarget {
2424
* @default 1
2525
*/
2626
readonly weight?: number;
27+
28+
/**
29+
* The port to match from the request.
30+
*
31+
* @default - do not match on port
32+
*/
33+
readonly port?: number;
2734
}
2835

2936
/**
@@ -603,6 +610,7 @@ function renderWeightedTargets(weightedTargets: WeightedTarget[]): CfnRoute.Weig
603610
renderedTargets.push({
604611
virtualNode: t.virtualNode.virtualNodeName,
605612
weight: t.weight == undefined ? 1 : t.weight,
613+
port: t.port,
606614
});
607615
}
608616
return renderedTargets;

packages/aws-cdk-lib/aws-appmesh/test/route.test.ts

+117
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,123 @@ describe('route', () => {
280280

281281
});
282282

283+
test('should allow weighted targets with port specified', () => {
284+
// GIVEN
285+
const stack = new cdk.Stack();
286+
const mesh = new appmesh.Mesh(stack, 'mesh', {
287+
meshName: 'test-mesh',
288+
});
289+
const router = new appmesh.VirtualRouter(stack, 'router', {
290+
mesh,
291+
});
292+
293+
// WHEN
294+
const node = mesh.addVirtualNode('test-node', {
295+
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
296+
listeners: [appmesh.VirtualNodeListener.http()],
297+
});
298+
299+
router.addRoute('test-http-route', {
300+
routeSpec: appmesh.RouteSpec.http({
301+
weightedTargets: [
302+
{
303+
virtualNode: node,
304+
port: 1234,
305+
},
306+
],
307+
match: {
308+
path: appmesh.HttpRoutePathMatch.startsWith('/node'),
309+
},
310+
timeout: {
311+
idle: cdk.Duration.seconds(10),
312+
perRequest: cdk.Duration.seconds(11),
313+
},
314+
}),
315+
});
316+
317+
// THEN
318+
Template.fromStack(stack).hasResourceProperties('AWS::AppMesh::Route', {
319+
Spec: {
320+
HttpRoute: {
321+
Action: {
322+
WeightedTargets: [
323+
{
324+
VirtualNode: {
325+
'Fn::GetAtt': [
326+
'meshtestnodeF93946D4',
327+
'VirtualNodeName',
328+
],
329+
},
330+
Weight: 1,
331+
Port: 1234,
332+
},
333+
],
334+
},
335+
},
336+
},
337+
RouteName: 'test-http-route',
338+
});
339+
340+
});
341+
342+
test('should not have weighted targets port when not specified', () => {
343+
// GIVEN
344+
const stack = new cdk.Stack();
345+
const mesh = new appmesh.Mesh(stack, 'mesh', {
346+
meshName: 'test-mesh',
347+
});
348+
const router = new appmesh.VirtualRouter(stack, 'router', {
349+
mesh,
350+
});
351+
352+
// WHEN
353+
const node = mesh.addVirtualNode('test-node', {
354+
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
355+
listeners: [appmesh.VirtualNodeListener.http()],
356+
});
357+
358+
router.addRoute('test-http-route', {
359+
routeSpec: appmesh.RouteSpec.http({
360+
weightedTargets: [
361+
{
362+
virtualNode: node,
363+
},
364+
],
365+
match: {
366+
path: appmesh.HttpRoutePathMatch.startsWith('/node'),
367+
},
368+
timeout: {
369+
idle: cdk.Duration.seconds(10),
370+
perRequest: cdk.Duration.seconds(11),
371+
},
372+
}),
373+
});
374+
375+
// THEN
376+
Template.fromStack(stack).hasResourceProperties('AWS::AppMesh::Route', {
377+
Spec: {
378+
HttpRoute: {
379+
Action: {
380+
WeightedTargets: [
381+
{
382+
VirtualNode: {
383+
'Fn::GetAtt': [
384+
'meshtestnodeF93946D4',
385+
'VirtualNodeName',
386+
],
387+
},
388+
Weight: 1,
389+
Port: Match.absent(),
390+
},
391+
],
392+
},
393+
},
394+
},
395+
RouteName: 'test-http-route',
396+
});
397+
398+
});
399+
283400
test('should allow http retries', () => {
284401
// GIVEN
285402
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)