Skip to content

Commit

Permalink
feat(app-mesh): support port property on weighted targets (aws#26114)
Browse files Browse the repository at this point in the history
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 aws#26083.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
neovasili authored and tmokmss committed Jul 9, 2023
1 parent 403475e commit 17534b3
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/22240821b1bd2dffca97a9e0b581bf2d1a2dc32765da9872c066c10f315cd391.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/7eca470b64972db3f33a642114983901fa5e45a5c40d071fe328451294b6f3db.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "32.0.0",
"files": {
"22240821b1bd2dffca97a9e0b581bf2d1a2dc32765da9872c066c10f315cd391": {
"7eca470b64972db3f33a642114983901fa5e45a5c40d071fe328451294b6f3db": {
"source": {
"path": "mesh-stack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "22240821b1bd2dffca97a9e0b581bf2d1a2dc32765da9872c066c10f315cd391.json",
"objectKey": "7eca470b64972db3f33a642114983901fa5e45a5c40d071fe328451294b6f3db.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
"Action": {
"WeightedTargets": [
{
"Port": 1234,
"VirtualNode": {
"Fn::GetAtt": [
"meshgrpcnode5DE90B75",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,8 @@
"VirtualNodeName"
]
},
"weight": 1
"weight": 1,
"port": 1234
}
]
},
Expand Down Expand Up @@ -1264,7 +1265,7 @@
"path": "appmesh-routes-port-matchers/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.26"
"version": "10.2.55"
}
},
"DeployAssert": {
Expand Down Expand Up @@ -1310,7 +1311,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.26"
"version": "10.2.55"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ httpRouter.addRoute('http-route', {

grpcRouter.addRoute('grpc-route', {
routeSpec: appmesh.RouteSpec.grpc({
weightedTargets: [{ virtualNode: grpcNode }],
weightedTargets: [{ virtualNode: grpcNode, port: 1234 }],
match: {
port: 1234,
},
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk-lib/aws-appmesh/lib/route-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface WeightedTarget {
* @default 1
*/
readonly weight?: number;

/**
* The port to match from the request.
*
* @default - do not match on port
*/
readonly port?: number;
}

/**
Expand Down Expand Up @@ -603,6 +610,7 @@ function renderWeightedTargets(weightedTargets: WeightedTarget[]): CfnRoute.Weig
renderedTargets.push({
virtualNode: t.virtualNode.virtualNodeName,
weight: t.weight == undefined ? 1 : t.weight,
port: t.port,
});
}
return renderedTargets;
Expand Down
117 changes: 117 additions & 0 deletions packages/aws-cdk-lib/aws-appmesh/test/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,123 @@ describe('route', () => {

});

test('should allow weighted targets with port specified', () => {
// GIVEN
const stack = new cdk.Stack();
const mesh = new appmesh.Mesh(stack, 'mesh', {
meshName: 'test-mesh',
});
const router = new appmesh.VirtualRouter(stack, 'router', {
mesh,
});

// WHEN
const node = mesh.addVirtualNode('test-node', {
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
listeners: [appmesh.VirtualNodeListener.http()],
});

router.addRoute('test-http-route', {
routeSpec: appmesh.RouteSpec.http({
weightedTargets: [
{
virtualNode: node,
port: 1234,
},
],
match: {
path: appmesh.HttpRoutePathMatch.startsWith('/node'),
},
timeout: {
idle: cdk.Duration.seconds(10),
perRequest: cdk.Duration.seconds(11),
},
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppMesh::Route', {
Spec: {
HttpRoute: {
Action: {
WeightedTargets: [
{
VirtualNode: {
'Fn::GetAtt': [
'meshtestnodeF93946D4',
'VirtualNodeName',
],
},
Weight: 1,
Port: 1234,
},
],
},
},
},
RouteName: 'test-http-route',
});

});

test('should not have weighted targets port when not specified', () => {
// GIVEN
const stack = new cdk.Stack();
const mesh = new appmesh.Mesh(stack, 'mesh', {
meshName: 'test-mesh',
});
const router = new appmesh.VirtualRouter(stack, 'router', {
mesh,
});

// WHEN
const node = mesh.addVirtualNode('test-node', {
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
listeners: [appmesh.VirtualNodeListener.http()],
});

router.addRoute('test-http-route', {
routeSpec: appmesh.RouteSpec.http({
weightedTargets: [
{
virtualNode: node,
},
],
match: {
path: appmesh.HttpRoutePathMatch.startsWith('/node'),
},
timeout: {
idle: cdk.Duration.seconds(10),
perRequest: cdk.Duration.seconds(11),
},
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppMesh::Route', {
Spec: {
HttpRoute: {
Action: {
WeightedTargets: [
{
VirtualNode: {
'Fn::GetAtt': [
'meshtestnodeF93946D4',
'VirtualNodeName',
],
},
Weight: 1,
Port: Match.absent(),
},
],
},
},
},
RouteName: 'test-http-route',
});

});

test('should allow http retries', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 17534b3

Please sign in to comment.