Skip to content

Commit

Permalink
fix: ensure dependencies are correctly rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
bvtujo committed Nov 30, 2022
1 parent 4182287 commit fbb22a9
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 10 deletions.
2 changes: 0 additions & 2 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/extensions/aliased-port.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as cloudmap from 'aws-cdk-lib/aws-servicediscovery';
import { Construct } from 'constructs';
import { Service } from '../service';
import { Container } from './container';
Expand Down Expand Up @@ -47,7 +48,7 @@ export class AliasedPortExtension extends ServiceExtension {
protected alias: string;
protected aliasPort?: number;
protected appProtocol?: ecs.AppProtocol;
protected namespace?: string;
protected namespace?: cloudmap.INamespace;

constructor(props: AliasedPortProps) {
super('aliasedPort');
Expand All @@ -62,12 +63,12 @@ export class AliasedPortExtension extends ServiceExtension {
this.scope = scope;

// If there isn't a default cloudmap namespace on the cluster, create a private HTTP namespace for SC.
if (!this.parentService.cluster.defaultCloudMapNamespace) {
if (!this.parentService.environment.cluster.defaultCloudMapNamespace) {
this.parentService.environment.addDefaultCloudMapNamespace({
name: this.parentService.environment.id,
});
}
this.namespace = this.parentService.environment.cluster.defaultCloudMapNamespace?.namespaceName;
this.namespace = this.parentService.environment.cluster.defaultCloudMapNamespace as cloudmap.INamespace;
}

public addHooks(): void {
Expand All @@ -84,7 +85,7 @@ export class AliasedPortExtension extends ServiceExtension {
}

public modifyServiceProps(props: ServiceBuild): ServiceBuild {
if (props.serviceConnectConfiguration && props.serviceConnectConfiguration.namespace !== this.namespace) {
if (props.serviceConnectConfiguration && props.serviceConnectConfiguration.namespace !== this.namespace?.namespaceName) {
throw new Error('Service connect cannot be enabled with two different namespaces.');
}

Expand Down Expand Up @@ -131,6 +132,13 @@ export class AliasedPortExtension extends ServiceExtension {
},
};
}

public useService(service: ecs.Ec2Service | ecs.FargateService): void {
if (!this.namespace) {
throw new Error('Environment must have a default Cloudmap namespace to enable Service Connect.');
}
service.node.addDependency(this.namespace);
}
}

export interface AliasedPortMutatingHookProps {
Expand Down
1 change: 1 addition & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,6 @@ export class Service extends Construct {
this.ecsService.enableServiceConnect({
namespace: this.environment.cluster.defaultCloudMapNamespace.namespaceName,
});
this.ecsService.node.addDependency(this.environment.cluster.defaultCloudMapNamespace);
}
}
4 changes: 2 additions & 2 deletions test/aliased-port.integ.snapshot/aws-ecs-integ.assets.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "21.0.0",
"files": {
"1904e1ff90c4f297a8a8c9f4f5573101aa4cb632abb96486f141dc588c222614": {
"4ac410b83bf75ad3eee119980f62663b332db0d347a80e1c7a1493bf14772c05": {
"source": {
"path": "aws-ecs-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "1904e1ff90c4f297a8a8c9f4f5573101aa4cb632abb96486f141dc588c222614.json",
"objectKey": "4ac410b83bf75ad3eee119980f62663b332db0d347a80e1c7a1493bf14772c05.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
141 changes: 140 additions & 1 deletion test/aliased-port.integ.snapshot/aws-ecs-integ.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,10 @@
"TaskDefinition": {
"Ref": "ServiceConnecttaskdefinitionB19E0536"
}
}
},
"DependsOn": [
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
]
},
"ServiceConnectserviceSecurityGroup0D1FCAE3": {
"Type": "AWS::EC2::SecurityGroup",
Expand All @@ -537,7 +540,143 @@
"VpcId": {
"Ref": "productionenvironmentvpcAEB47DF7"
}
},
"DependsOn": [
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
]
},
"ClientServicetaskdefinitionTaskRoleACC19FE3": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
}
}
],
"Version": "2012-10-17"
}
}
},
"ClientServicetaskdefinition89718CF4": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [
{
"Cpu": 256,
"Environment": [
{
"Name": "PORT",
"Value": "80"
},
{
"Name": "URL",
"Value": "http://name"
}
],
"Essential": true,
"Image": "nathanpeck/greeter",
"Memory": 512,
"Name": "app",
"PortMappings": [
{
"ContainerPort": 80,
"Protocol": "tcp"
}
],
"Ulimits": [
{
"HardLimit": 1024000,
"Name": "nofile",
"SoftLimit": 1024000
}
]
}
],
"Cpu": "256",
"Family": "awsecsintegClientServicetaskdefinition11AA48A8",
"Memory": "512",
"NetworkMode": "awsvpc",
"RequiresCompatibilities": [
"EC2",
"FARGATE"
],
"TaskRoleArn": {
"Fn::GetAtt": [
"ClientServicetaskdefinitionTaskRoleACC19FE3",
"Arn"
]
}
}
},
"ClientServiceserviceService8F3E675C": {
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "productionenvironmentclusterC6599D2D"
},
"DeploymentConfiguration": {
"MaximumPercent": 200,
"MinimumHealthyPercent": 100
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"AssignPublicIp": "DISABLED",
"SecurityGroups": [
{
"Fn::GetAtt": [
"ClientServiceserviceSecurityGroupFBB92787",
"GroupId"
]
}
],
"Subnets": [
{
"Ref": "productionenvironmentvpcPrivateSubnet1Subnet53F632E6"
},
{
"Ref": "productionenvironmentvpcPrivateSubnet2Subnet756FB93C"
}
]
}
},
"ServiceConnectConfiguration": {
"Enabled": true,
"Namespace": "production"
},
"TaskDefinition": {
"Ref": "ClientServicetaskdefinition89718CF4"
}
},
"DependsOn": [
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
]
},
"ClientServiceserviceSecurityGroupFBB92787": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "aws-ecs-integ/ClientService-service/SecurityGroup",
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
}
],
"VpcId": {
"Ref": "productionenvironmentvpcAEB47DF7"
}
},
"DependsOn": [
"productionenvironmentclusterDefaultServiceDiscoveryNamespaceBE74D64D"
]
}
},
"Parameters": {
Expand Down
22 changes: 21 additions & 1 deletion test/aliased-port.integ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@ aliasedPortServiceDescription.add(new AliasedPortExtension({
}));

new Service(stack, 'ServiceConnect', {
environment: environment,
environment,
serviceDescription: aliasedPortServiceDescription,
desiredCount: 1,
});

const otherDescription = new ServiceDescription();

otherDescription.add(new Container({
cpu: 256,
memoryMiB: 512,
trafficPort: 80,
image: ecs.ContainerImage.fromRegistry('nathanpeck/greeter'),
environment: {
PORT: '80',
URL: 'http://name',
},
}));

new Service(stack, 'ClientService', {
environment,
serviceDescription: otherDescription,
desiredCount: 1,
enableServiceConnect: true,
});

0 comments on commit fbb22a9

Please sign in to comment.