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

Reorg and refactor repo - continued #1684

Merged
merged 48 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0688f28
Decouple Blob
chanseokoh Apr 19, 2019
ec340f3
Rename ImageToTarTranslator
chanseokoh Apr 19, 2019
305f254
Rename test class
chanseokoh Apr 19, 2019
02b42ad
Decouple Blob from TarStreamBuilder
chanseokoh Apr 19, 2019
0ff040a
empty commit to stalled Kokoro
chanseokoh Apr 19, 2019
ac3ef64
Clean up
chanseokoh Apr 19, 2019
447573d
more intuitive code
chanseokoh Apr 19, 2019
8098dc3
wip
chanseokoh Apr 19, 2019
181dbad
wip
chanseokoh Apr 20, 2019
ca070aa
Merge branch 'master' into cleanup-3
chanseokoh Apr 29, 2019
efd85ef
Revert TarStreamBuilder
chanseokoh Apr 29, 2019
21067f7
wip
chanseokoh Apr 29, 2019
96a047e
Merge branch 'master' into cleanup-3
chanseokoh Apr 30, 2019
ec4a67f
Remove Blob dep from DigestUtil
chanseokoh Apr 30, 2019
2cf3c4b
Move WritableContents to hash package
chanseokoh Apr 30, 2019
e5bb5e9
More refactoring
chanseokoh Apr 30, 2019
8b82327
Decouple Blob from http Response
chanseokoh Apr 30, 2019
873ada8
wip
chanseokoh Apr 30, 2019
a647b80
Decouple Blob from http Request
chanseokoh Apr 30, 2019
8f96337
test
chanseokoh Apr 30, 2019
d3bfc7a
Reorg and refactor
chanseokoh Apr 30, 2019
c047f8c
Merge branch 'master' into cleanup-2
chanseokoh Apr 30, 2019
11d7b60
wip
chanseokoh Apr 30, 2019
52c6455
done
chanseokoh May 1, 2019
f293268
Fix wrong filename
chanseokoh May 1, 2019
e4461b4
Remove more Blob indirection
chanseokoh May 1, 2019
c7aee9e
Merge branch 'cleanup-2' into cleanup-4
chanseokoh May 1, 2019
40f6666
Rename
chanseokoh May 1, 2019
d6f02da
Rename
chanseokoh May 2, 2019
6eccb7e
Merge branch 'master' into cleanup-4
chanseokoh May 6, 2019
ea31cfc
private
chanseokoh May 6, 2019
f27ff67
Refactor
chanseokoh May 7, 2019
f2be62e
Clean up
chanseokoh May 7, 2019
e75f8e0
Fix Javadocs
chanseokoh May 7, 2019
523b4f7
Update Javadocs
chanseokoh May 7, 2019
a746d54
Merge remote-tracking branch 'origin/master' into cleanup-4
chanseokoh May 7, 2019
c74330c
Rename
chanseokoh May 7, 2019
e2b3434
Merge branch 'master' into cleanup-4
chanseokoh May 10, 2019
b8640af
Push up throttling decision
chanseokoh May 14, 2019
3fba22b
Rename "with..." to "from..."
chanseokoh May 15, 2019
6f9ea9b
Specialize ThrottledConsumer
chanseokoh May 15, 2019
70d0747
Fix Javadoc
chanseokoh May 15, 2019
998d56a
ListenableCountingOutputStream -> NotifyingOutputStream
chanseokoh May 16, 2019
70be4b2
optionalOutStream -> outStream
chanseokoh May 16, 2019
d6842e6
ThrottledLongConsumer -> ThrottledAccumulatingConsumer
chanseokoh May 16, 2019
c5fedc7
ThrottledLongConsumer -> ThrottledAccumulatingConsumer
chanseokoh May 16, 2019
60778ee
Merge branch 'master' into cleanup-4
chanseokoh May 17, 2019
421a032
Merge branch 'cleanup-4' of https://github.com/GoogleContainerTools/j…
chanseokoh May 17, 2019
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
Expand Up @@ -18,7 +18,6 @@

import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.event.EventDispatcher;
import com.google.cloud.tools.jib.http.TestBlobProgressListener;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import com.google.cloud.tools.jib.image.json.V21ManifestTemplate;
import com.google.common.io.ByteStreams;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void testPull() throws IOException, RegistryException, InterruptedExcepti
Assert.assertEquals(0, expectedSize.sum());
expectedSize.add(size);
},
new TestBlobProgressListener(totalByteCount::add));
totalByteCount::add);
Assert.assertEquals(realDigest, pulledBlob.writeTo(ByteStreams.nullOutputStream()).getDigest());
Assert.assertTrue(expectedSize.sum() > 0);
Assert.assertEquals(expectedSize.sum(), totalByteCount.sum());
Expand All @@ -83,7 +82,7 @@ public void testPull_unknownBlob() throws IOException, DigestException, Interrup

try {
registryClient
.pullBlob(nonexistentDigest, ignored -> {}, new TestBlobProgressListener(ignored -> {}))
.pullBlob(nonexistentDigest, ignored -> {}, ignored -> {})
.writeTo(ByteStreams.nullOutputStream());
Assert.fail("Trying to pull nonexistent blob should have errored");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.event.EventDispatcher;
import com.google.cloud.tools.jib.http.TestBlobProgressListener;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import java.io.IOException;
import java.security.DigestException;
Expand Down Expand Up @@ -47,8 +46,6 @@ public void testPush()
RegistryClient.factory(EVENT_DISPATCHER, "localhost:5000", "testimage")
.setAllowInsecureRegistries(true)
.newRegistryClient();
Assert.assertFalse(
registryClient.pushBlob(
testBlobDigest, testBlob, null, new TestBlobProgressListener(ignored -> {})));
Assert.assertFalse(registryClient.pushBlob(testBlobDigest, testBlob, null, ignored -> {}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.event.EventDispatcher;
import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.http.TestBlobProgressListener;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import com.google.cloud.tools.jib.image.json.ManifestTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
Expand Down Expand Up @@ -87,14 +86,13 @@ public void testPush()
.setAllowInsecureRegistries(true)
.newRegistryClient();
Assert.assertFalse(
registryClient.pushBlob(
testLayerBlobDigest, testLayerBlob, null, new TestBlobProgressListener(ignored -> {})));
registryClient.pushBlob(testLayerBlobDigest, testLayerBlob, null, ignored -> {}));
Assert.assertFalse(
registryClient.pushBlob(
testContainerConfigurationBlobDigest,
testContainerConfigurationBlob,
null,
new TestBlobProgressListener(ignored -> {})));
ignored -> {}));

// Pushes the manifest.
DescriptorDigest imageDigest = registryClient.pushManifest(expectedManifestTemplate, "latest");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.configuration.credentials.Credential;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.http.Authorizations;
import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException;
import com.google.cloud.tools.jib.registry.RegistryAuthenticator;
import com.google.cloud.tools.jib.registry.RegistryException;
Expand Down Expand Up @@ -102,7 +101,7 @@ public Authorization call()

return (registryCredential == null || registryCredential.isOAuth2RefreshToken())
? null
: Authorizations.withBasicCredentials(
: Authorization.withBasicCredentials(
registryCredential.getUsername(), registryCredential.getPassword());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.cloud.tools.jib.image.json.ImageToJsonTranslator;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.util.Objects;

Expand All @@ -41,9 +40,7 @@ static BuildResult fromImage(Image image, Class<? extends BuildableManifestTempl
throws IOException {
ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
BlobDescriptor containerConfigurationBlobDescriptor =
imageToJsonTranslator
.getContainerConfigurationBlob()
.writeTo(ByteStreams.nullOutputStream());
Digests.computeDigest(imageToJsonTranslator.getContainerConfiguration());
BuildableManifestTemplate manifestTemplate =
imageToJsonTranslator.getManifestTemplate(
targetFormat, containerConfigurationBlobDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import com.google.cloud.tools.jib.builder.BuildStepType;
import com.google.cloud.tools.jib.builder.ProgressEventDispatcher;
import com.google.cloud.tools.jib.http.BlobProgressListener;
import com.google.common.base.Preconditions;
import java.io.Closeable;
import java.time.Duration;
import javax.annotation.Nullable;

/**
Expand All @@ -32,14 +30,14 @@
* response headers are received, only after which can the {@link ProgressEventDispatcher} be
* created.
*/
class ProgressEventDispatcherContainer implements BlobProgressListener, Closeable {
class ProgressEventDispatcherWrapper implements Closeable {

private final ProgressEventDispatcher.Factory progressEventDispatcherFactory;
private final String description;
private final BuildStepType type;
@Nullable private ProgressEventDispatcher progressEventDispatcher;

ProgressEventDispatcherContainer(
ProgressEventDispatcherWrapper(
ProgressEventDispatcher.Factory progressEventDispatcherFactory,
String description,
BuildStepType type) {
Expand All @@ -48,15 +46,9 @@ class ProgressEventDispatcherContainer implements BlobProgressListener, Closeabl
this.type = type;
}

@Override
public void handleByteCount(long byteCount) {
void dispatchProgress(long progressUnits) {
Preconditions.checkNotNull(progressEventDispatcher);
progressEventDispatcher.dispatchProgress(byteCount);
}

@Override
public Duration getDelayBetweenCallbacks() {
return Duration.ofMillis(100);
progressEventDispatcher.dispatchProgress(progressUnits);
}

@Override
Expand All @@ -65,8 +57,9 @@ public void close() {
progressEventDispatcher.close();
}

void initializeWithBlobSize(long blobSize) {
void setProgressTarget(long allocationUnits) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a bit weird to have to call the constructor and this method to fully initialize a ProgressEventDispatcherWrapper, maybe we can consolidate this by changing the signature of RegistryClient#pullBlob?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I did not like this wrapper either. The problem Q originally faced was the progress reporting design that

  1. You have to know and set in advance how much progress units you will allocate when creating a progress event dispatcher.
  2. But you cannot know how much bytes (allocation units) you will download until you read Content-Length.
  3. So the wrapper that will create and hold a progress event dispatcher when this method is called. Other than that, this class is just a thin wrapper that delegates dispatchProgress().

For this reason, obviously it's best to get rid of this unfortunate wrapper if possible. As an attempt to work around the issue, Q probably also felt it doesn't make much sense to have this wrapper in the signature and make it a hard dependency from the API standpoint, also given that there are only just a couple places this wrapper is required. And as I said in the earlier comment, my intention is to actually decouple progress dispatcher stuff from RegistryClient.

Preconditions.checkState(progressEventDispatcher == null);
progressEventDispatcher = progressEventDispatcherFactory.create(type, description, blobSize);
progressEventDispatcher =
progressEventDispatcherFactory.create(type, description, allocationUnits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ public CachedLayer call() throws IOException, CacheCorruptedException {
.setAuthorization(pullAuthorization)
.newRegistryClient();

try (ProgressEventDispatcherContainer progressEventDispatcherContainer =
new ProgressEventDispatcherContainer(
try (ProgressEventDispatcherWrapper progressEventDispatcherWrapper =
new ProgressEventDispatcherWrapper(
progressEventDispatcher.newChildProducer(),
"pulling base image layer " + layerDigest,
BuildStepType.PULL_AND_CACHE_BASE_IMAGE_LAYER)) {
return cache.writeCompressedLayer(
registryClient.pullBlob(
layerDigest,
progressEventDispatcherContainer::initializeWithBlobSize,
progressEventDispatcherContainer));
progressEventDispatcherWrapper::setProgressTarget,
progressEventDispatcherWrapper::dispatchProgress));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.cloud.tools.jib.event.events.LogEvent;
import com.google.cloud.tools.jib.event.events.ProgressEvent;
import com.google.cloud.tools.jib.http.Authorization;
import com.google.cloud.tools.jib.http.Authorizations;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.ImageReference;
Expand Down Expand Up @@ -166,7 +165,7 @@ public BaseImageWithAuthorization call()
Authorization registryAuthorization =
registryCredential == null || registryCredential.isOAuth2RefreshToken()
? null
: Authorizations.withBasicCredentials(
: Authorization.withBasicCredentials(
registryCredential.getUsername(), registryCredential.getPassword());

try {
Expand Down Expand Up @@ -254,17 +253,17 @@ private Image pullBaseImage(
DescriptorDigest containerConfigurationDigest =
buildableManifestTemplate.getContainerConfiguration().getDigest();

try (ProgressEventDispatcherContainer progressEventDispatcherContainer =
new ProgressEventDispatcherContainer(
try (ProgressEventDispatcherWrapper progressEventDispatcherWrapper =
new ProgressEventDispatcherWrapper(
progressEventDispatcher.newChildProducer(),
"pull container configuration " + containerConfigurationDigest,
BuildStepType.PULL_BASE_IMAGE)) {
String containerConfigurationString =
Blobs.writeToString(
registryClient.pullBlob(
containerConfigurationDigest,
progressEventDispatcherContainer::initializeWithBlobSize,
progressEventDispatcherContainer));
progressEventDispatcherWrapper::setProgressTarget,
progressEventDispatcherWrapper::dispatchProgress));

ContainerConfigurationTemplate containerConfigurationTemplate =
JsonTemplateMapper.readJson(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,17 @@
import com.google.cloud.tools.jib.builder.TimerEventDispatcher;
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.event.events.LogEvent;
import com.google.cloud.tools.jib.http.BlobProgressListener;
import com.google.cloud.tools.jib.registry.RegistryClient;
import com.google.cloud.tools.jib.registry.RegistryException;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/** Pushes a BLOB to the target registry. */
class PushBlobStep implements AsyncStep<BlobDescriptor>, Callable<BlobDescriptor> {

private static class ForwardingProgressListener implements BlobProgressListener {

private final ProgressEventDispatcher progressEventDispatcher;

private ForwardingProgressListener(ProgressEventDispatcher progressEventDispatcher) {
this.progressEventDispatcher = progressEventDispatcher;
}

@Override
public void handleByteCount(long byteCount) {
progressEventDispatcher.dispatchProgress(byteCount);
}

@Override
public Duration getDelayBetweenCallbacks() {
return Duration.ofMillis(100);
}
}

private static final String DESCRIPTION = "Pushing BLOB ";

private final BuildConfiguration buildConfiguration;
Expand Down Expand Up @@ -122,10 +101,7 @@ public BlobDescriptor call() throws IOException, RegistryException, ExecutionExc

// todo: leverage cross-repository mounts
registryClient.pushBlob(
blobDescriptor.getDigest(),
blob,
null,
new ForwardingProgressListener(progressEventDispatcher));
blobDescriptor.getDigest(), blob, null, progressEventDispatcher::dispatchProgress);

return blobDescriptor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
import com.google.cloud.tools.jib.async.AsyncDependencies;
import com.google.cloud.tools.jib.async.AsyncStep;
import com.google.cloud.tools.jib.async.NonBlockingSteps;
import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.blob.BlobDescriptor;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.builder.BuildStepType;
import com.google.cloud.tools.jib.builder.ProgressEventDispatcher;
import com.google.cloud.tools.jib.builder.TimerEventDispatcher;
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.json.ImageToJsonTranslator;
import com.google.common.io.ByteStreams;
import com.google.cloud.tools.jib.json.JsonTemplate;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.io.IOException;
Expand Down Expand Up @@ -90,18 +91,17 @@ private PushBlobStep afterBuildConfigurationFutureFuture()
TimerEventDispatcher ignored =
new TimerEventDispatcher(buildConfiguration.getEventDispatcher(), DESCRIPTION)) {
Image image = NonBlockingSteps.get(NonBlockingSteps.get(buildImageStep));
Blob containerConfigurationBlob =
new ImageToJsonTranslator(image).getContainerConfigurationBlob();
BlobDescriptor blobDescriptor =
containerConfigurationBlob.writeTo(ByteStreams.nullOutputStream());
JsonTemplate containerConfiguration =
new ImageToJsonTranslator(image).getContainerConfiguration();
BlobDescriptor blobDescriptor = Digests.computeDigest(containerConfiguration);

return new PushBlobStep(
listeningExecutorService,
buildConfiguration,
progressEventDispatcher.newChildProducer(),
authenticatePushStep,
blobDescriptor,
containerConfigurationBlob,
Blobs.from(containerConfiguration),
BuildStepType.PUSH_CONTAINER_CONFIGURATION);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static DescriptorDigest getDiffIdByDecompressingFile(Path compressedFile
GZIPInputStream decompressorStream = new GZIPInputStream(fileInputStream)) {
ByteStreams.copy(decompressorStream, diffIdCaptureOutputStream);
}
return diffIdCaptureOutputStream.getDigest();
return diffIdCaptureOutputStream.computeDigest().getDigest();
}
}

Expand Down Expand Up @@ -337,8 +337,9 @@ private WrittenLayer writeUncompressedLayerBlobToDirectory(

// The GZIPOutputStream must be closed in order to write out the remaining compressed data.
compressorStream.close();
DescriptorDigest layerDigest = compressedDigestOutputStream.getDigest();
long layerSize = compressedDigestOutputStream.getBytesHahsed();
BlobDescriptor blobDescriptor = compressedDigestOutputStream.computeDigest();
DescriptorDigest layerDigest = blobDescriptor.getDigest();
long layerSize = blobDescriptor.getSize();

// Renames the temporary layer file to the correct filename.
Path layerFile = layerDirectory.resolve(cacheStorageFiles.getLayerFilename(layerDiffId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

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

import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.docker.json.DockerLoadManifestEntryTemplate;
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.ImageReference;
import com.google.cloud.tools.jib.image.Layer;
import com.google.cloud.tools.jib.image.json.ImageToJsonTranslator;
import com.google.cloud.tools.jib.json.JsonTemplate;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.tar.TarStreamBuilder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
Expand Down Expand Up @@ -71,16 +69,17 @@ public void writeTo(OutputStream out) throws IOException {
}

// Adds the container configuration to the tarball.
Blob containerConfigurationBlob =
new ImageToJsonTranslator(image).getContainerConfigurationBlob();
JsonTemplate containerConfiguration =
new ImageToJsonTranslator(image).getContainerConfiguration();
tarStreamBuilder.addByteEntry(
Blobs.writeToByteArray(containerConfigurationBlob), CONTAINER_CONFIGURATION_JSON_FILE_NAME);
JsonTemplateMapper.toByteArray(containerConfiguration),
CONTAINER_CONFIGURATION_JSON_FILE_NAME);

// Adds the manifest to tarball.
manifestTemplate.setRepoTags(imageReference.toStringWithTag());
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
JsonTemplateMapper.writeTo(Collections.singletonList(manifestTemplate), outStream);
tarStreamBuilder.addByteEntry(outStream.toByteArray(), MANIFEST_JSON_FILE_NAME);
tarStreamBuilder.addByteEntry(
JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)),
MANIFEST_JSON_FILE_NAME);

tarStreamBuilder.writeAsTarArchiveTo(out);
}
Expand Down
Loading