Skip to content

Commit 11fd607

Browse files
committed
move to constructor
1 parent 0cf85ec commit 11fd607

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

packages/@aws-cdk/aws-ecs/lib/container-definition.ts

+17-22
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ export class ContainerDefinition extends cdk.Construct {
361361

362362
private readonly imageConfig: ContainerImageConfig;
363363

364+
private readonly secrets?: CfnTaskDefinition.SecretProperty[];
365+
364366
/**
365367
* Constructs a new instance of the ContainerDefinition class.
366368
*/
@@ -382,6 +384,20 @@ export class ContainerDefinition extends cdk.Construct {
382384
this.logDriverConfig = props.logging.bind(this, this);
383385
}
384386
props.taskDefinition._linkContainer(this);
387+
388+
if (props.secrets) {
389+
this.secrets = [];
390+
for (const [name, secret] of Object.entries(props.secrets)) {
391+
if (this.taskDefinition.isFargateCompatible && secret.hasField) {
392+
throw new Error(`Cannot specify secret JSON field for a task using the FARGATE launch type: '${name}' in container '${this.node.id}'`);
393+
}
394+
secret.grantRead(this.taskDefinition.obtainExecutionRole());
395+
this.secrets.push({
396+
name,
397+
valueFrom: secret.arn,
398+
});
399+
}
400+
}
385401
}
386402

387403
/**
@@ -564,35 +580,14 @@ export class ContainerDefinition extends cdk.Construct {
564580
workingDirectory: this.props.workingDirectory,
565581
logConfiguration: this.logDriverConfig,
566582
environment: this.props.environment && renderKV(this.props.environment, 'name', 'value'),
567-
secrets: this.renderSecrets(),
583+
secrets: this.secrets,
568584
extraHosts: this.props.extraHosts && renderKV(this.props.extraHosts, 'hostname', 'ipAddress'),
569585
healthCheck: this.props.healthCheck && renderHealthCheck(this.props.healthCheck),
570586
links: cdk.Lazy.listValue({ produce: () => this.links }, { omitEmpty: true }),
571587
linuxParameters: this.linuxParameters && this.linuxParameters.renderLinuxParameters(),
572588
resourceRequirements: (this.props.gpuCount !== undefined) ? renderResourceRequirements(this.props.gpuCount) : undefined,
573589
};
574590
}
575-
576-
private renderSecrets(): CfnTaskDefinition.SecretProperty[] | undefined {
577-
if (!this.props.secrets) {
578-
return undefined;
579-
}
580-
581-
const secrets: CfnTaskDefinition.SecretProperty[] = [];
582-
583-
for (const [k, v] of Object.entries(this.props.secrets)) {
584-
if (this.taskDefinition.isFargateCompatible && v.hasField) {
585-
throw new Error(`Cannot specify secret JSON field for a task using the FARGATE launch type: '${k}' in container '${this.node.id}'`);
586-
}
587-
v.grantRead(this.taskDefinition.obtainExecutionRole());
588-
secrets.push({
589-
name: k,
590-
valueFrom: v.arn,
591-
});
592-
}
593-
594-
return secrets;
595-
}
596591
}
597592

598593
/**

packages/@aws-cdk/aws-ecs/test/test.container-definition.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,6 @@ export = {
795795
},
796796
}));
797797

798-
test.equal(ecs.Secret.fromSecretsManager(secret).hasField, false);
799-
test.equal(ecs.Secret.fromSsmParameter(parameter).hasField, undefined);
800-
801798
test.done();
802799

803800
},
@@ -848,23 +845,19 @@ export = {
848845

849846
'throws when using a specific secret JSON field as environment variable for a Fargate task'(test: Test) {
850847
// GIVEN
851-
const app = new cdk.App();
852-
const stack = new cdk.Stack(app, 'Stack');
848+
const stack = new cdk.Stack();
853849
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');
854850

855851
const secret = new secretsmanager.Secret(stack, 'Secret');
856852

857-
// WHEN
858-
taskDefinition.addContainer('cont', {
853+
// THEN
854+
test.throws(() => taskDefinition.addContainer('cont', {
859855
image: ecs.ContainerImage.fromRegistry('test'),
860856
memoryLimitMiB: 1024,
861857
secrets: {
862858
SECRET_KEY: ecs.Secret.fromSecretsManager(secret, 'specificKey'),
863859
},
864-
});
865-
866-
// THEN
867-
test.throws(() => app.synth(), /Cannot specify secret JSON field for a task using the FARGATE launch type/);
860+
}), /Cannot specify secret JSON field for a task using the FARGATE launch type/);
868861

869862
test.done();
870863
},

0 commit comments

Comments
 (0)