From 618f4a35b5fa53185e0282b93508390d0bb4b1ea Mon Sep 17 00:00:00 2001 From: Tad Cordle Date: Thu, 22 Aug 2019 15:26:30 -0400 Subject: [PATCH] Adjust TarImage API in preparation for local base image support (#1926) --- jib-core/CHANGELOG.md | 2 + .../jib/api/ContainerizerIntegrationTest.java | 4 +- .../tools/jib/api/JibIntegrationTest.java | 4 +- .../tools/jib/api/ReproducibleImageTest.java | 2 +- .../cloud/tools/jib/api/Containerizer.java | 10 ++- .../google/cloud/tools/jib/api/TarImage.java | 76 ++++++++----------- .../tools/jib/api/ContainerizerTest.java | 4 +- .../cloud/tools/jib/api/TarImageTest.java | 15 +++- .../common/PluginConfigurationProcessor.java | 2 +- 9 files changed, 60 insertions(+), 59 deletions(-) diff --git a/jib-core/CHANGELOG.md b/jib-core/CHANGELOG.md index 97984a9e4f..2d18038c34 100644 --- a/jib-core/CHANGELOG.md +++ b/jib-core/CHANGELOG.md @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file. ### Changed +- `TarImage` is constructed using `TarImage.at(...).named(...)` instead of `TarImage.named(...).saveTo(...)` ([#1918](https://github.com/GoogleContainerTools/jib/issues/1918)) + ### Fixed - Fixed an issue interacting with certain registries due to changes to URL handling in the underlying Apache HttpClient library. ([#1924](https://github.com/GoogleContainerTools/jib/issues/1924)) diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ContainerizerIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ContainerizerIntegrationTest.java index 46f7de15d9..09f5a5a6fe 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ContainerizerIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ContainerizerIntegrationTest.java @@ -322,9 +322,7 @@ private JibContainer buildTarImage( throws IOException, InterruptedException, RegistryException, CacheDirectoryCreationException, ExecutionException { return buildImage( - baseImage, - Containerizer.to(TarImage.named(targetImage).saveTo(outputPath)), - additionalTags); + baseImage, Containerizer.to(TarImage.at(outputPath).named(targetImage)), additionalTags); } private JibContainer buildImage( diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java index 8ad467771a..bc4705d25f 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/JibIntegrationTest.java @@ -201,8 +201,8 @@ public void testManifestListReferenceByShaDoesNotFail() ManifestPullerIntegrationTest.KNOWN_MANIFEST_LIST_SHA); Containerizer containerizer = Containerizer.to( - TarImage.named("whatever") - .saveTo(cacheFolder.newFolder("goose").toPath().resolve("moose"))); + TarImage.at(cacheFolder.newFolder("goose").toPath().resolve("moose")) + .named("whatever")); Jib.from(sourceImageReferenceAsManifestList).containerize(containerizer); // pass, no exceptions thrown diff --git a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ReproducibleImageTest.java b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ReproducibleImageTest.java index 689a222408..28af05adaf 100644 --- a/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ReproducibleImageTest.java +++ b/jib-core/src/integration-test/java/com/google/cloud/tools/jib/api/ReproducibleImageTest.java @@ -72,7 +72,7 @@ public static void createImage() imageTar = new File(imageLocation.getRoot(), "image.tar"); Containerizer containerizer = - Containerizer.to(TarImage.named("jib-core/reproducible").saveTo(imageTar.toPath())); + Containerizer.to(TarImage.at(imageTar.toPath()).named("jib-core/reproducible")); Jib.fromScratch() .setEntrypoint("echo", "Hello World") diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java b/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java index 119b38ae4e..5e8b2e4706 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/api/Containerizer.java @@ -100,12 +100,18 @@ public static Containerizer to(DockerDaemonImage dockerDaemonImage) { * @return a new {@link Containerizer} */ public static Containerizer to(TarImage tarImage) { + if (!tarImage.getImageReference().isPresent()) { + throw new IllegalArgumentException( + "Image name must be set when building a TarImage; use TarImage#named(...) to set the name" + + " of the target image"); + } + ImageConfiguration imageConfiguration = - ImageConfiguration.builder(tarImage.getImageReference()).build(); + ImageConfiguration.builder(tarImage.getImageReference().get()).build(); Function stepsRunnerFactory = buildConfiguration -> - StepsRunner.begin(buildConfiguration).tarBuildSteps(tarImage.getOutputFile()); + StepsRunner.begin(buildConfiguration).tarBuildSteps(tarImage.getPath()); return new Containerizer( DESCRIPTION_FOR_TARBALL, imageConfiguration, stepsRunnerFactory, false); diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/api/TarImage.java b/jib-core/src/main/java/com/google/cloud/tools/jib/api/TarImage.java index fbb0284b86..34da7112e3 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/api/TarImage.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/api/TarImage.java @@ -17,6 +17,8 @@ package com.google.cloud.tools.jib.api; import java.nio.file.Path; +import java.util.Optional; +import javax.annotation.Nullable; /** * Builds to a tarball archive. @@ -24,73 +26,59 @@ *

Usage example: * *

{@code
- * TarImage tarImage = TarImage.named("myimage")
- *                             .saveTo(Paths.get("image.tar"));
+ * TarImage tarImage = TarImage.at(Paths.get("image.tar"))
+ *                             .named("myimage");
  * }
*/ public class TarImage { - /** Finishes constructing a {@link TarImage}. */ - public static class Builder { - - private final ImageReference imageReference; + /** + * Constructs a {@link TarImage} with the specified path. + * + * @param path the path to the tarball archive + * @return a new {@link TarImage} + */ + public static TarImage at(Path path) { + return new TarImage(path); + } - private Builder(ImageReference imageReference) { - this.imageReference = imageReference; - } + private final Path path; + @Nullable private ImageReference imageReference; - /** - * Sets the output file to save the tarball archive to. - * - * @param outputFile the output file - * @return a new {@link TarImage} - */ - public TarImage saveTo(Path outputFile) { - return new TarImage(imageReference, outputFile); - } + /** Instantiate with {@link #at}. */ + private TarImage(Path path) { + this.path = path; } /** - * Configures the output tarball archive with an image reference. This image reference will be the - * name of the image if loaded into the Docker daemon. + * Sets the name of the image. This is the name that shows up when the tar is loaded by the Docker + * daemon. * * @param imageReference the image reference - * @return a {@link Builder} to finish constructing a new {@link TarImage} + * @return this */ - public static Builder named(ImageReference imageReference) { - return new Builder(imageReference); + public TarImage named(ImageReference imageReference) { + this.imageReference = imageReference; + return this; } /** - * Configures the output tarball archive with an image reference to set as its tag. + * Sets the name of the image. This is the name that shows up when the tar is loaded by the Docker + * daemon. * * @param imageReference the image reference - * @return a {@link Builder} to finish constructing a new {@link TarImage} + * @return this * @throws InvalidImageReferenceException if {@code imageReference} is not a valid image reference */ - public static Builder named(String imageReference) throws InvalidImageReferenceException { + public TarImage named(String imageReference) throws InvalidImageReferenceException { return named(ImageReference.parse(imageReference)); } - private final ImageReference imageReference; - private final Path outputFile; - - /** Instantiate with {@link #named}. */ - private TarImage(ImageReference imageReference, Path outputFile) { - this.imageReference = imageReference; - this.outputFile = outputFile; - } - - /** - * Gets the output file to save the tarball archive to. - * - * @return the output file - */ - Path getOutputFile() { - return outputFile; + Path getPath() { + return path; } - ImageReference getImageReference() { - return imageReference; + Optional getImageReference() { + return Optional.ofNullable(imageReference); } } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java index a2a8c22090..f439570efb 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/api/ContainerizerTest.java @@ -40,7 +40,7 @@ public void testTo() throws CacheDirectoryCreationException { DockerDaemonImage dockerDaemonImage = DockerDaemonImage.named(ImageReference.of(null, "repository", null)); TarImage tarImage = - TarImage.named(ImageReference.of(null, "repository", null)).saveTo(Paths.get("ignored")); + TarImage.at(Paths.get("ignored")).named(ImageReference.of(null, "repository", null)); verifyTo(Containerizer.to(registryImage)); verifyTo(Containerizer.to(dockerDaemonImage)); @@ -119,7 +119,7 @@ public void testGetImageConfiguration_dockerDaemonImage() throws InvalidImageRef @Test public void testGetImageConfiguration_tarImage() throws InvalidImageReferenceException { Containerizer containerizer = - Containerizer.to(TarImage.named("tar/image").saveTo(Paths.get("output/file"))); + Containerizer.to(TarImage.at(Paths.get("output/file")).named("tar/image")); ImageConfiguration imageConfiguration = containerizer.getImageConfiguration(); Assert.assertEquals("tar/image", imageConfiguration.getImage().toString()); diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/api/TarImageTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/api/TarImageTest.java index 321870b2d3..834ee60316 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/api/TarImageTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/api/TarImageTest.java @@ -24,9 +24,16 @@ public class TarImageTest { @Test - public void testGetters() throws InvalidImageReferenceException { - TarImage tarImage = TarImage.named("tar/image").saveTo(Paths.get("output/file")); - Assert.assertEquals("tar/image", tarImage.getImageReference().toString()); - Assert.assertEquals(Paths.get("output/file"), tarImage.getOutputFile()); + public void testGetters_bothSet() throws InvalidImageReferenceException { + TarImage tarImage = TarImage.at(Paths.get("output/file")).named("tar/image"); + Assert.assertEquals("tar/image", tarImage.getImageReference().get().toString()); + Assert.assertEquals(Paths.get("output/file"), tarImage.getPath()); + } + + @Test + public void testGetters_nameMissing() { + TarImage tarImage = TarImage.at(Paths.get("output/file")); + Assert.assertFalse(tarImage.getImageReference().isPresent()); + Assert.assertEquals(Paths.get("output/file"), tarImage.getPath()); } } diff --git a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java index 8764568f7d..8f1237b16e 100644 --- a/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java +++ b/jib-plugins-common/src/main/java/com/google/cloud/tools/jib/plugins/common/PluginConfigurationProcessor.java @@ -108,7 +108,7 @@ public static JibBuildRunner createJibBuildRunnerForTarImage( Path tarImagePath = projectProperties.getOutputDirectory().resolve("jib-image.tar"); ImageReference targetImageReference = getGeneratedTargetDockerTag(rawConfiguration, projectProperties, helpfulSuggestions); - TarImage targetImage = TarImage.named(targetImageReference).saveTo(tarImagePath); + TarImage targetImage = TarImage.at(tarImagePath).named(targetImageReference); Containerizer containerizer = Containerizer.to(targetImage); JibContainerBuilder jibContainerBuilder =