Search in sources :

Example 1 with RegistryClient

use of com.google.cloud.tools.jib.registry.RegistryClient 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 RegistryClient

use of com.google.cloud.tools.jib.registry.RegistryClient in project jib by google.

the class BuildAndPushContainerConfigurationStep method afterBaseImageLayerFuturesFuture.

/**
 * Depends on {@code pushAuthorizationFuture}, {@code pullBaseImageLayerFuturesFuture.get()}, and
 * {@code buildApplicationLayerFutures}.
 */
private BlobDescriptor afterBaseImageLayerFuturesFuture() throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException, RegistryException {
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pushAuthorizationFuture), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
        // Constructs the image.
        Image image = new Image();
        for (Future<CachedLayer> cachedLayerFuture : NonBlockingFutures.get(pullBaseImageLayerFuturesFuture)) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        for (Future<CachedLayer> cachedLayerFuture : buildApplicationLayerFutures) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        image.setEnvironment(buildConfiguration.getEnvironment());
        image.setEntrypoint(entrypoint);
        ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
        // Gets the container configuration content descriptor.
        Blob containerConfigurationBlob = imageToJsonTranslator.getContainerConfigurationBlob();
        CountingDigestOutputStream digestOutputStream = new CountingDigestOutputStream(ByteStreams.nullOutputStream());
        containerConfigurationBlob.writeTo(digestOutputStream);
        BlobDescriptor containerConfigurationBlobDescriptor = digestOutputStream.toBlobDescriptor();
        timer.lap("Pushing container configuration " + containerConfigurationBlobDescriptor.getDigest());
        // TODO: Use PushBlobStep.
        // Pushes the container configuration.
        registryClient.pushBlob(containerConfigurationBlobDescriptor.getDigest(), containerConfigurationBlob);
        return containerConfigurationBlobDescriptor;
    }
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Timer(com.google.cloud.tools.jib.Timer) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Image(com.google.cloud.tools.jib.image.Image)

Example 3 with RegistryClient

use of com.google.cloud.tools.jib.registry.RegistryClient in project jib by google.

the class PullAndCacheBaseImageLayerStep method call.

/**
 * Depends on {@code pullAuthorizationFuture}.
 */
@Override
public CachedLayer call() throws IOException, RegistryException, LayerPropertyNotFoundException, ExecutionException, InterruptedException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, layerDigest))) {
        RegistryClient registryClient = new RegistryClient(pullAuthorizationFuture.get(), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
        // Checks if the layer already exists in the cache.
        CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);
        if (cachedLayer != null) {
            return cachedLayer;
        }
        CacheWriter cacheWriter = new CacheWriter(cache);
        CountingOutputStream layerOutputStream = cacheWriter.getLayerOutputStream(layerDigest);
        registryClient.pullBlob(layerDigest, layerOutputStream);
        return cacheWriter.getCachedLayer(layerDigest, layerOutputStream);
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) Timer(com.google.cloud.tools.jib.Timer) CacheWriter(com.google.cloud.tools.jib.cache.CacheWriter) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) CacheReader(com.google.cloud.tools.jib.cache.CacheReader)

Example 4 with RegistryClient

use of com.google.cloud.tools.jib.registry.RegistryClient in project jib by google.

the class PushBlobStep method call.

/**
 * Depends on {@code pushAuthorizationFuture} and {@code pullLayerFuture}.
 */
@Override
public Void call() throws IOException, RegistryException, ExecutionException, InterruptedException {
    CachedLayer layer = pullLayerFuture.get();
    DescriptorDigest layerDigest = layer.getBlobDescriptor().getDigest();
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION + layerDigest)) {
        RegistryClient registryClient = new RegistryClient(pushAuthorizationFuture.get(), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
        if (registryClient.checkBlob(layerDigest) != null) {
            buildConfiguration.getBuildLogger().info("BLOB : " + layerDigest + " already exists on registry");
            return null;
        }
        registryClient.pushBlob(layerDigest, layer.getBlob());
        return null;
    }
}
Also used : Timer(com.google.cloud.tools.jib.Timer) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient)

Example 5 with RegistryClient

use of com.google.cloud.tools.jib.registry.RegistryClient in project jib by google.

the class PushImageStep method afterPushBaseImageLayerFuturesFuture.

/**
 * Depends on {@code pushAuthorizationFuture}, {@code pushBaseImageLayerFuturesFuture.get()},
 * {@code pushApplicationLayerFutures}, and (@code
 * containerConfigurationBlobDescriptorFutureFuture.get()}.
 */
private Void afterPushBaseImageLayerFuturesFuture() throws IOException, RegistryException, ExecutionException, InterruptedException, LayerPropertyNotFoundException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pushAuthorizationFuture), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository());
        // TODO: Consolidate with BuildAndPushContainerConfigurationStep.
        // Constructs the image.
        Image image = new Image();
        for (Future<CachedLayer> cachedLayerFuture : NonBlockingFutures.get(pullBaseImageLayerFuturesFuture)) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        for (Future<CachedLayer> cachedLayerFuture : buildApplicationLayerFutures) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
        // Pushes the image manifest.
        BuildableManifestTemplate manifestTemplate = imageToJsonTranslator.getManifestTemplate(buildConfiguration.getTargetFormat(), NonBlockingFutures.get(NonBlockingFutures.get(containerConfigurationBlobDescriptorFutureFuture)));
        registryClient.pushManifest(manifestTemplate, buildConfiguration.getTargetTag());
    }
    return null;
}
Also used : Timer(com.google.cloud.tools.jib.Timer) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Image(com.google.cloud.tools.jib.image.Image) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)

Aggregations

Timer (com.google.cloud.tools.jib.Timer)5 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)5 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)4 Image (com.google.cloud.tools.jib.image.Image)2 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)2 Blob (com.google.cloud.tools.jib.blob.Blob)1 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)1 CacheReader (com.google.cloud.tools.jib.cache.CacheReader)1 CacheWriter (com.google.cloud.tools.jib.cache.CacheWriter)1 CountingDigestOutputStream (com.google.cloud.tools.jib.hash.CountingDigestOutputStream)1 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)1 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)1 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)1 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)1 UnknownManifestFormatException (com.google.cloud.tools.jib.image.json.UnknownManifestFormatException)1 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)1 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)1 CountingOutputStream (com.google.common.io.CountingOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1