Search in sources :

Example 16 with Platform

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

the class PluginConfigurationProcessor method getPlatformsSet.

/**
 * Parses the list of platforms to a set of {@link Platform}.
 *
 * @param rawConfiguration raw configuration data
 * @return the set of parsed platforms
 * @throws InvalidPlatformException if there exists a {@link PlatformConfiguration} in the
 *     specified platforms list that is missing required fields or has invalid values
 */
@VisibleForTesting
static Set<Platform> getPlatformsSet(RawConfiguration rawConfiguration) throws InvalidPlatformException {
    Set<Platform> platforms = new LinkedHashSet<>();
    for (PlatformConfiguration platformConfiguration : rawConfiguration.getPlatforms()) {
        Optional<String> architecture = platformConfiguration.getArchitectureName();
        Optional<String> os = platformConfiguration.getOsName();
        String platformToString = "architecture=" + architecture.orElse("<missing>") + ", os=" + os.orElse("<missing>");
        if (!architecture.isPresent()) {
            throw new InvalidPlatformException("platform configuration is missing an architecture value", platformToString);
        }
        if (!os.isPresent()) {
            throw new InvalidPlatformException("platform configuration is missing an OS value", platformToString);
        }
        platforms.add(new Platform(architecture.get(), os.get()));
    }
    return platforms;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PlatformConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 17 with Platform

use of com.google.cloud.tools.jib.api.buildplan.Platform 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 18 with Platform

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

the class JibIntegrationTest method testScratch_defaultPlatform.

@Test
public void testScratch_defaultPlatform() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException {
    Jib.fromScratch().containerize(Containerizer.to(RegistryImage.named("localhost:5000/jib-scratch:default-platform")).setAllowInsecureRegistries(true));
    V22ManifestTemplate manifestTemplate = registryClient.pullManifest("default-platform", V22ManifestTemplate.class).getManifest();
    String containerConfig = Blobs.writeToString(registryClient.pullBlob(manifestTemplate.getContainerConfiguration().getDigest(), ignored -> {
    }, ignored -> {
    }));
    Assert.assertTrue(manifestTemplate.getLayers().isEmpty());
    Assert.assertTrue(containerConfig.contains("\"architecture\":\"amd64\""));
    Assert.assertTrue(containerConfig.contains("\"os\":\"linux\""));
}
Also used : BeforeClass(org.junit.BeforeClass) URISyntaxException(java.net.URISyntaxException) Command(com.google.cloud.tools.jib.Command) ManifestPullerIntegrationTest(com.google.cloud.tools.jib.registry.ManifestPullerIntegrationTest) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) LocalRegistry(com.google.cloud.tools.jib.registry.LocalRegistry) After(org.junit.After) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ImmutableSet(com.google.common.collect.ImmutableSet) Resources(com.google.common.io.Resources) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) IOException(java.io.IOException) Test(org.junit.Test) Executors(java.util.concurrent.Executors) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) ExecutionException(java.util.concurrent.ExecutionException) Rule(org.junit.Rule) Paths(java.nio.file.Paths) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestPullerIntegrationTest(com.google.cloud.tools.jib.registry.ManifestPullerIntegrationTest) Test(org.junit.Test)

Example 19 with Platform

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

the class JibIntegrationTest method testScratch_singlePlatform.

@Test
public void testScratch_singlePlatform() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException {
    Jib.fromScratch().setPlatforms(ImmutableSet.of(new Platform("arm64", "windows"))).containerize(Containerizer.to(RegistryImage.named("localhost:5000/jib-scratch:single-platform")).setAllowInsecureRegistries(true));
    V22ManifestTemplate manifestTemplate = registryClient.pullManifest("single-platform", V22ManifestTemplate.class).getManifest();
    String containerConfig = Blobs.writeToString(registryClient.pullBlob(manifestTemplate.getContainerConfiguration().getDigest(), ignored -> {
    }, ignored -> {
    }));
    Assert.assertTrue(manifestTemplate.getLayers().isEmpty());
    Assert.assertTrue(containerConfig.contains("\"architecture\":\"arm64\""));
    Assert.assertTrue(containerConfig.contains("\"os\":\"windows\""));
}
Also used : BeforeClass(org.junit.BeforeClass) URISyntaxException(java.net.URISyntaxException) Command(com.google.cloud.tools.jib.Command) ManifestPullerIntegrationTest(com.google.cloud.tools.jib.registry.ManifestPullerIntegrationTest) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) LocalRegistry(com.google.cloud.tools.jib.registry.LocalRegistry) After(org.junit.After) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ImmutableSet(com.google.common.collect.ImmutableSet) Resources(com.google.common.io.Resources) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) IOException(java.io.IOException) Test(org.junit.Test) Executors(java.util.concurrent.Executors) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) ExecutionException(java.util.concurrent.ExecutionException) Rule(org.junit.Rule) Paths(java.nio.file.Paths) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ManifestPullerIntegrationTest(com.google.cloud.tools.jib.registry.ManifestPullerIntegrationTest) Test(org.junit.Test)

Example 20 with Platform

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

the class JibIntegrationTest method testScratch_multiPlatform.

@Test
public void testScratch_multiPlatform() throws IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException, InvalidImageReferenceException {
    Jib.fromScratch().setPlatforms(ImmutableSet.of(new Platform("arm64", "windows"), new Platform("amd32", "windows"))).containerize(Containerizer.to(RegistryImage.named("localhost:5000/jib-scratch:multi-platform")).setAllowInsecureRegistries(true));
    V22ManifestListTemplate manifestList = (V22ManifestListTemplate) registryClient.pullManifest("multi-platform").getManifest();
    Assert.assertEquals(2, manifestList.getManifests().size());
    ManifestDescriptorTemplate.Platform platform1 = manifestList.getManifests().get(0).getPlatform();
    ManifestDescriptorTemplate.Platform platform2 = manifestList.getManifests().get(1).getPlatform();
    Assert.assertEquals("arm64", platform1.getArchitecture());
    Assert.assertEquals("windows", platform1.getOs());
    Assert.assertEquals("amd32", platform2.getArchitecture());
    Assert.assertEquals("windows", platform2.getOs());
}
Also used : V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) ManifestPullerIntegrationTest(com.google.cloud.tools.jib.registry.ManifestPullerIntegrationTest) Test(org.junit.Test)

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