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

[jib-cli] Add option to create an output JSON file that contains image metadata for the built image #3187

Merged
merged 8 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions jib-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.

### Added

- Added `--image-metadata-out` option to specify JSON output file that should contain image metadata (image ID, digest, and tags) after build is complete. ([#3187](https://github.com/GoogleContainerTools/jib/pull/3187))

### Changed

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.jib.cli;

import com.google.cloud.tools.jib.api.Containerizer;
import com.google.cloud.tools.jib.api.JibContainer;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.cli.buildfile.BuildFiles;
Expand Down Expand Up @@ -131,7 +132,8 @@ public Integer call() {
GlobalConfig globalConfig = GlobalConfig.readConfig();
Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);

containerBuilder.containerize(containerizer);
JibContainer jibContainer = containerBuilder.containerize(containerizer);
JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
} catch (Exception ex) {
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ private static class FromUsernamePassword {
@SuppressWarnings("NullAway.Init") // initialized by picocli
private boolean serialize;

@CommandLine.Option(
names = "--image-metadata-out",
paramLabel = "<path-to-json>",
description =
"path to the json file that should contain image metadata (for example, digest, id and tags) after build is"
+ "complete")
@SuppressWarnings("NullAway.Init") // initialized by picocli
private Path imageJsonPath;

public Verbosity getVerbosity() {
return Verify.verifyNotNull(verbosity);
}
Expand Down Expand Up @@ -400,6 +409,15 @@ public List<String> getAdditionalTags() {
return additionalTags;
}

/**
* Returns the full path to the image metadata json file, if provided.
*
* @return optional value of path to image json file
*/
public Optional<Path> getImageJsonPath() {
return Optional.ofNullable(imageJsonPath);
}

/** Validates parameters defined in this class that could not be done declaratively. */
public void validate() {
if (targetImage.startsWith(TAR_IMAGE_PREFIX) && name == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.tools.jib.cli;

import com.google.cloud.tools.jib.api.Containerizer;
import com.google.cloud.tools.jib.api.JibContainer;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.api.Ports;
Expand Down Expand Up @@ -202,7 +203,8 @@ public Integer call() {
GlobalConfig globalConfig = GlobalConfig.readConfig();
Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);

containerBuilder.containerize(containerizer);
JibContainer jibContainer = containerBuilder.containerize(containerizer);
JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
} catch (Exception ex) {
JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
return 1;
Expand Down
25 changes: 25 additions & 0 deletions jib-cli/src/main/java/com/google/cloud/tools/jib/cli/JibCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.cloud.tools.jib.api.JibContainer;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.plugins.common.ImageMetadataOutput;
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -67,6 +74,24 @@ static void logTerminatingException(
+ "\u001B[0m");
}

/**
* Writes image details (imageId, digest, tags, etc.) to a json file, if the path to the json is
* provided.
*
* @param imageJsonOutputPath optional path to json file (for example,
* path/to/json/jib-image.json)
* @param jibContainer the {@link JibContainer} to derive image details from
* @throws IOException if error occurs when writing to the json file.
*/
static void writeImageJson(Optional<Path> imageJsonOutputPath, JibContainer jibContainer)
throws IOException {
if (imageJsonOutputPath.isPresent()) {
ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
Files.write(
imageJsonOutputPath.get(), metadataOutput.toJson().getBytes(StandardCharsets.UTF_8));
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* The magic starts here.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void testParse_defaults() {
assertThat(commonCliOptions.isStacktrace()).isFalse();
assertThat(commonCliOptions.getHttpTrace()).isEqualTo(HttpTraceLevel.off);
assertThat(commonCliOptions.isSerialize()).isFalse();
assertThat(commonCliOptions.getImageJsonPath()).isEmpty();
}

@Test
Expand Down Expand Up @@ -103,6 +104,7 @@ public void testParse_shortFormParams() {
assertThat(commonCliOptions.isStacktrace()).isFalse();
assertThat(commonCliOptions.getHttpTrace()).isEqualTo(HttpTraceLevel.off);
assertThat(commonCliOptions.isSerialize()).isFalse();
assertThat(commonCliOptions.getImageJsonPath()).isEmpty();
}

@Test
Expand All @@ -125,7 +127,8 @@ public void testParse_longFormParams() {
"--verbosity=info",
"--stacktrace",
"--http-trace",
"--serialize");
"--serialize",
"--image-metadata-out=path/to/json/jib-image.json");
CommonCliOptions commonCliOptions = buildCommand.commonCliOptions;
assertThat(commonCliOptions.getTargetImage()).isEqualTo("test-image-ref");
assertThat(commonCliOptions.getUsernamePassword()).isEmpty();
Expand All @@ -149,6 +152,8 @@ public void testParse_longFormParams() {
assertThat(commonCliOptions.isStacktrace()).isTrue();
assertThat(commonCliOptions.getHttpTrace()).isEqualTo(HttpTraceLevel.config);
assertThat(commonCliOptions.isSerialize()).isTrue();
assertThat(commonCliOptions.getImageJsonPath())
.hasValue(Paths.get("path/to/json/jib-image.json"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testParse_defaults() {
assertThat(commonCliOptions.isStacktrace()).isFalse();
assertThat(commonCliOptions.getHttpTrace()).isEqualTo(HttpTraceLevel.off);
assertThat(commonCliOptions.isSerialize()).isFalse();
assertThat(commonCliOptions.getImageJsonPath()).isEmpty();
assertThat(jarCommand.getFrom()).isEmpty();
assertThat(jarCommand.getJvmFlags()).isEmpty();
assertThat(jarCommand.getExposedPorts()).isEmpty();
Expand Down Expand Up @@ -119,6 +120,7 @@ public void testParse_shortFormParams() {
assertThat(commonCliOptions.isStacktrace()).isFalse();
assertThat(commonCliOptions.getHttpTrace()).isEqualTo(HttpTraceLevel.off);
assertThat(commonCliOptions.isSerialize()).isFalse();
assertThat(commonCliOptions.getImageJsonPath()).isEmpty();
}

@Test
Expand All @@ -138,6 +140,7 @@ public void testParse_longFormParams() {
"--stacktrace",
"--http-trace",
"--serialize",
"--image-metadata-out=path/to/json/jib-image.json",
"my-app.jar");
CommonCliOptions commonCliOptions = jarCommand.commonCliOptions;
assertThat(commonCliOptions.getTargetImage()).isEqualTo("test-image-ref");
Expand All @@ -157,6 +160,8 @@ public void testParse_longFormParams() {
assertThat(commonCliOptions.isStacktrace()).isTrue();
assertThat(commonCliOptions.getHttpTrace()).isEqualTo(HttpTraceLevel.config);
assertThat(commonCliOptions.isSerialize()).isTrue();
assertThat(commonCliOptions.getImageJsonPath())
.hasValue(Paths.get("path/to/json/jib-image.json"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,41 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.google.cloud.tools.jib.api.DescriptorDigest;
import com.google.cloud.tools.jib.api.ImageReference;
import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
import com.google.cloud.tools.jib.api.JibContainer;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.plugins.common.ImageMetadataOutput;
import com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.DigestException;
import java.util.Optional;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class JibCliTest {

@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Mock private JibContainer mockJibContainer;

@Test
public void testConfigureHttpLogging() {
Logger logger = JibCli.configureHttpLogging(Level.ALL);
Expand Down Expand Up @@ -68,4 +91,26 @@ public void testLogTerminatingException_stackTrace() {
.log(LogEvent.Level.ERROR, "\u001B[31;1mjava.io.IOException: test error message\u001B[0m");
verifyNoMoreInteractions(logger);
}

@Test
public void testWriteImageJson()
throws InvalidImageReferenceException, IOException, DigestException {
String imageId = "sha256:61bb3ec31a47cb730eb58a38bbfa813761a51dca69d10e39c24c3d00a7b2c7a9";
String digest = "sha256:3f1be7e19129edb202c071a659a4db35280ab2bb1a16f223bfd5d1948657b6fc";
when(mockJibContainer.getTargetImage()).thenReturn(ImageReference.parse("adoptopenjdk:8-jre"));
when(mockJibContainer.getImageId()).thenReturn(DescriptorDigest.fromDigest(imageId));
when(mockJibContainer.getDigest()).thenReturn(DescriptorDigest.fromDigest(digest));
when(mockJibContainer.getTags()).thenReturn(ImmutableSet.of("latest", "tag-2"));

Path outputPath = temporaryFolder.getRoot().toPath().resolve("jib-image.json");
JibCli.writeImageJson(Optional.of(outputPath), mockJibContainer);

String outputJson = new String(Files.readAllBytes(outputPath), StandardCharsets.UTF_8);
ImageMetadataOutput metadataOutput =
JsonTemplateMapper.readJson(outputJson, ImageMetadataOutput.class);
assertThat(metadataOutput.getImage()).isEqualTo("adoptopenjdk:8-jre");
assertThat(metadataOutput.getImageId()).isEqualTo(imageId);
assertThat(metadataOutput.getImageDigest()).isEqualTo(digest);
assertThat(metadataOutput.getTags()).containsExactly("latest", "tag-2");
}
}