use of com.google.cloud.tools.jib.event.EventHandlers 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.event.EventHandlers in project jib by google.
the class PullBaseImageStep method pullBaseImages.
/**
* Pulls the base images specified in the platforms list.
*
* @param registryClient to communicate with remote registry
* @param progressDispatcherFactory the {@link ProgressEventDispatcher.Factory} for emitting
* {@link ProgressEvent}s
* @return the list of pulled base images and a registry client
* @throws IOException when an I/O exception occurs during the pulling
* @throws RegistryException if communicating with the registry caused a known error
* @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
* information
* @throws LayerPropertyNotFoundException if adding image layers fails
* @throws BadContainerConfigurationFormatException if the container configuration is in a bad
* format
*/
private List<Image> pullBaseImages(RegistryClient registryClient, ProgressEventDispatcher.Factory progressDispatcherFactory) throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, BadContainerConfigurationFormatException {
Cache cache = buildContext.getBaseImageLayersCache();
EventHandlers eventHandlers = buildContext.getEventHandlers();
ImageConfiguration baseImageConfig = buildContext.getBaseImageConfiguration();
try (ProgressEventDispatcher progressDispatcher1 = progressDispatcherFactory.create("pulling base image manifest and container config", 2)) {
ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest(baseImageConfig.getImageQualifier());
eventHandlers.dispatch(LogEvent.lifecycle("Using base image with digest: " + manifestAndDigest.getDigest()));
progressDispatcher1.dispatchProgress(1);
ProgressEventDispatcher.Factory childProgressDispatcherFactory = progressDispatcher1.newChildProducer();
ManifestTemplate manifestTemplate = manifestAndDigest.getManifest();
if (manifestTemplate instanceof V21ManifestTemplate) {
V21ManifestTemplate v21Manifest = (V21ManifestTemplate) manifestTemplate;
cache.writeMetadata(baseImageConfig.getImage(), v21Manifest);
return Collections.singletonList(JsonToImageTranslator.toImage(v21Manifest));
} else if (manifestTemplate instanceof BuildableManifestTemplate) {
// V22ManifestTemplate or OciManifestTemplate
BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) manifestTemplate;
ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(manifestAndDigest, registryClient, childProgressDispatcherFactory);
PlatformChecker.checkManifestPlatform(buildContext, containerConfig);
cache.writeMetadata(baseImageConfig.getImage(), imageManifest, containerConfig);
return Collections.singletonList(JsonToImageTranslator.toImage(imageManifest, containerConfig));
}
// TODO: support OciIndexTemplate once AbstractManifestPuller starts to accept it.
Verify.verify(manifestTemplate instanceof V22ManifestListTemplate);
List<ManifestAndConfigTemplate> manifestsAndConfigs = new ArrayList<>();
ImmutableList.Builder<Image> images = ImmutableList.builder();
Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
try (ProgressEventDispatcher progressDispatcher2 = childProgressDispatcherFactory.create("pulling platform-specific manifests and container configs", 2L * platforms.size())) {
// If a manifest list, search for the manifests matching the given platforms.
for (Platform platform : platforms) {
String message = "Searching for architecture=%s, os=%s in the base image manifest list";
eventHandlers.dispatch(LogEvent.info(String.format(message, platform.getArchitecture(), platform.getOs())));
String manifestDigest = lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestTemplate, platform);
// TODO: pull multiple manifests (+ container configs) in parallel.
ManifestAndDigest<?> imageManifestAndDigest = registryClient.pullManifest(manifestDigest);
progressDispatcher2.dispatchProgress(1);
BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) imageManifestAndDigest.getManifest();
ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(imageManifestAndDigest, registryClient, progressDispatcher2.newChildProducer());
manifestsAndConfigs.add(new ManifestAndConfigTemplate(imageManifest, containerConfig, manifestDigest));
images.add(JsonToImageTranslator.toImage(imageManifest, containerConfig));
}
}
cache.writeMetadata(baseImageConfig.getImage(), new ImageMetadataTemplate(manifestTemplate, /* manifest list */
manifestsAndConfigs));
return images.build();
}
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.
the class PushBlobStep method call.
@Override
public BlobDescriptor call() throws IOException, RegistryException {
EventHandlers eventHandlers = buildContext.getEventHandlers();
DescriptorDigest blobDigest = blobDescriptor.getDigest();
try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("pushing blob " + blobDigest, blobDescriptor.getSize());
TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION + blobDescriptor);
ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress)) {
// check if the BLOB is available
if (!forcePush && registryClient.checkBlob(blobDigest).isPresent()) {
eventHandlers.dispatch(LogEvent.info("Skipping push; BLOB already exists on target registry : " + blobDescriptor));
return blobDescriptor;
}
// If base and target images are in the same registry, then use mount/from to try mounting the
// BLOB from the base image repository to the target image repository and possibly avoid
// having to push the BLOB. See
// https://docs.docker.com/registry/spec/api/#cross-repository-blob-mount for details.
String baseRegistry = buildContext.getBaseImageConfiguration().getImageRegistry();
String baseRepository = buildContext.getBaseImageConfiguration().getImageRepository();
String targetRegistry = buildContext.getTargetImageConfiguration().getImageRegistry();
String sourceRepository = targetRegistry.equals(baseRegistry) ? baseRepository : null;
registryClient.pushBlob(blobDigest, blob, sourceRepository, throttledProgressReporter);
return blobDescriptor;
}
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.
the class PushImageStep method makeList.
static ImmutableList<PushImageStep> makeList(BuildContext buildContext, ProgressEventDispatcher.Factory progressEventDispatcherFactory, RegistryClient registryClient, BlobDescriptor containerConfigurationDigestAndSize, Image builtImage, boolean manifestAlreadyExists) throws IOException {
// Gets the image manifest to push.
BuildableManifestTemplate manifestTemplate = new ImageToJsonTranslator(builtImage).getManifestTemplate(buildContext.getTargetFormat(), containerConfigurationDigestAndSize);
DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate);
Set<String> imageQualifiers = getImageQualifiers(buildContext, builtImage, manifestDigest);
EventHandlers eventHandlers = buildContext.getEventHandlers();
try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Preparing manifest pushers");
ProgressEventDispatcher progressDispatcher = progressEventDispatcherFactory.create("launching manifest pushers", imageQualifiers.size())) {
if (JibSystemProperties.skipExistingImages() && manifestAlreadyExists) {
eventHandlers.dispatch(LogEvent.info("Skipping pushing manifest; already exists."));
return ImmutableList.of();
}
return imageQualifiers.stream().map(qualifier -> new PushImageStep(buildContext, progressDispatcher.newChildProducer(), registryClient, manifestTemplate, qualifier, manifestDigest, containerConfigurationDigestAndSize.getDigest())).collect(ImmutableList.toImmutableList());
}
}
use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.
the class BuildManifestListOrSingleManifestStep method call.
@Override
public ManifestTemplate call() throws IOException {
Preconditions.checkState(!builtImages.isEmpty(), "no images given");
EventHandlers eventHandlers = buildContext.getEventHandlers();
try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION);
ProgressEventDispatcher ignored2 = progressEventDispatcherFactory.create("building a manifest list or a single manifest", 1)) {
if (builtImages.size() == 1) {
eventHandlers.dispatch(LogEvent.info("Building a single manifest"));
ImageToJsonTranslator imageTranslator = new ImageToJsonTranslator(builtImages.get(0));
BlobDescriptor configDescriptor = Digests.computeDigest(imageTranslator.getContainerConfiguration());
return imageTranslator.getManifestTemplate(buildContext.getTargetFormat(), configDescriptor);
}
eventHandlers.dispatch(LogEvent.info("Building a manifest list"));
return new ManifestListGenerator(builtImages).getManifestListTemplate(buildContext.getTargetFormat());
}
}
Aggregations