Search in sources :

Example 16 with ImagesAndRegistryClient

use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by google.

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 17 with ImagesAndRegistryClient

use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by google.

the class StepsRunnerTest method testObtainBaseImageLayers_skipObtainingDuplicateLayers.

@Test
public void testObtainBaseImageLayers_skipObtainingDuplicateLayers() throws DigestException, InterruptedException, ExecutionException {
    Mockito.when(executorService.submit(Mockito.any(PullBaseImageStep.class))).thenReturn(Futures.immediateFuture(new ImagesAndRegistryClient(null, null)));
    // Pretend that a thread pulling base images returned some (meaningless) result.
    stepsRunner.pullBaseImages(progressDispatcherFactory);
    DescriptorDigest digest1 = DescriptorDigest.fromHash("1111111111111111111111111111111111111111111111111111111111111111");
    DescriptorDigest digest2 = DescriptorDigest.fromHash("2222222222222222222222222222222222222222222222222222222222222222");
    DescriptorDigest digest3 = DescriptorDigest.fromHash("3333333333333333333333333333333333333333333333333333333333333333");
    DigestOnlyLayer layer1 = new DigestOnlyLayer(digest1);
    DigestOnlyLayer layer2 = new DigestOnlyLayer(digest2);
    DigestOnlyLayer layer3 = new DigestOnlyLayer(digest3);
    PreparedLayer preparedLayer1 = Mockito.mock(PreparedLayer.class);
    PreparedLayer preparedLayer2 = Mockito.mock(PreparedLayer.class);
    PreparedLayer preparedLayer3 = Mockito.mock(PreparedLayer.class);
    Mockito.when(executorService.submit(Mockito.any(ObtainBaseImageLayerStep.class))).thenReturn(Futures.immediateFuture(preparedLayer1)).thenReturn(Futures.immediateFuture(preparedLayer2)).thenReturn(Futures.immediateFuture(preparedLayer3));
    Map<DescriptorDigest, Future<PreparedLayer>> preparedLayersCache = new HashMap<>();
    // 1. Should schedule two threads to obtain new layers.
    Image image = Mockito.mock(Image.class);
    Mockito.when(image.getLayers()).thenReturn(ImmutableList.of(layer1, layer2));
    stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
    // two new layers cached
    Assert.assertEquals(2, preparedLayersCache.size());
    Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
    Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
    // 2. Should not schedule threads for existing layers.
    stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
    // no new layers cached (still 2)
    Assert.assertEquals(2, preparedLayersCache.size());
    Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
    Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
    // 3. Another image with one duplicate layer.
    Mockito.when(image.getLayers()).thenReturn(ImmutableList.of(layer3, layer2));
    stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
    // one new layer cached
    Assert.assertEquals(3, preparedLayersCache.size());
    Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
    Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
    Assert.assertEquals(preparedLayer3, preparedLayersCache.get(digest3).get());
    // Total three threads scheduled for the three unique layers.
    Mockito.verify(executorService, Mockito.times(3)).submit(Mockito.any(ObtainBaseImageLayerStep.class));
}
Also used : HashMap(java.util.HashMap) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) DigestOnlyLayer(com.google.cloud.tools.jib.image.DigestOnlyLayer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) Image(com.google.cloud.tools.jib.image.Image) Test(org.junit.Test)

Example 18 with ImagesAndRegistryClient

use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.

the class PullBaseImageStepTest method testTryMirrors_noMatchingMirrors.

@Test
public void testTryMirrors_noMatchingMirrors() throws LayerCountMismatchException, BadContainerConfigurationFormatException {
    Mockito.when(imageConfiguration.getImageRegistry()).thenReturn("registry");
    Mockito.when(buildContext.getRegistryMirrors()).thenReturn(ImmutableListMultimap.of("unmatched1", "mirror1", "unmatched2", "mirror2"));
    Optional<ImagesAndRegistryClient> result = pullBaseImageStep.tryMirrors(buildContext, progressDispatcherFactory);
    Assert.assertEquals(Optional.empty(), result);
    InOrder inOrder = Mockito.inOrder(eventHandlers);
    inOrder.verify(eventHandlers).dispatch(LogEvent.debug("mirror config: unmatched1 --> mirror1"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.debug("mirror config: unmatched2 --> mirror2"));
    Mockito.verify(buildContext, Mockito.never()).newBaseImageRegistryClientFactory(Mockito.any());
}
Also used : InOrder(org.mockito.InOrder) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) Test(org.junit.Test)

Example 19 with ImagesAndRegistryClient

use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.

the class PullBaseImageStepTest method testCall_digestBaseImage.

@Test
public void testCall_digestBaseImage() throws LayerPropertyNotFoundException, IOException, RegistryException, LayerCountMismatchException, BadContainerConfigurationFormatException, CacheCorruptedException, CredentialRetrievalException, InvalidImageReferenceException {
    ImageReference imageReference = ImageReference.parse("awesome@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    Assert.assertTrue(imageReference.getDigest().isPresent());
    Mockito.when(imageConfiguration.getImage()).thenReturn(imageReference);
    ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate();
    containerConfigJson.setArchitecture("slim arch");
    containerConfigJson.setOs("fat system");
    ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(new V22ManifestTemplate(), containerConfigJson, "sha256:digest");
    ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    ImagesAndRegistryClient result = pullBaseImageStep.call();
    Assert.assertEquals("fat system", result.images.get(0).getOs());
    Assert.assertEquals(registryClient, result.registryClient);
}
Also used : ImageReference(com.google.cloud.tools.jib.api.ImageReference) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) Test(org.junit.Test)

Example 20 with ImagesAndRegistryClient

use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.

the class PullBaseImageStepTest method testCall_scratch_singlePlatform.

@Test
public void testCall_scratch_singlePlatform() throws LayerPropertyNotFoundException, IOException, RegistryException, LayerCountMismatchException, BadContainerConfigurationFormatException, CacheCorruptedException, CredentialRetrievalException {
    Mockito.when(imageConfiguration.getImage()).thenReturn(ImageReference.scratch());
    ImagesAndRegistryClient result = pullBaseImageStep.call();
    Assert.assertEquals(1, result.images.size());
    Assert.assertEquals("slim arch", result.images.get(0).getArchitecture());
    Assert.assertEquals("fat system", result.images.get(0).getOs());
    Assert.assertNull(result.registryClient);
}
Also used : ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) Test(org.junit.Test)

Aggregations

ImagesAndRegistryClient (com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient)22 Test (org.junit.Test)18 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 InOrder (org.mockito.InOrder)8 ImageReference (com.google.cloud.tools.jib.api.ImageReference)6 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Image (com.google.cloud.tools.jib.image.Image)6 IOException (java.io.IOException)6 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)4 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)4 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)4 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)4 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)4 ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)4 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)4 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)4 Credential (com.google.cloud.tools.jib.api.Credential)2 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)2 DigestOnlyLayer (com.google.cloud.tools.jib.image.DigestOnlyLayer)2