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