Search in sources :

Example 6 with RegistryException

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

the class PullBaseImageStep method tryMirrors.

@VisibleForTesting
Optional<ImagesAndRegistryClient> tryMirrors(BuildContext buildContext, ProgressEventDispatcher.Factory progressDispatcherFactory) throws LayerCountMismatchException, BadContainerConfigurationFormatException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    Collection<Map.Entry<String, String>> mirrorEntries = buildContext.getRegistryMirrors().entries();
    try (ProgressEventDispatcher progressDispatcher1 = progressDispatcherFactory.create("trying mirrors", mirrorEntries.size());
        TimerEventDispatcher ignored1 = new TimerEventDispatcher(eventHandlers, "trying mirrors")) {
        for (Map.Entry<String, String> entry : mirrorEntries) {
            String registry = entry.getKey();
            String mirror = entry.getValue();
            eventHandlers.dispatch(LogEvent.debug("mirror config: " + registry + " --> " + mirror));
            if (!buildContext.getBaseImageConfiguration().getImageRegistry().equals(registry)) {
                progressDispatcher1.dispatchProgress(1);
                continue;
            }
            eventHandlers.dispatch(LogEvent.info("trying mirror " + mirror + " for the base image"));
            try (ProgressEventDispatcher progressDispatcher2 = progressDispatcher1.newChildProducer().create("trying mirror " + mirror, 2)) {
                RegistryClient registryClient = buildContext.newBaseImageRegistryClientFactory(mirror).newRegistryClient();
                List<Image> images = pullPublicImages(registryClient, progressDispatcher2);
                eventHandlers.dispatch(LogEvent.info("pulled manifest from mirror " + mirror));
                return Optional.of(new ImagesAndRegistryClient(images, registryClient));
            } catch (IOException | RegistryException ex) {
                // Ignore errors from this mirror and continue.
                eventHandlers.dispatch(LogEvent.debug("failed to get manifest from mirror " + mirror + ": " + ex.getMessage()));
            }
        }
        return Optional.empty();
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) IOException(java.io.IOException) Image(com.google.cloud.tools.jib.image.Image) RegistryException(com.google.cloud.tools.jib.api.RegistryException) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with RegistryException

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

the class JibBuildRunner method runBuild.

/**
 * Runs the Jib build.
 *
 * @return the built {@link JibContainer}
 * @throws BuildStepsExecutionException if another exception is thrown during the build
 * @throws IOException if an I/O exception occurs
 * @throws CacheDirectoryCreationException if the cache directory could not be created
 */
public JibContainer runBuild() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException {
    try {
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(startupMessage));
        JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(successMessage));
        // when an image is built, write out the digest and id
        if (imageDigestOutputPath != null) {
            String imageDigest = jibContainer.getDigest().toString();
            Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
        }
        if (imageIdOutputPath != null) {
            String imageId = jibContainer.getImageId().toString();
            Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
        }
        if (imageJsonOutputPath != null) {
            ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
            String imageJson = metadataOutput.toJson();
            Files.write(imageJsonOutputPath, imageJson.getBytes(StandardCharsets.UTF_8));
        }
        return jibContainer;
    } catch (HttpHostConnectException ex) {
        // Failed to connect to registry.
        throw new BuildStepsExecutionException(helpfulSuggestions.forHttpHostConnect(), ex);
    } catch (RegistryUnauthorizedException ex) {
        handleRegistryUnauthorizedException(ex, helpfulSuggestions);
    } catch (RegistryCredentialsNotSentException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forCredentialsNotSent(), ex);
    } catch (RegistryAuthenticationFailedException ex) {
        if (ex.getCause() instanceof ResponseException) {
            handleRegistryUnauthorizedException(new RegistryUnauthorizedException(ex.getServerUrl(), ex.getImageName(), (ResponseException) ex.getCause()), helpfulSuggestions);
        } else {
            // Unknown cause
            throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
        }
    } catch (UnknownHostException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forUnknownHost(), ex);
    } catch (InsecureRegistryException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forInsecureRegistry(), ex);
    } catch (RegistryException ex) {
        // keep null-away happy
        String message = Verify.verifyNotNull(ex.getMessage());
        throw new BuildStepsExecutionException(message, ex);
    } catch (ExecutionException ex) {
        String message = ex.getCause().getMessage();
        throw new BuildStepsExecutionException(message == null ? "(null exception message)" : message, ex.getCause());
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
    }
    throw new IllegalStateException("unreachable");
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) UnknownHostException(java.net.UnknownHostException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) JibContainer(com.google.cloud.tools.jib.api.JibContainer) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) RegistryException(com.google.cloud.tools.jib.api.RegistryException) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) RegistryCredentialsNotSentException(com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with RegistryException

use of com.google.cloud.tools.jib.api.RegistryException in project jkube by eclipse.

the class JibServiceUtil method buildContainer.

/**
 * Build container image using JIB
 *
 * @param jibContainerBuilder jib container builder object
 * @param image tarball for image
 * @param logger kit logger
 * @throws InterruptedException in case thread is interrupted
 */
public static void buildContainer(JibContainerBuilder jibContainerBuilder, TarImage image, KitLogger logger) throws InterruptedException {
    final ExecutorService jibBuildExecutor = Executors.newCachedThreadPool();
    try {
        jibContainerBuilder.setCreationTime(Instant.now());
        jibContainerBuilder.containerize(Containerizer.to(image).setAllowInsecureRegistries(true).setExecutorService(jibBuildExecutor).addEventHandler(LogEvent.class, log(logger)).addEventHandler(ProgressEvent.class, new ProgressEventHandler(logUpdate())));
        logUpdateFinished();
    } catch (CacheDirectoryCreationException | IOException | ExecutionException | RegistryException ex) {
        logger.error("Unable to build the image tarball: ", ex);
        throw new IllegalStateException(ex);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw ex;
    } finally {
        jibBuildExecutor.shutdown();
        jibBuildExecutor.awaitTermination(JIB_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) ProgressEventHandler(com.google.cloud.tools.jib.event.progress.ProgressEventHandler) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) ExecutionException(java.util.concurrent.ExecutionException) RegistryException(com.google.cloud.tools.jib.api.RegistryException)

Example 9 with RegistryException

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

the class BlobPusherIntegrationTest method testPush.

@Test
public void testPush() throws DigestException, IOException, RegistryException {
    Blob testBlob = Blobs.from("crepecake");
    // Known digest for 'crepecake'
    DescriptorDigest testBlobDigest = DescriptorDigest.fromHash("52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "testimage", httpClient).newRegistryClient();
    Assert.assertFalse(registryClient.pushBlob(testBlobDigest, testBlob, null, ignored -> {
    }));
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) DigestException(java.security.DigestException) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) Assert(org.junit.Assert) ClassRule(org.junit.ClassRule) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Blob(com.google.cloud.tools.jib.blob.Blob) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 10 with RegistryException

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

the class ManifestPusherIntegrationTest method testPush.

/**
 * Tests manifest pushing. This test is a comprehensive test of push and pull.
 */
@Test
public void testPush() throws DigestException, IOException, RegistryException {
    Blob testLayerBlob = Blobs.from("crepecake");
    // Known digest for 'crepecake'
    DescriptorDigest testLayerBlobDigest = DescriptorDigest.fromHash("52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
    Blob testContainerConfigurationBlob = Blobs.from("12345");
    DescriptorDigest testContainerConfigurationBlobDigest = DescriptorDigest.fromHash("5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5");
    // Creates a valid image manifest.
    V22ManifestTemplate expectedManifestTemplate = new V22ManifestTemplate();
    expectedManifestTemplate.addLayer(9, testLayerBlobDigest);
    expectedManifestTemplate.setContainerConfiguration(5, testContainerConfigurationBlobDigest);
    // Pushes the BLOBs.
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "testimage", httpClient).newRegistryClient();
    Assert.assertFalse(registryClient.pushBlob(testLayerBlobDigest, testLayerBlob, null, ignored -> {
    }));
    Assert.assertFalse(registryClient.pushBlob(testContainerConfigurationBlobDigest, testContainerConfigurationBlob, null, ignored -> {
    }));
    // Pushes the manifest.
    DescriptorDigest imageDigest = registryClient.pushManifest(expectedManifestTemplate, "latest");
    // Pulls the manifest.
    V22ManifestTemplate manifestTemplate = registryClient.pullManifest("latest", V22ManifestTemplate.class).getManifest();
    Assert.assertEquals(1, manifestTemplate.getLayers().size());
    Assert.assertEquals(testLayerBlobDigest, manifestTemplate.getLayers().get(0).getDigest());
    Assert.assertNotNull(manifestTemplate.getContainerConfiguration());
    Assert.assertEquals(testContainerConfigurationBlobDigest, manifestTemplate.getContainerConfiguration().getDigest());
    // Pulls the manifest by digest.
    V22ManifestTemplate manifestTemplateByDigest = registryClient.pullManifest(imageDigest.toString(), V22ManifestTemplate.class).getManifest();
    Assert.assertEquals(Digests.computeJsonDigest(manifestTemplate), Digests.computeJsonDigest(manifestTemplateByDigest));
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Digests(com.google.cloud.tools.jib.hash.Digests) HttpStatusCodes(com.google.api.client.http.HttpStatusCodes) ResponseException(com.google.cloud.tools.jib.http.ResponseException) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) DigestException(java.security.DigestException) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) Assert(org.junit.Assert) ClassRule(org.junit.ClassRule) Blob(com.google.cloud.tools.jib.blob.Blob) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Aggregations

RegistryException (com.google.cloud.tools.jib.api.RegistryException)23 IOException (java.io.IOException)15 Test (org.junit.Test)12 ExecutionException (java.util.concurrent.ExecutionException)9 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 CacheDirectoryCreationException (com.google.cloud.tools.jib.api.CacheDirectoryCreationException)7 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)6 Blob (com.google.cloud.tools.jib.blob.Blob)6 ImagesAndRegistryClient (com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient)6 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)6 DigestException (java.security.DigestException)6 ExecutorService (java.util.concurrent.ExecutorService)6 Assert (org.junit.Assert)6 InsecureRegistryException (com.google.cloud.tools.jib.api.InsecureRegistryException)4 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)4 Blobs (com.google.cloud.tools.jib.blob.Blobs)4 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)4 ProgressEvent (com.google.cloud.tools.jib.event.events.ProgressEvent)4 ProgressEventHandler (com.google.cloud.tools.jib.event.progress.ProgressEventHandler)4 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)4