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);
}
}
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;
}
}
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;
}
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;
}
}
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();
}
}
Aggregations