Skip to content

Commit

Permalink
Add imagePushed field to jib-image.json to note registry push when ji…
Browse files Browse the repository at this point in the history
…b.skipExistingImages option is in use.
  • Loading branch information
wwadge committed Apr 11, 2022
1 parent 4d9336b commit bfa02f7
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ 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...
Assert.assertTrue(image1.isImagePushed());
// ...while the second one was skipped
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)
&& 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 @@ -576,7 +576,9 @@ private Future<BuildResult> pushImage(
return manifestPushResults.isEmpty()
? new BuildResult(
results.manifestCheckResult.get().get().getDigest(),
Verify.verifyNotNull(containerConfigPushResult).get().getDigest())
Verify.verifyNotNull(containerConfigPushResult).get().getDigest(),
!(JibSystemProperties.skipExistingImages()
&& results.manifestCheckResult.get().isPresent()))
// 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.assertEquals(true, 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,16 +47,17 @@ 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);

Assert.assertEquals(container1, container2);
Assert.assertEquals(container1.hashCode(), container2.hashCode());
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());

Assert.assertEquals(ImmutableList.of("latest", "tag"), output.getTags());
}
Expand Down

0 comments on commit bfa02f7

Please sign in to comment.