Search in sources :

Example 11 with ImageReference

use of com.google.cloud.tools.jib.api.ImageReference in project jib by GoogleContainerTools.

the class PluginConfigurationProcessor method createJibBuildRunnerForTarImage.

/**
 * Generate a runner for image builds to tar file.
 *
 * @param rawConfiguration the raw configuration from the plugin
 * @param inferredAuthProvider the plugin specific auth provider
 * @param projectProperties an plugin specific implementation of {@link ProjectProperties}
 * @param globalConfig the Jib global config
 * @param helpfulSuggestions a plugin specific instance of {@link HelpfulSuggestions}
 * @return new {@link JibBuildRunner} to execute a build
 * @throws InvalidImageReferenceException if the image reference is invalid
 * @throws MainClassInferenceException if a main class could not be found
 * @throws InvalidAppRootException if the specific path for application root is invalid
 * @throws IOException if an error occurs creating the container builder
 * @throws InvalidWorkingDirectoryException if the working directory specified for the build is
 *     invalid
 * @throws InvalidPlatformException if there exists a {@link PlatformConfiguration} in the
 *     specified platforms list that is missing required fields or has invalid values
 * @throws InvalidContainerVolumeException if a specific container volume is invalid
 * @throws IncompatibleBaseImageJavaVersionException if the base image java version cannot support
 *     this build
 * @throws NumberFormatException if a string to number conversion operation fails
 * @throws InvalidContainerizingModeException if an invalid {@link ContainerizingMode} was
 *     specified
 * @throws InvalidFilesModificationTimeException if configured modification time could not be
 *     parsed
 * @throws InvalidCreationTimeException if configured creation time could not be parsed
 * @throws JibPluginExtensionException if an error occurred while running plugin extensions
 * @throws ExtraDirectoryNotFoundException if the extra directory specified for the build is not
 *     found
 */
public static JibBuildRunner createJibBuildRunnerForTarImage(RawConfiguration rawConfiguration, InferredAuthProvider inferredAuthProvider, ProjectProperties projectProperties, GlobalConfig globalConfig, HelpfulSuggestions helpfulSuggestions) throws InvalidImageReferenceException, MainClassInferenceException, InvalidAppRootException, IOException, InvalidWorkingDirectoryException, InvalidPlatformException, InvalidContainerVolumeException, IncompatibleBaseImageJavaVersionException, NumberFormatException, InvalidContainerizingModeException, InvalidFilesModificationTimeException, InvalidCreationTimeException, JibPluginExtensionException, ExtraDirectoryNotFoundException {
    ImageReference targetImageReference = getGeneratedTargetDockerTag(rawConfiguration, projectProperties, helpfulSuggestions);
    TarImage targetImage = TarImage.at(rawConfiguration.getTarOutputPath()).named(targetImageReference);
    Containerizer containerizer = Containerizer.to(targetImage);
    Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
    JibContainerBuilder jibContainerBuilder = processCommonConfiguration(rawConfiguration, inferredAuthProvider, projectProperties, containerizer);
    JibContainerBuilder updatedContainerBuilder = projectProperties.runPluginExtensions(rawConfiguration.getPluginExtensions(), jibContainerBuilder);
    return JibBuildRunner.forBuildTar(updatedContainerBuilder, containerizer, projectProperties::log, helpfulSuggestions, rawConfiguration.getTarOutputPath()).writeImageDigest(rawConfiguration.getDigestOutputPath()).writeImageId(rawConfiguration.getImageIdOutputPath()).writeImageJson(rawConfiguration.getImageJsonOutputPath());
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) TarImage(com.google.cloud.tools.jib.api.TarImage) Containerizer(com.google.cloud.tools.jib.api.Containerizer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 12 with ImageReference

use of com.google.cloud.tools.jib.api.ImageReference in project jib by GoogleContainerTools.

the class ConfigurationPropertyValidatorTest method testGetGeneratedTargetDockerTag.

@Test
public void testGetGeneratedTargetDockerTag() throws InvalidImageReferenceException {
    HelpfulSuggestions helpfulSuggestions = new HelpfulSuggestions("", "", "to", "--to", "build.txt");
    // Target configured
    ProjectProperties mockProjectProperties = mock(ProjectProperties.class);
    when(mockProjectProperties.getName()).thenReturn("project-name");
    when(mockProjectProperties.getVersion()).thenReturn("project-version");
    ImageReference result = ConfigurationPropertyValidator.getGeneratedTargetDockerTag("a/b:c", mockProjectProperties, helpfulSuggestions);
    assertThat(result.getRepository()).isEqualTo("a/b");
    assertThat(result.getTag()).hasValue("c");
    verify(mockLogger, never()).accept(LogEvent.lifecycle(any()));
    // Target not configured
    result = ConfigurationPropertyValidator.getGeneratedTargetDockerTag(null, mockProjectProperties, helpfulSuggestions);
    assertThat(result.getRepository()).isEqualTo("project-name");
    assertThat(result.getTag()).hasValue("project-version");
    verify(mockProjectProperties).log(LogEvent.lifecycle("Tagging image with generated image reference project-name:project-version. If you'd " + "like to specify a different tag, you can set the to parameter in your " + "build.txt, or use the --to=<MY IMAGE> commandline flag."));
    // Generated tag invalid
    when(mockProjectProperties.getName()).thenReturn("%#&///*@(");
    when(mockProjectProperties.getVersion()).thenReturn("%$#//&*@($");
    assertThrows(InvalidImageReferenceException.class, () -> ConfigurationPropertyValidator.getGeneratedTargetDockerTag(null, mockProjectProperties, helpfulSuggestions));
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) Test(org.junit.Test)

Example 13 with ImageReference

use of com.google.cloud.tools.jib.api.ImageReference in project geronimo-arthur by apache.

the class ImageMojo method createContainer.

@Override
protected Containerizer createContainer() throws InvalidImageReferenceException {
    final ImageReference reference = ImageReference.parse(to);
    final RegistryImage image = RegistryImage.named(reference);
    registerCredentials(reference, image);
    return Containerizer.to(image);
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage)

Example 14 with ImageReference

use of com.google.cloud.tools.jib.api.ImageReference in project jib by google.

the class JibBuildRunnerTest method testBuildImage_writesImageJson.

@Test
public void testBuildImage_writesImageJson() throws Exception {
    final ImageReference targetImageReference = ImageReference.parse("gcr.io/distroless/java:11");
    final String imageId = "sha256:61bb3ec31a47cb730eb58a38bbfa813761a51dca69d10e39c24c3d00a7b2c7a9";
    final String digest = "sha256:3f1be7e19129edb202c071a659a4db35280ab2bb1a16f223bfd5d1948657b6fc";
    final Set<String> tags = ImmutableSet.of("latest", "0.1.41-69d10e-20200116T101403");
    final Path outputPath = temporaryFolder.newFile("jib-image.json").toPath();
    Mockito.when(mockJibContainer.getTargetImage()).thenReturn(targetImageReference);
    Mockito.when(mockJibContainer.getImageId()).thenReturn(DescriptorDigest.fromDigest(imageId));
    Mockito.when(mockJibContainer.getDigest()).thenReturn(DescriptorDigest.fromDigest(digest));
    Mockito.when(mockJibContainer.getTags()).thenReturn(tags);
    Mockito.when(mockJibContainerBuilder.containerize(mockContainerizer)).thenReturn(mockJibContainer);
    Mockito.when(mockJibContainer.isImagePushed()).thenReturn(true);
    testJibBuildRunner.writeImageJson(outputPath).runBuild();
    final String outputJson = new String(Files.readAllBytes(outputPath), StandardCharsets.UTF_8);
    final ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJson(outputJson);
    Assert.assertEquals(targetImageReference.toString(), metadataOutput.getImage());
    Assert.assertEquals(imageId, metadataOutput.getImageId());
    Assert.assertEquals(digest, metadataOutput.getImageDigest());
    Assert.assertEquals(tags, ImmutableSet.copyOf(metadataOutput.getTags()));
    Assert.assertTrue(metadataOutput.isImagePushed());
}
Also used : Path(java.nio.file.Path) ImageReference(com.google.cloud.tools.jib.api.ImageReference) Test(org.junit.Test)

Example 15 with ImageReference

use of com.google.cloud.tools.jib.api.ImageReference in project jib by google.

the class PluginConfigurationProcessor method getJavaContainerBuilderWithBaseImage.

/**
 * Returns a {@link JavaContainerBuilder} with the correctly parsed base image configuration.
 *
 * @param rawConfiguration contains the base image configuration
 * @param projectProperties used for providing additional information
 * @param inferredAuthProvider provides inferred auths for registry images
 * @return a new {@link JavaContainerBuilder} with the configured base image
 * @throws IncompatibleBaseImageJavaVersionException when the Java version in the base image is
 *     incompatible with the Java version of the application to be containerized
 * @throws InvalidImageReferenceException if the base image configuration can't be parsed
 * @throws FileNotFoundException if a credential helper can't be found
 */
@VisibleForTesting
static JavaContainerBuilder getJavaContainerBuilderWithBaseImage(RawConfiguration rawConfiguration, ProjectProperties projectProperties, InferredAuthProvider inferredAuthProvider) throws IncompatibleBaseImageJavaVersionException, InvalidImageReferenceException, FileNotFoundException {
    // Use image configuration as-is if it's a local base image
    Optional<String> image = rawConfiguration.getFromImage();
    String baseImageConfig = image.isPresent() ? image.get() : getDefaultBaseImage(projectProperties);
    if (baseImageConfig.startsWith(Jib.TAR_IMAGE_PREFIX)) {
        return JavaContainerBuilder.from(baseImageConfig);
    }
    // Verify Java version is compatible
    List<String> splits = Splitter.on("://").splitToList(baseImageConfig);
    String prefixRemoved = splits.get(splits.size() - 1);
    int javaVersion = projectProperties.getMajorJavaVersion();
    if (isKnownJava8Image(prefixRemoved) && javaVersion > 8) {
        throw new IncompatibleBaseImageJavaVersionException(8, javaVersion);
    }
    if (isKnownJava11Image(prefixRemoved) && javaVersion > 11) {
        throw new IncompatibleBaseImageJavaVersionException(11, javaVersion);
    }
    if (isKnownJava17Image(prefixRemoved) && javaVersion > 17) {
        throw new IncompatibleBaseImageJavaVersionException(17, javaVersion);
    }
    ImageReference baseImageReference = ImageReference.parse(prefixRemoved);
    if (baseImageConfig.startsWith(Jib.DOCKER_DAEMON_IMAGE_PREFIX)) {
        DockerDaemonImage dockerDaemonImage = DockerDaemonImage.named(baseImageReference).setDockerEnvironment(rawConfiguration.getDockerEnvironment());
        Optional<Path> dockerExecutable = rawConfiguration.getDockerExecutable();
        if (dockerExecutable.isPresent()) {
            dockerDaemonImage.setDockerExecutable(dockerExecutable.get());
        }
        return JavaContainerBuilder.from(dockerDaemonImage);
    }
    RegistryImage baseImage = RegistryImage.named(baseImageReference);
    configureCredentialRetrievers(rawConfiguration, projectProperties, baseImage, baseImageReference, PropertyNames.FROM_AUTH_USERNAME, PropertyNames.FROM_AUTH_PASSWORD, rawConfiguration.getFromAuth(), inferredAuthProvider, rawConfiguration.getFromCredHelper());
    return JavaContainerBuilder.from(baseImage);
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ImageReference(com.google.cloud.tools.jib.api.ImageReference) DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ImageReference (com.google.cloud.tools.jib.api.ImageReference)46 Test (org.junit.Test)26 ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)22 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)22 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)18 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)14 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)12 Image (com.google.cloud.tools.jib.image.Image)12 Path (java.nio.file.Path)12 RegistryImage (com.google.cloud.tools.jib.api.RegistryImage)10 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)10 Containerizer (com.google.cloud.tools.jib.api.Containerizer)9 DockerDaemonImage (com.google.cloud.tools.jib.api.DockerDaemonImage)9 ImagesAndRegistryClient (com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient)8 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)7 TarImage (com.google.cloud.tools.jib.api.TarImage)6 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)6 List (java.util.List)6 Credential (com.google.cloud.tools.jib.api.Credential)4 LogEvent (com.google.cloud.tools.jib.api.LogEvent)4