Search in sources :

Example 56 with FileEntriesLayer

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

the class SpringBootExplodedProcessor method createLayersForLayeredSpringBootJar.

/**
 * Creates layers as specified by the layers.idx file (located in the BOOT-INF/ directory of the
 * JAR).
 *
 * @param localExplodedJarRoot Path to exploded JAR content root
 * @return list of {@link FileEntriesLayer}
 * @throws IOException when an IO error occurs
 */
private static List<FileEntriesLayer> createLayersForLayeredSpringBootJar(Path localExplodedJarRoot) throws IOException {
    Path layerIndexPath = localExplodedJarRoot.resolve(BOOT_INF).resolve("layers.idx");
    Pattern layerNamePattern = Pattern.compile("- \"(.*)\":");
    Pattern layerEntryPattern = Pattern.compile("  - \"(.*)\"");
    Map<String, List<String>> layersMap = new LinkedHashMap<>();
    List<String> layerEntries = null;
    for (String line : Files.readAllLines(layerIndexPath, StandardCharsets.UTF_8)) {
        Matcher layerMatcher = layerNamePattern.matcher(line);
        Matcher entryMatcher = layerEntryPattern.matcher(line);
        if (layerMatcher.matches()) {
            layerEntries = new ArrayList<>();
            String layerName = layerMatcher.group(1);
            layersMap.put(layerName, layerEntries);
        } else if (entryMatcher.matches()) {
            Verify.verifyNotNull(layerEntries).add(entryMatcher.group(1));
        } else {
            throw new IllegalStateException("Unable to parse BOOT-INF/layers.idx file in the JAR. Please check the format of layers.idx.");
        }
    }
    // If the layers.idx file looks like this, for example:
    // - "dependencies":
    // - "BOOT-INF/lib/dependency1.jar"
    // - "application":
    // - "BOOT-INF/classes/"
    // - "META-INF/"
    // The predicate for the "dependencies" layer will be true if `path` is equal to
    // `BOOT-INF/lib/dependency1.jar` and the predicate for the "spring-boot-loader" layer will be
    // true if `path` is in either 'BOOT-INF/classes/` or `META-INF/`.
    List<FileEntriesLayer> layers = new ArrayList<>();
    for (Map.Entry<String, List<String>> entry : layersMap.entrySet()) {
        String layerName = entry.getKey();
        List<String> contents = entry.getValue();
        if (!contents.isEmpty()) {
            Predicate<Path> belongsToThisLayer = isInListedDirectoryOrIsSameFile(contents, localExplodedJarRoot);
            layers.add(ArtifactLayers.getDirectoryContentsAsLayer(layerName, localExplodedJarRoot, belongsToThisLayer, JarLayers.APP_ROOT));
        }
    }
    return layers;
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 57 with FileEntriesLayer

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

the class BuildContextTest method testBuilder.

@Test
public void testBuilder() throws Exception {
    String expectedBaseImageServerUrl = "someserver";
    String expectedBaseImageName = "baseimage";
    String expectedBaseImageTag = "baseimagetag";
    String expectedTargetServerUrl = "someotherserver";
    String expectedTargetImageName = "targetimage";
    String expectedTargetTag = "targettag";
    Set<String> additionalTargetImageTags = ImmutableSet.of("tag1", "tag2", "tag3");
    Set<String> expectedTargetImageTags = ImmutableSet.of("targettag", "tag1", "tag2", "tag3");
    List<CredentialRetriever> credentialRetrievers = Collections.singletonList(() -> Optional.of(Credential.from("username", "password")));
    Instant expectedCreationTime = Instant.ofEpochSecond(10000);
    List<String> expectedEntrypoint = Arrays.asList("some", "entrypoint");
    List<String> expectedProgramArguments = Arrays.asList("arg1", "arg2");
    Map<String, String> expectedEnvironment = ImmutableMap.of("key", "value");
    Set<Port> expectedExposedPorts = ImmutableSet.of(Port.tcp(1000), Port.tcp(2000));
    Map<String, String> expectedLabels = ImmutableMap.of("key1", "value1", "key2", "value2");
    Class<? extends BuildableManifestTemplate> expectedTargetFormat = OciManifestTemplate.class;
    Path expectedApplicationLayersCacheDirectory = Paths.get("application/layers");
    Path expectedBaseImageLayersCacheDirectory = Paths.get("base/image/layers");
    List<FileEntriesLayer> expectedLayerConfigurations = Collections.singletonList(FileEntriesLayer.builder().addEntry(Paths.get("sourceFile"), AbsoluteUnixPath.get("/path/in/container")).build());
    String expectedCreatedBy = "createdBy";
    ListMultimap<String, String> expectedRegistryMirrors = ImmutableListMultimap.of("some.registry", "mirror1", "some.registry", "mirror2");
    ImageConfiguration baseImageConfiguration = ImageConfiguration.builder(ImageReference.of(expectedBaseImageServerUrl, expectedBaseImageName, expectedBaseImageTag)).build();
    ImageConfiguration targetImageConfiguration = ImageConfiguration.builder(ImageReference.of(expectedTargetServerUrl, expectedTargetImageName, expectedTargetTag)).setCredentialRetrievers(credentialRetrievers).build();
    ContainerConfiguration containerConfiguration = ContainerConfiguration.builder().setCreationTime(expectedCreationTime).setEntrypoint(expectedEntrypoint).setProgramArguments(expectedProgramArguments).setEnvironment(expectedEnvironment).setExposedPorts(expectedExposedPorts).setLabels(expectedLabels).build();
    BuildContext.Builder buildContextBuilder = BuildContext.builder().setBaseImageConfiguration(baseImageConfiguration).setTargetImageConfiguration(targetImageConfiguration).setAdditionalTargetImageTags(additionalTargetImageTags).setContainerConfiguration(containerConfiguration).setApplicationLayersCacheDirectory(expectedApplicationLayersCacheDirectory).setBaseImageLayersCacheDirectory(expectedBaseImageLayersCacheDirectory).setTargetFormat(ImageFormat.OCI).setEnablePlatformTags(true).setAllowInsecureRegistries(true).setLayerConfigurations(expectedLayerConfigurations).setToolName(expectedCreatedBy).setRegistryMirrors(expectedRegistryMirrors);
    BuildContext buildContext = buildContextBuilder.build();
    Assert.assertEquals(expectedCreationTime, buildContext.getContainerConfiguration().getCreationTime());
    Assert.assertEquals(expectedBaseImageServerUrl, buildContext.getBaseImageConfiguration().getImageRegistry());
    Assert.assertEquals(expectedBaseImageName, buildContext.getBaseImageConfiguration().getImageRepository());
    Assert.assertEquals(expectedBaseImageTag, buildContext.getBaseImageConfiguration().getImageQualifier());
    Assert.assertEquals(expectedTargetServerUrl, buildContext.getTargetImageConfiguration().getImageRegistry());
    Assert.assertEquals(expectedTargetImageName, buildContext.getTargetImageConfiguration().getImageRepository());
    Assert.assertEquals(expectedTargetTag, buildContext.getTargetImageConfiguration().getImageQualifier());
    Assert.assertEquals(expectedTargetImageTags, buildContext.getAllTargetImageTags());
    Assert.assertEquals(Credential.from("username", "password"), buildContext.getTargetImageConfiguration().getCredentialRetrievers().get(0).retrieve().orElseThrow(AssertionError::new));
    Assert.assertEquals(expectedProgramArguments, buildContext.getContainerConfiguration().getProgramArguments());
    Assert.assertEquals(expectedEnvironment, buildContext.getContainerConfiguration().getEnvironmentMap());
    Assert.assertEquals(expectedExposedPorts, buildContext.getContainerConfiguration().getExposedPorts());
    Assert.assertEquals(expectedLabels, buildContext.getContainerConfiguration().getLabels());
    Assert.assertEquals(expectedTargetFormat, buildContext.getTargetFormat());
    Assert.assertEquals(expectedApplicationLayersCacheDirectory, buildContextBuilder.getApplicationLayersCacheDirectory());
    Assert.assertEquals(expectedBaseImageLayersCacheDirectory, buildContextBuilder.getBaseImageLayersCacheDirectory());
    Assert.assertEquals(expectedLayerConfigurations, buildContext.getLayerConfigurations());
    Assert.assertEquals(expectedEntrypoint, buildContext.getContainerConfiguration().getEntrypoint());
    Assert.assertEquals(expectedCreatedBy, buildContext.getToolName());
    Assert.assertEquals(expectedRegistryMirrors, buildContext.getRegistryMirrors());
    Assert.assertNotNull(buildContext.getExecutorService());
    Assert.assertTrue(buildContext.getEnablePlatformTags());
}
Also used : CredentialRetriever(com.google.cloud.tools.jib.api.CredentialRetriever) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) Port(com.google.cloud.tools.jib.api.buildplan.Port) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) Test(org.junit.Test)

Example 58 with FileEntriesLayer

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

the class LayersTest method testToLayers_properties.

@Test
public void testToLayers_properties() throws IOException, URISyntaxException {
    Path testRoot = getLayersTestRoot("propertiesTest");
    List<FileEntriesLayer> layers = parseLayers(testRoot, 4);
    checkLayer(layers.get(0), "level 0 passthrough", ImmutableSet.of(newEntry(testRoot, "dir", "/app", "700", 0, "0:0"), newEntry(testRoot, "dir/file.txt", "/app/file.txt", "000", 0, "0:0")));
    checkLayer(layers.get(1), "level 1 overrides", ImmutableSet.of(newEntry(testRoot, "dir", "/app", "711", 1000, "1:1"), newEntry(testRoot, "dir/file.txt", "/app/file.txt", "111", 1000, "1:1")));
    checkLayer(layers.get(2), "level 2 overrides", ImmutableSet.of(newEntry(testRoot, "dir", "/app", "722", 2000, "2:2"), newEntry(testRoot, "dir/file.txt", "/app/file.txt", "222", 2000, "2:2")));
    checkLayer(layers.get(3), "partial overrides", ImmutableSet.of(newEntry(testRoot, "dir", "/app", "711", 2000, "0:2"), newEntry(testRoot, "dir/file.txt", "/app/file.txt", "111", 2000, "0:2")));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Example 59 with FileEntriesLayer

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

the class JarFilesTest method testToJibContainerBuilder_explodedStandard_basicInfo.

@Test
public void testToJibContainerBuilder_explodedStandard_basicInfo() throws IOException, InvalidImageReferenceException {
    when(mockStandardExplodedProcessor.getJavaVersion()).thenReturn(8);
    FileEntriesLayer layer = FileEntriesLayer.builder().setName("classes").addEntry(Paths.get("path/to/tempDirectory/class1.class"), AbsoluteUnixPath.get("/app/explodedJar/class1.class")).build();
    when(mockStandardExplodedProcessor.createLayers()).thenReturn(Arrays.asList(layer));
    when(mockStandardExplodedProcessor.computeEntrypoint(anyList())).thenReturn(ImmutableList.of("java", "-cp", "/app/explodedJar:/app/dependencies/*", "HelloWorld"));
    when(mockCommonContainerConfigCliOptions.getFrom()).thenReturn(Optional.empty());
    JibContainerBuilder containerBuilder = JarFiles.toJibContainerBuilder(mockStandardExplodedProcessor, mockJarCommand, mockCommonCliOptions, mockCommonContainerConfigCliOptions, mockLogger);
    ContainerBuildPlan buildPlan = containerBuilder.toContainerBuildPlan();
    assertThat(buildPlan.getBaseImage()).isEqualTo("eclipse-temurin:8-jre");
    assertThat(buildPlan.getPlatforms()).isEqualTo(ImmutableSet.of(new Platform("amd64", "linux")));
    assertThat(buildPlan.getCreationTime()).isEqualTo(Instant.EPOCH);
    assertThat(buildPlan.getFormat()).isEqualTo(ImageFormat.Docker);
    assertThat(buildPlan.getEnvironment()).isEmpty();
    assertThat(buildPlan.getLabels()).isEmpty();
    assertThat(buildPlan.getVolumes()).isEmpty();
    assertThat(buildPlan.getExposedPorts()).isEmpty();
    assertThat(buildPlan.getUser()).isNull();
    assertThat(buildPlan.getWorkingDirectory()).isNull();
    assertThat(buildPlan.getEntrypoint()).containsExactly("java", "-cp", "/app/explodedJar:/app/dependencies/*", "HelloWorld").inOrder();
    assertThat(buildPlan.getLayers()).hasSize(1);
    assertThat(buildPlan.getLayers().get(0).getName()).isEqualTo("classes");
    assertThat(((FileEntriesLayer) buildPlan.getLayers().get(0)).getEntries()).containsExactlyElementsIn(FileEntriesLayer.builder().addEntry(Paths.get("path/to/tempDirectory/class1.class"), AbsoluteUnixPath.get("/app/explodedJar/class1.class")).build().getEntries());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 60 with FileEntriesLayer

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

the class JarFilesTest method testToJibContainerBuilder_explodedLayeredSpringBoot_basicInfo.

@Test
public void testToJibContainerBuilder_explodedLayeredSpringBoot_basicInfo() throws IOException, InvalidImageReferenceException {
    when(mockSpringBootExplodedProcessor.getJavaVersion()).thenReturn(8);
    FileEntriesLayer layer = FileEntriesLayer.builder().setName("classes").addEntry(Paths.get("path/to/tempDirectory/BOOT-INF/classes/class1.class"), AbsoluteUnixPath.get("/app/BOOT-INF/classes/class1.class")).build();
    when(mockCommonContainerConfigCliOptions.getFrom()).thenReturn(Optional.empty());
    when(mockSpringBootExplodedProcessor.createLayers()).thenReturn(Arrays.asList(layer));
    when(mockSpringBootExplodedProcessor.computeEntrypoint(anyList())).thenReturn(ImmutableList.of("java", "-cp", "/app", "org.springframework.boot.loader.JarLauncher"));
    when(mockCommonContainerConfigCliOptions.getFrom()).thenReturn(Optional.empty());
    JibContainerBuilder containerBuilder = JarFiles.toJibContainerBuilder(mockSpringBootExplodedProcessor, mockJarCommand, mockCommonCliOptions, mockCommonContainerConfigCliOptions, mockLogger);
    ContainerBuildPlan buildPlan = containerBuilder.toContainerBuildPlan();
    assertThat(buildPlan.getBaseImage()).isEqualTo("eclipse-temurin:8-jre");
    assertThat(buildPlan.getPlatforms()).isEqualTo(ImmutableSet.of(new Platform("amd64", "linux")));
    assertThat(buildPlan.getCreationTime()).isEqualTo(Instant.EPOCH);
    assertThat(buildPlan.getFormat()).isEqualTo(ImageFormat.Docker);
    assertThat(buildPlan.getEnvironment()).isEmpty();
    assertThat(buildPlan.getLabels()).isEmpty();
    assertThat(buildPlan.getVolumes()).isEmpty();
    assertThat(buildPlan.getExposedPorts()).isEmpty();
    assertThat(buildPlan.getUser()).isNull();
    assertThat(buildPlan.getWorkingDirectory()).isNull();
    assertThat(buildPlan.getEntrypoint()).containsExactly("java", "-cp", "/app", "org.springframework.boot.loader.JarLauncher").inOrder();
    assertThat(buildPlan.getLayers()).hasSize(1);
    assertThat(buildPlan.getLayers().get(0).getName()).isEqualTo("classes");
    assertThat(((FileEntriesLayer) buildPlan.getLayers().get(0)).getEntries()).containsExactlyElementsIn(FileEntriesLayer.builder().addEntry(Paths.get("path/to/tempDirectory/BOOT-INF/classes/class1.class"), AbsoluteUnixPath.get("/app/BOOT-INF/classes/class1.class")).build().getEntries());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Aggregations

FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)154 Test (org.junit.Test)111 Path (java.nio.file.Path)92 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)85 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)55 List (java.util.List)29 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)24 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)23 Map (java.util.Map)23 Files (java.nio.file.Files)21 Predicate (java.util.function.Predicate)20 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)16 Collectors (java.util.stream.Collectors)16 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)13 Paths (java.nio.file.Paths)13 Optional (java.util.Optional)13 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)12 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)12