Skip to content

Commit

Permalink
feat(aws-cdk): support fixed repository name for DockerImageAsset (#2032
Browse files Browse the repository at this point in the history
)

Add a `repositoryName` argument to `DockerImageAsset` to control the name of the
repository that the image gets uploaded to.

This makes it easier to reference Docker Images created and deployed by CDK from EKS (Kubernetes) YAML resource files.
  • Loading branch information
alex-berger authored and rix0rrr committed Mar 19, 2019
1 parent 133dc98 commit 942f938
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
14 changes: 13 additions & 1 deletion packages/@aws-cdk/assets-docker/lib/image-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ export interface DockerImageAssetProps {
* The directory where the Dockerfile is stored
*/
directory: string;

/**
* ECR repository name
*
* Specify this property if you need to statically address the image, e.g.
* from a Kubernetes Pod. Note, this is only the repository name, without the
* registry and the tag parts.
*
* @default automatically derived from the asset's ID.
*/
repositoryName?: string;
}

/**
Expand Down Expand Up @@ -55,7 +66,8 @@ export class DockerImageAsset extends cdk.Construct {
packaging: 'container-image',
path: this.directory,
id: this.node.uniqueId,
imageNameParameter: imageNameParameter.logicalId
imageNameParameter: imageNameParameter.logicalId,
repositoryName: props.repositoryName,
};

this.node.addMetadata(cxapi.ASSET_METADATA, asset);
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/cx-api/lib/metadata/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ export interface ContainerImageAssetMetadataEntry {
* ECR Repository name and tag (separated by ":") where this asset is stored.
*/
imageNameParameter: string;

/**
* ECR repository name, if omitted a default name based on the asset's
* ID is used instead. Specify this property if you need to statically
* address the image, e.g. from a Kubernetes Pod.
* Note, this is only the repository name, without the registry and
* the tag parts.
*
* * @default automatically derived from the asset's ID.
*/
repositoryName?: string;
}

export type AssetMetadataEntry = FileAssetMetadataEntry | ContainerImageAssetMetadataEntry;
14 changes: 10 additions & 4 deletions packages/aws-cdk/lib/api/toolkit-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,17 @@ export class ToolkitInfo {
/**
* Prepare an ECR repository for uploading to using Docker
*/
public async prepareEcrRepository(assetId: string): Promise<EcrRepositoryInfo> {
public async prepareEcrRepository(asset: cxapi.ContainerImageAssetMetadataEntry): Promise<EcrRepositoryInfo> {
const ecr = await this.props.sdk.ecr(this.props.environment, Mode.ForWriting);

// Repository name based on asset id
const repositoryName = 'cdk/' + assetId.replace(/[:/]/g, '-').toLowerCase();
let repositoryName;
if ( asset.repositoryName ) {
// Repository name provided by user
repositoryName = asset.repositoryName;
} else {
// Repository name based on asset id
const assetId = asset.id;
repositoryName = 'cdk/' + assetId.replace(/[:/]/g, '-').toLowerCase();
}

let repository;
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function prepareContainerAsset(asset: ContainerImageAssetMetadataEn

const buildHold = new PleaseHold(` ⌛ Building Asset Docker image ${asset.id} from ${asset.path}; this may take a while.`);
try {
const ecr = await toolkitInfo.prepareEcrRepository(asset.id);
const ecr = await toolkitInfo.prepareEcrRepository(asset);
const latest = `${ecr.repositoryUri}:latest`;

let loggedIn = false;
Expand Down
54 changes: 54 additions & 0 deletions packages/aws-cdk/test/test.docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import cxapi = require('@aws-cdk/cx-api');
import { Test } from 'nodeunit';
import { ToolkitInfo } from '../lib';
import { prepareContainerAsset } from '../lib/docker';
import { MockSDK } from './util/mock-sdk';

export = {
async 'creates repository with given name'(test: Test) {
// GIVEN

let createdName;

const sdk = new MockSDK();
sdk.stubEcr({
describeRepositories() {
return { repositories: [] };
},

createRepository(req) {
createdName = req.repositoryName;

// Stop the test so that we don't actually docker build
throw new Error('STOPTEST');
},
});

const toolkit = new ToolkitInfo({
sdk,
bucketName: 'BUCKET_NAME',
bucketEndpoint: 'BUCKET_ENDPOINT',
environment: { name: 'env', account: '1234', region: 'abc' }
});

// WHEN
const asset: cxapi.ContainerImageAssetMetadataEntry = {
id: 'assetId',
imageNameParameter: 'MyParameter',
packaging: 'container-image',
path: '/foo',
repositoryName: 'some-name',
};

try {
await prepareContainerAsset(asset, toolkit, false);
} catch (e) {
if (!/STOPTEST/.test(e.toString())) { throw e; }
}

// THEN
test.deepEqual(createdName, 'some-name');

test.done();
},
};
7 changes: 7 additions & 0 deletions packages/aws-cdk/test/util/mock-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export class MockSDK extends SDK {
public stubCloudFormation(stubs: SyncHandlerSubsetOf<AWS.CloudFormation>) {
this.sandbox.stub(this, 'cloudFormation').returns(Promise.resolve(partialAwsService<AWS.CloudFormation>(stubs)));
}

/**
* Replace the ECR client with the given object
*/
public stubEcr(stubs: SyncHandlerSubsetOf<AWS.ECR>) {
this.sandbox.stub(this, 'ecr').returns(Promise.resolve(partialAwsService<AWS.ECR>(stubs)));
}
}

/**
Expand Down

0 comments on commit 942f938

Please sign in to comment.