Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(appmesh): Move Client Policy from Virtual Service to backend structure #12943

Merged
merged 12 commits into from
Mar 12, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export class AppMeshExtension extends ServiceExtension {
// Next update the app mesh config so that the local Envoy
// proxy on this service knows how to route traffic to
// nodes from the other service.
this.virtualNode.addBackend({ virtualService: otherAppMesh.virtualService });
this.virtualNode.addBackend(appmesh.Backend.virtualServiceBackend({ virtualService: otherAppMesh.virtualService }));
}

private routeSpec(weightedTargets: appmesh.WeightedTarget[], serviceName: string): appmesh.RouteSpec {
Expand Down
70 changes: 61 additions & 9 deletions packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,83 @@ class FileAccessLog extends AccessLog {
}

/**
* Represents the properties needed to define a backend
* Represents the properties needed to define backend defaults
*/
export interface BackendDefaults {
/**
* Client policy for backend defaults
*
* @default none
*/
readonly clientPolicy?: ClientPolicy;
}

/**
* Represents the properties needed to define a Virtual Service backend
*/
export interface Backend {
export interface VirtualServiceBackendOptions {
/**
* The Virtual Service this backend points to
*/
readonly virtualService: IVirtualService;

/**
* Client policy for a backend
* Client policy for the backend
*
* @default none
*/
readonly clientPolicy?: ClientPolicy;
}

/**
* Represents the properties needed to define backend defaults
* Properties for a backend
*/
export interface BackendDefaults {
export interface BackendConfig {
/**
* Client policy for backend defaults
*
* @default none
* Config for a Virtual Service backend
*/
readonly clientPolicy?: ClientPolicy;
readonly virtualServiceBackend: CfnVirtualNode.BackendProperty;
}


/**
* Contains static factory methods to create backends
*/
export abstract class Backend {
/**
* Construct a Virtual Service backend
*/
public static virtualServiceBackend(props: VirtualServiceBackendOptions): Backend {
alexbrjo marked this conversation as resolved.
Show resolved Hide resolved
return new VirtualServiceBackend(props.virtualService, props.clientPolicy);
}

/**
* Return backend config
*/
public abstract bind(_scope: Construct): BackendConfig;
}

/**
* Represents the properties needed to define a Virtual Service backend
*/
class VirtualServiceBackend extends Backend {

constructor (private readonly virtualService: IVirtualService,
private readonly clientPolicy: ClientPolicy | undefined) {
skinny85 marked this conversation as resolved.
Show resolved Hide resolved
super();
}

/**
* Return config for a Virtual Service backend
*/
public bind(_scope: Construct): BackendConfig {
return {
virtualServiceBackend: {
virtualService: {
virtualServiceName: this.virtualService.virtualServiceName,
clientPolicy: this.clientPolicy?.bind(_scope).clientPolicy,
},
},
};
}
}
7 changes: 1 addition & 6 deletions packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ export class VirtualNode extends VirtualNodeBase {
* Add a Virtual Services that this node is expected to send outbound traffic to
*/
public addBackend(backend: Backend) {
this.backends.push({
virtualService: {
virtualServiceName: backend.virtualService.virtualServiceName,
clientPolicy: backend.clientPolicy?.bind(this).clientPolicy,
},
});
this.backends.push(backend.bind(this).virtualServiceBackend);
}
}

Expand Down
20 changes: 8 additions & 12 deletions packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ const node = mesh.addVirtualNode('node', {
path: '/check-path',
},
})],
backends: [
{
virtualService: virtualService,
},
],
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: virtualService,
})],
});

node.addBackend({
node.addBackend(appmesh.Backend.virtualServiceBackend({
virtualService: virtualService2,
});
}));

router.addRoute('route-1', {
routeSpec: appmesh.RouteSpec.http({
Expand Down Expand Up @@ -93,11 +91,9 @@ const node2 = mesh.addVirtualNode('node2', {
certificateChain: 'path/to/cert',
}),
},
backends: [
{
virtualService: virtualService3,
},
],
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: virtualService3,
})],
});

const node3 = mesh.addVirtualNode('node3', {
Expand Down
8 changes: 3 additions & 5 deletions packages/@aws-cdk/aws-appmesh/test/test.mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,9 @@ export = {
listeners: [appmesh.VirtualNodeListener.http({
port: 8080,
})],
backends: [
{
virtualService: service1,
},
],
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service1,
})],
});

// THEN
Expand Down
10 changes: 6 additions & 4 deletions packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export = {
const node = new appmesh.VirtualNode(stack, 'test-node', {
mesh,
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
backends: [{ virtualService: service1 }],
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service1,
})],
});

node.addBackend({ virtualService: service2 });
node.addBackend(appmesh.Backend.virtualServiceBackend({ virtualService: service2 }));

// THEN
expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', {
Expand Down Expand Up @@ -324,13 +326,13 @@ export = {
virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh),
});

node.addBackend({
node.addBackend(appmesh.Backend.virtualServiceBackend({
virtualService: service1,
clientPolicy: appmesh.ClientPolicy.fileTrust({
certificateChain: 'path-to-certificate',
ports: [8080, 8081],
}),
});
}));

// THEN
expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', {
Expand Down
20 changes: 10 additions & 10 deletions packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ export = {
listeners: [appmesh.VirtualNodeListener.http({
port: 8080,
})],
backends: [{
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service1,
}],
})],
});

router.addRoute('route-1', {
Expand Down Expand Up @@ -184,27 +184,27 @@ export = {
listeners: [appmesh.VirtualNodeListener.http({
port: 8080,
})],
backends: [{
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service1,
}],
})],
});
const node2 = mesh.addVirtualNode('test-node2', {
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
listeners: [appmesh.VirtualNodeListener.http({
port: 8080,
})],
backends: [{
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service2,
}],
})],
});
const node3 = mesh.addVirtualNode('test-node3', {
serviceDiscovery: appmesh.ServiceDiscovery.dns('test'),
listeners: [appmesh.VirtualNodeListener.http({
port: 8080,
})],
backends: [{
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service1,
}],
})],
});

router.addRoute('route-1', {
Expand Down Expand Up @@ -342,9 +342,9 @@ export = {
listeners: [appmesh.VirtualNodeListener.http({
port: 8080,
})],
backends: [{
backends: [appmesh.Backend.virtualServiceBackend({
virtualService: service1,
}],
})],
});

router.addRoute('route-tcp-1', {
Expand Down