Skip to content

Commit 4e98c63

Browse files
authored
chore: HTTP Namespace in Cluster (#23328)
--- Cluster doesn't behave right when you try to "enableCloudMapNamespace" with an HTTPNamespace. And enablelcoudmap in base-service.ts for the service have some problems with using just an httpNameSpace. - To enable Defaultcloudmap namespace to http with both public and private DNS here I added logic which handles httpNameSpace along with private DNS namespace and public DNS namespace. - Updated Unit-test cases as well. Below you can see yarn build && yarn test results. <img width="755" alt="image" src="https://user-images.githubusercontent.com/115483524/207423568-2dd5942b-1153-4946-9d28-dde637bd27ee.png"> ### All Submissions: * [ x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] 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 9b2573b commit 4e98c63

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

packages/@aws-cdk/aws-ecs/lib/base/base-service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,10 @@ export abstract class BaseService extends Resource
973973
throw new Error('Cannot enable service discovery if a Cloudmap Namespace has not been created in the cluster.');
974974
}
975975

976+
if (sdNamespace.type === cloudmap.NamespaceType.HTTP) {
977+
throw new Error('Cannot enable DNS service discovery for HTTP Cloudmap Namespace.');
978+
}
979+
976980
// Determine DNS type based on network mode
977981
const networkMode = this.taskDefinition.networkMode;
978982
if (networkMode === NetworkMode.NONE) {

packages/@aws-cdk/aws-ecs/lib/cluster.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ export class Cluster extends Resource implements ICluster {
288288

289289
/**
290290
* Add an AWS Cloud Map DNS namespace for this cluster.
291-
* NOTE: HttpNamespaces are not supported, as ECS always requires a DNSConfig when registering an instance to a Cloud
292-
* Map service.
291+
* NOTE: HttpNamespaces are supported only for use cases involving Service Connect. For use cases involving both Service-
292+
* Discovery and Service Connect, customers should manage the HttpNamespace outside of the Cluster.addDefaultCloudMapNamespace method.
293293
*/
294294
public addDefaultCloudMapNamespace(options: CloudMapNamespaceOptions): cloudmap.INamespace {
295295
if (this._defaultCloudMapNamespace !== undefined) {
@@ -300,14 +300,27 @@ export class Cluster extends Resource implements ICluster {
300300
? options.type
301301
: cloudmap.NamespaceType.DNS_PRIVATE;
302302

303-
const sdNamespace = namespaceType === cloudmap.NamespaceType.DNS_PRIVATE ?
304-
new cloudmap.PrivateDnsNamespace(this, 'DefaultServiceDiscoveryNamespace', {
305-
name: options.name,
306-
vpc: this.vpc,
307-
}) :
308-
new cloudmap.PublicDnsNamespace(this, 'DefaultServiceDiscoveryNamespace', {
309-
name: options.name,
310-
});
303+
let sdNamespace;
304+
switch (namespaceType) {
305+
case cloudmap.NamespaceType.DNS_PRIVATE:
306+
sdNamespace = new cloudmap.PrivateDnsNamespace(this, 'DefaultServiceDiscoveryNamespace', {
307+
name: options.name,
308+
vpc: this.vpc,
309+
});
310+
break;
311+
case cloudmap.NamespaceType.DNS_PUBLIC:
312+
sdNamespace = new cloudmap.PublicDnsNamespace(this, 'DefaultServiceDiscoveryNamespace', {
313+
name: options.name,
314+
});
315+
break;
316+
case cloudmap.NamespaceType.HTTP:
317+
sdNamespace = new cloudmap.HttpNamespace(this, 'DefaultServiceDiscoveryNamespace', {
318+
name: options.name,
319+
});
320+
break;
321+
default:
322+
throw new Error(`Namespace type ${namespaceType} is not supported.`);
323+
}
311324

312325
this._defaultCloudMapNamespace = sdNamespace;
313326
if (options.useForServiceConnect) {

packages/@aws-cdk/aws-ecs/test/cluster.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,20 @@ describe('cluster', () => {
10241024
expect((cluster as any)._cfnCluster.serviceConnectDefaults.namespace).toBe('foo.com');
10251025
});
10261026

1027+
test('allows setting cluster _defaultCloudMapNamespace for HTTP namespace', () => {
1028+
// GIVEN
1029+
const stack = new cdk.Stack();
1030+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
1031+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
1032+
// WHEN
1033+
const namespace = cluster.addDefaultCloudMapNamespace({
1034+
name: 'foo',
1035+
type: cloudmap.NamespaceType.HTTP,
1036+
});
1037+
// THEN
1038+
expect(namespace.namespaceName).toBe('foo');
1039+
});
1040+
10271041
/*
10281042
* TODO:v2.0.0 END OF OBSOLETE BLOCK
10291043
*/

packages/@aws-cdk/aws-ecs/test/ec2/ec2-service.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,37 @@ describe('ec2 service', () => {
26212621
}).toThrow(/Cannot enable service discovery if a Cloudmap Namespace has not been created in the cluster./);
26222622

26232623

2624+
});
2625+
2626+
test('fails to enable Service Discovery with HTTP defaultCloudmapNamespace', () => {
2627+
// GIVEN
2628+
const stack = new cdk.Stack();
2629+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
2630+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
2631+
addDefaultCapacityProvider(cluster, stack, vpc);
2632+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef', {
2633+
networkMode: ecs.NetworkMode.NONE,
2634+
});
2635+
const container = taskDefinition.addContainer('MainContainer', {
2636+
image: ecs.ContainerImage.fromRegistry('hello'),
2637+
memoryLimitMiB: 512,
2638+
});
2639+
container.addPortMappings({ containerPort: 8000 });
2640+
2641+
cluster.addDefaultCloudMapNamespace({ name: 'foo.com', type: cloudmap.NamespaceType.HTTP });
2642+
2643+
// THEN
2644+
expect(() => {
2645+
new ecs.Ec2Service(stack, 'Service', {
2646+
cluster,
2647+
taskDefinition,
2648+
cloudMapOptions: {
2649+
name: 'myApp',
2650+
},
2651+
});
2652+
}).toThrow(/Cannot enable DNS service discovery for HTTP Cloudmap Namespace./);
2653+
2654+
26242655
});
26252656

26262657
test('throws if network mode is none', () => {

0 commit comments

Comments
 (0)