Search in sources :

Example 6 with Timer

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

use of com.google.cloud.tools.jib.Timer 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 8 with Timer

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

Example 9 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class RetrieveRegistryCredentialsStep method call.

@Override
@Nullable
public Authorization call() throws IOException, NonexistentDockerCredentialHelperException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, buildConfiguration.getTargetRegistry()))) {
        // Tries to get registry credentials from Docker credential helpers.
        for (String credentialHelperSuffix : buildConfiguration.getCredentialHelperNames()) {
            Authorization authorization = retrieveFromCredentialHelper(credentialHelperSuffix);
            if (authorization != null) {
                return authorization;
            }
        }
        // Tries to get registry credentials from known registry credentials.
        String credentialSource = buildConfiguration.getKnownRegistryCredentials().getCredentialSource(registry);
        if (credentialSource != null) {
            logGotCredentialsFrom(credentialSource);
            return buildConfiguration.getKnownRegistryCredentials().getAuthorization(registry);
        }
        // Tries to get registry credentials from the Docker config.
        try {
            Authorization dockerConfigAuthorization = dockerConfigCredentialRetriever.retrieve();
            if (dockerConfigAuthorization != null) {
                buildConfiguration.getBuildLogger().info("Using credentials from Docker config for " + registry);
                return dockerConfigAuthorization;
            }
        } catch (IOException ex) {
            buildConfiguration.getBuildLogger().info("Unable to parse Docker config");
        }
        // Tries to infer common credential helpers for known registries.
        for (String registrySuffix : COMMON_CREDENTIAL_HELPERS.keySet()) {
            if (registry.endsWith(registrySuffix)) {
                try {
                    String commonCredentialHelper = COMMON_CREDENTIAL_HELPERS.get(registrySuffix);
                    if (commonCredentialHelper == null) {
                        throw new IllegalStateException("No COMMON_CREDENTIAL_HELPERS should be null");
                    }
                    Authorization authorization = retrieveFromCredentialHelper(commonCredentialHelper);
                    if (authorization != null) {
                        return authorization;
                    }
                } catch (NonexistentDockerCredentialHelperException ex) {
                    if (ex.getMessage() != null) {
                        // Warns the user that the specified (or inferred) credential helper is not on the
                        // system.
                        buildConfiguration.getBuildLogger().warn(ex.getMessage());
                    }
                }
            }
        }
        /*
       * If no credentials found, give an info (not warning because in most cases, the base image is
       * public and does not need extra credentials) and return null.
       */
        buildConfiguration.getBuildLogger().info("No credentials could be retrieved for registry " + registry);
        return null;
    }
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Timer(com.google.cloud.tools.jib.Timer) IOException(java.io.IOException) NonexistentDockerCredentialHelperException(com.google.cloud.tools.jib.registry.credentials.NonexistentDockerCredentialHelperException) Nullable(javax.annotation.Nullable)

Example 10 with Timer

use of com.google.cloud.tools.jib.Timer in project jib by google.

the class AuthenticatePullStep method call.

/**
 * Depends on {@link RetrieveRegistryCredentialsStep}.
 */
@Override
public Authorization call() throws RegistryAuthenticationFailedException, IOException, RegistryException, ExecutionException, InterruptedException {
    try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, buildConfiguration.getBaseImageRegistry()))) {
        Authorization registryCredentials = NonBlockingFutures.get(registryCredentialsFuture);
        RegistryAuthenticator registryAuthenticator = RegistryAuthenticators.forOther(buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
        if (registryAuthenticator == null) {
            return registryCredentials;
        }
        return registryAuthenticator.setAuthorization(registryCredentials).authenticatePull();
    }
}
Also used : Authorization(com.google.cloud.tools.jib.http.Authorization) Timer(com.google.cloud.tools.jib.Timer) RegistryAuthenticator(com.google.cloud.tools.jib.registry.RegistryAuthenticator)

Aggregations

Timer (com.google.cloud.tools.jib.Timer)12 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)5 Authorization (com.google.cloud.tools.jib.http.Authorization)4 Image (com.google.cloud.tools.jib.image.Image)3 CacheReader (com.google.cloud.tools.jib.cache.CacheReader)2 CacheWriter (com.google.cloud.tools.jib.cache.CacheWriter)2 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)2 RegistryAuthenticator (com.google.cloud.tools.jib.registry.RegistryAuthenticator)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Nullable (javax.annotation.Nullable)2 Blob (com.google.cloud.tools.jib.blob.Blob)1 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)1 Cache (com.google.cloud.tools.jib.cache.Cache)1 Caches (com.google.cloud.tools.jib.cache.Caches)1 CountingDigestOutputStream (com.google.cloud.tools.jib.hash.CountingDigestOutputStream)1 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)1 LayerBuilder (com.google.cloud.tools.jib.image.LayerBuilder)1 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)1 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)1