Skip to content

Commit

Permalink
fix(appmesh): adding support with gateway route priority (#17694)
Browse files Browse the repository at this point in the history
Adding the Gateway Route `Priority` support. This is not a new feature but it was missed from the implementation.

The implementation method is mimicking how Route's `Priority` is implemented: 
 - [route-spec.ts](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-appmesh/lib/route-spec.ts)
 - [route.ts](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-appmesh/lib/route.ts)

Fixes #16821

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Seiya6329 committed Dec 14, 2021
1 parent c21320d commit a61576f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
33 changes: 31 additions & 2 deletions packages/@aws-cdk/aws-appmesh/lib/gateway-route-spec.ts
Expand Up @@ -148,10 +148,24 @@ export interface GrpcGatewayRouteMatch {
readonly rewriteRequestHostname?: boolean;
}

/**
* Base options for all gateway route specs.
*/
export interface CommonGatewayRouteSpecOptions {
/**
* The priority for the gateway route. When a Virtual Gateway has multiple gateway routes, gateway route match
* is performed in the order of specified value, where 0 is the highest priority,
* and first matched gateway route is selected.
*
* @default - no particular priority
*/
readonly priority?: number;
}

/**
* Properties specific for HTTP Based GatewayRoutes
*/
export interface HttpGatewayRouteSpecOptions {
export interface HttpGatewayRouteSpecOptions extends CommonGatewayRouteSpecOptions {
/**
* The criterion for determining a request match for this GatewayRoute.
* When path match is defined, this may optionally determine the path rewrite configuration.
Expand All @@ -169,7 +183,7 @@ export interface HttpGatewayRouteSpecOptions {
/**
* Properties specific for a gRPC GatewayRoute
*/
export interface GrpcGatewayRouteSpecOptions {
export interface GrpcGatewayRouteSpecOptions extends CommonGatewayRouteSpecOptions {
/**
* The criterion for determining a request match for this GatewayRoute
*/
Expand Down Expand Up @@ -205,6 +219,15 @@ export interface GatewayRouteSpecConfig {
* @default - no grpc spec
*/
readonly grpcSpecConfig?: CfnGatewayRoute.GrpcGatewayRouteProperty;

/**
* The priority for the gateway route. When a Virtual Gateway has multiple gateway routes, gateway route match
* is performed in the order of specified value, where 0 is the highest priority,
* and first matched gateway route is selected.
*
* @default - no particular priority
*/
readonly priority?: number;
}

/**
Expand Down Expand Up @@ -257,12 +280,14 @@ class HttpGatewayRouteSpec extends GatewayRouteSpec {
* Type of route you are creating
*/
readonly routeType: Protocol;
readonly priority?: number;

constructor(options: HttpGatewayRouteSpecOptions, protocol: Protocol.HTTP | Protocol.HTTP2) {
super();
this.routeTarget = options.routeTarget;
this.routeType = protocol;
this.match = options.match;
this.priority = options.priority;
}

public bind(scope: Construct): GatewayRouteSpecConfig {
Expand Down Expand Up @@ -301,6 +326,7 @@ class HttpGatewayRouteSpec extends GatewayRouteSpec {
},
};
return {
priority: this.priority,
httpSpecConfig: this.routeType === Protocol.HTTP ? httpConfig : undefined,
http2SpecConfig: this.routeType === Protocol.HTTP2 ? httpConfig : undefined,
};
Expand All @@ -314,11 +340,13 @@ class GrpcGatewayRouteSpec extends GatewayRouteSpec {
* The VirtualService this GatewayRoute directs traffic to
*/
readonly routeTarget: IVirtualService;
readonly priority?: number;

constructor(options: GrpcGatewayRouteSpecOptions) {
super();
this.match = options.match;
this.routeTarget = options.routeTarget;
this.priority = options.priority;
}

public bind(scope: Construct): GatewayRouteSpecConfig {
Expand Down Expand Up @@ -349,6 +377,7 @@ class GrpcGatewayRouteSpec extends GatewayRouteSpec {
},
},
},
priority: this.priority,
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-appmesh/lib/gateway-route.ts
Expand Up @@ -119,6 +119,7 @@ export class GatewayRoute extends cdk.Resource implements IGatewayRoute {
httpRoute: routeSpecConfig.httpSpecConfig,
http2Route: routeSpecConfig.http2SpecConfig,
grpcRoute: routeSpecConfig.grpcSpecConfig,
priority: routeSpecConfig.priority,
},
virtualGatewayName: this.virtualGateway.virtualGatewayName,
});
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-appmesh/lib/route-spec.ts
Expand Up @@ -120,8 +120,8 @@ export interface GrpcRouteMatch {
*/
export interface RouteSpecOptionsBase {
/**
* The priority for the route. Routes are matched based on the specified
* value, where 0 is the highest priority.
* The priority for the route. When a Virtual Router has multiple routes, route match is performed in the
* order of specified value, where 0 is the highest priority, and first matched route is selected.
*
* @default - no particular priority
*/
Expand Down Expand Up @@ -357,8 +357,8 @@ export interface RouteSpecConfig {
readonly tcpRouteSpec?: CfnRoute.TcpRouteProperty;

/**
* The priority for the route. Routes are matched based on the specified
* value, where 0 is the highest priority.
* The priority for the route. When a Virtual Router has multiple routes, route match is performed in the
* order of specified value, where 0 is the highest priority, and first matched route is selected.
*
* @default - no particular priority
*/
Expand Down
78 changes: 78 additions & 0 deletions packages/@aws-cdk/aws-appmesh/test/gateway-route.test.ts
Expand Up @@ -1122,8 +1122,86 @@ describe('gateway route', () => {
},
},
});
});
});

describe('with priority', () => {
test('should set the priority for http gateway route', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const mesh = new appmesh.Mesh(stack, 'mesh', {
meshName: 'test-mesh',
});

const virtualGateway = new appmesh.VirtualGateway(stack, 'gateway-1', {
listeners: [appmesh.VirtualGatewayListener.http()],
mesh: mesh,
});

const virtualService = new appmesh.VirtualService(stack, 'vs-1', {
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
virtualServiceName: 'target.local',
});

// Add an HTTP Route
virtualGateway.addGatewayRoute('gateway-http-route', {
routeSpec: appmesh.GatewayRouteSpec.http({
routeTarget: virtualService,
match: {
},
priority: 100,
}),
gatewayRouteName: 'gateway-http-route',
});

// THEN
expect(stack).toHaveResourceLike('AWS::AppMesh::GatewayRoute', {
Spec: {
Priority: 100,
},
});
});

test('should set the priority for grpc gateway route', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const mesh = new appmesh.Mesh(stack, 'mesh', {
meshName: 'test-mesh',
});

const virtualGateway = new appmesh.VirtualGateway(stack, 'gateway-1', {
listeners: [appmesh.VirtualGatewayListener.grpc()],
mesh: mesh,
});

const virtualService = new appmesh.VirtualService(stack, 'vs-1', {
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
virtualServiceName: 'target.local',
});

// Add an Grpc Route
new appmesh.GatewayRoute(stack, 'test-node', {
routeSpec: appmesh.GatewayRouteSpec.grpc({
match: {
serviceName: virtualService.virtualServiceName,
},
routeTarget: virtualService,
priority: 500,
}),
virtualGateway: virtualGateway,
gatewayRouteName: 'routeWithPriority',
});

// THEN
expect(stack).toHaveResourceLike('AWS::AppMesh::GatewayRoute', {
Spec: {
Priority: 500,
},
});
});
});

Expand Down

0 comments on commit a61576f

Please sign in to comment.