Search in sources :

Example 1 with UnknownManifestFormatException

use of com.google.cloud.tools.jib.image.json.UnknownManifestFormatException in project jib by google.

the class PullBaseImageStep method call.

/**
 * Depends on {@code pullAuthorizationFuture}.
 */
@Override
public Image call() throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, ExecutionException, InterruptedException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pullAuthorizationFuture), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
        ManifestTemplate manifestTemplate = registryClient.pullManifest(buildConfiguration.getBaseImageTag());
        // TODO: Make schema version be enum.
        switch(manifestTemplate.getSchemaVersion()) {
            case 1:
                V21ManifestTemplate v21ManifestTemplate = (V21ManifestTemplate) manifestTemplate;
                return JsonToImageTranslator.toImage(v21ManifestTemplate);
            case 2:
                V22ManifestTemplate v22ManifestTemplate = (V22ManifestTemplate) manifestTemplate;
                if (v22ManifestTemplate.getContainerConfiguration() == null || v22ManifestTemplate.getContainerConfiguration().getDigest() == null) {
                    throw new UnknownManifestFormatException("Invalid container configuration in Docker V2.2 manifest: \n" + Blobs.writeToString(JsonTemplateMapper.toBlob(v22ManifestTemplate)));
                }
                ByteArrayOutputStream containerConfigurationOutputStream = new ByteArrayOutputStream();
                registryClient.pullBlob(v22ManifestTemplate.getContainerConfiguration().getDigest(), containerConfigurationOutputStream);
                String containerConfigurationString = new String(containerConfigurationOutputStream.toByteArray(), StandardCharsets.UTF_8);
                ContainerConfigurationTemplate containerConfigurationTemplate = JsonTemplateMapper.readJson(containerConfigurationString, ContainerConfigurationTemplate.class);
                return JsonToImageTranslator.toImage(v22ManifestTemplate, containerConfigurationTemplate);
        }
        throw new IllegalStateException("Unknown manifest schema version");
    }
}
Also used : V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Timer(com.google.cloud.tools.jib.Timer) UnknownManifestFormatException(com.google.cloud.tools.jib.image.json.UnknownManifestFormatException) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate)

Example 2 with UnknownManifestFormatException

use of com.google.cloud.tools.jib.image.json.UnknownManifestFormatException in project jib by google.

the class ManifestPuller method getManifestTemplateFromJson.

/**
 * Instantiates a {@link ManifestTemplate} from a JSON string. This checks the {@code
 * schemaVersion} field of the JSON to determine which manifest version to use.
 */
private T getManifestTemplateFromJson(String jsonString) throws IOException, UnknownManifestFormatException {
    ObjectNode node = new ObjectMapper().readValue(jsonString, ObjectNode.class);
    if (!node.has("schemaVersion")) {
        throw new UnknownManifestFormatException("Cannot find field 'schemaVersion' in manifest");
    }
    if (!manifestTemplateClass.equals(ManifestTemplate.class)) {
        return JsonTemplateMapper.readJson(jsonString, manifestTemplateClass);
    }
    int schemaVersion = node.get("schemaVersion").asInt(-1);
    if (schemaVersion == -1) {
        throw new UnknownManifestFormatException("`schemaVersion` field is not an integer");
    }
    if (schemaVersion == 1) {
        return manifestTemplateClass.cast(JsonTemplateMapper.readJson(jsonString, V21ManifestTemplate.class));
    }
    if (schemaVersion == 2) {
        // 'schemaVersion' of 2 can be either Docker V2.2 or OCI.
        String mediaType = node.get("mediaType").asText();
        if (V22ManifestTemplate.MANIFEST_MEDIA_TYPE.equals(mediaType)) {
            return manifestTemplateClass.cast(JsonTemplateMapper.readJson(jsonString, V22ManifestTemplate.class));
        }
        if (OCIManifestTemplate.MANIFEST_MEDIA_TYPE.equals(mediaType)) {
            return manifestTemplateClass.cast(JsonTemplateMapper.readJson(jsonString, OCIManifestTemplate.class));
        }
        throw new UnknownManifestFormatException("Unknown mediaType: " + mediaType);
    }
    throw new UnknownManifestFormatException("Unknown schemaVersion: " + schemaVersion + " - only 1 and 2 are supported");
}
Also used : V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) UnknownManifestFormatException(com.google.cloud.tools.jib.image.json.UnknownManifestFormatException) OCIManifestTemplate(com.google.cloud.tools.jib.image.json.OCIManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) OCIManifestTemplate(com.google.cloud.tools.jib.image.json.OCIManifestTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 UnknownManifestFormatException (com.google.cloud.tools.jib.image.json.UnknownManifestFormatException)2 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Timer (com.google.cloud.tools.jib.Timer)1 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)1 OCIManifestTemplate (com.google.cloud.tools.jib.image.json.OCIManifestTemplate)1 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1