From 8472ea11a0e8aeacd26acd2f68058993986e92dc Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Mon, 8 Feb 2021 21:36:53 -0800 Subject: [PATCH 1/9] Move Client Policy from Virtual Service to backend structure. issue https://github.com/aws/aws-cdk/issues/11996 --- packages/@aws-cdk/aws-appmesh/lib/backend.ts | 92 +++++++++++++++++++ packages/@aws-cdk/aws-appmesh/lib/index.ts | 1 + .../aws-appmesh/lib/virtual-gateway.ts | 6 +- .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 18 ++-- .../aws-appmesh/lib/virtual-service.ts | 24 ----- packages/@aws-cdk/aws-appmesh/package.json | 4 +- .../aws-appmesh/test/integ.mesh.expected.json | 14 +-- .../@aws-cdk/aws-appmesh/test/integ.mesh.ts | 49 ++++++---- .../aws-appmesh/test/test.health-check.ts | 2 - .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 4 +- .../aws-appmesh/test/test.virtual-gateway.ts | 6 +- .../aws-appmesh/test/test.virtual-node.ts | 20 ++-- .../aws-appmesh/test/test.virtual-router.ts | 28 +++--- 13 files changed, 177 insertions(+), 91 deletions(-) create mode 100644 packages/@aws-cdk/aws-appmesh/lib/backend.ts diff --git a/packages/@aws-cdk/aws-appmesh/lib/backend.ts b/packages/@aws-cdk/aws-appmesh/lib/backend.ts new file mode 100644 index 0000000000000..3281d455d130a --- /dev/null +++ b/packages/@aws-cdk/aws-appmesh/lib/backend.ts @@ -0,0 +1,92 @@ + +import { CfnVirtualNode } from './appmesh.generated'; +import { ClientPolicy } from './client-policy'; +import { IVirtualService } from './virtual-service'; + +// keep this import separate from other imports to reduce chance for merge conflicts with v2-main +// eslint-disable-next-line no-duplicate-imports, import/order +import { Construct } from '@aws-cdk/core'; + +/** + * Represents the properties needed to define backend defaults + */ +export interface BackendDefaultsOptions { + /** + * Client policy for backend defaults + * + * @default none + */ + readonly clientPolicy?: ClientPolicy; +} + +/** + * Represents the properties needed to define a backend + */ +export interface BackendOptions { + /** + * The Virtual Service this backend points to + */ + readonly virtualService: IVirtualService; + /** + * Client policy for a backend + * + * @default none + */ + readonly clientPolicy?: ClientPolicy; +} + +/** + * Provides static factory methods to generate backend API structures + */ +export class Backends { + /** + * Creates a backend defaults + */ + public static backendDefaults(props: BackendDefaultsOptions): BackendDefaults { + return new BackendDefaults(props.clientPolicy); + } + /** + * Creates a named backend + */ + public static backend(props: BackendOptions): Backend { + return new Backend(props.virtualService, props.clientPolicy); + } +} + +/** + * Represents all the backends that aren't specifically defined using the backend . + */ +export class BackendDefaults { + + constructor (private readonly clientPolicy: ClientPolicy | undefined) {} + + /** + * Return backend defaults config + */ + public bind(_scope: Construct): CfnVirtualNode.BackendDefaultsProperty { + return { + clientPolicy: this.clientPolicy?.bind(_scope).clientPolicy, + }; + } +} + +/** + * Represents the backend that a virtual node will send outbound traffic to + */ +export class Backend { + + constructor (private readonly virtualService: IVirtualService, + private readonly clientPolicy: ClientPolicy | undefined) {} + + /** + * Return backend config + */ + public bind(_scope: Construct): CfnVirtualNode.BackendProperty { + return { + virtualService: { + virtualServiceName: this.virtualService.virtualServiceName, + clientPolicy: this.clientPolicy?.bind(_scope).clientPolicy, + }, + }; + } +} diff --git a/packages/@aws-cdk/aws-appmesh/lib/index.ts b/packages/@aws-cdk/aws-appmesh/lib/index.ts index 1f5ca87def34d..d24f178449694 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/index.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/index.ts @@ -16,3 +16,4 @@ export * from './virtual-gateway-listener'; export * from './gateway-route'; export * from './gateway-route-spec'; export * from './client-policy'; +export * from './backend'; diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index 1e1144fed1038..ad8839363d041 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -1,7 +1,7 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualGateway } from './appmesh.generated'; -import { ClientPolicy } from './client-policy'; +import { BackendDefaults } from './backend'; import { GatewayRoute, GatewayRouteBaseProps } from './gateway-route'; import { IMesh, Mesh } from './mesh'; import { AccessLog } from './shared-interfaces'; @@ -66,7 +66,7 @@ export interface VirtualGatewayBaseProps { * * @default - No Config */ - readonly backendsDefaultClientPolicy?: ClientPolicy; + readonly backendDefaults?: BackendDefaults; } /** @@ -180,7 +180,7 @@ export class VirtualGateway extends VirtualGatewayBase { meshName: this.mesh.meshName, spec: { listeners: this.listeners.map(listener => listener.listener), - backendDefaults: props.backendsDefaultClientPolicy?.bind(this), + backendDefaults: props.backendDefaults?.bind(this), logging: accessLogging !== undefined ? { accessLog: accessLogging.virtualGatewayAccessLog, } : undefined, diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index 2cf56c74631a2..20aa407ef0578 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -1,12 +1,11 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualNode } from './appmesh.generated'; -import { ClientPolicy } from './client-policy'; +import { Backend, BackendDefaults } from './backend'; import { IMesh, Mesh } from './mesh'; import { ServiceDiscovery } from './service-discovery'; import { AccessLog } from './shared-interfaces'; import { VirtualNodeListener, VirtualNodeListenerConfig } from './virtual-node-listener'; -import { IVirtualService } from './virtual-service'; /** * Interface which all VirtualNode based classes must implement @@ -61,7 +60,7 @@ export interface VirtualNodeBaseProps { * * @default - No backends */ - readonly backends?: IVirtualService[]; + readonly backends?: Backend[]; /** * Initial listener for the virtual node @@ -82,7 +81,7 @@ export interface VirtualNodeBaseProps { * * @default - No Config */ - readonly backendsDefaultClientPolicy?: ClientPolicy; + readonly backendDefaults?: BackendDefaults; } /** @@ -185,7 +184,7 @@ export class VirtualNode extends VirtualNodeBase { spec: { backends: cdk.Lazy.anyValue({ produce: () => this.backends }, { omitEmptyArray: true }), listeners: cdk.Lazy.anyValue({ produce: () => this.listeners.map(listener => listener.listener) }, { omitEmptyArray: true }), - backendDefaults: props.backendsDefaultClientPolicy?.bind(this), + backendDefaults: props.backendDefaults?.bind(this), serviceDiscovery: { dns: serviceDiscovery?.dns, awsCloudMap: serviceDiscovery?.cloudmap, @@ -214,13 +213,8 @@ export class VirtualNode extends VirtualNodeBase { /** * Add a Virtual Services that this node is expected to send outbound traffic to */ - public addBackend(virtualService: IVirtualService) { - this.backends.push({ - virtualService: { - virtualServiceName: virtualService.virtualServiceName, - clientPolicy: virtualService.clientPolicy?.bind(this).clientPolicy, - }, - }); + public addBackend(backend: Backend) { + this.backends.push(backend.bind(this)); } } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts index 5685b8b08c1f8..d41b47d554178 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-service.ts @@ -1,7 +1,6 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualService } from './appmesh.generated'; -import { ClientPolicy } from './client-policy'; import { IMesh, Mesh } from './mesh'; import { IVirtualNode } from './virtual-node'; import { IVirtualRouter } from './virtual-router'; @@ -28,11 +27,6 @@ export interface IVirtualService extends cdk.IResource { * The Mesh which the VirtualService belongs to */ readonly mesh: IMesh; - - /** - * Client policy for this Virtual Service - */ - readonly clientPolicy?: ClientPolicy; } /** @@ -50,13 +44,6 @@ export interface VirtualServiceProps { */ readonly virtualServiceName?: string; - /** - * Client policy for this Virtual Service - * - * @default - none - */ - readonly clientPolicy?: ClientPolicy; - /** * The VirtualNode or VirtualRouter which the VirtualService uses as its provider */ @@ -90,7 +77,6 @@ export class VirtualService extends cdk.Resource implements IVirtualService { return new class extends cdk.Resource implements IVirtualService { readonly virtualServiceName = attrs.virtualServiceName; readonly mesh = attrs.mesh; - readonly clientPolicy = attrs.clientPolicy; readonly virtualServiceArn = cdk.Stack.of(this).formatArn({ service: 'appmesh', resource: `mesh/${attrs.mesh.meshName}/virtualService`, @@ -114,14 +100,11 @@ export class VirtualService extends cdk.Resource implements IVirtualService { */ public readonly mesh: IMesh; - public readonly clientPolicy?: ClientPolicy; - constructor(scope: Construct, id: string, props: VirtualServiceProps) { super(scope, id, { physicalName: props.virtualServiceName || cdk.Lazy.string({ produce: () => cdk.Names.uniqueId(this) }), }); - this.clientPolicy = props.clientPolicy; const providerConfig = props.virtualServiceProvider.bind(this); this.mesh = providerConfig.mesh; @@ -160,13 +143,6 @@ export interface VirtualServiceAttributes { * The Mesh which the VirtualService belongs to */ readonly mesh: IMesh; - - /** - * Client policy for this Virtual Service - * - * @default - none - */ - readonly clientPolicy?: ClientPolicy; } /** diff --git a/packages/@aws-cdk/aws-appmesh/package.json b/packages/@aws-cdk/aws-appmesh/package.json index cf21301f13e5c..168361392fe1d 100644 --- a/packages/@aws-cdk/aws-appmesh/package.json +++ b/packages/@aws-cdk/aws-appmesh/package.json @@ -10,7 +10,9 @@ "dotnet": { "namespace": "Amazon.CDK.AWS.AppMesh", "packageId": "Amazon.CDK.AWS.AppMesh", - "iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png" + "iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png", + "signAssembly": true, + "assemblyOriginatorKeyFile": "../../key.snk" }, "java": { "package": "software.amazon.awscdk.services.appmesh", diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json index 5f4a9ca206725..83c6a7cc1942e 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json @@ -640,7 +640,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service6D174F83", + "service16B72496E", "VirtualServiceName" ] } @@ -650,7 +650,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service27C65CF7D", + "service2B2862B6B", "VirtualServiceName" ] } @@ -711,7 +711,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service3859EB104", + "service3529E2EB1", "VirtualServiceName" ] } @@ -846,7 +846,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service6D174F83", + "service16B72496E", "VirtualServiceName" ] } @@ -883,7 +883,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service6D174F83", + "service16B72496E", "VirtualServiceName" ] } @@ -920,7 +920,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service6D174F83", + "service16B72496E", "VirtualServiceName" ] } @@ -930,7 +930,7 @@ "Match": { "ServiceName": { "Fn::GetAtt": [ - "service6D174F83", + "service16B72496E", "VirtualServiceName" ] } diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index 90e54586f7f51..2be5881eaf8bf 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -23,11 +23,21 @@ const router = mesh.addVirtualRouter('router', { ], }); -const virtualService = new appmesh.VirtualService(stack, 'service', { +const virtualService1 = new appmesh.VirtualService(stack, 'service1', { virtualServiceProvider: appmesh.VirtualServiceProvider.virtualRouter(router), virtualServiceName: 'service1.domain.local', }); +const virtualService2 = new appmesh.VirtualService(stack, 'service2', { + virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), + virtualServiceName: 'service2.domain.local', +}); + +const virtualService3 = new appmesh.VirtualService(stack, 'service3', { + virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), + virtualServiceName: 'service3.domain.local', +}); + const node = mesh.addVirtualNode('node', { serviceDiscovery: appmesh.ServiceDiscovery.dns(`node1.${namespace.namespaceName}`), listeners: [appmesh.VirtualNodeListener.http({ @@ -37,15 +47,15 @@ const node = mesh.addVirtualNode('node', { }, })], backends: [ - virtualService, + appmesh.Backends.backend({ + virtualService: virtualService1, + }), ], }); -node.addBackend(new appmesh.VirtualService(stack, 'service-2', { - virtualServiceName: 'service2.domain.local', - virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), -}), -); +node.addBackend(appmesh.Backends.backend({ + virtualService: virtualService2, +})); router.addRoute('route-1', { routeSpec: appmesh.RouteSpec.http({ @@ -78,13 +88,14 @@ const node2 = mesh.addVirtualNode('node2', { unhealthyThreshold: 2, }, })], - backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({ - certificateChain: 'path/to/cert', + backendDefaults: appmesh.Backends.backendDefaults({ + clientPolicy: appmesh.ClientPolicy.fileTrust({ + certificateChain: 'path/to/cert', + }), }), backends: [ - new appmesh.VirtualService(stack, 'service-3', { - virtualServiceName: 'service3.domain.local', - virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), + appmesh.Backends.backend({ + virtualService: virtualService3, }), ], }); @@ -102,8 +113,10 @@ const node3 = mesh.addVirtualNode('node3', { unhealthyThreshold: 2, }, })], - backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({ - certificateChain: 'path-to-certificate', + backendDefaults: appmesh.Backends.backendDefaults({ + clientPolicy: appmesh.ClientPolicy.fileTrust({ + certificateChain: 'path-to-certificate', + }), }), accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'), }); @@ -162,21 +175,21 @@ new appmesh.VirtualGateway(stack, 'gateway2', { gateway.addGatewayRoute('gateway1-route-http', { routeSpec: appmesh.GatewayRouteSpec.http({ - routeTarget: virtualService, + routeTarget: virtualService1, }), }); gateway.addGatewayRoute('gateway1-route-http2', { routeSpec: appmesh.GatewayRouteSpec.http2({ - routeTarget: virtualService, + routeTarget: virtualService1, }), }); gateway.addGatewayRoute('gateway1-route-grpc', { routeSpec: appmesh.GatewayRouteSpec.grpc({ - routeTarget: virtualService, + routeTarget: virtualService1, match: { - serviceName: virtualService.virtualServiceName, + serviceName: virtualService1.virtualServiceName, }, }), }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.health-check.ts b/packages/@aws-cdk/aws-appmesh/test/test.health-check.ts index 7eec2b6d450b9..1ba7dc425da07 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.health-check.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.health-check.ts @@ -66,8 +66,6 @@ export = { // THEN test.doesNotThrow(() => toThrow(min)); test.doesNotThrow(() => toThrow(max)); - // falsy, falls back to portMapping.port - // test.throws(() => toThrow(min - 1), /below the minimum threshold/); test.throws(() => toThrow(max + 1), /above the maximum threshold/); test.done(); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index ce50c1402a7c3..9e503050054b8 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -271,7 +271,9 @@ export = { port: 8080, })], backends: [ - service1, + appmesh.Backends.backend({ + virtualService: service1, + }), ], }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts index b9d3ed70cae43..52ffdb85bdae7 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts @@ -392,8 +392,10 @@ export = { new appmesh.VirtualGateway(stack, 'virtual-gateway', { virtualGatewayName: 'virtual-gateway', mesh: mesh, - backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({ - certificateChain: 'path-to-certificate', + backendDefaults: appmesh.Backends.backendDefaults({ + clientPolicy: appmesh.ClientPolicy.fileTrust({ + certificateChain: 'path-to-certificate', + }), }), }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index 4337973230854..8fc34ef2adc7f 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -29,10 +29,10 @@ export = { const node = new appmesh.VirtualNode(stack, 'test-node', { mesh, serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), - backends: [service1], + backends: [appmesh.Backends.backend({ virtualService: service1 } )], }); - node.addBackend(service2); + node.addBackend(appmesh.Backends.backend({ virtualService: service2 } )); // THEN expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', { @@ -272,9 +272,11 @@ export = { new appmesh.VirtualNode(stack, 'test-node', { mesh, serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), - backendsDefaultClientPolicy: appmesh.ClientPolicy.acmTrust({ - certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)], - ports: [8080, 8081], + backendDefaults: appmesh.Backends.backendDefaults({ + clientPolicy: appmesh.ClientPolicy.acmTrust({ + certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)], + ports: [8080, 8081], + }), }), }); @@ -320,13 +322,15 @@ export = { const service1 = new appmesh.VirtualService(stack, 'service-1', { virtualServiceName: 'service1.domain.local', virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), + }); + + node.addBackend(appmesh.Backends.backend({ + virtualService: service1, clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path-to-certificate', ports: [8080, 8081], }), - }); - - node.addBackend(service1); + })); // THEN expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 2732adb4cba17..f94c8370814b1 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -109,7 +109,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [service1], + backends: [appmesh.Backends.backend({ + virtualService: service1, + })], }); router.addRoute('route-1', { @@ -182,27 +184,27 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [ - service1, - ], + backends: [appmesh.Backends.backend({ + virtualService: service1, + })], }); const node2 = mesh.addVirtualNode('test-node2', { serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [ - service2, - ], + backends: [appmesh.Backends.backend({ + virtualService: service2, + })], }); const node3 = mesh.addVirtualNode('test-node3', { serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [ - service1, - ], + backends: [appmesh.Backends.backend({ + virtualService: service1, + })], }); router.addRoute('route-1', { @@ -340,9 +342,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [ - service1, - ], + backends: [appmesh.Backends.backend({ + virtualService: service1, + })], }); router.addRoute('route-tcp-1', { From 3a784bd326c1b10df18a06d16354dd962adcefcb Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Thu, 11 Feb 2021 08:04:28 -0800 Subject: [PATCH 2/9] Definied backends as an interface type and addressed other review comments --- packages/@aws-cdk/aws-appmesh/lib/backend.ts | 92 ------------------- packages/@aws-cdk/aws-appmesh/lib/index.ts | 1 - .../aws-appmesh/lib/shared-interfaces.ts | 31 +++++++ .../aws-appmesh/lib/virtual-gateway.ts | 7 +- .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 14 ++- .../aws-appmesh/test/integ.mesh.expected.json | 10 +- .../@aws-cdk/aws-appmesh/test/integ.mesh.ts | 32 +++---- .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 4 +- .../aws-appmesh/test/test.virtual-gateway.ts | 4 +- .../aws-appmesh/test/test.virtual-node.ts | 12 +-- .../aws-appmesh/test/test.virtual-router.ts | 20 ++-- 11 files changed, 86 insertions(+), 141 deletions(-) delete mode 100644 packages/@aws-cdk/aws-appmesh/lib/backend.ts diff --git a/packages/@aws-cdk/aws-appmesh/lib/backend.ts b/packages/@aws-cdk/aws-appmesh/lib/backend.ts deleted file mode 100644 index 3281d455d130a..0000000000000 --- a/packages/@aws-cdk/aws-appmesh/lib/backend.ts +++ /dev/null @@ -1,92 +0,0 @@ - -import { CfnVirtualNode } from './appmesh.generated'; -import { ClientPolicy } from './client-policy'; -import { IVirtualService } from './virtual-service'; - -// keep this import separate from other imports to reduce chance for merge conflicts with v2-main -// eslint-disable-next-line no-duplicate-imports, import/order -import { Construct } from '@aws-cdk/core'; - -/** - * Represents the properties needed to define backend defaults - */ -export interface BackendDefaultsOptions { - /** - * Client policy for backend defaults - * - * @default none - */ - readonly clientPolicy?: ClientPolicy; -} - -/** - * Represents the properties needed to define a backend - */ -export interface BackendOptions { - /** - * The Virtual Service this backend points to - */ - readonly virtualService: IVirtualService; - /** - * Client policy for a backend - * - * @default none - */ - readonly clientPolicy?: ClientPolicy; -} - -/** - * Provides static factory methods to generate backend API structures - */ -export class Backends { - /** - * Creates a backend defaults - */ - public static backendDefaults(props: BackendDefaultsOptions): BackendDefaults { - return new BackendDefaults(props.clientPolicy); - } - /** - * Creates a named backend - */ - public static backend(props: BackendOptions): Backend { - return new Backend(props.virtualService, props.clientPolicy); - } -} - -/** - * Represents all the backends that aren't specifically defined using the backend . - */ -export class BackendDefaults { - - constructor (private readonly clientPolicy: ClientPolicy | undefined) {} - - /** - * Return backend defaults config - */ - public bind(_scope: Construct): CfnVirtualNode.BackendDefaultsProperty { - return { - clientPolicy: this.clientPolicy?.bind(_scope).clientPolicy, - }; - } -} - -/** - * Represents the backend that a virtual node will send outbound traffic to - */ -export class Backend { - - constructor (private readonly virtualService: IVirtualService, - private readonly clientPolicy: ClientPolicy | undefined) {} - - /** - * Return backend config - */ - public bind(_scope: Construct): CfnVirtualNode.BackendProperty { - return { - virtualService: { - virtualServiceName: this.virtualService.virtualServiceName, - clientPolicy: this.clientPolicy?.bind(_scope).clientPolicy, - }, - }; - } -} diff --git a/packages/@aws-cdk/aws-appmesh/lib/index.ts b/packages/@aws-cdk/aws-appmesh/lib/index.ts index d24f178449694..1f5ca87def34d 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/index.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/index.ts @@ -16,4 +16,3 @@ export * from './virtual-gateway-listener'; export * from './gateway-route'; export * from './gateway-route-spec'; export * from './client-policy'; -export * from './backend'; diff --git a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts index 831db66e49e0c..9aa5e76b3ce67 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts @@ -1,5 +1,7 @@ import * as cdk from '@aws-cdk/core'; import { CfnVirtualGateway, CfnVirtualNode } from './appmesh.generated'; +import { ClientPolicy } from './client-policy'; +import { IVirtualService } from './virtual-service'; // keep this import separate from other imports to reduce chance for merge conflicts with v2-main // eslint-disable-next-line no-duplicate-imports, import/order @@ -194,3 +196,32 @@ class FileAccessLog extends AccessLog { } } +/** + * Represents the properties needed to define a backend + */ +export interface Backend { + /** + * The Virtual Service this backend points to + */ + readonly virtualService: IVirtualService; + + /** + * Client policy for a backend + * + * @default none + */ + readonly clientPolicy?: ClientPolicy; +} + +/** + * Represents the properties needed to define backend defaults + */ +export interface BackendDefaults { + /** + * Client policy for backend defaults + * + * @default none + */ + readonly clientPolicy?: ClientPolicy; +} + diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index ad8839363d041..69783b4e5fe5a 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -1,10 +1,9 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualGateway } from './appmesh.generated'; -import { BackendDefaults } from './backend'; import { GatewayRoute, GatewayRouteBaseProps } from './gateway-route'; import { IMesh, Mesh } from './mesh'; -import { AccessLog } from './shared-interfaces'; +import { AccessLog, BackendDefaults } from './shared-interfaces'; import { VirtualGatewayListener, VirtualGatewayListenerConfig } from './virtual-gateway-listener'; /** @@ -180,7 +179,9 @@ export class VirtualGateway extends VirtualGatewayBase { meshName: this.mesh.meshName, spec: { listeners: this.listeners.map(listener => listener.listener), - backendDefaults: props.backendDefaults?.bind(this), + backendDefaults: props.backendDefaults !== undefined ? { + clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy, + } : undefined, logging: accessLogging !== undefined ? { accessLog: accessLogging.virtualGatewayAccessLog, } : undefined, diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index 20aa407ef0578..4f9ae0bfa1807 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -1,10 +1,9 @@ import * as cdk from '@aws-cdk/core'; import { Construct } from 'constructs'; import { CfnVirtualNode } from './appmesh.generated'; -import { Backend, BackendDefaults } from './backend'; import { IMesh, Mesh } from './mesh'; import { ServiceDiscovery } from './service-discovery'; -import { AccessLog } from './shared-interfaces'; +import { AccessLog, BackendDefaults, Backend } from './shared-interfaces'; import { VirtualNodeListener, VirtualNodeListenerConfig } from './virtual-node-listener'; /** @@ -184,7 +183,9 @@ export class VirtualNode extends VirtualNodeBase { spec: { backends: cdk.Lazy.anyValue({ produce: () => this.backends }, { omitEmptyArray: true }), listeners: cdk.Lazy.anyValue({ produce: () => this.listeners.map(listener => listener.listener) }, { omitEmptyArray: true }), - backendDefaults: props.backendDefaults?.bind(this), + backendDefaults: props.backendDefaults !== undefined ? { + clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy, + } : undefined, serviceDiscovery: { dns: serviceDiscovery?.dns, awsCloudMap: serviceDiscovery?.cloudmap, @@ -214,7 +215,12 @@ 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(backend.bind(this)); + this.backends.push({ + virtualService: { + virtualServiceName: backend.virtualService.virtualServiceName, + clientPolicy: backend.clientPolicy?.bind(this).clientPolicy, + }, + }); } } diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json index 83c6a7cc1942e..9c6869dc538e3 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json @@ -640,7 +640,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service16B72496E", + "service6D174F83", "VirtualServiceName" ] } @@ -846,7 +846,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service16B72496E", + "service6D174F83", "VirtualServiceName" ] } @@ -883,7 +883,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service16B72496E", + "service6D174F83", "VirtualServiceName" ] } @@ -920,7 +920,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service16B72496E", + "service6D174F83", "VirtualServiceName" ] } @@ -930,7 +930,7 @@ "Match": { "ServiceName": { "Fn::GetAtt": [ - "service16B72496E", + "service6D174F83", "VirtualServiceName" ] } diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index 2be5881eaf8bf..a6a5d22c5f971 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -23,7 +23,7 @@ const router = mesh.addVirtualRouter('router', { ], }); -const virtualService1 = new appmesh.VirtualService(stack, 'service1', { +const virtualService = new appmesh.VirtualService(stack, 'service', { virtualServiceProvider: appmesh.VirtualServiceProvider.virtualRouter(router), virtualServiceName: 'service1.domain.local', }); @@ -47,15 +47,15 @@ const node = mesh.addVirtualNode('node', { }, })], backends: [ - appmesh.Backends.backend({ - virtualService: virtualService1, - }), + { + virtualService: virtualService, + }, ], }); -node.addBackend(appmesh.Backends.backend({ +node.addBackend({ virtualService: virtualService2, -})); +}); router.addRoute('route-1', { routeSpec: appmesh.RouteSpec.http({ @@ -88,15 +88,15 @@ const node2 = mesh.addVirtualNode('node2', { unhealthyThreshold: 2, }, })], - backendDefaults: appmesh.Backends.backendDefaults({ + backendDefaults: { clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path/to/cert', }), - }), + }, backends: [ - appmesh.Backends.backend({ + { virtualService: virtualService3, - }), + }, ], }); @@ -113,11 +113,11 @@ const node3 = mesh.addVirtualNode('node3', { unhealthyThreshold: 2, }, })], - backendDefaults: appmesh.Backends.backendDefaults({ + backendDefaults: { clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path-to-certificate', }), - }), + }, accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'), }); @@ -175,21 +175,21 @@ new appmesh.VirtualGateway(stack, 'gateway2', { gateway.addGatewayRoute('gateway1-route-http', { routeSpec: appmesh.GatewayRouteSpec.http({ - routeTarget: virtualService1, + routeTarget: virtualService, }), }); gateway.addGatewayRoute('gateway1-route-http2', { routeSpec: appmesh.GatewayRouteSpec.http2({ - routeTarget: virtualService1, + routeTarget: virtualService, }), }); gateway.addGatewayRoute('gateway1-route-grpc', { routeSpec: appmesh.GatewayRouteSpec.grpc({ - routeTarget: virtualService1, + routeTarget: virtualService, match: { - serviceName: virtualService1.virtualServiceName, + serviceName: virtualService.virtualServiceName, }, }), }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index 9e503050054b8..a62b4bc05bf4b 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -271,9 +271,9 @@ export = { port: 8080, })], backends: [ - appmesh.Backends.backend({ + { virtualService: service1, - }), + }, ], }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts index 52ffdb85bdae7..25b7974983f2a 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-gateway.ts @@ -392,11 +392,11 @@ export = { new appmesh.VirtualGateway(stack, 'virtual-gateway', { virtualGatewayName: 'virtual-gateway', mesh: mesh, - backendDefaults: appmesh.Backends.backendDefaults({ + backendDefaults: { clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path-to-certificate', }), - }), + }, }); // THEN diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index 8fc34ef2adc7f..a98518a1cb726 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -29,10 +29,10 @@ export = { const node = new appmesh.VirtualNode(stack, 'test-node', { mesh, serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), - backends: [appmesh.Backends.backend({ virtualService: service1 } )], + backends: [{ virtualService: service1 }], }); - node.addBackend(appmesh.Backends.backend({ virtualService: service2 } )); + node.addBackend({ virtualService: service2 }); // THEN expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', { @@ -272,12 +272,12 @@ export = { new appmesh.VirtualNode(stack, 'test-node', { mesh, serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), - backendDefaults: appmesh.Backends.backendDefaults({ + backendDefaults: { clientPolicy: appmesh.ClientPolicy.acmTrust({ certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)], ports: [8080, 8081], }), - }), + }, }); // THEN @@ -324,13 +324,13 @@ export = { virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), }); - node.addBackend(appmesh.Backends.backend({ + node.addBackend({ virtualService: service1, clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path-to-certificate', ports: [8080, 8081], }), - })); + }); // THEN expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index f94c8370814b1..de8bc423a20ba 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -109,9 +109,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backends.backend({ + backends: [{ virtualService: service1, - })], + }], }); router.addRoute('route-1', { @@ -184,27 +184,27 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backends.backend({ + backends: [{ virtualService: service1, - })], + }], }); const node2 = mesh.addVirtualNode('test-node2', { serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backends.backend({ + backends: [{ virtualService: service2, - })], + }], }); const node3 = mesh.addVirtualNode('test-node3', { serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backends.backend({ + backends: [{ virtualService: service1, - })], + }], }); router.addRoute('route-1', { @@ -342,9 +342,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backends.backend({ + backends: [{ virtualService: service1, - })], + }], }); router.addRoute('route-tcp-1', { From 29c36265153b6c6daa753bc9d429b52ebe3f7c02 Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Thu, 11 Feb 2021 08:12:57 -0800 Subject: [PATCH 3/9] Remove unneeded diff from package.json, fix integ test --- packages/@aws-cdk/aws-appmesh/package.json | 4 +--- packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/package.json b/packages/@aws-cdk/aws-appmesh/package.json index 168361392fe1d..cf21301f13e5c 100644 --- a/packages/@aws-cdk/aws-appmesh/package.json +++ b/packages/@aws-cdk/aws-appmesh/package.json @@ -10,9 +10,7 @@ "dotnet": { "namespace": "Amazon.CDK.AWS.AppMesh", "packageId": "Amazon.CDK.AWS.AppMesh", - "iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png", - "signAssembly": true, - "assemblyOriginatorKeyFile": "../../key.snk" + "iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/master/logo/default-256-dark.png" }, "java": { "package": "software.amazon.awscdk.services.appmesh", diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json index 9c6869dc538e3..4f9906ddf02c6 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json @@ -969,7 +969,7 @@ "VirtualServiceName": "service1.domain.local" } }, - "service27C65CF7D": { + "service2B2862B6B": { "Type": "AWS::AppMesh::VirtualService", "Properties": { "MeshName": { @@ -982,7 +982,7 @@ "VirtualServiceName": "service2.domain.local" } }, - "service3859EB104": { + "service3529E2EB1": { "Type": "AWS::AppMesh::VirtualService", "Properties": { "MeshName": { From fa5f6c035c1b69bd1d23aafe28a0836973a2710c Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Thu, 11 Feb 2021 09:40:28 -0800 Subject: [PATCH 4/9] Fix compilation in ecs-service-extensions --- .../ecs-service-extensions/lib/extensions/appmesh.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 35436d11ce691..564f0520086e2 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -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(otherAppMesh.virtualService); + this.virtualNode.addBackend({ virtualService: otherAppMesh.virtualService }); } private routeSpec(weightedTargets: appmesh.WeightedTarget[], serviceName: string): appmesh.RouteSpec { From db3821e076537d0bc7875890e057f8ff0d9edaf8 Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Thu, 11 Feb 2021 17:16:31 -0800 Subject: [PATCH 5/9] Use a class pattern for VS backend --- .../lib/extensions/appmesh.ts | 2 +- .../aws-appmesh/lib/shared-interfaces.ts | 70 ++++++++++++++++--- .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 7 +- .../@aws-cdk/aws-appmesh/test/integ.mesh.ts | 20 +++--- .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 8 +-- .../aws-appmesh/test/test.virtual-node.ts | 10 +-- .../aws-appmesh/test/test.virtual-router.ts | 20 +++--- 7 files changed, 90 insertions(+), 47 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 564f0520086e2..f3a401abd1513 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -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 { diff --git a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts index 9aa5e76b3ce67..cb0fd2bdb9818 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts @@ -197,16 +197,28 @@ 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 */ @@ -214,14 +226,54 @@ export interface Backend { } /** - * 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 { + 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) { + 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, + }, + }, + }; + } +} diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index 4f9ae0bfa1807..d8c6a40742883 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -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); } } diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index a6a5d22c5f971..5e3a5036c7aca 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -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({ @@ -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', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index a62b4bc05bf4b..41ad186e8f5e3 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -270,11 +270,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [ - { - virtualService: service1, - }, - ], + backends: [appmesh.Backend.virtualServiceBackend({ + virtualService: service1, + })], }); // THEN diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index a98518a1cb726..e8cc78531da31 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -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', { @@ -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', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index de8bc423a20ba..4d58578a51a30 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -109,9 +109,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [{ + backends: [appmesh.Backend.virtualServiceBackend({ virtualService: service1, - }], + })], }); router.addRoute('route-1', { @@ -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', { @@ -342,9 +342,9 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [{ + backends: [appmesh.Backend.virtualServiceBackend({ virtualService: service1, - }], + })], }); router.addRoute('route-tcp-1', { From 8b327f0b5775080b6ff9a5e53be308d68ef8ff08 Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Mon, 8 Mar 2021 17:30:00 -0800 Subject: [PATCH 6/9] Add README and address Adam's review feedback --- packages/@aws-cdk/aws-appmesh/README.md | 26 ++++++++++++------- .../aws-appmesh/lib/shared-interfaces.ts | 2 +- .../aws-appmesh/lib/virtual-gateway.ts | 8 +++--- .../@aws-cdk/aws-appmesh/lib/virtual-node.ts | 8 +++--- .../aws-appmesh/test/integ.mesh.expected.json | 8 +++--- .../@aws-cdk/aws-appmesh/test/integ.mesh.ts | 26 ++++++++----------- .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 2 +- .../aws-appmesh/test/test.virtual-node.ts | 6 ++--- .../aws-appmesh/test/test.virtual-router.ts | 10 +++---- 9 files changed, 51 insertions(+), 45 deletions(-) diff --git a/packages/@aws-cdk/aws-appmesh/README.md b/packages/@aws-cdk/aws-appmesh/README.md index c400cbb0af05d..edd91e6fad510 100644 --- a/packages/@aws-cdk/aws-appmesh/README.md +++ b/packages/@aws-cdk/aws-appmesh/README.md @@ -186,9 +186,11 @@ const node = new VirtualNode(this, 'node', { idle: cdk.Duration.seconds(5), }, })], - backendsDefaultClientPolicy: appmesh.ClientPolicy.fileTrust({ - certificateChain: '/keys/local_cert_chain.pem', - }), + backendDefaults: { + clientPolicy: appmesh.ClientPolicy.fileTrust({ + certificateChain: '/keys/local_cert_chain.pem', + }), + }, accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'), }); @@ -230,14 +232,16 @@ const virtualService = new appmesh.VirtualService(stack, 'service-1', { }), }); -node.addBackend(virtualService); +node.addBackend(appmesh.Backend.virtualService({ + virtualService: virtualService, +})); ``` The `listeners` property can be left blank and added later with the `node.addListener()` method. The `healthcheck` and `timeout` properties are optional but if specifying a listener, the `port` must be added. The `backends` property can be added with `node.addBackend()`. We define a virtual service and add it to the virtual node to allow egress traffic to other node. -The `backendsDefaultClientPolicy` property are added to the node while creating the virtual node. These are virtual node's service backends client policy defaults. +The `backendDefaults` property are added to the node while creating the virtual node. These are virtual node's default settings for all backends. ## Adding TLS to a listener @@ -369,10 +373,12 @@ const gateway = new appmesh.VirtualGateway(stack, 'gateway', { interval: cdk.Duration.seconds(10), }, })], - backendsDefaultClientPolicy: appmesh.ClientPolicy.acmTrust({ - certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)], - ports: [8080, 8081], - }), + backendDefaults: { + clientPolicy: appmesh.ClientPolicy.acmTrust({ + certificateAuthorities: [acmpca.CertificateAuthority.fromCertificateAuthorityArn(stack, 'certificate', certificateAuthorityArn)], + ports: [8080, 8081], + }), + }, accessLog: appmesh.AccessLog.fromFilePath('/dev/stdout'), virtualGatewayName: 'virtualGateway', }); @@ -396,7 +402,7 @@ const gateway = mesh.addVirtualGateway('gateway', { The listeners field can be omitted which will default to an HTTP Listener on port 8080. A gateway route can be added using the `gateway.addGatewayRoute()` method. -The `backendsDefaultClientPolicy` property are added to the node while creating the virtual gateway. These are virtual gateway's service backends client policy defaults. +The `backendDefaults` property is added to the node while creating the virtual gateway. These are virtual gateway's default settings for all backends. ## Adding a Gateway Route diff --git a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts index cb0fd2bdb9818..3c23c9aa9545d 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts @@ -243,7 +243,7 @@ export abstract class Backend { /** * Construct a Virtual Service backend */ - public static virtualServiceBackend(props: VirtualServiceBackendOptions): Backend { + public static virtualService(props: VirtualServiceBackendOptions): Backend { return new VirtualServiceBackend(props.virtualService, props.clientPolicy); } diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts index 69783b4e5fe5a..d2f0a873a0849 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-gateway.ts @@ -179,9 +179,11 @@ export class VirtualGateway extends VirtualGatewayBase { meshName: this.mesh.meshName, spec: { listeners: this.listeners.map(listener => listener.listener), - backendDefaults: props.backendDefaults !== undefined ? { - clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy, - } : undefined, + backendDefaults: props.backendDefaults !== undefined + ? { + clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy, + } + : undefined, logging: accessLogging !== undefined ? { accessLog: accessLogging.virtualGatewayAccessLog, } : undefined, diff --git a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts index d8c6a40742883..60ca92bb142ca 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/virtual-node.ts @@ -183,9 +183,11 @@ export class VirtualNode extends VirtualNodeBase { spec: { backends: cdk.Lazy.anyValue({ produce: () => this.backends }, { omitEmptyArray: true }), listeners: cdk.Lazy.anyValue({ produce: () => this.listeners.map(listener => listener.listener) }, { omitEmptyArray: true }), - backendDefaults: props.backendDefaults !== undefined ? { - clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy, - } : undefined, + backendDefaults: props.backendDefaults !== undefined + ? { + clientPolicy: props.backendDefaults?.clientPolicy?.bind(this).clientPolicy, + } + : undefined, serviceDiscovery: { dns: serviceDiscovery?.dns, awsCloudMap: serviceDiscovery?.cloudmap, diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json index 4f9906ddf02c6..5f4a9ca206725 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json @@ -650,7 +650,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service2B2862B6B", + "service27C65CF7D", "VirtualServiceName" ] } @@ -711,7 +711,7 @@ "VirtualService": { "VirtualServiceName": { "Fn::GetAtt": [ - "service3529E2EB1", + "service3859EB104", "VirtualServiceName" ] } @@ -969,7 +969,7 @@ "VirtualServiceName": "service1.domain.local" } }, - "service2B2862B6B": { + "service27C65CF7D": { "Type": "AWS::AppMesh::VirtualService", "Properties": { "MeshName": { @@ -982,7 +982,7 @@ "VirtualServiceName": "service2.domain.local" } }, - "service3529E2EB1": { + "service3859EB104": { "Type": "AWS::AppMesh::VirtualService", "Properties": { "MeshName": { diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index 5e3a5036c7aca..fdb4bf89fce57 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -28,16 +28,6 @@ const virtualService = new appmesh.VirtualService(stack, 'service', { virtualServiceName: 'service1.domain.local', }); -const virtualService2 = new appmesh.VirtualService(stack, 'service2', { - virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), - virtualServiceName: 'service2.domain.local', -}); - -const virtualService3 = new appmesh.VirtualService(stack, 'service3', { - virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), - virtualServiceName: 'service3.domain.local', -}); - const node = mesh.addVirtualNode('node', { serviceDiscovery: appmesh.ServiceDiscovery.dns(`node1.${namespace.namespaceName}`), listeners: [appmesh.VirtualNodeListener.http({ @@ -46,13 +36,16 @@ const node = mesh.addVirtualNode('node', { path: '/check-path', }, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: virtualService, })], }); -node.addBackend(appmesh.Backend.virtualServiceBackend({ - virtualService: virtualService2, +node.addBackend(appmesh.Backend.virtualService({ + virtualService: new appmesh.VirtualService(stack, 'service-2', { + virtualServiceName: 'service2.domain.local', + virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), + }), })); router.addRoute('route-1', { @@ -91,8 +84,11 @@ const node2 = mesh.addVirtualNode('node2', { certificateChain: 'path/to/cert', }), }, - backends: [appmesh.Backend.virtualServiceBackend({ - virtualService: virtualService3, + backends: [appmesh.Backend.virtualService({ + virtualService: new appmesh.VirtualService(stack, 'service-3', { + virtualServiceName: 'service3.domain.local', + virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), + }), })], }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index 41ad186e8f5e3..d445301ccdf37 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -270,7 +270,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service1, })], }); diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index e8cc78531da31..797cde5dbfce7 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -29,12 +29,12 @@ export = { const node = new appmesh.VirtualNode(stack, 'test-node', { mesh, serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service1, })], }); - node.addBackend(appmesh.Backend.virtualServiceBackend({ virtualService: service2 })); + node.addBackend(appmesh.Backend.virtualService({ virtualService: service2 })); // THEN expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', { @@ -326,7 +326,7 @@ export = { virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), }); - node.addBackend(appmesh.Backend.virtualServiceBackend({ + node.addBackend(appmesh.Backend.virtualService({ virtualService: service1, clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path-to-certificate', diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 4d58578a51a30..481bd30e7a98c 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -109,7 +109,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service1, })], }); @@ -184,7 +184,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service1, })], }); @@ -193,7 +193,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service2, })], }); @@ -202,7 +202,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service1, })], }); @@ -342,7 +342,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualServiceBackend({ + backends: [appmesh.Backend.virtualService({ virtualService: service1, })], }); From 9075284a284adfd6a7bd9ccf46cb61c5cefea10a Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Tue, 9 Mar 2021 11:31:16 -0800 Subject: [PATCH 7/9] Fix compilation error in ecs service extensions --- .../ecs-service-extensions/lib/extensions/appmesh.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 3d555648b6cc7..41a9245eb8024 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -347,7 +347,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(appmesh.Backend.virtualServiceBackend({ virtualService: otherAppMesh.virtualService })); + this.virtualNode.addBackend(appmesh.Backend.virtualService({ virtualService: otherAppMesh.virtualService })); } private routeSpec(weightedTargets: appmesh.WeightedTarget[], serviceName: string): appmesh.RouteSpec { From 3a9ea8e88bfc61945583de8e27c769e211e58572 Mon Sep 17 00:00:00 2001 From: Alexander Johnson Date: Wed, 10 Mar 2021 16:54:26 -0800 Subject: [PATCH 8/9] Simify the VirtualService backend factory method. --- .../lib/extensions/appmesh.ts | 2 +- .../aws-appmesh/lib/shared-interfaces.ts | 8 ++------ .../@aws-cdk/aws-appmesh/test/integ.mesh.ts | 16 +++++++-------- .../@aws-cdk/aws-appmesh/test/test.mesh.ts | 4 +--- .../aws-appmesh/test/test.virtual-node.ts | 9 +++------ .../aws-appmesh/test/test.virtual-router.ts | 20 +++++-------------- 6 files changed, 19 insertions(+), 40 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 41a9245eb8024..346b3c2cf61a5 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -347,7 +347,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(appmesh.Backend.virtualService({ virtualService: otherAppMesh.virtualService })); + this.virtualNode.addBackend(appmesh.Backend.virtualService(otherAppMesh.virtualService )); } private routeSpec(weightedTargets: appmesh.WeightedTarget[], serviceName: string): appmesh.RouteSpec { diff --git a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts index 3c23c9aa9545d..007f67c4a7a9b 100644 --- a/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts +++ b/packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts @@ -212,10 +212,6 @@ export interface BackendDefaults { * Represents the properties needed to define a Virtual Service backend */ export interface VirtualServiceBackendOptions { - /** - * The Virtual Service this backend points to - */ - readonly virtualService: IVirtualService; /** * Client policy for the backend @@ -243,8 +239,8 @@ export abstract class Backend { /** * Construct a Virtual Service backend */ - public static virtualService(props: VirtualServiceBackendOptions): Backend { - return new VirtualServiceBackend(props.virtualService, props.clientPolicy); + public static virtualService(virtualService: IVirtualService, props: VirtualServiceBackendOptions = {}): Backend { + return new VirtualServiceBackend(virtualService, props.clientPolicy); } /** diff --git a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts index fdb4bf89fce57..eedcb70d9cbd7 100644 --- a/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts @@ -36,17 +36,15 @@ const node = mesh.addVirtualNode('node', { path: '/check-path', }, })], - backends: [appmesh.Backend.virtualService({ - virtualService: virtualService, - })], + backends: [appmesh.Backend.virtualService(virtualService)], }); -node.addBackend(appmesh.Backend.virtualService({ - virtualService: new appmesh.VirtualService(stack, 'service-2', { +node.addBackend(appmesh.Backend.virtualService( + new appmesh.VirtualService(stack, 'service-2', { virtualServiceName: 'service2.domain.local', virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), }), -})); +)); router.addRoute('route-1', { routeSpec: appmesh.RouteSpec.http({ @@ -84,12 +82,12 @@ const node2 = mesh.addVirtualNode('node2', { certificateChain: 'path/to/cert', }), }, - backends: [appmesh.Backend.virtualService({ - virtualService: new appmesh.VirtualService(stack, 'service-3', { + backends: [appmesh.Backend.virtualService( + new appmesh.VirtualService(stack, 'service-3', { virtualServiceName: 'service3.domain.local', virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), }), - })], + )], }); const node3 = mesh.addVirtualNode('node3', { diff --git a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts index d445301ccdf37..5c9c1cea7a9a1 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.mesh.ts @@ -270,9 +270,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualService({ - virtualService: service1, - })], + backends: [appmesh.Backend.virtualService(service1)], }); // THEN diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts index 797cde5dbfce7..c09bdef5badbd 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-node.ts @@ -29,12 +29,10 @@ export = { const node = new appmesh.VirtualNode(stack, 'test-node', { mesh, serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), - backends: [appmesh.Backend.virtualService({ - virtualService: service1, - })], + backends: [appmesh.Backend.virtualService(service1)], }); - node.addBackend(appmesh.Backend.virtualService({ virtualService: service2 })); + node.addBackend(appmesh.Backend.virtualService(service2)); // THEN expect(stack).to(haveResourceLike('AWS::AppMesh::VirtualNode', { @@ -326,8 +324,7 @@ export = { virtualServiceProvider: appmesh.VirtualServiceProvider.none(mesh), }); - node.addBackend(appmesh.Backend.virtualService({ - virtualService: service1, + node.addBackend(appmesh.Backend.virtualService(service1, { clientPolicy: appmesh.ClientPolicy.fileTrust({ certificateChain: 'path-to-certificate', ports: [8080, 8081], diff --git a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts index 481bd30e7a98c..fef86e6bd7e7a 100644 --- a/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts +++ b/packages/@aws-cdk/aws-appmesh/test/test.virtual-router.ts @@ -109,9 +109,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualService({ - virtualService: service1, - })], + backends: [appmesh.Backend.virtualService(service1)], }); router.addRoute('route-1', { @@ -184,27 +182,21 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualService({ - virtualService: service1, - })], + backends: [appmesh.Backend.virtualService(service1)], }); const node2 = mesh.addVirtualNode('test-node2', { serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualService({ - virtualService: service2, - })], + backends: [appmesh.Backend.virtualService(service2)], }); const node3 = mesh.addVirtualNode('test-node3', { serviceDiscovery: appmesh.ServiceDiscovery.dns('test'), listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualService({ - virtualService: service1, - })], + backends: [appmesh.Backend.virtualService(service1)], }); router.addRoute('route-1', { @@ -342,9 +334,7 @@ export = { listeners: [appmesh.VirtualNodeListener.http({ port: 8080, })], - backends: [appmesh.Backend.virtualService({ - virtualService: service1, - })], + backends: [appmesh.Backend.virtualService(service1)], }); router.addRoute('route-tcp-1', { From 1ee03f4f961889aac56bcce004b2855b82b4d15f Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Thu, 11 Mar 2021 15:46:57 -0800 Subject: [PATCH 9/9] Apply suggestions from code review --- .../ecs-service-extensions/lib/extensions/appmesh.ts | 2 +- packages/@aws-cdk/aws-appmesh/README.md | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts index 346b3c2cf61a5..95220dc1ea3b4 100644 --- a/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts +++ b/packages/@aws-cdk-containers/ecs-service-extensions/lib/extensions/appmesh.ts @@ -347,7 +347,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(appmesh.Backend.virtualService(otherAppMesh.virtualService )); + this.virtualNode.addBackend(appmesh.Backend.virtualService(otherAppMesh.virtualService)); } private routeSpec(weightedTargets: appmesh.WeightedTarget[], serviceName: string): appmesh.RouteSpec { diff --git a/packages/@aws-cdk/aws-appmesh/README.md b/packages/@aws-cdk/aws-appmesh/README.md index edd91e6fad510..612d2cff793e7 100644 --- a/packages/@aws-cdk/aws-appmesh/README.md +++ b/packages/@aws-cdk/aws-appmesh/README.md @@ -232,9 +232,7 @@ const virtualService = new appmesh.VirtualService(stack, 'service-1', { }), }); -node.addBackend(appmesh.Backend.virtualService({ - virtualService: virtualService, -})); +node.addBackend(appmesh.Backend.virtualService(virtualService)); ``` The `listeners` property can be left blank and added later with the `node.addListener()` method. The `healthcheck` and `timeout` properties are optional but if specifying a listener, the `port` must be added.