Search in sources :

Example 61 with Platform

use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.

the class JibContainerBuilderTest method testToBuildContext_containerConfigurationAdd.

@Test
public void testToBuildContext_containerConfigurationAdd() throws InvalidImageReferenceException, CacheDirectoryCreationException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder jibContainerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).addPlatform("testArchitecture", "testOS").setEntrypoint("entry", "point").setEnvironment(ImmutableMap.of("name", "value")).addEnvironmentVariable("environment", "variable").setExposedPorts(Port.tcp(1234), Port.udp(5678)).addExposedPort(Port.tcp(1337)).setLabels(ImmutableMap.of("key", "value")).addLabel("added", "label").setProgramArguments("program", "arguments");
    BuildContext buildContext = jibContainerBuilder.toBuildContext(Containerizer.to(RegistryImage.named("target/image")));
    ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS"), new Platform("amd64", "linux")), containerConfiguration.getPlatforms());
    Assert.assertEquals(Arrays.asList("entry", "point"), containerConfiguration.getEntrypoint());
    Assert.assertEquals(ImmutableMap.of("name", "value", "environment", "variable"), containerConfiguration.getEnvironmentMap());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678), Port.tcp(1337)), containerConfiguration.getExposedPorts());
    Assert.assertEquals(ImmutableMap.of("key", "value", "added", "label"), containerConfiguration.getLabels());
    Assert.assertEquals(Arrays.asList("program", "arguments"), containerConfiguration.getProgramArguments());
    Assert.assertEquals(Instant.EPOCH, containerConfiguration.getCreationTime());
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration) Test(org.junit.Test)

Example 62 with Platform

use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.

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 63 with Platform

use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.

the class ContainerBuilders method create.

/**
 * Creates a {@link JibContainerBuilder} depending on the base image specified.
 *
 * @param baseImageReference base image reference
 * @param platforms platforms for multi-platform support in build command
 * @param commonCliOptions common cli options
 * @param logger console logger
 * @return a {@link JibContainerBuilder}
 * @throws InvalidImageReferenceException if the baseImage reference cannot be parsed
 * @throws FileNotFoundException if credential helper file cannot be found
 */
public static JibContainerBuilder create(String baseImageReference, Set<Platform> platforms, CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
    if (baseImageReference.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
        return Jib.from(DockerDaemonImage.named(baseImageReference.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
    }
    if (baseImageReference.startsWith(TAR_IMAGE_PREFIX)) {
        return Jib.from(TarImage.at(Paths.get(baseImageReference.replaceFirst(TAR_IMAGE_PREFIX, ""))));
    }
    ImageReference imageReference = ImageReference.parse(baseImageReference.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
    Credentials.getFromCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
    JibContainerBuilder containerBuilder = Jib.from(registryImage);
    if (!platforms.isEmpty()) {
        containerBuilder.setPlatforms(platforms);
    }
    return containerBuilder;
}
Also used : DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) ImageReference(com.google.cloud.tools.jib.api.ImageReference) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) TAR_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.TAR_IMAGE_PREFIX) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) Set(java.util.Set) TarImage(com.google.cloud.tools.jib.api.TarImage) DefaultCredentialRetrievers(com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers) DOCKER_DAEMON_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.DOCKER_DAEMON_IMAGE_PREFIX) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) FileNotFoundException(java.io.FileNotFoundException) REGISTRY_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.REGISTRY_IMAGE_PREFIX) Jib(com.google.cloud.tools.jib.api.Jib) Paths(java.nio.file.Paths) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) ImageReference(com.google.cloud.tools.jib.api.ImageReference) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) DefaultCredentialRetrievers(com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage)

Example 64 with Platform

use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.

the class PluginConfigurationProcessorTest method testGetPlatformsSet.

@Test
public void testGetPlatformsSet() throws InvalidPlatformException {
    Mockito.<List<?>>when(rawConfiguration.getPlatforms()).thenReturn(Arrays.asList(new TestPlatformConfiguration("testArchitecture", "testOs")));
    assertThat(PluginConfigurationProcessor.getPlatformsSet(rawConfiguration)).containsExactly(new Platform("testArchitecture", "testOs"));
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) Test(org.junit.Test)

Example 65 with Platform

use of com.google.cloud.tools.jib.api.buildplan.Platform in project quarkus by quarkusio.

the class PlatformHelper method parse.

public static Platform parse(String specifier) {
    Platform platform;
    String[] elements = specifier.split("/", 2);
    if (elements.length == 1) {
        Optional<String> os = normalizeOs(elements[0]);
        if (os.filter(PlatformHelper::isKnownOs).isPresent()) {
            platform = new Platform(ARCH_DEFAULT, os.get());
        } else {
            platform = new Platform(normalizeArch(elements[0]).orElse(ARCH_DEFAULT), OS_DEFAULT);
        }
    } else {
        Optional<String> arch = normalizeArch(specifier);
        if (arch.filter(PlatformHelper::isKnownArch).isPresent()) {
            platform = new Platform(arch.get(), OS_DEFAULT);
        } else {
            platform = new Platform(normalizeArch(elements[1]).orElse(ARCH_DEFAULT), normalizeOs(elements[0]).orElse(OS_DEFAULT));
        }
    }
    log.debug("Platform Added=" + PlatformHelper.platformToString(platform) + " from specifier: " + specifier);
    return platform;
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform)

Aggregations

Platform (com.google.cloud.tools.jib.api.buildplan.Platform)65 Test (org.junit.Test)50 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)18 ContainerConfigurationTemplate (com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate)18 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)18 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)16 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)14 ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)14 ImageReference (com.google.cloud.tools.jib.api.ImageReference)12 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)12 Path (java.nio.file.Path)12 ImageMetadataTemplate (com.google.cloud.tools.jib.image.json.ImageMetadataTemplate)10 ManifestAndConfigTemplate (com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate)10 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)10 Image (com.google.cloud.tools.jib.image.Image)9 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)8 ManifestDescriptorTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate)8 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)7 IOException (java.io.IOException)7 Blobs (com.google.cloud.tools.jib.blob.Blobs)6