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

feat(appmesh): ipv6 support for app mesh #20766

Merged
merged 8 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/@aws-cdk/aws-appmesh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ const mesh = new appmesh.Mesh(this, 'AppMesh', {
});
```

A mesh with an IP preference can be created by providing the property `serviceDiscovery` that specifes an `ipPreference`.

```ts
const mesh = new appmesh.Mesh(this, 'AppMesh', {
meshName: 'myAwsMesh',
serviceDiscovery: {
ipPreference: appmesh.IpPreference.IPV4_ONLY,
},
});
```

## Adding VirtualRouters

A _mesh_ uses _virtual routers_ as logical units to route requests to _virtual nodes_.
Expand Down Expand Up @@ -425,6 +436,48 @@ const gateway = new appmesh.VirtualGateway(this, 'gateway', {
});
```

### Adding an IP Preference to a Virtual Node

An `ipPreference` can be specified as part of a Virtual Node's service discovery. An IP preference defines how clients for this Virtual Node will interact with it.

There a four different IP preferences available to use which each specify what IP versions this Virtual Node will use and prefer.

- `IPv4_ONLY` - Only use IPv4. For CloudMap service discovery, only IPv4 addresses returned from CloudMap will be used. For DNS service discovery, Envoy's DNS resolver will only resolve DNS queries for IPv4.

- `IPv4_PREFERRED` - Prefer IPv4 and fall back to IPv6. For CloudMap service discovery, an IPv4 address will be used if returned from CloudMap. Otherwise, an IPv6 address will be used if available. For DNS service discovery, Envoy's DNS resolver will first attempt to resolve DNS queries using IPv4 and fall back to IPv6.

- `IPv6_ONLY` - Only use IPv6. For CloudMap service discovery, only IPv6 addresses returned from CloudMap will be used. For DNS service discovery, Envoy's DNS resolver will only resolve DNS queries for IPv6.

- `IPv6_PREFERRED` - Prefer IPv6 and fall back to IPv4. For CloudMap service discovery, an IPv6 address will be used if returned from CloudMap. Otherwise, an IPv4 address will be used if available. For DNS service discovery, Envoy's DNS resolver will first attempt to resolve DNS queries using IPv6 and fall back to IPv4.

```ts
const mesh = new appmesh.Mesh(stack, 'mesh', {
meshName: 'mesh-with-preference',
});

// Virtual Node with DNS service discovery and an IP preference
const dnsNode = new appmesh.VirtualNode(stack, 'dns-node', {
mesh,
serviceDiscovery: appmesh.ServiceDiscovery.dns('test', appmesh.DnsResponseType.LOAD_BALANCER, appmesh.IpPreference.IPV4_ONLY),
});

// Virtual Node with CloudMap service discovery and an IP preference
const vpc = new ec2.Vpc(stack, 'vpc');
const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', {
vpc,
name: 'domain.local',
});
const service = namespace.createService('Svc');

const instanceAttribute : { [key: string]: string} = {};
instanceAttribute.testKey = 'testValue';

const cloudmapNode = new appmesh.VirtualNode(stack, 'cloudmap-node', {
mesh,
serviceDiscovery: appmesh.ServiceDiscovery.cloudMap(service, instanceAttribute, appmesh.IpPreference.IPV4_ONLY),
});
```

## Adding a Route

A _route_ matches requests with an associated virtual router and distributes traffic to its associated virtual nodes.
Expand Down
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-appmesh/lib/mesh.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnMesh } from './appmesh.generated';
import { MeshServiceDiscovery } from './service-discovery';
import { VirtualGateway, VirtualGatewayBaseProps } from './virtual-gateway';
import { VirtualNode, VirtualNodeBaseProps } from './virtual-node';
import { VirtualRouter, VirtualRouterBaseProps } from './virtual-router';
Expand Down Expand Up @@ -124,6 +125,13 @@ export interface MeshProps {
* @default DROP_ALL
*/
readonly egressFilter?: MeshFilterType;

/**
* Defines how upstream clients will discover VirtualNodes in the Mesh
*
* @default - No Service Discovery
*/
readonly serviceDiscovery?: MeshServiceDiscovery;
}

/**
Expand Down Expand Up @@ -187,6 +195,7 @@ export class Mesh extends MeshBase {
egressFilter: props.egressFilter ? {
type: props.egressFilter,
} : undefined,
serviceDiscovery: props.serviceDiscovery,
},
});

Expand Down
66 changes: 60 additions & 6 deletions packages/@aws-cdk/aws-appmesh/lib/service-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,52 @@ import * as cloudmap from '@aws-cdk/aws-servicediscovery';
import { Construct } from 'constructs';
import { CfnVirtualNode } from './appmesh.generated';

/**
* Enum of supported IP preferences.
* Used to dictate the IP version for mesh wide and virtual node service discovery.
* Also used to specify the IP version that a sidecar Envoy uses when sending traffic to a local application.
*/

export enum IpPreference {
/**
* Use IPv4 when sending traffic to a local application.
* Only use IPv4 for service discovery.
*/
IPV4_ONLY = 'IPv4_ONLY',
/**
* Use IPv4 when sending traffic to a local application.
* First attempt to use IPv4 and fall back to IPv6 for service discovery.
*/
IPV4_PREFERRED = 'IPv4_PREFERRED',
/**
* Use IPv6 when sending traffic to a local application.
* Only use IPv6 for service discovery.
*/
IPV6_ONLY = 'IPv6_ONLY',
/**
* Use IPv6 when sending traffic to a local application.
* First attempt to use IPv6 and fall back to IPv4 for service discovery.
*/
IPV6_PREFERRED = 'IPv6_PREFERRED'
}

/**
* Properties for Mesh Service Discovery
*/
export interface MeshServiceDiscovery {
/**
* IP preference applied to all Virtual Nodes in the Mesh
*
* @default - No IP preference is applied to any of the Virtual Nodes in the Mesh.
* Virtual Nodes without an IP preference will have the following configured.
* Envoy listeners are configured to bind only to IPv4.
* Envoy will use IPv4 when sending traffic to a local application.
* For DNS service discovery, the Envoy DNS resolver to prefer using IPv6 and fall back to IPv4.
* For CloudMap service discovery, App Mesh will prefer using IPv4 and fall back to IPv6 for IPs returned by CloudMap.
*/
readonly ipPreference?: IpPreference;
}

/**
* Properties for VirtualNode Service Discovery
*/
Expand Down Expand Up @@ -48,9 +94,10 @@ export abstract class ServiceDiscovery {
* @param hostname
* @param responseType Specifies the DNS response type for the virtual node.
* The default is `DnsResponseType.LOAD_BALANCER`.
* @param ipPreference No IP preference is applied to the Virtual Node.
*/
public static dns(hostname: string, responseType?: DnsResponseType): ServiceDiscovery {
return new DnsServiceDiscovery(hostname, responseType);
public static dns(hostname: string, responseType?: DnsResponseType, ipPreference?: IpPreference): ServiceDiscovery {
return new DnsServiceDiscovery(hostname, responseType, ipPreference);
}

/**
Expand All @@ -61,9 +108,10 @@ export abstract class ServiceDiscovery {
* filter instances by any custom attribute that you specified when you
* registered the instance. Only instances that match all of the specified
* key/value pairs will be returned.
* @param ipPreference No IP preference is applied to the Virtual Node.
*/
public static cloudMap(service: cloudmap.IService, instanceAttributes?: {[key: string]: string}): ServiceDiscovery {
return new CloudMapServiceDiscovery(service, instanceAttributes);
public static cloudMap(service: cloudmap.IService, instanceAttributes?: {[key: string]: string}, ipPreference?: IpPreference): ServiceDiscovery {
return new CloudMapServiceDiscovery(service, instanceAttributes, ipPreference);
}

/**
Expand All @@ -75,18 +123,21 @@ export abstract class ServiceDiscovery {
class DnsServiceDiscovery extends ServiceDiscovery {
private readonly hostname: string;
private readonly responseType?: DnsResponseType;
private readonly ipPreference?: IpPreference;

constructor(hostname: string, responseType?: DnsResponseType) {
constructor(hostname: string, responseType?: DnsResponseType, ipPreference?: IpPreference) {
super();
this.hostname = hostname;
this.responseType = responseType;
this.ipPreference = ipPreference;
}

public bind(_scope: Construct): ServiceDiscoveryConfig {
return {
dns: {
hostname: this.hostname,
responseType: this.responseType,
ipPreference: this.ipPreference,
},
};
}
Expand All @@ -95,11 +146,13 @@ class DnsServiceDiscovery extends ServiceDiscovery {
class CloudMapServiceDiscovery extends ServiceDiscovery {
private readonly service: cloudmap.IService;
private readonly instanceAttributes?: {[key: string]: string};
private readonly ipPreference?: IpPreference;

constructor(service: cloudmap.IService, instanceAttributes?: {[key: string]: string}) {
constructor(service: cloudmap.IService, instanceAttributes?: {[key: string]: string}, ipPreference?: IpPreference) {
super();
this.service = service;
this.instanceAttributes = instanceAttributes;
this.ipPreference = ipPreference;
}

public bind(_scope: Construct): ServiceDiscoveryConfig {
Expand All @@ -108,6 +161,7 @@ class CloudMapServiceDiscovery extends ServiceDiscovery {
namespaceName: this.service.namespace.namespaceName,
serviceName: this.service.serviceName,
attributes: renderAttributes(this.instanceAttributes),
ipPreference: this.ipPreference,
},
};
}
Expand Down
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', {
});

const mesh = new appmesh.Mesh(stack, 'mesh');
new appmesh.Mesh(stack, 'mesh-with-preference', {
serviceDiscovery: {
ipPreference: appmesh.IpPreference.IPV4_ONLY,
},
});
const router = mesh.addVirtualRouter('router', {
listeners: [
appmesh.VirtualRouterListener.http(),
Expand All @@ -29,7 +34,7 @@ const virtualService = new appmesh.VirtualService(stack, 'service', {
});

const node = mesh.addVirtualNode('node', {
serviceDiscovery: appmesh.ServiceDiscovery.dns(`node1.${namespace.namespaceName}`),
serviceDiscovery: appmesh.ServiceDiscovery.dns(`node1.${namespace.namespaceName}`, undefined, appmesh.IpPreference.IPV4_ONLY),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does the IP preference of the serviceDiscovery in this node interact with the ip preference of the serviceDiscovery set on lines 21-23? Does one overwrite the other? Is this allowed? This behavior should be documented in the comment block of the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two resource levels we allowing setting this preference as you point out. A Mesh is the top level resource which users will be creating Virtual Nodes within. A Mesh preference will apply to all Virtual Nodes as a default value and each individual Virtual Node can set its own preference to override the Mesh preference. If there is no preference set anywhere then no default preference is applied.

listeners: [appmesh.VirtualNodeListener.http({
healthCheck: appmesh.HealthCheck.http({
healthyThreshold: 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"17.0.0"}
{"version":"20.0.0"}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "18.0.0",
"version": "20.0.0",
"testCases": {
"aws-appmesh/test/integ.mesh": {
"integ.mesh": {
"stacks": [
"mesh-stack"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "17.0.0",
"version": "20.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
Expand Down Expand Up @@ -291,6 +291,12 @@
"data": "meshgateway1gateway1routegrpc2FAC1FF36"
}
],
"/mesh-stack/mesh-with-preference/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "meshwithpreferenceCC9682C9"
}
],
"/mesh-stack/service/Resource": [
{
"type": "aws:cdk:logicalId",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "20.0.0",
"files": {
"be244c434fce5ce2d030a96121c147910d423314d1807320ddf66a562a53550d": {
"source": {
"path": "mesh-stack.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "be244c434fce5ce2d030a96121c147910d423314d1807320ddf66a562a53550d.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,8 @@
],
"ServiceDiscovery": {
"DNS": {
"Hostname": "node1.domain.local"
"Hostname": "node1.domain.local",
"IpPreference": "IPv4_ONLY"
}
}
},
Expand Down Expand Up @@ -1672,6 +1673,17 @@
"GatewayRouteName": "meshstackmeshgateway1gateway1routegrpc2AE8379FD"
}
},
"meshwithpreferenceCC9682C9": {
"Type": "AWS::AppMesh::Mesh",
"Properties": {
"MeshName": "meshstackmeshwithpreference13C624E1",
"Spec": {
"ServiceDiscovery": {
"IpPreference": "IPv4_ONLY"
}
}
}
},
"service6D174F83": {
"Type": "AWS::AppMesh::VirtualService",
"Properties": {
Expand Down
36 changes: 33 additions & 3 deletions packages/@aws-cdk/aws-appmesh/test/mesh.integ.snapshot/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"id": "Tree",
"path": "Tree",
"constructInfo": {
"fqn": "@aws-cdk/core.Construct",
"version": "0.0.0"
"fqn": "constructs.Construct",
"version": "10.0.9"
}
},
"mesh-stack": {
Expand Down Expand Up @@ -1464,7 +1464,8 @@
],
"serviceDiscovery": {
"dns": {
"hostname": "node1.domain.local"
"hostname": "node1.domain.local",
"ipPreference": "IPv4_ONLY"
}
}
},
Expand Down Expand Up @@ -2382,6 +2383,35 @@
"version": "0.0.0"
}
},
"mesh-with-preference": {
"id": "mesh-with-preference",
"path": "mesh-stack/mesh-with-preference",
"children": {
"Resource": {
"id": "Resource",
"path": "mesh-stack/mesh-with-preference/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::AppMesh::Mesh",
"aws:cdk:cloudformation:props": {
"meshName": "meshstackmeshwithpreference13C624E1",
"spec": {
"serviceDiscovery": {
"ipPreference": "IPv4_ONLY"
}
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appmesh.CfnMesh",
"version": "0.0.0"
}
}
},
"constructInfo": {
"fqn": "@aws-cdk/aws-appmesh.Mesh",
"version": "0.0.0"
}
},
"service": {
"id": "service",
"path": "mesh-stack/service",
Expand Down
Loading