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();
}
}
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));
}
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());
}
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);
}
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);
}
Aggregations