Skip to content

Commit

Permalink
Adjust TarImage API in preparation for local base image support (Goog…
Browse files Browse the repository at this point in the history
  • Loading branch information
TadCordle authored Aug 22, 2019
1 parent 50bc8f0 commit 618f4a3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 59 deletions.
2 changes: 2 additions & 0 deletions jib-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuildConfiguration, StepsRunner> stepsRunnerFactory =
buildConfiguration ->
StepsRunner.begin(buildConfiguration).tarBuildSteps(tarImage.getOutputFile());
StepsRunner.begin(buildConfiguration).tarBuildSteps(tarImage.getPath());

return new Containerizer(
DESCRIPTION_FOR_TARBALL, imageConfiguration, stepsRunnerFactory, false);
Expand Down
76 changes: 32 additions & 44 deletions jib-core/src/main/java/com/google/cloud/tools/jib/api/TarImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,68 @@
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.
*
* <p>Usage example:
*
* <pre>{@code
* TarImage tarImage = TarImage.named("myimage")
* .saveTo(Paths.get("image.tar"));
* TarImage tarImage = TarImage.at(Paths.get("image.tar"))
* .named("myimage");
* }</pre>
*/
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<ImageReference> getImageReference() {
return Optional.ofNullable(imageReference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 618f4a3

Please sign in to comment.