Skip to content

Commit

Permalink
Reorg and refactor code around Blob abuse (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored May 6, 2019
1 parent fc9fce8 commit 62ab86b
Show file tree
Hide file tree
Showing 37 changed files with 315 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

import com.google.cloud.tools.jib.blob.Blob;
import com.google.cloud.tools.jib.event.EventDispatcher;
import com.google.cloud.tools.jib.hash.CountingDigestOutputStream;
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;
Expand Down Expand Up @@ -54,23 +54,19 @@ public void testPull() throws IOException, RegistryException, InterruptedExcepti
DescriptorDigest realDigest = manifestTemplate.getLayerDigests().get(0);

// Pulls a layer BLOB of the busybox image.
CountingDigestOutputStream layerOutputStream =
new CountingDigestOutputStream(ByteStreams.nullOutputStream());
LongAdder totalByteCount = new LongAdder();
LongAdder expectedSize = new LongAdder();
registryClient
.pullBlob(
Blob pulledBlob =
registryClient.pullBlob(
realDigest,
size -> {
Assert.assertEquals(0, expectedSize.sum());
expectedSize.add(size);
},
new TestBlobProgressListener(totalByteCount::add))
.writeTo(layerOutputStream);
new TestBlobProgressListener(totalByteCount::add));
Assert.assertEquals(realDigest, pulledBlob.writeTo(ByteStreams.nullOutputStream()).getDigest());
Assert.assertTrue(expectedSize.sum() > 0);
Assert.assertEquals(expectedSize.sum(), totalByteCount.sum());

Assert.assertEquals(realDigest, layerOutputStream.toBlobDescriptor().getDigest());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
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.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;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.security.DigestException;
import org.junit.Assert;
Expand Down Expand Up @@ -114,11 +113,7 @@ public void testPush()
V22ManifestTemplate manifestTemplateByDigest =
registryClient.pullManifest(imageDigest.toString(), V22ManifestTemplate.class);
Assert.assertEquals(
JsonTemplateMapper.toBlob(manifestTemplate)
.writeTo(ByteStreams.nullOutputStream())
.getDigest(),
JsonTemplateMapper.toBlob(manifestTemplateByDigest)
.writeTo(ByteStreams.nullOutputStream())
.getDigest());
Digests.computeJsonDigest(manifestTemplate),
Digests.computeJsonDigest(manifestTemplateByDigest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@

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

import com.google.cloud.tools.jib.hash.CountingDigestOutputStream;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/** Contains properties describing a BLOB, including its digest and possibly its size (in bytes). */
public class BlobDescriptor {
Expand All @@ -31,24 +26,6 @@ public class BlobDescriptor {
/** The size of the BLOB (in bytes). Negative if unknown. */
private final long size;

/**
* Creates a new {@link BlobDescriptor} from the contents of an {@link InputStream} while piping
* to an {@link OutputStream}. Does not close either streams.
*
* @param inputStream the stream to read the contents from
* @param outputStream the {@link OutputStream} to pipe to
* @return a {@link BlobDescriptor} of the piped contents
* @throws IOException if reading from or writing to the streams fails
*/
static BlobDescriptor fromPipe(InputStream inputStream, OutputStream outputStream)
throws IOException {
CountingDigestOutputStream countingDigestOutputStream =
new CountingDigestOutputStream(outputStream);
ByteStreams.copy(inputStream, countingDigestOutputStream);
countingDigestOutputStream.flush();
return countingDigestOutputStream.toBlobDescriptor();
}

public BlobDescriptor(long size, DescriptorDigest digest) {
this.size = size;
this.digest = digest;
Expand Down
41 changes: 8 additions & 33 deletions jib-core/src/main/java/com/google/cloud/tools/jib/blob/Blobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

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

import com.google.cloud.tools.jib.filesystem.FileOperations;
import com.google.cloud.tools.jib.hash.WritableContents;
import com.google.cloud.tools.jib.json.JsonTemplate;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/** Static methods for {@link Blob}. */
Expand All @@ -36,6 +35,10 @@ public static Blob from(Path file) {
return new FileBlob(file);
}

public static Blob from(JsonTemplate template) {
return new JsonBlob(template);
}

/**
* Creates a {@link StringBlob} with UTF-8 encoding.
*
Expand All @@ -46,8 +49,8 @@ public static Blob from(String content) {
return new StringBlob(content);
}

public static Blob from(BlobWriter writer) {
return new WriterBlob(writer);
public static Blob from(WritableContents writable) {
return new WritableContentsBlob(writable);
}

/**
Expand All @@ -74,33 +77,5 @@ public static byte[] writeToByteArray(Blob blob) throws IOException {
return byteArrayOutputStream.toByteArray();
}

/**
* Writes the BLOB to a file.
*
* @param blob the BLOB to to write
* @param blobFile the file to write to
* @return the {@link BlobDescriptor} of the written BLOB
* @throws IOException if writing the BLOB fails
*/
public static BlobDescriptor writeToFile(Blob blob, Path blobFile) throws IOException {
try (OutputStream outputStream = Files.newOutputStream(blobFile)) {
return blob.writeTo(outputStream);
}
}

/**
* Writes the BLOB to a file with an exclusive lock.
*
* @param blob the BLOB to to write
* @param blobFile the file to write to
* @return the {@link BlobDescriptor} of the written BLOB
* @throws IOException if writing the BLOB fails
*/
public static BlobDescriptor writeToFileWithLock(Blob blob, Path blobFile) throws IOException {
try (OutputStream outputStream = FileOperations.newLockingOutputStream(blobFile)) {
return blob.writeTo(outputStream);
}
}

private Blobs() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.google.cloud.tools.jib.hash.Digests;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -34,8 +35,8 @@ class FileBlob implements Blob {

@Override
public BlobDescriptor writeTo(OutputStream outputStream) throws IOException {
try (InputStream fileStream = new BufferedInputStream(Files.newInputStream(file))) {
return BlobDescriptor.fromPipe(fileStream, outputStream);
try (InputStream fileIn = new BufferedInputStream(Files.newInputStream(file))) {
return Digests.computeDigest(fileIn, outputStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.google.cloud.tools.jib.hash.Digests;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -39,7 +40,7 @@ public BlobDescriptor writeTo(OutputStream outputStream) throws IOException {
throw new IllegalStateException("Cannot rewrite Blob backed by an InputStream");
}
try (InputStream inputStream = this.inputStream) {
return BlobDescriptor.fromPipe(inputStream, outputStream);
return Digests.computeDigest(inputStream, outputStream);

} finally {
isWritten = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019 Google LLC.
*
* 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;

import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.json.JsonTemplate;
import java.io.IOException;
import java.io.OutputStream;

/** A {@link Blob} that holds {@link JsonTemplate}. */
class JsonBlob implements Blob {

private final JsonTemplate template;

JsonBlob(JsonTemplate template) {
this.template = template;
}

@Override
public BlobDescriptor writeTo(OutputStream outputStream) throws IOException {
return Digests.computeDigest(template, outputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.google.cloud.tools.jib.hash.Digests;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -33,9 +34,9 @@ class StringBlob implements Blob {

@Override
public BlobDescriptor writeTo(OutputStream outputStream) throws IOException {
try (InputStream stringInputStream =
try (InputStream stringIn =
new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
return BlobDescriptor.fromPipe(stringInputStream, outputStream);
return Digests.computeDigest(stringIn, outputStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@

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

import com.google.cloud.tools.jib.hash.CountingDigestOutputStream;
import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.hash.WritableContents;
import java.io.IOException;
import java.io.OutputStream;

/** A {@link Blob} that writes with a {@link BlobWriter} function and hashes the bytes. */
class WriterBlob implements Blob {
/** A {@link Blob} that holds {@link WritableContents}. */
class WritableContentsBlob implements Blob {

private final BlobWriter writer;
private final WritableContents writableContents;

WriterBlob(BlobWriter writer) {
this.writer = writer;
WritableContentsBlob(WritableContents writableContents) {
this.writableContents = writableContents;
}

@Override
public BlobDescriptor writeTo(OutputStream outputStream) throws IOException {
CountingDigestOutputStream countingDigestOutputStream =
new CountingDigestOutputStream(outputStream);
writer.writeTo(countingDigestOutputStream);
countingDigestOutputStream.flush();
return countingDigestOutputStream.toBlobDescriptor();
return Digests.computeDigest(writableContents, outputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package com.google.cloud.tools.jib.builder.steps;

import com.google.cloud.tools.jib.blob.BlobDescriptor;
import com.google.cloud.tools.jib.hash.Digests;
import com.google.cloud.tools.jib.image.DescriptorDigest;
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.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.util.Objects;
Expand All @@ -47,10 +47,7 @@ static BuildResult fromImage(Image image, Class<? extends BuildableManifestTempl
BuildableManifestTemplate manifestTemplate =
imageToJsonTranslator.getManifestTemplate(
targetFormat, containerConfigurationBlobDescriptor);
DescriptorDigest imageDigest =
JsonTemplateMapper.toBlob(manifestTemplate)
.writeTo(ByteStreams.nullOutputStream())
.getDigest();
DescriptorDigest imageDigest = Digests.computeJsonDigest(manifestTemplate);
DescriptorDigest imageId = containerConfigurationBlobDescriptor.getDigest();
return new BuildResult(imageDigest, imageId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private Image pullBaseImage(
|| buildableManifestTemplate.getContainerConfiguration().getDigest() == null) {
throw new UnknownManifestFormatException(
"Invalid container configuration in Docker V2.2/OCI manifest: \n"
+ Blobs.writeToString(JsonTemplateMapper.toBlob(buildableManifestTemplate)));
+ JsonTemplateMapper.toUtf8String(buildableManifestTemplate));
}

DescriptorDigest containerConfigurationDigest =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
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.hash.Digests;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.cloud.tools.jib.image.json.ImageToJsonTranslator;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.registry.RegistryClient;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
Expand Down Expand Up @@ -165,10 +164,7 @@ private BuildResult afterAllPushed()
}));
}

DescriptorDigest imageDigest =
JsonTemplateMapper.toBlob(manifestTemplate)
.writeTo(ByteStreams.nullOutputStream())
.getDigest();
DescriptorDigest imageDigest = Digests.computeJsonDigest(manifestTemplate);
DescriptorDigest imageId = containerConfigurationBlobDescriptor.getDigest();
BuildResult result = new BuildResult(imageDigest, imageId);

Expand Down
Loading

0 comments on commit 62ab86b

Please sign in to comment.