Search in sources :

Example 16 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.

the class PlatformChecker method checkManifestPlatform.

/**
 * Assuming the base image is not a manifest list, checks and warns misconfigured platforms.
 *
 * @param buildContext the {@link BuildContext}
 * @param containerConfig container configuration JSON of the base image
 */
static void checkManifestPlatform(BuildContext buildContext, ContainerConfigurationTemplate containerConfig) {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    Optional<Path> path = buildContext.getBaseImageConfiguration().getTarPath();
    String baseImageName = path.map(Path::toString).orElse(buildContext.getBaseImageConfiguration().getImage().toString());
    Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
    Verify.verify(!platforms.isEmpty());
    if (platforms.size() != 1) {
        eventHandlers.dispatch(LogEvent.warn("platforms configured, but '" + baseImageName + "' is not a manifest list"));
    } else {
        Platform platform = platforms.iterator().next();
        if (!platform.getArchitecture().equals(containerConfig.getArchitecture()) || !platform.getOs().equals(containerConfig.getOs())) {
            // configure it. Skip reporting to suppress false alarm.
            if (!(platform.getArchitecture().equals("amd64") && platform.getOs().equals("linux"))) {
                String warning = "the configured platform (%s/%s) doesn't match the platform (%s/%s) of the base " + "image (%s)";
                eventHandlers.dispatch(LogEvent.warn(String.format(warning, platform.getArchitecture(), platform.getOs(), containerConfig.getArchitecture(), containerConfig.getOs(), baseImageName)));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 17 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.

the class BuildContextTest method testBuilder_digestWarning.

@Test
public void testBuilder_digestWarning() throws CacheDirectoryCreationException, InvalidImageReferenceException {
    EventHandlers mockEventHandlers = Mockito.mock(EventHandlers.class);
    BuildContext.Builder builder = createBasicTestBuilder().setEventHandlers(mockEventHandlers);
    builder.setBaseImageConfiguration(ImageConfiguration.builder(ImageReference.parse("image@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")).build()).build();
    Mockito.verify(mockEventHandlers, Mockito.never()).dispatch(LogEvent.warn(Mockito.anyString()));
    builder.setBaseImageConfiguration(ImageConfiguration.builder(ImageReference.parse("image:tag")).build()).build();
    Mockito.verify(mockEventHandlers).dispatch(LogEvent.warn("Base image 'image:tag' does not use a specific image digest - build may not be reproducible"));
}
Also used : EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 18 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.

the class PullBaseImageStep method lookUpPlatformSpecificImageManifest.

/**
 * Looks through a manifest list for the manifest matching the {@code platform} and returns the
 * digest of the first manifest it finds.
 */
// TODO: support OciIndexTemplate once AbstractManifestPuller starts to accept it.
@VisibleForTesting
String lookUpPlatformSpecificImageManifest(V22ManifestListTemplate manifestListTemplate, Platform platform) throws UnlistedPlatformInManifestListException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    List<String> digests = manifestListTemplate.getDigestsForPlatform(platform.getArchitecture(), platform.getOs());
    if (digests.isEmpty()) {
        String errorTemplate = buildContext.getBaseImageConfiguration().getImage() + " is a manifest list, but the list does not contain an image for architecture=%s, " + "os=%s. If your intention was to specify a platform for your image, see " + "https://github.com/GoogleContainerTools/jib/blob/master/docs/faq.md#how-do-i-specify-a-platform-in-the-manifest-list-or-oci-index-of-a-base-image";
        String error = String.format(errorTemplate, platform.getArchitecture(), platform.getOs());
        eventHandlers.dispatch(LogEvent.error(error));
        throw new UnlistedPlatformInManifestListException(error);
    }
    // TODO: perhaps we should return multiple digests matching the platform.
    return digests.get(0);
}
Also used : EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) UnlistedPlatformInManifestListException(com.google.cloud.tools.jib.image.json.UnlistedPlatformInManifestListException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 19 with EventHandlers

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

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

Aggregations

EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)42 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)24 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)22 Test (org.junit.Test)14 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)10 Image (com.google.cloud.tools.jib.image.Image)10 IOException (java.io.IOException)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 ImmutableList (com.google.common.collect.ImmutableList)8 Map (java.util.Map)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)6 Cache (com.google.cloud.tools.jib.cache.Cache)6 Allocation (com.google.cloud.tools.jib.event.progress.Allocation)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 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6