Skip to content

Commit

Permalink
refactor(ecs): update LogDriver to new bind pattern (#2990)
Browse files Browse the repository at this point in the history
Update LogDriver bind in accordance with how we do binds in the
rest of the library.

BREAKING CHANGES:

* **ecs**: `AwsLogDriver` no longer takes construct properties.
  • Loading branch information
rix0rrr authored Jun 21, 2019
1 parent b662188 commit ad72271
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct {
}

private createAWSLogDriver(prefix: string): ecs.AwsLogDriver {
return new ecs.AwsLogDriver(this, 'Logging', { streamPrefix: prefix });
return new ecs.AwsLogDriver({ streamPrefix: prefix });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct {
* @param prefix the Cloudwatch logging prefix
*/
private createAWSLogDriver(prefix: string): ecs.AwsLogDriver {
return new ecs.AwsLogDriver(this, 'ProcessingContainerLogging', { streamPrefix: prefix });
return new ecs.AwsLogDriver({ streamPrefix: prefix });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class ScheduledEc2Task extends cdk.Construct {
cpu: props.cpu,
command: props.command,
environment: props.environment,
logging: new ecs.AwsLogDriver(this, 'ScheduledTaskLogging', { streamPrefix: this.node.id })
logging: new ecs.AwsLogDriver({ streamPrefix: this.node.id })
});

// Use Ec2TaskEventRuleTarget as the target of the EventRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@
"LogDriver": "awslogs",
"Options": {
"awslogs-group": {
"Ref": "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3"
"Ref": "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6"
},
"awslogs-stream-prefix": "ScheduledEc2Task",
"awslogs-region": {
Expand Down Expand Up @@ -740,7 +740,7 @@
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3",
"ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6",
"Arn"
]
}
Expand Down Expand Up @@ -826,7 +826,7 @@
]
}
},
"ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3": {
"ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6": {
"Type": "AWS::Logs::LogGroup",
"DeletionPolicy": "Retain"
},
Expand Down Expand Up @@ -868,4 +868,4 @@
"Default": "/aws/service/ecs/optimized-ami/amazon-linux/recommended/image_id"
}
}
}
}
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export = {
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": { Ref: "ServiceLoggingLogGroupC3D6A581" },
"awslogs-group": { Ref: "ServiceTaskDefwebLogGroup2A898F61" },
"awslogs-stream-prefix": "Service",
"awslogs-region": { Ref: "AWS::Region" }
}
Expand Down Expand Up @@ -175,7 +175,7 @@ export = {
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": { Ref: "ServiceLoggingLogGroupC3D6A581" },
"awslogs-group": { Ref: "ServiceTaskDefwebLogGroup2A898F61" },
"awslogs-stream-prefix": "Service",
"awslogs-region": { Ref: "AWS::Region" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export = {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ServiceProcessingContainerLoggingLogGroupF40B9C5D"
Ref: "ServiceQueueProcessingTaskDefQueueProcessingContainerLogGroupD52338D1"
},
"awslogs-stream-prefix": "Service",
"awslogs-region": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export = {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3"
Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6"
},
"awslogs-stream-prefix": "ScheduledEc2Task",
"awslogs-region": {
Expand Down Expand Up @@ -126,7 +126,7 @@ export = {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3"
Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6"
},
"awslogs-stream-prefix": "ScheduledEc2Task",
"awslogs-region": {
Expand Down Expand Up @@ -174,7 +174,7 @@ export = {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3"
Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6"
},
"awslogs-stream-prefix": "ScheduledEc2Task",
"awslogs-region": {
Expand Down Expand Up @@ -228,7 +228,7 @@ export = {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ScheduledEc2TaskScheduledTaskLoggingLogGroupDBEF2BF3"
Ref: "ScheduledEc2TaskScheduledTaskDefScheduledContainerLogGroupA85E11E6"
},
"awslogs-stream-prefix": "ScheduledEc2Task",
"awslogs-region": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
"ClusterEB0386A7": {
"Type": "AWS::ECS::Cluster"
},
"FargateServiceLoggingLogGroup9B16742A": {
"FargateServiceTaskDefwebLogGroup71FAF541": {
"Type": "AWS::Logs::LogGroup",
"DeletionPolicy": "Retain"
},
Expand Down Expand Up @@ -589,7 +589,7 @@
"LogDriver": "awslogs",
"Options": {
"awslogs-group": {
"Ref": "FargateServiceLoggingLogGroup9B16742A"
"Ref": "FargateServiceTaskDefwebLogGroup71FAF541"
},
"awslogs-stream-prefix": "FargateService",
"awslogs-region": {
Expand Down Expand Up @@ -739,7 +739,7 @@
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"FargateServiceLoggingLogGroup9B16742A",
"FargateServiceTaskDefwebLogGroup71FAF541",
"Arn"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"L3LoggingLogGroupBD1F02DD",
"L3TaskDefwebLogGroupC6E4A38A",
"Arn"
]
}
Expand Down Expand Up @@ -554,7 +554,7 @@
"LogDriver": "awslogs",
"Options": {
"awslogs-group": {
"Ref": "L3LoggingLogGroupBD1F02DD"
"Ref": "L3TaskDefwebLogGroupC6E4A38A"
},
"awslogs-stream-prefix": "L3",
"awslogs-region": {
Expand Down Expand Up @@ -596,7 +596,7 @@
"Volumes": []
}
},
"L3LoggingLogGroupBD1F02DD": {
"L3TaskDefwebLogGroupC6E4A38A": {
"Type": "AWS::Logs::LogGroup",
"DeletionPolicy": "Retain"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@
"LogDriver": "awslogs",
"Options": {
"awslogs-group": {
"Ref": "L3LoggingLogGroupBD1F02DD"
"Ref": "L3TaskDefwebLogGroupC6E4A38A"
},
"awslogs-stream-prefix": "L3",
"awslogs-region": {
Expand Down Expand Up @@ -567,7 +567,7 @@
"Effect": "Allow",
"Resource": {
"Fn::GetAtt": [
"L3LoggingLogGroupBD1F02DD",
"L3TaskDefwebLogGroupC6E4A38A",
"Arn"
]
}
Expand All @@ -583,7 +583,7 @@
]
}
},
"L3LoggingLogGroupBD1F02DD": {
"L3TaskDefwebLogGroupC6E4A38A": {
"Type": "AWS::Logs::LogGroup",
"DeletionPolicy": "Retain"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export = {
LogDriver: "awslogs",
Options: {
"awslogs-group": {
Ref: "ServiceProcessingContainerLoggingLogGroupF40B9C5D"
Ref: "ServiceQueueProcessingTaskDefQueueProcessingContainerLogGroupD52338D1"
},
"awslogs-stream-prefix": "Service",
"awslogs-region": {
Expand Down
10 changes: 7 additions & 3 deletions packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NetworkMode, TaskDefinition } from './base/task-definition';
import { ContainerImage, ContainerImageConfig } from './container-image';
import { CfnTaskDefinition } from './ecs.generated';
import { LinuxParameters } from './linux-parameters';
import { LogDriver } from './log-drivers/log-driver';
import { LogDriver, LogDriverConfig } from './log-drivers/log-driver';

export interface ContainerDefinitionOptions {
/**
Expand Down Expand Up @@ -249,6 +249,8 @@ export class ContainerDefinition extends cdk.Construct {

private readonly imageConfig: ContainerImageConfig;

private readonly logDriverConfig?: LogDriverConfig;

constructor(scope: cdk.Construct, id: string, private readonly props: ContainerDefinitionProps) {
super(scope, id);
this.essential = props.essential !== undefined ? props.essential : true;
Expand All @@ -257,7 +259,9 @@ export class ContainerDefinition extends cdk.Construct {
this.linuxParameters = props.linuxParameters;

this.imageConfig = props.image.bind(this, this);
if (props.logging) { props.logging.bind(this); }
if (props.logging) {
this.logDriverConfig = props.logging.bind(this, this);
}
props.taskDefinition._linkContainer(this);
}

Expand Down Expand Up @@ -410,7 +414,7 @@ export class ContainerDefinition extends cdk.Construct {
user: this.props.user,
volumesFrom: this.volumesFrom.map(renderVolumeFrom),
workingDirectory: this.props.workingDirectory,
logConfiguration: this.props.logging && this.props.logging.renderLogDriver(),
logConfiguration: this.logDriverConfig,
environment: this.props.environment && renderKV(this.props.environment, 'name', 'value'),
extraHosts: this.props.extraHosts && renderKV(this.props.extraHosts, 'hostname', 'ipAddress'),
healthCheck: this.props.healthCheck && renderHealthCheck(this.props.healthCheck),
Expand Down
31 changes: 13 additions & 18 deletions packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import logs = require('@aws-cdk/aws-logs');
import cdk = require('@aws-cdk/cdk');
import { Stack } from '@aws-cdk/cdk';
import { Construct, Stack } from '@aws-cdk/cdk';
import { ContainerDefinition } from '../container-definition';
import { CfnTaskDefinition } from '../ecs.generated';
import { LogDriver } from "./log-driver";
import { LogDriver, LogDriverConfig } from "./log-driver";

/**
* Properties for defining a new AWS Log Driver
Expand Down Expand Up @@ -67,38 +65,35 @@ export interface AwsLogDriverProps {
export class AwsLogDriver extends LogDriver {
/**
* The log group that the logs will be sent to
*
* Only available after the LogDriver has been bound to a ContainerDefinition.
*/
public readonly logGroup: logs.ILogGroup;
public logGroup?: logs.ILogGroup;

constructor(scope: cdk.Construct, id: string, private readonly props: AwsLogDriverProps) {
super(scope, id);
constructor(private readonly props: AwsLogDriverProps) {
super();

if (props.logGroup && props.logRetention) {
throw new Error('Cannot specify both `logGroup` and `logRetentionDays`.');
}

this.logGroup = props.logGroup || new logs.LogGroup(this, 'LogGroup', {
retention: props.logRetention || Infinity,
});
}

/**
* Called when the log driver is configured on a container
*/
public bind(containerDefinition: ContainerDefinition): void {
public bind(scope: Construct, containerDefinition: ContainerDefinition): LogDriverConfig {
this.logGroup = this.props.logGroup || new logs.LogGroup(scope, 'LogGroup', {
retention: this.props.logRetention || Infinity,
});

this.logGroup.grantWrite(containerDefinition.taskDefinition.obtainExecutionRole());
}

/**
* Return the log driver CloudFormation JSON
*/
public renderLogDriver(): CfnTaskDefinition.LogConfigurationProperty {
return {
logDriver: 'awslogs',
options: removeEmpty({
'awslogs-group': this.logGroup.logGroupName,
'awslogs-stream-prefix': this.props.streamPrefix,
'awslogs-region': Stack.of(this).region,
'awslogs-region': Stack.of(containerDefinition).region,
'awslogs-datetime-format': this.props.datetimeFormat,
'awslogs-multiline-pattern': this.props.multilinePattern,
}),
Expand Down
29 changes: 23 additions & 6 deletions packages/@aws-cdk/aws-ecs/lib/log-drivers/log-driver.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import cdk = require('@aws-cdk/cdk');
import { Construct } from '@aws-cdk/cdk';
import { ContainerDefinition } from '../container-definition';
import { CfnTaskDefinition } from '../ecs.generated';
import { AwsLogDriver, AwsLogDriverProps } from './aws-log-driver';

/**
* Base class for log drivers
*/
export abstract class LogDriver extends cdk.Construct {
export abstract class LogDriver {
/**
* Return the log driver CloudFormation JSON
* Create an AWS Logs logdriver
*/
public abstract renderLogDriver(): CfnTaskDefinition.LogConfigurationProperty;
public static awsLogs(props: AwsLogDriverProps): LogDriver {
return new AwsLogDriver(props);
}

/**
* Called when the log driver is configured on a container
*/
public abstract bind(containerDefinition: ContainerDefinition): void;
public abstract bind(scope: Construct, containerDefinition: ContainerDefinition): LogDriverConfig;
}

/**
* Configuration to create a log driver from
*/
export interface LogDriverConfig {
/**
* Name of the log driver to use
*/
readonly logDriver: string;

/**
* Log-driver specific option set
*/
readonly options?: { [key: string]: string };
}
Loading

0 comments on commit ad72271

Please sign in to comment.