Skip to content

Commit

Permalink
chore(core): remove all references to BundlingDockerImage in the publ…
Browse files Browse the repository at this point in the history
…ic API (#13814)

A previous commit - ad01099 - deprecated BundlingDockerImage in favour
of DockerImage.

However, there are still uses of BundlingDockerImage that remain. Since
bundling is still experimental, swap all uses of BundlingDockerImage and
replace with DockerImage.

Motivation
No non-deprecated public API can reference a deprecated type as part of
CDKv2.

BREAKING CHANGE: The type of the `image` property in `BundlingOptions`
is changed from `BundlingDockerImage` to `DockerImage`.
* **core:** The return type of the `DockerImage.fromBuild()` API is
changed from `BundlingDockerImage` to `DockerImage`.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar committed Mar 26, 2021
1 parent dd22e8f commit 9cceb3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
58 changes: 29 additions & 29 deletions packages/@aws-cdk/core/lib/bundling.ts
Expand Up @@ -12,7 +12,7 @@ export interface BundlingOptions {
/**
* The Docker image where the command will run.
*/
readonly image: BundlingDockerImage;
readonly image: DockerImage;

/**
* The entrypoint to run in the Docker container.
Expand Down Expand Up @@ -158,33 +158,7 @@ export class BundlingDockerImage {
* @deprecated use DockerImage.fromBuild()
*/
public static fromAsset(path: string, options: DockerBuildOptions = {}) {
const buildArgs = options.buildArgs || {};

if (options.file && isAbsolute(options.file)) {
throw new Error(`"file" must be relative to the docker build directory. Got ${options.file}`);
}

// Image tag derived from path and build options
const input = JSON.stringify({ path, ...options });
const tagHash = crypto.createHash('sha256').update(input).digest('hex');
const tag = `cdk-${tagHash}`;

const dockerArgs: string[] = [
'build', '-t', tag,
...(options.file ? ['-f', join(path, options.file)] : []),
...flatten(Object.entries(buildArgs).map(([k, v]) => ['--build-arg', `${k}=${v}`])),
path,
];

dockerExec(dockerArgs);

// Fingerprints the directory containing the Dockerfile we're building and
// differentiates the fingerprint based on build arguments. We do this so
// we can provide a stable image hash. Otherwise, the image ID will be
// different every time the Docker layer cache is cleared, due primarily to
// timestamps.
const hash = FileSystem.fingerprint(path, { extraHash: JSON.stringify(options) });
return new BundlingDockerImage(tag, hash);
DockerImage.fromBuild(path, options) as BundlingDockerImage;
}

/** @param image The Docker image */
Expand Down Expand Up @@ -276,7 +250,33 @@ export class DockerImage extends BundlingDockerImage {
* @param options Docker build options
*/
public static fromBuild(path: string, options: DockerBuildOptions = {}) {
return BundlingDockerImage.fromAsset(path, options);
const buildArgs = options.buildArgs || {};

if (options.file && isAbsolute(options.file)) {
throw new Error(`"file" must be relative to the docker build directory. Got ${options.file}`);
}

// Image tag derived from path and build options
const input = JSON.stringify({ path, ...options });
const tagHash = crypto.createHash('sha256').update(input).digest('hex');
const tag = `cdk-${tagHash}`;

const dockerArgs: string[] = [
'build', '-t', tag,
...(options.file ? ['-f', join(path, options.file)] : []),
...flatten(Object.entries(buildArgs).map(([k, v]) => ['--build-arg', `${k}=${v}`])),
path,
];

dockerExec(dockerArgs);

// Fingerprints the directory containing the Dockerfile we're building and
// differentiates the fingerprint based on build arguments. We do this so
// we can provide a stable image hash. Otherwise, the image ID will be
// different every time the Docker layer cache is cleared, due primarily to
// timestamps.
const hash = FileSystem.fingerprint(path, { extraHash: JSON.stringify(options) });
return new DockerImage(tag, hash);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/core/test/bundling.test.ts
Expand Up @@ -60,7 +60,7 @@ nodeunitShim({
const fingerprintStub = sinon.stub(FileSystem, 'fingerprint');
fingerprintStub.callsFake(() => imageHash);

const image = BundlingDockerImage.fromAsset('docker-path', {
const image = DockerImage.fromBuild('docker-path', {
buildArgs: {
TEST_ARG: 'cdk-test',
},
Expand Down Expand Up @@ -139,7 +139,7 @@ nodeunitShim({
const fingerprintStub = sinon.stub(FileSystem, 'fingerprint');
fingerprintStub.callsFake(() => imageHash);

const image = BundlingDockerImage.fromAsset('docker-path');
const image = DockerImage.fromBuild('docker-path');

const tagHash = crypto.createHash('sha256').update(JSON.stringify({
path: 'docker-path',
Expand Down

0 comments on commit 9cceb3f

Please sign in to comment.