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

Simplify how we handle cache-from values when doing a pull. #137

Merged
merged 3 commits into from
Feb 7, 2020
Merged
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
22 changes: 8 additions & 14 deletions sdk/nodejs/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,15 @@ async function buildAndPushImageWorkerAsync(
}

// If the container specified a cacheFrom parameter, first set up the cached stages.
let cacheFrom = Promise.resolve<string[] | undefined>(undefined);
let cacheFrom: string[] = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first, passing around 'undefined' just made later code more complex. We can instead just pass around an empty array. The later code already checks that the array is non-empty before proceeding.

if (pullFromCache) {
const dockerBuild = <pulumi.UnwrappedObject<DockerBuild>>pathOrBuild;
const cacheFromParam = (typeof dockerBuild.cacheFrom === "boolean" ? {} : dockerBuild.cacheFrom) || {};
cacheFrom = pullCacheAsync(baseImageName, cacheFromParam, repositoryUrl, logResource);
cacheFrom = await pullCacheAsync(baseImageName, cacheFromParam, repositoryUrl, logResource);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

second, concurrent execution of docker pull wasn't buying us anything. We would immediately get to a point where we would just await this promise unilaterally before proceeding. So it's not like this allowed us any extra performance. It was just extra complexity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: there is an opportunity for concurrency in that we can pull on all the different cache stages concurrently. However, we already don't do that today. And, if we did, it would be entirely in the body of pullCacheAsync.

I'm not going to make that change as i don't want that to cause any potential problems in case there's an issue with too many docker commands running concurrently, or interacting with some repository.

}

// Next, build the image.
const {imageId, stages} = await buildImageAsync(baseImageName, pathOrBuild, logResource, cacheFrom);
if (imageId === undefined) {
throw new Error("Internal error: docker build did not produce an imageId.");
}

// Generate a name that uniquely will identify this built image. This is similar in purpose to
// the name@digest form that can be normally be retrieved from a docker repository. However,
Expand Down Expand Up @@ -332,11 +329,11 @@ async function pullCacheAsync(
imageName: string,
cacheFrom: pulumi.Unwrap<CacheFrom>,
repoUrl: string,
logResource: pulumi.Resource): Promise<string[] | undefined> {
logResource: pulumi.Resource): Promise<string[]> {

// Ensure that we have a repository URL. If we don't, we won't be able to pull anything.
if (!repoUrl) {
return undefined;
return [];
}

pulumi.log.debug(`pulling cache for ${imageName} from ${repoUrl}`, logResource);
Expand Down Expand Up @@ -373,7 +370,7 @@ async function buildImageAsync(
imageName: string,
pathOrBuild: string | pulumi.Unwrap<DockerBuild>,
logResource: pulumi.Resource,
cacheFrom: Promise<string[] | undefined>): Promise<BuildResult> {
cacheFrom: string[]): Promise<BuildResult> {

let build: pulumi.Unwrap<DockerBuild>;
if (typeof pathOrBuild === "string") {
Expand Down Expand Up @@ -436,7 +433,7 @@ async function buildImageAsync(
async function dockerBuild(
imageName: string,
build: pulumi.Unwrap<DockerBuild>,
cacheFrom: Promise<string[] | undefined>,
cacheFrom: string[],
logResource: pulumi.Resource,
target?: string): Promise<void> {

Expand All @@ -453,11 +450,8 @@ async function dockerBuild(
if (build.target) {
buildArgs.push(...["--target", build.target]);
}
if (build.cacheFrom) {
const cacheFromImages = await cacheFrom;
if (cacheFromImages && cacheFromImages.length) {
buildArgs.push(...["--cache-from", cacheFromImages.join()]);
}
if (cacheFrom.length) {
buildArgs.push(...["--cache-from", cacheFrom.join()]);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check of build.cacheFrom is unnecessary. We would only have a non-empty cacheFrom value if build.cacheFrom was there.

if (build.extraOptions) {
buildArgs.push(...build.extraOptions);
Expand Down