use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib by GoogleContainerTools.
the class BuildAndCacheApplicationLayerStepTest method testRun.
@Test
public void testRun() throws LayerPropertyNotFoundException, IOException, CacheCorruptedException {
ImmutableList<FileEntriesLayer> fakeLayerConfigurations = ImmutableList.of(fakeDependenciesLayerConfiguration, fakeSnapshotDependenciesLayerConfiguration, fakeResourcesLayerConfiguration, fakeClassesLayerConfiguration, fakeExtraFilesLayerConfiguration);
Mockito.when(mockBuildContext.getLayerConfigurations()).thenReturn(fakeLayerConfigurations);
// Populates the cache.
List<Layer> applicationLayers = buildFakeLayersToCache();
Assert.assertEquals(5, applicationLayers.size());
ImmutableList<FileEntry> dependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(0).getEntries());
ImmutableList<FileEntry> snapshotDependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(1).getEntries());
ImmutableList<FileEntry> resourcesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(2).getEntries());
ImmutableList<FileEntry> classesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(3).getEntries());
ImmutableList<FileEntry> extraFilesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(4).getEntries());
CachedLayer dependenciesCachedLayer = cache.retrieve(dependenciesLayerEntries).orElseThrow(AssertionError::new);
CachedLayer snapshotDependenciesCachedLayer = cache.retrieve(snapshotDependenciesLayerEntries).orElseThrow(AssertionError::new);
CachedLayer resourcesCachedLayer = cache.retrieve(resourcesLayerEntries).orElseThrow(AssertionError::new);
CachedLayer classesCachedLayer = cache.retrieve(classesLayerEntries).orElseThrow(AssertionError::new);
CachedLayer extraFilesCachedLayer = cache.retrieve(extraFilesLayerEntries).orElseThrow(AssertionError::new);
// Verifies that the cached layers are up-to-date.
Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor().getDigest(), dependenciesCachedLayer.getDigest());
Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor().getDigest(), snapshotDependenciesCachedLayer.getDigest());
Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor().getDigest(), resourcesCachedLayer.getDigest());
Assert.assertEquals(applicationLayers.get(3).getBlobDescriptor().getDigest(), classesCachedLayer.getDigest());
Assert.assertEquals(applicationLayers.get(4).getBlobDescriptor().getDigest(), extraFilesCachedLayer.getDigest());
// Verifies that the cache reader gets the same layers as the newest application layers.
assertBlobsEqual(applicationLayers.get(0).getBlob(), dependenciesCachedLayer.getBlob());
assertBlobsEqual(applicationLayers.get(1).getBlob(), snapshotDependenciesCachedLayer.getBlob());
assertBlobsEqual(applicationLayers.get(2).getBlob(), resourcesCachedLayer.getBlob());
assertBlobsEqual(applicationLayers.get(3).getBlob(), classesCachedLayer.getBlob());
assertBlobsEqual(applicationLayers.get(4).getBlob(), extraFilesCachedLayer.getBlob());
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib by GoogleContainerTools.
the class JavaContainerBuilder method toContainerBuilder.
/**
* Returns a new {@link JibContainerBuilder} using the parameters specified on the {@link
* JavaContainerBuilder}.
*
* @return a new {@link JibContainerBuilder} using the parameters specified on the {@link
* JavaContainerBuilder}
* @throws IOException if building the {@link JibContainerBuilder} fails.
*/
public JibContainerBuilder toContainerBuilder() throws IOException {
if (mainClass == null && !jvmFlags.isEmpty()) {
throw new IllegalStateException("Failed to construct entrypoint on JavaContainerBuilder; " + "jvmFlags were set, but mainClass is null. Specify the main class using " + "JavaContainerBuilder#setMainClass(String), or consider using MainClassFinder to " + "infer the main class.");
}
if (classpathOrder.isEmpty()) {
throw new IllegalStateException("Failed to construct entrypoint because no files were added to the JavaContainerBuilder");
}
Map<LayerType, FileEntriesLayer.Builder> layerBuilders = new EnumMap<>(LayerType.class);
// Add classes to layer configuration
for (PathPredicatePair directory : addedClasses) {
addDirectoryContentsToLayer(layerBuilders, LayerType.CLASSES, directory.path, directory.predicate, appRoot.resolve(classesDestination));
}
// Add resources to layer configuration
for (PathPredicatePair directory : addedResources) {
addDirectoryContentsToLayer(layerBuilders, LayerType.RESOURCES, directory.path, directory.predicate, appRoot.resolve(resourcesDestination));
}
// Detect duplicate filenames across all dependency layer types
Map<String, Long> occurrences = Streams.concat(addedDependencies.stream(), addedSnapshotDependencies.stream(), addedProjectDependencies.stream()).map(path -> path.getFileName().toString()).collect(Collectors.groupingBy(filename -> filename, Collectors.counting()));
List<String> duplicates = occurrences.entrySet().stream().filter(entry -> entry.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());
ImmutableMap<LayerType, List<Path>> layerMap = ImmutableMap.of(LayerType.DEPENDENCIES, addedDependencies, LayerType.SNAPSHOT_DEPENDENCIES, addedSnapshotDependencies, LayerType.PROJECT_DEPENDENCIES, addedProjectDependencies);
for (Map.Entry<LayerType, List<Path>> entry : layerMap.entrySet()) {
for (Path file : Preconditions.checkNotNull(entry.getValue())) {
// Handle duplicates by appending filesize to the end of the file. This renaming logic
// must be in sync with the code that does the same in the other place. See
// https://github.com/GoogleContainerTools/jib/issues/3331
String jarName = file.getFileName().toString();
if (duplicates.contains(jarName)) {
jarName = jarName.replaceFirst("\\.jar$", "-" + Files.size(file)) + ".jar";
}
// Add dependencies to layer configuration
addFileToLayer(layerBuilders, entry.getKey(), file, appRoot.resolve(dependenciesDestination).resolve(jarName));
}
}
// Add others to layer configuration
for (Path path : addedOthers) {
if (Files.isDirectory(path)) {
addDirectoryContentsToLayer(layerBuilders, LayerType.EXTRA_FILES, path, ignored -> true, appRoot.resolve(othersDestination));
} else {
addFileToLayer(layerBuilders, LayerType.EXTRA_FILES, path, appRoot.resolve(othersDestination).resolve(path.getFileName()));
}
}
// Add layer configurations to container builder
List<FileEntriesLayer> layers = new ArrayList<>();
layerBuilders.forEach((type, builder) -> layers.add(builder.setName(type.getName()).build()));
jibContainerBuilder.setFileEntriesLayers(layers);
if (mainClass != null) {
// Construct entrypoint. Ensure classpath elements are in the same order as the files were
// added to the JavaContainerBuilder.
List<String> classpathElements = new ArrayList<>();
for (LayerType path : classpathOrder) {
switch(path) {
case CLASSES:
classpathElements.add(appRoot.resolve(classesDestination).toString());
break;
case RESOURCES:
classpathElements.add(appRoot.resolve(resourcesDestination).toString());
break;
case DEPENDENCIES:
classpathElements.add(appRoot.resolve(dependenciesDestination).resolve("*").toString());
break;
case EXTRA_FILES:
classpathElements.add(appRoot.resolve(othersDestination).toString());
break;
default:
throw new RuntimeException("Bug in jib-core; please report the bug at " + ProjectInfo.GITHUB_NEW_ISSUE_URL);
}
}
String classpathString = String.join(":", classpathElements);
List<String> entrypoint = new ArrayList<>(4 + jvmFlags.size());
entrypoint.add("java");
entrypoint.addAll(jvmFlags);
entrypoint.add("-cp");
entrypoint.add(classpathString);
entrypoint.add(mainClass);
jibContainerBuilder.setEntrypoint(entrypoint);
}
return jibContainerBuilder;
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project jib by GoogleContainerTools.
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 quarkus by quarkusio.
the class ContainerBuilderHelper method extraDirectoryLayerConfiguration.
/**
* Returns a {@link FileEntriesLayer} for adding the extra directory to the container.
*
* @param sourceDirectory the source extra directory path
* @param targetDirectory the root directory on the container to place the files in
* @param extraDirectoryPermissions map from path on container to file permissions
* @param modificationTimeProvider file modification time provider
* @return a {@link FileEntriesLayer} for adding the extra directory to the container
* @throws IOException if walking the extra directory fails
*/
public static FileEntriesLayer extraDirectoryLayerConfiguration(Path sourceDirectory, AbsoluteUnixPath targetDirectory, Map<String, FilePermissions> extraDirectoryPermissions, BiFunction<Path, AbsoluteUnixPath, Instant> modificationTimeProvider) throws IOException {
FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(JavaContainerBuilder.LayerType.EXTRA_FILES.getName());
Map<PathMatcher, FilePermissions> pathMatchers = new LinkedHashMap<>();
for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
pathMatchers.put(FileSystems.getDefault().getPathMatcher("glob:" + entry.getKey()), entry.getValue());
}
new DirectoryWalker(sourceDirectory).filterRoot().walk(localPath -> {
AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
Instant modificationTime = modificationTimeProvider.apply(localPath, pathOnContainer);
FilePermissions permissions = extraDirectoryPermissions.get(pathOnContainer.toString());
if (permissions == null) {
// Check for matching globs
Path containerPath = Paths.get(pathOnContainer.toString());
for (Map.Entry<PathMatcher, FilePermissions> entry : pathMatchers.entrySet()) {
if (entry.getKey().matches(containerPath)) {
builder.addEntry(localPath, pathOnContainer, entry.getValue(), modificationTime);
return;
}
}
// Add with default permissions
if (localPath.toFile().canExecute()) {
// make sure the file or directory can be executed
builder.addEntry(localPath, pathOnContainer, FilePermissions.fromOctalString("755"), modificationTime);
} else {
builder.addEntry(localPath, pathOnContainer, modificationTime);
}
} else {
// Add with explicit permissions
builder.addEntry(localPath, pathOnContainer, permissions, modificationTime);
}
});
return builder.build();
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project quarkus by quarkusio.
the class JibProcessor method handleExtraFiles.
/**
* Allow users to have custom files in {@code src/main/jib} that will be copied into the built container's file system
* in same manner as the Jib Maven and Gradle plugins do.
* For example, {@code src/main/jib/foo/bar} would add {@code /foo/bar} into the container filesystem.
*
* See: https://github.com/GoogleContainerTools/jib/blob/v0.15.0-core/docs/faq.md#can-i-add-a-custom-directory-to-the-image
*/
private void handleExtraFiles(OutputTargetBuildItem outputTarget, JibContainerBuilder jibContainerBuilder) {
Path outputDirectory = outputTarget.getOutputDirectory();
PathsUtil.findMainSourcesRoot(outputTarget.getOutputDirectory());
Map.Entry<Path, Path> mainSourcesRoot = findMainSourcesRoot(outputDirectory);
if (mainSourcesRoot == null) {
// this should never happen
return;
}
Path jibFilesRoot = mainSourcesRoot.getKey().resolve("jib");
if (!jibFilesRoot.toFile().exists()) {
return;
}
FileEntriesLayer extraFilesLayer;
try {
extraFilesLayer = ContainerBuilderHelper.extraDirectoryLayerConfiguration(jibFilesRoot, AbsoluteUnixPath.get("/"), Collections.emptyMap(), (localPath, ignored2) -> {
try {
return Files.getLastModifiedTime(localPath).toInstant();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
jibContainerBuilder.addFileEntriesLayer(extraFilesLayer);
} catch (IOException e) {
throw new UncheckedIOException("Unable to add extra files in '" + jibFilesRoot.toAbsolutePath().toString() + "' to the container", e);
}
}
Aggregations