Search in sources :

Example 1 with RegistryImage

use of com.google.cloud.tools.jib.api.RegistryImage in project fabric8-maven-plugin by fabric8io.

the class JibServiceUtil method pushImage.

/**
 * @param baseImage Base TarImage from where the image will be built.
 * @param targetImageName Full name of the target Image to be pushed to the registry
 * @param credential
 * @param logger
 */
private static void pushImage(TarImage baseImage, String targetImageName, Credential credential, Logger logger) throws InterruptedException {
    final ExecutorService jibBuildExecutor = Executors.newCachedThreadPool();
    try {
        RegistryImage targetImage = RegistryImage.named(targetImageName);
        if (credential != null && !credential.getUsername().isEmpty() && !credential.getPassword().isEmpty()) {
            targetImage.addCredential(credential.getUsername(), credential.getPassword());
        }
        Jib.from(baseImage).containerize(Containerizer.to(targetImage).setExecutorService(jibBuildExecutor).addEventHandler(LogEvent.class, log(logger)).addEventHandler(TimerEvent.class, new TimerEventHandler(logger::debug)).addEventHandler(ProgressEvent.class, new ProgressEventHandler(logUpdate())));
        logUpdateFinished();
    } catch (RegistryException | CacheDirectoryCreationException | InvalidImageReferenceException | IOException | ExecutionException e) {
        logger.error("Exception occurred while pushing the image: %s", targetImageName);
        throw new IllegalStateException(e.getMessage(), e);
    } catch (InterruptedException ex) {
        logger.error("Thread interrupted", ex);
        throw ex;
    } finally {
        jibBuildExecutor.shutdown();
        jibBuildExecutor.awaitTermination(JIB_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : LogEvent(com.google.cloud.tools.jib.api.LogEvent) TimerEventHandler(com.google.cloud.tools.jib.plugins.common.TimerEventHandler) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) IOException(java.io.IOException) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) RegistryException(com.google.cloud.tools.jib.api.RegistryException) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) ProgressEventHandler(com.google.cloud.tools.jib.event.progress.ProgressEventHandler) ExecutorService(java.util.concurrent.ExecutorService) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with RegistryImage

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

the class Containerizers method create.

private static Containerizer create(CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
    String imageSpec = commonCliOptions.getTargetImage();
    if (imageSpec.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
        // TODO: allow setting docker env and docker executable (along with path/env)
        return Containerizer.to(DockerDaemonImage.named(imageSpec.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
    }
    if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
        return Containerizer.to(TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, ""))).named(commonCliOptions.getName()));
    }
    ImageReference imageReference = ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
    Credentials.getToCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
    return Containerizer.to(registryImage);
}
Also used : DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) ImageReference(com.google.cloud.tools.jib.api.ImageReference) TAR_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.TAR_IMAGE_PREFIX) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) TarImage(com.google.cloud.tools.jib.api.TarImage) DefaultCredentialRetrievers(com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers) DOCKER_DAEMON_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.DOCKER_DAEMON_IMAGE_PREFIX) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) ProgressEventHandler(com.google.cloud.tools.jib.event.progress.ProgressEventHandler) FileNotFoundException(java.io.FileNotFoundException) REGISTRY_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.REGISTRY_IMAGE_PREFIX) LogEvent(com.google.cloud.tools.jib.api.LogEvent) List(java.util.List) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) ProgressDisplayGenerator(com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) ImageReference(com.google.cloud.tools.jib.api.ImageReference) DefaultCredentialRetrievers(com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage)

Example 3 with RegistryImage

use of com.google.cloud.tools.jib.api.RegistryImage in project quarkus by quarkusio.

the class JibProcessor method toRegistryImage.

private RegistryImage toRegistryImage(ImageReference imageReference, Optional<String> username, Optional<String> password) {
    CredentialRetrieverFactory credentialRetrieverFactory = CredentialRetrieverFactory.forImage(imageReference, log::info);
    RegistryImage registryImage = RegistryImage.named(imageReference);
    if (username.isPresent() && password.isPresent()) {
        registryImage.addCredential(username.get(), password.get());
    } else {
        registryImage.addCredentialRetriever(credentialRetrieverFactory.wellKnownCredentialHelpers());
        registryImage.addCredentialRetriever(credentialRetrieverFactory.dockerConfig());
    }
    return registryImage;
}
Also used : CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage)

Example 4 with RegistryImage

use of com.google.cloud.tools.jib.api.RegistryImage 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 5 with RegistryImage

use of com.google.cloud.tools.jib.api.RegistryImage 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

RegistryImage (com.google.cloud.tools.jib.api.RegistryImage)13 ImageReference (com.google.cloud.tools.jib.api.ImageReference)10 DockerDaemonImage (com.google.cloud.tools.jib.api.DockerDaemonImage)7 Containerizer (com.google.cloud.tools.jib.api.Containerizer)5 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)5 CredentialRetrieverFactory (com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory)5 DOCKER_DAEMON_IMAGE_PREFIX (com.google.cloud.tools.jib.api.Jib.DOCKER_DAEMON_IMAGE_PREFIX)4 REGISTRY_IMAGE_PREFIX (com.google.cloud.tools.jib.api.Jib.REGISTRY_IMAGE_PREFIX)4 TAR_IMAGE_PREFIX (com.google.cloud.tools.jib.api.Jib.TAR_IMAGE_PREFIX)4 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)4 TarImage (com.google.cloud.tools.jib.api.TarImage)4 DefaultCredentialRetrievers (com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers)4 ConsoleLogger (com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger)4 FileNotFoundException (java.io.FileNotFoundException)4 Paths (java.nio.file.Paths)4 LogEvent (com.google.cloud.tools.jib.api.LogEvent)3 ProgressEvent (com.google.cloud.tools.jib.event.events.ProgressEvent)3 ProgressEventHandler (com.google.cloud.tools.jib.event.progress.ProgressEventHandler)3 Jib (com.google.cloud.tools.jib.api.Jib)2 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)2