Search in sources :

Example 16 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class PullBaseImageStep method call.

@Override
public ImagesAndRegistryClient call() throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, BadContainerConfigurationFormatException, CacheCorruptedException, CredentialRetrievalException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (ProgressEventDispatcher progressDispatcher = progressDispatcherFactory.create("pulling base image manifest", 4);
        TimerEventDispatcher ignored1 = new TimerEventDispatcher(eventHandlers, DESCRIPTION)) {
        // Skip this step if this is a scratch image
        ImageReference imageReference = buildContext.getBaseImageConfiguration().getImage();
        if (imageReference.isScratch()) {
            Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
            Verify.verify(!platforms.isEmpty());
            eventHandlers.dispatch(LogEvent.progress("Getting scratch base image..."));
            ImmutableList.Builder<Image> images = ImmutableList.builder();
            for (Platform platform : platforms) {
                Image.Builder imageBuilder = Image.builder(buildContext.getTargetFormat());
                imageBuilder.setArchitecture(platform.getArchitecture()).setOs(platform.getOs());
                images.add(imageBuilder.build());
            }
            return new ImagesAndRegistryClient(images.build(), null);
        }
        eventHandlers.dispatch(LogEvent.progress("Getting manifest for base image " + imageReference + "..."));
        if (buildContext.isOffline()) {
            List<Image> images = getCachedBaseImages();
            if (!images.isEmpty()) {
                return new ImagesAndRegistryClient(images, null);
            }
            throw new IOException("Cannot run Jib in offline mode; " + imageReference + " not found in local Jib cache");
        } else if (imageReference.getDigest().isPresent()) {
            List<Image> images = getCachedBaseImages();
            if (!images.isEmpty()) {
                RegistryClient noAuthRegistryClient = buildContext.newBaseImageRegistryClientFactory().newRegistryClient();
                // https://github.com/GoogleContainerTools/jib/issues/2220
                return new ImagesAndRegistryClient(images, noAuthRegistryClient);
            }
        }
        Optional<ImagesAndRegistryClient> mirrorPull = tryMirrors(buildContext, progressDispatcher.newChildProducer());
        if (mirrorPull.isPresent()) {
            return mirrorPull.get();
        }
        try {
            // First, try with no credentials. This works with public GCR images (but not Docker Hub).
            // TODO: investigate if we should just pass credentials up front. However, this involves
            // some risk. https://github.com/GoogleContainerTools/jib/pull/2200#discussion_r359069026
            // contains some related discussions.
            RegistryClient noAuthRegistryClient = buildContext.newBaseImageRegistryClientFactory().newRegistryClient();
            return new ImagesAndRegistryClient(pullBaseImages(noAuthRegistryClient, progressDispatcher.newChildProducer()), noAuthRegistryClient);
        } catch (RegistryUnauthorizedException ex) {
            eventHandlers.dispatch(LogEvent.lifecycle("The base image requires auth. Trying again for " + imageReference + "..."));
            Credential credential = RegistryCredentialRetriever.getBaseImageCredential(buildContext).orElse(null);
            RegistryClient registryClient = buildContext.newBaseImageRegistryClientFactory().setCredential(credential).newRegistryClient();
            String wwwAuthenticate = ex.getHttpResponseException().getHeaders().getAuthenticate();
            if (wwwAuthenticate != null) {
                eventHandlers.dispatch(LogEvent.debug("WWW-Authenticate for " + imageReference + ": " + wwwAuthenticate));
                registryClient.authPullByWwwAuthenticate(wwwAuthenticate);
                return new ImagesAndRegistryClient(pullBaseImages(registryClient, progressDispatcher.newChildProducer()), registryClient);
            } else {
                // TODO: consider removing this fallback branch.
                if (credential != null && !credential.isOAuth2RefreshToken()) {
                    eventHandlers.dispatch(LogEvent.debug("Trying basic auth as fallback for " + imageReference + "..."));
                    registryClient.configureBasicAuth();
                    try {
                        return new ImagesAndRegistryClient(pullBaseImages(registryClient, progressDispatcher.newChildProducer()), registryClient);
                    } catch (RegistryUnauthorizedException ignored) {
                    // Fall back to try bearer auth.
                    }
                }
                eventHandlers.dispatch(LogEvent.debug("Trying bearer auth as fallback for " + imageReference + "..."));
                registryClient.doPullBearerAuth();
                return new ImagesAndRegistryClient(pullBaseImages(registryClient, progressDispatcher.newChildProducer()), registryClient);
            }
        }
    }
}
Also used : Credential(com.google.cloud.tools.jib.api.Credential) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Image(com.google.cloud.tools.jib.image.Image) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 17 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class PushImageStep method call.

@Override
public BuildResult call() throws IOException, RegistryException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION);
        ProgressEventDispatcher ignored2 = progressEventDispatcherFactory.create("pushing manifest for " + imageQualifier, 1)) {
        eventHandlers.dispatch(LogEvent.info("Pushing manifest for " + imageQualifier + "..."));
        registryClient.pushManifest(manifestTemplate, imageQualifier);
        return new BuildResult(imageDigest, imageId, true);
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 18 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class PushImageStep method makeListForManifestList.

static ImmutableList<PushImageStep> makeListForManifestList(BuildContext buildContext, ProgressEventDispatcher.Factory progressEventDispatcherFactory, RegistryClient registryClient, ManifestTemplate manifestList, boolean manifestListAlreadyExists) throws IOException {
    Set<String> tags = buildContext.getAllTargetImageTags();
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Preparing manifest list pushers");
        ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("launching manifest list pushers", tags.size())) {
        boolean singlePlatform = buildContext.getContainerConfiguration().getPlatforms().size() == 1;
        if (singlePlatform) {
            // single image; no need to push a manifest list
            return ImmutableList.of();
        }
        if (JibSystemProperties.skipExistingImages() && manifestListAlreadyExists) {
            eventHandlers.dispatch(LogEvent.info("Skipping pushing manifest list; already exists."));
            return ImmutableList.of();
        }
        DescriptorDigest manifestListDigest = Digests.computeJsonDigest(manifestList);
        return tags.stream().map(tag -> new PushImageStep(buildContext, progressEventDispatcher.newChildProducer(), registryClient, manifestList, tag, manifestListDigest, // return value and type.
        manifestListDigest)).collect(ImmutableList.toImmutableList());
    }
}
Also used : TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) Set(java.util.Set) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) RegistryException(com.google.cloud.tools.jib.api.RegistryException) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Collectors(java.util.stream.Collectors) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Digests(com.google.cloud.tools.jib.hash.Digests) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) LogEvent(com.google.cloud.tools.jib.api.LogEvent) ImmutableList(com.google.common.collect.ImmutableList) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Collections(java.util.Collections) Image(com.google.cloud.tools.jib.image.Image) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 19 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class StepsRunner method obtainBaseImageLayers.

// This method updates the given "preparedLayersCache" and should not be called concurrently.
@VisibleForTesting
List<Future<PreparedLayer>> obtainBaseImageLayers(Image baseImage, boolean layersRequiredLocally, Map<DescriptorDigest, Future<PreparedLayer>> preparedLayersCache, ProgressEventDispatcher.Factory progressDispatcherFactory) throws InterruptedException, ExecutionException {
    List<Future<PreparedLayer>> preparedLayers = new ArrayList<>();
    try (ProgressEventDispatcher progressDispatcher = progressDispatcherFactory.create("launching base image layer pullers", baseImage.getLayers().size())) {
        for (Layer layer : baseImage.getLayers()) {
            DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
            Future<PreparedLayer> preparedLayer = preparedLayersCache.get(digest);
            if (preparedLayer != null) {
                progressDispatcher.dispatchProgress(1);
            } else {
                // If we haven't obtained this layer yet, launcher a puller.
                preparedLayer = executorService.submit(layersRequiredLocally ? ObtainBaseImageLayerStep.forForcedDownload(buildContext, progressDispatcher.newChildProducer(), layer, results.baseImagesAndRegistryClient.get().registryClient) : ObtainBaseImageLayerStep.forSelectiveDownload(buildContext, progressDispatcher.newChildProducer(), layer, results.baseImagesAndRegistryClient.get().registryClient, results.targetRegistryClient.get()));
                preparedLayersCache.put(digest, preparedLayer);
            }
            preparedLayers.add(preparedLayer);
        }
        return preparedLayers;
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Layer(com.google.cloud.tools.jib.image.Layer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project component-runtime by Talend.

the class RemoteEngineCustomizer method loadImage.

private Image loadImage(final String from, final String to, final ExecutorService executor, final Path baseCache, final Path appCache, final DockerConfiguration dockerConfiguration, final ImageType fromImageType) throws Exception {
    final StepsRunner steps = StepsRunner.begin(BuildContext.builder().setBaseImageConfiguration(createBaseImage(from, dockerConfiguration, fromImageType)).setTargetFormat(ImageFormat.OCI).setTargetImageConfiguration(ImageConfiguration.builder(ImageReference.parse(to)).build()).setToolName("Talend Component Kit Remote Engine Customizer " + Versions.VERSION).setExecutorService(executor).setBaseImageLayersCacheDirectory(baseCache).setApplicationLayersCacheDirectory(appCache).build());
    final String description = "Extracting registry properties";
    final Field rootProgressDescription = asAccessible(StepsRunner.class.getDeclaredField("rootProgressDescription"));
    rootProgressDescription.set(steps, description);
    final List<Runnable> stepsInstance = (List<Runnable>) asAccessible(StepsRunner.class.getDeclaredField("stepsToRun")).get(steps);
    try {
        asAccessible(StepsRunner.class.getDeclaredMethod("addRetrievalSteps", boolean.class)).invoke(steps, true);
    } catch (final IllegalAccessException | NoSuchMethodException e) {
        throw new IllegalStateException(e);
    } catch (final InvocationTargetException e) {
        throw new IllegalStateException(e.getTargetException());
    }
    Stream.of("buildAndCacheApplicationLayers", "buildImage").forEach(method -> {
        stepsInstance.add(() -> {
            try {
                asAccessible(StepsRunner.class.getDeclaredMethod(method)).invoke(steps);
            } catch (final IllegalAccessException | NoSuchMethodException e) {
                throw new IllegalStateException(e);
            } catch (final InvocationTargetException e) {
                throw new IllegalStateException(e.getTargetException());
            }
        });
    });
    try (final ProgressEventDispatcher progressEventDispatcher = ProgressEventDispatcher.newRoot(EventHandlers.builder().add(LogEvent.class, le -> {
        switch(le.getLevel()) {
            case WARN:
                log.warn(le.getMessage());
                break;
            case DEBUG:
                log.debug(le.getMessage());
                break;
            case ERROR:
                log.error(le.getMessage());
                break;
            case INFO:
                log.error(le.getMessage());
                break;
            default:
                log.info("(" + le.getLevel() + ") " + le.getMessage());
                break;
        }
    }).build(), description, stepsInstance.size())) {
        asAccessible(StepsRunner.class.getDeclaredField("rootProgressDispatcher")).set(steps, progressEventDispatcher);
        stepsInstance.forEach(Runnable::run);
    }
    final Object stepResult = asAccessible(StepsRunner.class.getDeclaredField("results")).get(steps);
    return ((Future<Image>) asAccessible(stepResult.getClass().getDeclaredField("builtImage")).get(stepResult)).get();
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) StepsRunner(com.google.cloud.tools.jib.builder.steps.StepsRunner) Future(java.util.concurrent.Future) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Aggregations

ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)35 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)28 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)24 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)14 Image (com.google.cloud.tools.jib.image.Image)12 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)10 IOException (java.io.IOException)10 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)8 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayList (java.util.ArrayList)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Cache (com.google.cloud.tools.jib.cache.Cache)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6 ThrottledAccumulatingConsumer (com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)6 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)6 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)6 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Credential (com.google.cloud.tools.jib.api.Credential)4 LogEvent (com.google.cloud.tools.jib.api.LogEvent)4