Skip to content

Commit b1e6d62

Browse files
authored
feat(appmesh): ipv6 support for app mesh (#20766)
App Mesh has released IPv6 support. This has been exposed in the form of IP preferences which have been added to the Mesh and Virtual Node resources. IP preferences are optional for both resources and there is no default IP preference that is applied by App Mesh. The following are samples of App Mesh resources with IP preferences configured. ``` # Mesh "spec": { "serviceDiscovery": { "ipPreference": "IPv6_PREFERRED" } } ``` ``` # Virtual Node "spec": { "listeners": [ { "healthCheck": { "healthyThreshold": 2, "intervalMillis": 5000, "path": "/ping", "protocol": "http", "timeoutMillis": 2000, "unhealthyThreshold": 2 }, "portMapping": { "port": 9080, "protocol": "http" } } ], "serviceDiscovery": { "dns": { "hostname": "colorteller-red.default.svc.cluster.local", "ipPreference": "IPv4_ONLY" } } } ``` IP preferences on a Mesh apply the preference to all Virtual Nodes contained within that Mesh. IP preferences set on a Virtual Node will only apply to that particular Virtual Node. Additionally, Virtual Node IP preferences will override the Mesh IP preference if there is one present. There are three areas in which the IP preference impacts how Envoy configuration generation. Firstly, setting any IP preference will change the Envoy's listeners (ingress and egress) to bind to IPv4 and IPv6 allowing the Envoy to serve all traffic from both IP versions. Secondly, the IP version specified in the name of the preference will be the IP version used for sending traffic to the local application for Envoys running as a sidecar to an application. (IPv4_ONLY/PREFERRED - IPv4, IPv6_ONLY/PREFERRED - IPv6) Lastly, it will impact how each service discovery option will be treated. For CloudMap service discovery, ONLY options will only return IPs from CloudMap for the matching version type and PREFERRED options will first used the primary IP version first and fall back to the other IP version for the IPs returned from CloudMap. For DNS service discovery, it will be similar to CloudMap service discovery in terms of only using one IP version or fall back behavior. However, this will come in the form of changing the Envoy's DNS resolver to exhibit this behavior when performing DNS resolution. This is a summarized version of the feature. For more details, a more thorough write up can be found here: https://github.com/aws/aws-app-mesh-examples/tree/main/walkthroughs/howto-ipv6#ip-preferences-in-meshes-and-virtual-nodes Closes #20737 ### All Submissions: * [Y] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [N] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [Y] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [Y] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2151a0e commit b1e6d62

File tree

12 files changed

+349
-15
lines changed

12 files changed

+349
-15
lines changed

packages/@aws-cdk/aws-appmesh/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ const mesh = new appmesh.Mesh(this, 'AppMesh', {
4949
});
5050
```
5151

52+
A mesh with an IP preference can be created by providing the property `serviceDiscovery` that specifes an `ipPreference`.
53+
54+
```ts
55+
const mesh = new appmesh.Mesh(this, 'AppMesh', {
56+
meshName: 'myAwsMesh',
57+
serviceDiscovery: {
58+
ipPreference: appmesh.IpPreference.IPV4_ONLY,
59+
},
60+
});
61+
```
62+
5263
## Adding VirtualRouters
5364

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

439+
### Adding an IP Preference to a Virtual Node
440+
441+
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.
442+
443+
There a four different IP preferences available to use which each specify what IP versions this Virtual Node will use and prefer.
444+
445+
- `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.
446+
447+
- `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.
448+
449+
- `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.
450+
451+
- `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.
452+
453+
```ts
454+
const mesh = new appmesh.Mesh(stack, 'mesh', {
455+
meshName: 'mesh-with-preference',
456+
});
457+
458+
// Virtual Node with DNS service discovery and an IP preference
459+
const dnsNode = new appmesh.VirtualNode(stack, 'dns-node', {
460+
mesh,
461+
serviceDiscovery: appmesh.ServiceDiscovery.dns('test', appmesh.DnsResponseType.LOAD_BALANCER, appmesh.IpPreference.IPV4_ONLY),
462+
});
463+
464+
// Virtual Node with CloudMap service discovery and an IP preference
465+
const vpc = new ec2.Vpc(stack, 'vpc');
466+
const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', {
467+
vpc,
468+
name: 'domain.local',
469+
});
470+
const service = namespace.createService('Svc');
471+
472+
const instanceAttribute : { [key: string]: string} = {};
473+
instanceAttribute.testKey = 'testValue';
474+
475+
const cloudmapNode = new appmesh.VirtualNode(stack, 'cloudmap-node', {
476+
mesh,
477+
serviceDiscovery: appmesh.ServiceDiscovery.cloudMap(service, instanceAttribute, appmesh.IpPreference.IPV4_ONLY),
478+
});
479+
```
480+
428481
## Adding a Route
429482

430483
A _route_ matches requests with an associated virtual router and distributes traffic to its associated virtual nodes.

packages/@aws-cdk/aws-appmesh/lib/mesh.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as cdk from '@aws-cdk/core';
22
import { Construct } from 'constructs';
33
import { CfnMesh } from './appmesh.generated';
4+
import { MeshServiceDiscovery } from './service-discovery';
45
import { VirtualGateway, VirtualGatewayBaseProps } from './virtual-gateway';
56
import { VirtualNode, VirtualNodeBaseProps } from './virtual-node';
67
import { VirtualRouter, VirtualRouterBaseProps } from './virtual-router';
@@ -124,6 +125,13 @@ export interface MeshProps {
124125
* @default DROP_ALL
125126
*/
126127
readonly egressFilter?: MeshFilterType;
128+
129+
/**
130+
* Defines how upstream clients will discover VirtualNodes in the Mesh
131+
*
132+
* @default - No Service Discovery
133+
*/
134+
readonly serviceDiscovery?: MeshServiceDiscovery;
127135
}
128136

129137
/**
@@ -187,6 +195,7 @@ export class Mesh extends MeshBase {
187195
egressFilter: props.egressFilter ? {
188196
type: props.egressFilter,
189197
} : undefined,
198+
serviceDiscovery: props.serviceDiscovery,
190199
},
191200
});
192201

packages/@aws-cdk/aws-appmesh/lib/service-discovery.ts

+60-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,52 @@ import * as cloudmap from '@aws-cdk/aws-servicediscovery';
22
import { Construct } from 'constructs';
33
import { CfnVirtualNode } from './appmesh.generated';
44

5+
/**
6+
* Enum of supported IP preferences.
7+
* Used to dictate the IP version for mesh wide and virtual node service discovery.
8+
* Also used to specify the IP version that a sidecar Envoy uses when sending traffic to a local application.
9+
*/
10+
11+
export enum IpPreference {
12+
/**
13+
* Use IPv4 when sending traffic to a local application.
14+
* Only use IPv4 for service discovery.
15+
*/
16+
IPV4_ONLY = 'IPv4_ONLY',
17+
/**
18+
* Use IPv4 when sending traffic to a local application.
19+
* First attempt to use IPv4 and fall back to IPv6 for service discovery.
20+
*/
21+
IPV4_PREFERRED = 'IPv4_PREFERRED',
22+
/**
23+
* Use IPv6 when sending traffic to a local application.
24+
* Only use IPv6 for service discovery.
25+
*/
26+
IPV6_ONLY = 'IPv6_ONLY',
27+
/**
28+
* Use IPv6 when sending traffic to a local application.
29+
* First attempt to use IPv6 and fall back to IPv4 for service discovery.
30+
*/
31+
IPV6_PREFERRED = 'IPv6_PREFERRED'
32+
}
33+
34+
/**
35+
* Properties for Mesh Service Discovery
36+
*/
37+
export interface MeshServiceDiscovery {
38+
/**
39+
* IP preference applied to all Virtual Nodes in the Mesh
40+
*
41+
* @default - No IP preference is applied to any of the Virtual Nodes in the Mesh.
42+
* Virtual Nodes without an IP preference will have the following configured.
43+
* Envoy listeners are configured to bind only to IPv4.
44+
* Envoy will use IPv4 when sending traffic to a local application.
45+
* For DNS service discovery, the Envoy DNS resolver to prefer using IPv6 and fall back to IPv4.
46+
* For CloudMap service discovery, App Mesh will prefer using IPv4 and fall back to IPv6 for IPs returned by CloudMap.
47+
*/
48+
readonly ipPreference?: IpPreference;
49+
}
50+
551
/**
652
* Properties for VirtualNode Service Discovery
753
*/
@@ -48,9 +94,10 @@ export abstract class ServiceDiscovery {
4894
* @param hostname
4995
* @param responseType Specifies the DNS response type for the virtual node.
5096
* The default is `DnsResponseType.LOAD_BALANCER`.
97+
* @param ipPreference No IP preference is applied to the Virtual Node.
5198
*/
52-
public static dns(hostname: string, responseType?: DnsResponseType): ServiceDiscovery {
53-
return new DnsServiceDiscovery(hostname, responseType);
99+
public static dns(hostname: string, responseType?: DnsResponseType, ipPreference?: IpPreference): ServiceDiscovery {
100+
return new DnsServiceDiscovery(hostname, responseType, ipPreference);
54101
}
55102

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

69117
/**
@@ -75,18 +123,21 @@ export abstract class ServiceDiscovery {
75123
class DnsServiceDiscovery extends ServiceDiscovery {
76124
private readonly hostname: string;
77125
private readonly responseType?: DnsResponseType;
126+
private readonly ipPreference?: IpPreference;
78127

79-
constructor(hostname: string, responseType?: DnsResponseType) {
128+
constructor(hostname: string, responseType?: DnsResponseType, ipPreference?: IpPreference) {
80129
super();
81130
this.hostname = hostname;
82131
this.responseType = responseType;
132+
this.ipPreference = ipPreference;
83133
}
84134

85135
public bind(_scope: Construct): ServiceDiscoveryConfig {
86136
return {
87137
dns: {
88138
hostname: this.hostname,
89139
responseType: this.responseType,
140+
ipPreference: this.ipPreference,
90141
},
91142
};
92143
}
@@ -95,11 +146,13 @@ class DnsServiceDiscovery extends ServiceDiscovery {
95146
class CloudMapServiceDiscovery extends ServiceDiscovery {
96147
private readonly service: cloudmap.IService;
97148
private readonly instanceAttributes?: {[key: string]: string};
149+
private readonly ipPreference?: IpPreference;
98150

99-
constructor(service: cloudmap.IService, instanceAttributes?: {[key: string]: string}) {
151+
constructor(service: cloudmap.IService, instanceAttributes?: {[key: string]: string}, ipPreference?: IpPreference) {
100152
super();
101153
this.service = service;
102154
this.instanceAttributes = instanceAttributes;
155+
this.ipPreference = ipPreference;
103156
}
104157

105158
public bind(_scope: Construct): ServiceDiscoveryConfig {
@@ -108,6 +161,7 @@ class CloudMapServiceDiscovery extends ServiceDiscovery {
108161
namespaceName: this.service.namespace.namespaceName,
109162
serviceName: this.service.serviceName,
110163
attributes: renderAttributes(this.instanceAttributes),
164+
ipPreference: this.ipPreference,
111165
},
112166
};
113167
}

packages/@aws-cdk/aws-appmesh/test/integ.mesh.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const namespace = new cloudmap.PrivateDnsNamespace(stack, 'test-namespace', {
1717
});
1818

1919
const mesh = new appmesh.Mesh(stack, 'mesh');
20+
new appmesh.Mesh(stack, 'mesh-with-preference', {
21+
serviceDiscovery: {
22+
ipPreference: appmesh.IpPreference.IPV4_ONLY,
23+
},
24+
});
2025
const router = mesh.addVirtualRouter('router', {
2126
listeners: [
2227
appmesh.VirtualRouterListener.http(),
@@ -29,7 +34,7 @@ const virtualService = new appmesh.VirtualService(stack, 'service', {
2934
});
3035

3136
const node = mesh.addVirtualNode('node', {
32-
serviceDiscovery: appmesh.ServiceDiscovery.dns(`node1.${namespace.namespaceName}`),
37+
serviceDiscovery: appmesh.ServiceDiscovery.dns(`node1.${namespace.namespaceName}`, undefined, appmesh.IpPreference.IPV4_ONLY),
3338
listeners: [appmesh.VirtualNodeListener.http({
3439
healthCheck: appmesh.HealthCheck.http({
3540
healthyThreshold: 3,
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"17.0.0"}
1+
{"version":"20.0.0"}

packages/@aws-cdk/aws-appmesh/test/mesh.integ.snapshot/integ.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"version": "18.0.0",
2+
"version": "20.0.0",
33
"testCases": {
4-
"aws-appmesh/test/integ.mesh": {
4+
"integ.mesh": {
55
"stacks": [
66
"mesh-stack"
77
],

packages/@aws-cdk/aws-appmesh/test/mesh.integ.snapshot/manifest.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "17.0.0",
2+
"version": "20.0.0",
33
"artifacts": {
44
"Tree": {
55
"type": "cdk:tree",
@@ -291,6 +291,12 @@
291291
"data": "meshgateway1gateway1routegrpc2FAC1FF36"
292292
}
293293
],
294+
"/mesh-stack/mesh-with-preference/Resource": [
295+
{
296+
"type": "aws:cdk:logicalId",
297+
"data": "meshwithpreferenceCC9682C9"
298+
}
299+
],
294300
"/mesh-stack/service/Resource": [
295301
{
296302
"type": "aws:cdk:logicalId",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "20.0.0",
3+
"files": {
4+
"be244c434fce5ce2d030a96121c147910d423314d1807320ddf66a562a53550d": {
5+
"source": {
6+
"path": "mesh-stack.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "be244c434fce5ce2d030a96121c147910d423314d1807320ddf66a562a53550d.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}

packages/@aws-cdk/aws-appmesh/test/mesh.integ.snapshot/mesh-stack.template.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@
969969
],
970970
"ServiceDiscovery": {
971971
"DNS": {
972-
"Hostname": "node1.domain.local"
972+
"Hostname": "node1.domain.local",
973+
"IpPreference": "IPv4_ONLY"
973974
}
974975
}
975976
},
@@ -1672,6 +1673,17 @@
16721673
"GatewayRouteName": "meshstackmeshgateway1gateway1routegrpc2AE8379FD"
16731674
}
16741675
},
1676+
"meshwithpreferenceCC9682C9": {
1677+
"Type": "AWS::AppMesh::Mesh",
1678+
"Properties": {
1679+
"MeshName": "meshstackmeshwithpreference13C624E1",
1680+
"Spec": {
1681+
"ServiceDiscovery": {
1682+
"IpPreference": "IPv4_ONLY"
1683+
}
1684+
}
1685+
}
1686+
},
16751687
"service6D174F83": {
16761688
"Type": "AWS::AppMesh::VirtualService",
16771689
"Properties": {

packages/@aws-cdk/aws-appmesh/test/mesh.integ.snapshot/tree.json

+33-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"id": "Tree",
99
"path": "Tree",
1010
"constructInfo": {
11-
"fqn": "@aws-cdk/core.Construct",
12-
"version": "0.0.0"
11+
"fqn": "constructs.Construct",
12+
"version": "10.0.9"
1313
}
1414
},
1515
"mesh-stack": {
@@ -1464,7 +1464,8 @@
14641464
],
14651465
"serviceDiscovery": {
14661466
"dns": {
1467-
"hostname": "node1.domain.local"
1467+
"hostname": "node1.domain.local",
1468+
"ipPreference": "IPv4_ONLY"
14681469
}
14691470
}
14701471
},
@@ -2382,6 +2383,35 @@
23822383
"version": "0.0.0"
23832384
}
23842385
},
2386+
"mesh-with-preference": {
2387+
"id": "mesh-with-preference",
2388+
"path": "mesh-stack/mesh-with-preference",
2389+
"children": {
2390+
"Resource": {
2391+
"id": "Resource",
2392+
"path": "mesh-stack/mesh-with-preference/Resource",
2393+
"attributes": {
2394+
"aws:cdk:cloudformation:type": "AWS::AppMesh::Mesh",
2395+
"aws:cdk:cloudformation:props": {
2396+
"meshName": "meshstackmeshwithpreference13C624E1",
2397+
"spec": {
2398+
"serviceDiscovery": {
2399+
"ipPreference": "IPv4_ONLY"
2400+
}
2401+
}
2402+
}
2403+
},
2404+
"constructInfo": {
2405+
"fqn": "@aws-cdk/aws-appmesh.CfnMesh",
2406+
"version": "0.0.0"
2407+
}
2408+
}
2409+
},
2410+
"constructInfo": {
2411+
"fqn": "@aws-cdk/aws-appmesh.Mesh",
2412+
"version": "0.0.0"
2413+
}
2414+
},
23852415
"service": {
23862416
"id": "service",
23872417
"path": "mesh-stack/service",

0 commit comments

Comments
 (0)