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

Break apart container configuration build and push step #252

Merged
merged 8 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.blob;

/** Struct for pairing a Blob and its digest. */
public class BlobAndDigest {
private Blob blob;
private BlobDescriptor blobDescriptor;

public BlobAndDigest(Blob blob, BlobDescriptor blobDescriptor) {
this.blob = blob;
this.blobDescriptor = blobDescriptor;
}

public Blob getBlob() {
return blob;
}

public BlobDescriptor getBlobDescriptor() {
return blobDescriptor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

import com.google.cloud.tools.jib.Timer;
import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.blob.BlobDescriptor;
import com.google.cloud.tools.jib.blob.BlobAndDigest;
import com.google.cloud.tools.jib.cache.CachedLayer;
import com.google.cloud.tools.jib.hash.CountingDigestOutputStream;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.LayerPropertyNotFoundException;
import com.google.cloud.tools.jib.image.json.ImageToJsonTranslator;
import com.google.cloud.tools.jib.registry.RegistryClient;
import com.google.cloud.tools.jib.registry.RegistryException;
import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand All @@ -38,7 +36,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

class BuildAndPushContainerConfigurationStep implements Callable<ListenableFuture<BlobDescriptor>> {
class BuildContainerConfigurationStep implements Callable<ListenableFuture<BlobAndDigest>> {

private static final String DESCRIPTION = "Building container configuration";

Expand All @@ -50,7 +48,7 @@ class BuildAndPushContainerConfigurationStep implements Callable<ListenableFutur
private final List<ListenableFuture<CachedLayer>> buildApplicationLayerFutures;
private final List<String> entrypoint;

BuildAndPushContainerConfigurationStep(
BuildContainerConfigurationStep(
BuildConfiguration buildConfiguration,
ListeningExecutorService listeningExecutorService,
ListenableFuture<Authorization> pushAuthorizationFuture,
Expand All @@ -67,7 +65,7 @@ class BuildAndPushContainerConfigurationStep implements Callable<ListenableFutur

/** Depends on {@code pullBaseImageLayerFuturesFuture}. */
@Override
public ListenableFuture<BlobDescriptor> call() throws ExecutionException, InterruptedException {
public ListenableFuture<BlobAndDigest> call() throws ExecutionException, InterruptedException {
// TODO: This might need to belong in BuildImageSteps.
List<ListenableFuture<?>> afterBaseImageLayerFuturesFutureDependencies = new ArrayList<>();
afterBaseImageLayerFuturesFutureDependencies.add(pushAuthorizationFuture);
Expand All @@ -82,17 +80,9 @@ public ListenableFuture<BlobDescriptor> call() throws ExecutionException, Interr
* Depends on {@code pushAuthorizationFuture}, {@code pullBaseImageLayerFuturesFuture.get()}, and
* {@code buildApplicationLayerFutures}.
*/
private BlobDescriptor afterBaseImageLayerFuturesFuture()
throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException,
RegistryException {
try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
RegistryClient registryClient =
new RegistryClient(
NonBlockingFutures.get(pushAuthorizationFuture),
buildConfiguration.getTargetRegistry(),
buildConfiguration.getTargetRepository())
.setTimer(timer);

private BlobAndDigest afterBaseImageLayerFuturesFuture()
throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException {
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
// Constructs the image.
Image image = new Image();
for (Future<CachedLayer> cachedLayerFuture :
Expand All @@ -112,17 +102,8 @@ private BlobDescriptor afterBaseImageLayerFuturesFuture()
CountingDigestOutputStream digestOutputStream =
new CountingDigestOutputStream(ByteStreams.nullOutputStream());
containerConfigurationBlob.writeTo(digestOutputStream);
BlobDescriptor containerConfigurationBlobDescriptor = digestOutputStream.toBlobDescriptor();

timer.lap(
"Pushing container configuration " + containerConfigurationBlobDescriptor.getDigest());

// TODO: Use PushBlobStep.
// Pushes the container configuration.
registryClient.pushBlob(
containerConfigurationBlobDescriptor.getDigest(), containerConfigurationBlob);

return containerConfigurationBlobDescriptor;
return new BlobAndDigest(containerConfigurationBlob, digestOutputStream.toBlobDescriptor());
Copy link
Contributor

Choose a reason for hiding this comment

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

This could probably avoid creating a new tuple class BlobAndDigest by just returning the Blob here and generating the Digest in the push step.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I wasn't sure if the Digest generation should be kept in the build step.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.jib.builder;

import com.google.cloud.tools.jib.Timer;
import com.google.cloud.tools.jib.blob.BlobAndDigest;
import com.google.cloud.tools.jib.blob.BlobDescriptor;
import com.google.cloud.tools.jib.cache.Cache;
import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException;
Expand Down Expand Up @@ -144,13 +145,13 @@ public void run()
listeningExecutorService)
.call();

timer2.lap("Setting up container configuration push");
// Builds and pushes the container configuration.
ListenableFuture<ListenableFuture<BlobDescriptor>>
buildAndPushContainerConfigurationFutureFuture =
timer2.lap("Setting up build container configuration");
// Builds the container configuration.
ListenableFuture<ListenableFuture<BlobAndDigest>>
buildContainerConfigurationFutureFuture =
Futures.whenAllSucceed(pullBaseImageLayerFuturesFuture)
.call(
new BuildAndPushContainerConfigurationStep(
new BuildContainerConfigurationStep(
buildConfiguration,
listeningExecutorService,
authenticatePushFuture,
Expand All @@ -159,6 +160,19 @@ public void run()
entrypoint),
listeningExecutorService);

timer2.lap("Setting up container configuration push");
// Pushes the container configuration.
ListenableFuture<ListenableFuture<BlobDescriptor>>
pushContainerConfigurationFutureFuture =
Futures.whenAllSucceed(buildContainerConfigurationFutureFuture)
.call(
new PushContainerConfigurationStep(
buildConfiguration,
authenticatePushFuture,
buildContainerConfigurationFutureFuture.get(),
Copy link
Contributor

@coollog coollog May 3, 2018

Choose a reason for hiding this comment

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

This call to get would be executed before anything is run, which would cause race conditions and blocking. Any get for a Future should be called in the call method of the step that uses it and should be wrapped in a NonBlockingFutures.get for busy-wait safety (it makes sure that the get does not block).

listeningExecutorService),
listeningExecutorService);

timer2.lap("Setting up application layer push");
// Pushes the application layers.
List<ListenableFuture<Void>> pushApplicationLayersFuture =
Expand All @@ -173,8 +187,7 @@ public void run()
// Pushes the new image manifest.
ListenableFuture<Void> pushImageFuture =
Futures.whenAllSucceed(
pushBaseImageLayerFuturesFuture,
buildAndPushContainerConfigurationFutureFuture)
pushBaseImageLayerFuturesFuture, pushContainerConfigurationFutureFuture)
.call(
new PushImageStep(
buildConfiguration,
Expand All @@ -184,7 +197,7 @@ public void run()
buildAndCacheApplicationLayerFutures,
pushBaseImageLayerFuturesFuture,
pushApplicationLayersFuture,
buildAndPushContainerConfigurationFutureFuture),
pushContainerConfigurationFutureFuture),
listeningExecutorService);

timer2.lap("Running push new image");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2018 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.builder;

import com.google.cloud.tools.jib.Timer;
import com.google.cloud.tools.jib.blob.BlobAndDigest;
import com.google.cloud.tools.jib.blob.BlobDescriptor;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.registry.RegistryClient;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.concurrent.Callable;

class PushContainerConfigurationStep implements Callable<ListenableFuture<BlobDescriptor>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Short javadoc here


private static final String DESCRIPTION = "Pushing container configuration";

private final BuildConfiguration buildConfiguration;
private final ListenableFuture<Authorization> pushAuthorizationFuture;
private final ListenableFuture<BlobAndDigest> blobAndDigestFuture;
private final ListeningExecutorService listeningExecutorService;

PushContainerConfigurationStep(
BuildConfiguration buildConfiguration,
ListenableFuture<Authorization> pushAuthorizationFuture,
ListenableFuture<BlobAndDigest> blobAndDigestFuture,
ListeningExecutorService listeningExecutorService) {
this.buildConfiguration = buildConfiguration;
this.pushAuthorizationFuture = pushAuthorizationFuture;
this.blobAndDigestFuture = blobAndDigestFuture;
this.listeningExecutorService = listeningExecutorService;
}

@Override
public ListenableFuture<BlobDescriptor> call() {
return listeningExecutorService.submit(
() -> {
try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
RegistryClient registryClient =
new RegistryClient(
NonBlockingFutures.get(pushAuthorizationFuture),
buildConfiguration.getTargetRegistry(),
buildConfiguration.getTargetRepository())
.setTimer(timer);

// TODO: Use PushBlobStep.
// Pushes the container configuration.
BlobAndDigest blobAndDigest = blobAndDigestFuture.get();
registryClient.pushBlob(
blobAndDigest.getBlobDescriptor().getDigest(), blobAndDigest.getBlob());
return blobAndDigest.getBlobDescriptor();
}
});
}
}