use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class CacheStorageReaderTest method setupCachedMetadataV22.
private static void setupCachedMetadataV22(Path cacheDirectory) throws IOException, URISyntaxException {
Path imageDirectory = cacheDirectory.resolve("images/test/image!tag");
Files.createDirectories(imageDirectory);
ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(loadJsonResource("core/json/v22manifest.json", V22ManifestTemplate.class), loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class), "sha256:digest");
try (OutputStream out = Files.newOutputStream(imageDirectory.resolve("manifests_configs.json"))) {
JsonTemplateMapper.writeTo(new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig)), out);
}
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate 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.image.json.ImageMetadataTemplate in project jib by google.
the class Cache method writeMetadata.
/**
* Saves a schema 2 manifest for an image reference. This is a simple wrapper around {@link
* #writeMetadata(ImageReference, ImageMetadataTemplate)} to save a single manifest without a
* manifest list.
*
* @param imageReference the image reference to save the manifest for
* @param manifest the V2.2 or OCI manifest
* @param containerConfiguration the container configuration
* @throws IOException if an I/O exception occurs
*/
public void writeMetadata(ImageReference imageReference, BuildableManifestTemplate manifest, ContainerConfigurationTemplate containerConfiguration) throws IOException {
List<ManifestAndConfigTemplate> singleton = Collections.singletonList(new ManifestAndConfigTemplate(manifest, containerConfiguration));
cacheStorageWriter.writeMetadata(imageReference, new ImageMetadataTemplate(null, singleton));
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class CacheStorageReader method retrieveMetadata.
/**
* Retrieves the cached image metadata (a manifest list and a list of manifest/container
* configuration pairs) for an image reference.
*
* @param imageReference the image reference
* @return the image metadata for the image reference, if found
* @throws IOException if an I/O exception occurs
* @throws CacheCorruptedException if the cache is corrupted
*/
Optional<ImageMetadataTemplate> retrieveMetadata(ImageReference imageReference) throws IOException, CacheCorruptedException {
Path imageDirectory = cacheStorageFiles.getImageDirectory(imageReference);
Path metadataPath = imageDirectory.resolve("manifests_configs.json");
if (!Files.exists(metadataPath)) {
return Optional.empty();
}
ImageMetadataTemplate metadata;
try (LockFile ignored = LockFile.lock(imageDirectory.resolve("lock"))) {
metadata = JsonTemplateMapper.readJsonFromFile(metadataPath, ImageMetadataTemplate.class);
}
verifyImageMetadata(metadata, imageDirectory);
return Optional.of(metadata);
}
use of com.google.cloud.tools.jib.image.json.ImageMetadataTemplate in project jib by google.
the class CacheStorageReader method verifyImageMetadata.
@VisibleForTesting
static void verifyImageMetadata(ImageMetadataTemplate metadata, Path metadataCacheDirectory) throws CacheCorruptedException {
List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.getManifestsAndConfigs();
if (manifestsAndConfigs.isEmpty()) {
throw new CacheCorruptedException(metadataCacheDirectory, "Manifest cache empty");
}
if (manifestsAndConfigs.stream().anyMatch(entry -> entry.getManifest() == null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Manifest(s) missing");
}
if (metadata.getManifestList() == null && manifestsAndConfigs.size() != 1) {
throw new CacheCorruptedException(metadataCacheDirectory, "Manifest list missing");
}
ManifestTemplate firstManifest = manifestsAndConfigs.get(0).getManifest();
if (firstManifest instanceof V21ManifestTemplate) {
if (metadata.getManifestList() != null || manifestsAndConfigs.stream().anyMatch(entry -> entry.getConfig() != null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Schema 1 manifests corrupted");
}
} else if (firstManifest instanceof BuildableManifestTemplate) {
if (manifestsAndConfigs.stream().anyMatch(entry -> entry.getConfig() == null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Schema 2 manifests corrupted");
}
if (metadata.getManifestList() != null && manifestsAndConfigs.stream().anyMatch(entry -> entry.getManifestDigest() == null)) {
throw new CacheCorruptedException(metadataCacheDirectory, "Schema 2 manifests corrupted");
}
} else {
throw new CacheCorruptedException(metadataCacheDirectory, "Unknown manifest type: " + firstManifest);
}
}
Aggregations