Skip to content

Commit

Permalink
Merge pull request #39 from webratz/master
Browse files Browse the repository at this point in the history
feat: ignore image definitions for containers when assets are ignored
  • Loading branch information
hupe1980 authored Aug 28, 2024
2 parents 6d4be15 + ff28073 commit fc34305
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
124 changes: 124 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,130 @@ exports[`ignore assets 1`] = `
}
`;
exports[`ignore assets including container images 1`] = `
{
"Resources": {
"TaskDefinitionB36D86D9": {
"Properties": {
"ContainerDefinitions": [
{
"Essential": true,
"Image": Any<Object>,
"Name": "Container",
},
],
"Cpu": "2048",
"ExecutionRoleArn": {
"Fn::GetAtt": [
"TaskDefinitionExecutionRole8D61C2FB",
"Arn",
],
},
"Family": "TaskDefinition",
"Memory": "6144",
"NetworkMode": "awsvpc",
"RequiresCompatibilities": [
"FARGATE",
],
"TaskRoleArn": {
"Fn::GetAtt": [
"TaskDefinitionTaskRoleFD40A61D",
"Arn",
],
},
},
"Type": "AWS::ECS::TaskDefinition",
},
"TaskDefinitionExecutionRole8D61C2FB": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::IAM::Role",
},
"TaskDefinitionExecutionRoleDefaultPolicy1F3406F5": {
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
],
"Effect": "Allow",
"Resource": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition",
},
":ecr:",
{
"Ref": "AWS::Region",
},
":",
{
"Ref": "AWS::AccountId",
},
":repository/",
{
"Fn::Sub": "cdk-hnb659fds-container-assets-\${AWS::AccountId}-\${AWS::Region}",
},
],
],
},
},
{
"Action": "ecr:GetAuthorizationToken",
"Effect": "Allow",
"Resource": "*",
},
],
"Version": "2012-10-17",
},
"PolicyName": "TaskDefinitionExecutionRoleDefaultPolicy1F3406F5",
"Roles": [
{
"Ref": "TaskDefinitionExecutionRole8D61C2FB",
},
],
},
"Type": "AWS::IAM::Policy",
},
"TaskDefinitionTaskRoleFD40A61D": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::IAM::Role",
},
},
}
`;
exports[`ignore assets without resources 1`] = `
{
"Parameters": {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/fixtures/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine
21 changes: 21 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Bucket } from "aws-cdk-lib/aws-s3";
import { Topic } from "aws-cdk-lib/aws-sns";
import * as path from "path";
import "../index";
import { ContainerImage, FargateTaskDefinition } from "aws-cdk-lib/aws-ecs";

test("default setup", () => {
const stack = new Stack();
Expand Down Expand Up @@ -106,6 +107,26 @@ test("ignore assets without resources", () => {
});
});

test("ignore assets including container images", () => {
const stack = new Stack();

const taskDefinition = new FargateTaskDefinition(stack, "TaskDefinition", {
cpu: 2048,
memoryLimitMiB: 6144,
});

taskDefinition.addContainer("Container", {
image: ContainerImage.fromAsset(
path.join(__dirname, "fixtures", "docker"),
{},
),
});

expect(stack).toMatchCdkSnapshot({
ignoreAssets: true,
});
});

test("ignore current version", () => {
const stack = new Stack();

Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ const convertStack = (stack: Stack, options: Options = {}) => {
if (resource?.Properties?.Code) {
resource.Properties.Code = anyObject;
}

if (resource?.Properties?.ContainerDefinitions) {
resource?.Properties?.ContainerDefinitions.forEach(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(definition: any) => {
definition.Image = anyObject;
},
);
}
});
}

Expand Down

0 comments on commit fc34305

Please sign in to comment.