Skip to content

Commit

Permalink
feat(appmesh): ipv6 support for app mesh (#20766)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
AKBarcenas authored Jun 28, 2022
1 parent 2151a0e commit b1e6d62
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 15 deletions.
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),
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

0 comments on commit b1e6d62

Please sign in to comment.