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

Add imagePushed field to jib-image.json #3641

Merged
merged 7 commits into from
May 26, 2022
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
Expand Up @@ -270,6 +270,10 @@ public void testSteps_forBuildToDockerRegistry_skipExistingDigest()
// Test that both images have the same properties.
Assert.assertEquals(image1.getDigest(), image2.getDigest());
Assert.assertEquals(image1.getImageId(), image2.getImageId());

// Test that the first image was pushed while the second one was skipped
Assert.assertTrue(image1.isImagePushed());
Assert.assertFalse(image2.isImagePushed());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ public class JibContainer {
private final DescriptorDigest imageDigest;
private final DescriptorDigest imageId;
private final Set<String> tags;
private final boolean imagePushed;

@VisibleForTesting
JibContainer(
ImageReference targetImage,
DescriptorDigest imageDigest,
DescriptorDigest imageId,
Set<String> tags) {
Set<String> tags,
boolean imagePushed) {
this.targetImage = targetImage;
this.imageDigest = imageDigest;
this.imageId = imageId;
this.tags = tags;
this.imagePushed = imagePushed;
}

static JibContainer from(BuildContext buildContext, BuildResult buildResult) {
ImageReference targetImage = buildContext.getTargetImageConfiguration().getImage();
DescriptorDigest imageDigest = buildResult.getImageDigest();
DescriptorDigest imageId = buildResult.getImageId();
Set<String> tags = buildContext.getAllTargetImageTags();
return new JibContainer(targetImage, imageDigest, imageId, tags);
return new JibContainer(targetImage, imageDigest, imageId, tags, buildResult.isImagePushed());
}

/**
Expand All @@ -59,6 +62,15 @@ public ImageReference getTargetImage() {
return targetImage;
}

/**
* Returns true if we pushed this image all the way to a registry.
*
* @return true if pushed.
*/
public boolean isImagePushed() {
return imagePushed;
}

/**
* Gets the digest of the registry image manifest built by Jib. This digest can be used to fetch a
* specific image from the registry in the form {@code myregistry/myimage@digest}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ static BuildResult fromImage(Image image, Class<? extends BuildableManifestTempl
targetFormat, containerConfigurationBlobDescriptor);
DescriptorDigest imageDigest = Digests.computeJsonDigest(manifestTemplate);
DescriptorDigest imageId = containerConfigurationBlobDescriptor.getDigest();
return new BuildResult(imageDigest, imageId);
return new BuildResult(imageDigest, imageId, false);
}

private final DescriptorDigest imageDigest;
private final DescriptorDigest imageId;
private final Boolean imagePushed;

BuildResult(DescriptorDigest imageDigest, DescriptorDigest imageId) {
BuildResult(DescriptorDigest imageDigest, DescriptorDigest imageId, boolean imagePushed) {
this.imageDigest = imageDigest;
this.imageId = imageId;
this.imagePushed = imagePushed;
}

public DescriptorDigest getImageDigest() {
Expand All @@ -65,6 +67,10 @@ public DescriptorDigest getImageId() {
return imageId;
}

public boolean isImagePushed() {
return imagePushed;
}

@Override
public int hashCode() {
return Objects.hash(imageDigest, imageId);
Expand All @@ -80,6 +86,7 @@ public boolean equals(Object other) {
}
BuildResult otherBuildResult = (BuildResult) other;
return imageDigest.equals(otherBuildResult.imageDigest)
&& imageId.equals(otherBuildResult.imageId);
&& imageId.equals(otherBuildResult.imageId)
wwadge marked this conversation as resolved.
Show resolved Hide resolved
&& imagePushed.equals(otherBuildResult.imagePushed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public BuildResult call() throws IOException, RegistryException {
eventHandlers.dispatch(LogEvent.info("Pushing manifest for " + imageQualifier + "..."));

registryClient.pushManifest(manifestTemplate, imageQualifier);
return new BuildResult(imageDigest, imageId);
return new BuildResult(imageDigest, imageId, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,16 @@ private Future<BuildResult> pushImage(
results.manifestCheckResult.get().isPresent()));

realizeFutures(manifestPushResults);

boolean imagePushed =
!(JibSystemProperties.skipExistingImages()
&& results.manifestCheckResult.get().isPresent());

return manifestPushResults.isEmpty()
? new BuildResult(
results.manifestCheckResult.get().get().getDigest(),
Verify.verifyNotNull(containerConfigPushResult).get().getDigest())
Verify.verifyNotNull(containerConfigPushResult).get().getDigest(),
imagePushed)
// Manifest pushers return the same BuildResult.
: manifestPushResults.get(0).get();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,54 +53,55 @@ public void setUp() throws DigestException, InvalidImageReferenceException {

@Test
public void testCreation() {
JibContainer container = new JibContainer(targetImage1, digest1, digest2, tags1);
JibContainer container = new JibContainer(targetImage1, digest1, digest2, tags1, true);

Assert.assertEquals(targetImage1, container.getTargetImage());
Assert.assertEquals(digest1, container.getDigest());
Assert.assertEquals(digest2, container.getImageId());
Assert.assertEquals(tags1, container.getTags());
Assert.assertTrue(container.isImagePushed());
}

@Test
public void testEquality() {
JibContainer container1 = new JibContainer(targetImage1, digest1, digest2, tags1);
JibContainer container2 = new JibContainer(targetImage1, digest1, digest2, tags1);
JibContainer container1 = new JibContainer(targetImage1, digest1, digest2, tags1, true);
JibContainer container2 = new JibContainer(targetImage1, digest1, digest2, tags1, true);

Assert.assertEquals(container1, container2);
Assert.assertEquals(container1.hashCode(), container2.hashCode());
}

@Test
public void testEquality_differentTargetImage() {
JibContainer container1 = new JibContainer(targetImage1, digest1, digest2, tags1);
JibContainer container2 = new JibContainer(targetImage2, digest1, digest2, tags1);
JibContainer container1 = new JibContainer(targetImage1, digest1, digest2, tags1, true);
JibContainer container2 = new JibContainer(targetImage2, digest1, digest2, tags1, true);

Assert.assertNotEquals(container1, container2);
Assert.assertNotEquals(container1.hashCode(), container2.hashCode());
}

@Test
public void testEquality_differentImageDigest() {
JibContainer container1 = new JibContainer(targetImage1, digest1, digest2, tags1);
JibContainer container2 = new JibContainer(targetImage1, digest2, digest2, tags1);
JibContainer container1 = new JibContainer(targetImage1, digest1, digest2, tags1, true);
JibContainer container2 = new JibContainer(targetImage1, digest2, digest2, tags1, true);

Assert.assertNotEquals(container1, container2);
Assert.assertNotEquals(container1.hashCode(), container2.hashCode());
}

@Test
public void testEquality_differentImageId() {
JibContainer container1 = new JibContainer(targetImage1, digest1, digest1, tags1);
JibContainer container2 = new JibContainer(targetImage1, digest1, digest2, tags1);
JibContainer container1 = new JibContainer(targetImage1, digest1, digest1, tags1, true);
JibContainer container2 = new JibContainer(targetImage1, digest1, digest2, tags1, true);

Assert.assertNotEquals(container1, container2);
Assert.assertNotEquals(container1.hashCode(), container2.hashCode());
}

@Test
public void testEquality_differentTags() {
JibContainer container1 = new JibContainer(targetImage1, digest1, digest1, tags1);
JibContainer container2 = new JibContainer(targetImage1, digest1, digest1, tags2);
JibContainer container1 = new JibContainer(targetImage1, digest1, digest1, tags1, true);
JibContainer container2 = new JibContainer(targetImage1, digest1, digest1, tags2, true);

Assert.assertNotEquals(container1, container2);
Assert.assertNotEquals(container1.hashCode(), container2.hashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ public void setUp() throws DigestException {

@Test
public void testCreated() {
BuildResult container = new BuildResult(digest1, id);
BuildResult container = new BuildResult(digest1, id, true);
Assert.assertEquals(digest1, container.getImageDigest());
Assert.assertEquals(id, container.getImageId());
Assert.assertTrue(container.isImagePushed());
}

@Test
public void testEquality() {
BuildResult container1 = new BuildResult(digest1, id);
BuildResult container2 = new BuildResult(digest1, id);
BuildResult container3 = new BuildResult(digest2, id);
BuildResult container1 = new BuildResult(digest1, id, true);
BuildResult container2 = new BuildResult(digest1, id, true);
BuildResult container3 = new BuildResult(digest2, id, true);
BuildResult container4 = new BuildResult(digest1, id, false);

Assert.assertEquals(container1, container2);
Assert.assertEquals(container1.hashCode(), container2.hashCode());
Assert.assertEquals(container1.hashCode(), container4.hashCode());
Assert.assertNotEquals(container1, container3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ public class ImageMetadataOutput implements JsonTemplate {
private final String imageId;
private final String imageDigest;
private final List<String> tags;
private final Boolean imagePushed;

@JsonCreator
ImageMetadataOutput(
@JsonProperty(value = "image", required = true) String image,
@JsonProperty(value = "imageId", required = true) String imageId,
@JsonProperty(value = "imageDigest", required = true) String imageDigest,
@JsonProperty(value = "tags", required = true) List<String> tags) {
@JsonProperty(value = "tags", required = true) List<String> tags,
@JsonProperty(value = "imagePushed", required = true) Boolean imagePushed) {
this.image = image;
this.imageId = imageId;
this.imageDigest = imageDigest;
this.tags = tags;
this.imagePushed = imagePushed;
}

@VisibleForTesting
Expand All @@ -74,11 +77,12 @@ public static ImageMetadataOutput fromJibContainer(JibContainer jibContainer) {
String image = jibContainer.getTargetImage().toString();
String imageId = jibContainer.getImageId().toString();
String imageDigest = jibContainer.getDigest().toString();
Boolean imagePushed = jibContainer.isImagePushed();

// Make sure tags always appear in a predictable way, by sorting them into a list
List<String> tags = ImmutableList.sortedCopyOf(jibContainer.getTags());

return new ImageMetadataOutput(image, imageId, imageDigest, tags);
return new ImageMetadataOutput(image, imageId, imageDigest, tags, imagePushed);
}

public String getImage() {
Expand All @@ -97,6 +101,10 @@ public List<String> getTags() {
return tags;
}

public Boolean isImagePushed() {
return imagePushed;
}

public String toJson() throws IOException {
return JsonTemplateMapper.toUtf8String(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class ImageMetadataOutputTest {
+ "\"sha256:61bb3ec31a47cb730eb58a38bbfa813761a51dca69d10e39c24c3d00a7b2c7a9\","
+ "\"imageDigest\":"
+ "\"sha256:3f1be7e19129edb202c071a659a4db35280ab2bb1a16f223bfd5d1948657b6fc\","
+ "\"tags\":[\"latest\",\"tag\"]"
+ "\"tags\":[\"latest\",\"tag\"],"
+ "\"imagePushed\":true"
+ "}";

@Test
Expand All @@ -43,6 +44,7 @@ public void testFromJson() throws IOException {
Assert.assertEquals(
"sha256:3f1be7e19129edb202c071a659a4db35280ab2bb1a16f223bfd5d1948657b6fc",
output.getImageDigest());
Assert.assertTrue(output.isImagePushed());
wwadge marked this conversation as resolved.
Show resolved Hide resolved

Assert.assertEquals(ImmutableList.of("latest", "tag"), output.getTags());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,6 @@ public void testBuildImage_writesImageJson() throws Exception {
Assert.assertEquals(imageId, metadataOutput.getImageId());
Assert.assertEquals(digest, metadataOutput.getImageDigest());
Assert.assertEquals(tags, ImmutableSet.copyOf(metadataOutput.getTags()));
Assert.assertTrue(metadataOutput.isImagePushed());
wwadge marked this conversation as resolved.
Show resolved Hide resolved
}
}