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;
}
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());
}
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")));
}
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());
}
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());
}
Aggregations