Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-cdk): add support for user provided repository name to DockerImageAsset #2032

Merged
merged 17 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
alex-berger marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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