Search in sources :

Example 1 with RegistryException

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

the class JibServiceUtil method buildContainer.

static void buildContainer(JibContainerBuilder jibContainerBuilder, TarImage image, Logger logger) throws InterruptedException {
    final ExecutorService jibBuildExecutor = Executors.newCachedThreadPool();
    try {
        jibContainerBuilder.setCreationTime(Instant.now());
        jibContainerBuilder.containerize(Containerizer.to(image).setExecutorService(jibBuildExecutor).addEventHandler(LogEvent.class, log(logger)).addEventHandler(TimerEvent.class, new TimerEventHandler(logger::debug)).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) {
        logger.error("Thread interrupted", ex);
        throw ex;
    } finally {
        jibBuildExecutor.shutdown();
        jibBuildExecutor.awaitTermination(JIB_EXECUTOR_SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) LogEvent(com.google.cloud.tools.jib.api.LogEvent) TimerEventHandler(com.google.cloud.tools.jib.plugins.common.TimerEventHandler) 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) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionException(java.util.concurrent.ExecutionException) RegistryException(com.google.cloud.tools.jib.api.RegistryException)

Example 2 with RegistryException

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

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

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)

Example 4 with RegistryException

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

the class ObtainBaseImageLayerStepTest method setUp.

@Before
public void setUp() throws IOException, RegistryException, DigestException {
    existingLayerDigest = DescriptorDigest.fromHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    freshLayerDigest = DescriptorDigest.fromHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    DescriptorDigest diffId = Mockito.mock(DescriptorDigest.class);
    existingLayer = new ReferenceLayer(new BlobDescriptor(existingLayerDigest), diffId);
    freshLayer = new ReferenceLayer(new BlobDescriptor(freshLayerDigest), diffId);
    Mockito.when(registryClient.checkBlob(existingLayerDigest)).thenReturn(Optional.of(Mockito.mock(BlobDescriptor.class)));
    Mockito.when(registryClient.checkBlob(freshLayerDigest)).thenReturn(Optional.empty());
    // necessary to prevent error from classes dealing with progress report
    Answer3<Blob, DescriptorDigest, Consumer<Long>, Consumer<Long>> progressSizeSetter = (ignored1, progressSizeConsumer, ignored2) -> {
        progressSizeConsumer.accept(Long.valueOf(12345));
        return null;
    };
    Mockito.when(registryClient.pullBlob(Mockito.any(), Mockito.any(), Mockito.any())).thenAnswer(AdditionalAnswers.answer(progressSizeSetter));
}
Also used : StateInTarget(com.google.cloud.tools.jib.builder.steps.PreparedLayer.StateInTarget) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) DigestException(java.security.DigestException) Before(org.junit.Before) Answer3(org.mockito.stubbing.Answer3) Layer(com.google.cloud.tools.jib.image.Layer) Answers(org.mockito.Answers) Blob(com.google.cloud.tools.jib.blob.Blob) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) AdditionalAnswers(org.mockito.AdditionalAnswers) CacheCorruptedException(com.google.cloud.tools.jib.cache.CacheCorruptedException) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) Optional(java.util.Optional) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Blob(com.google.cloud.tools.jib.blob.Blob) Consumer(java.util.function.Consumer) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Before(org.junit.Before)

Example 5 with RegistryException

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

the class PullBaseImageStepTest method testTryMirrors_multipleMirrors.

@Test
public void testTryMirrors_multipleMirrors() throws LayerCountMismatchException, BadContainerConfigurationFormatException, IOException, RegistryException, InvalidImageReferenceException {
    Mockito.when(imageConfiguration.getImage()).thenReturn(ImageReference.parse("registry/repo"));
    Mockito.when(imageConfiguration.getImageRegistry()).thenReturn("registry");
    Mockito.when(buildContext.getRegistryMirrors()).thenReturn(ImmutableListMultimap.of("registry", "quay.io", "registry", "gcr.io"));
    Mockito.when(buildContext.newBaseImageRegistryClientFactory("quay.io")).thenReturn(registryClientFactory);
    Mockito.when(registryClient.pullManifest(Mockito.any())).thenThrow(new RegistryException("not found"));
    RegistryClient.Factory gcrRegistryClientFactory = setUpWorkingRegistryClientFactory();
    Mockito.when(buildContext.newBaseImageRegistryClientFactory("gcr.io")).thenReturn(gcrRegistryClientFactory);
    Optional<ImagesAndRegistryClient> result = pullBaseImageStep.tryMirrors(buildContext, progressDispatcherFactory);
    Assert.assertTrue(result.isPresent());
    Assert.assertEquals(gcrRegistryClientFactory.newRegistryClient(), result.get().registryClient);
    InOrder inOrder = Mockito.inOrder(eventHandlers);
    inOrder.verify(eventHandlers).dispatch(LogEvent.info("trying mirror quay.io for the base image"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.debug("failed to get manifest from mirror quay.io: not found"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.info("trying mirror gcr.io for the base image"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.info("pulled manifest from mirror gcr.io"));
}
Also used : InOrder(org.mockito.InOrder) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) RegistryException(com.google.cloud.tools.jib.api.RegistryException) 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