use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtension method moveParentDepsToNewLayers.
private ContainerBuildPlan moveParentDepsToNewLayers(ContainerBuildPlan buildPlan, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
logger.log(LogLevel.INFO, "Moving parent dependencies to new layers.");
// The key is the source file path for the parent dependency. We only consider artifacts that
// have been resolved.
Map<Path, Artifact> parentDependencies = getParentDependencies(mavenData).stream().map(Dependency::getArtifact).filter(artifact -> artifact.getFile() != null).collect(Collectors.toMap(artifact -> artifact.getFile().toPath(), artifact -> artifact));
// Parent dependencies that have not been found in any layer (due to different version or
// filtering). Only needed for logging.
Map<Path, Artifact> parentDependenciesNotFound = new HashMap<>(parentDependencies);
List<FileEntriesLayer.Builder> newLayerBuilders = new ArrayList<>();
@SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = ((List<FileEntriesLayer>) buildPlan.getLayers());
for (FileEntriesLayer originalLayer : originalLayers) {
String parentLayerName = originalLayer.getName() + "-parent";
FileEntriesLayer.Builder parentLayerBuilder = FileEntriesLayer.builder().setName(parentLayerName);
FileEntriesLayer.Builder layerBuilder = originalLayer.toBuilder().setEntries(Collections.emptyList());
newLayerBuilders.add(parentLayerBuilder);
newLayerBuilders.add(layerBuilder);
for (FileEntry entry : originalLayer.getEntries()) {
Path sourceFilePath = entry.getSourceFile();
if (parentDependencies.containsKey(sourceFilePath)) {
// move to parent layer
logger.log(LogLevel.DEBUG, "Moving " + sourceFilePath + " to " + parentLayerName + ".");
parentLayerBuilder.addEntry(entry);
// mark parent dep as found
parentDependenciesNotFound.remove(sourceFilePath);
} else {
// keep in original layer
logger.log(LogLevel.DEBUG, "Keeping " + sourceFilePath + " in " + originalLayer.getName() + ".");
layerBuilder.addEntry(entry);
}
}
}
logMissingParentDependencies(logger, parentDependenciesNotFound, originalLayers);
List<FileEntriesLayer> newLayers = newLayerBuilders.stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).collect(Collectors.toList());
return buildPlan.toBuilder().setLayers(newLayers).build();
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.
the class JibLayerFilterExtension method extendContainerBuildPlan.
@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Configuration> config, GradleData gradleData, ExtensionLogger logger) throws JibPluginExtensionException {
logger.log(LogLevel.LIFECYCLE, "Running Jib Layer Filter Extension");
if (!config.isPresent()) {
logger.log(LogLevel.WARN, "Nothing configured for Jib Layer Filter Extension");
return buildPlan;
}
preparePathMatchersAndLayerBuilders(buildPlan, config.get());
ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder();
newPlanBuilder.setLayers(Collections.emptyList());
@SuppressWarnings("unchecked") List<FileEntriesLayer> originalLayers = (List<FileEntriesLayer>) buildPlan.getLayers();
// Start filtering original layers.
for (FileEntriesLayer layer : originalLayers) {
List<FileEntry> filesToKeep = new ArrayList<>();
for (FileEntry entry : layer.getEntries()) {
Optional<String> finalLayerName = determineFinalLayerName(entry, layer.getName());
// Either keep, move, or delete this FileEntry.
if (finalLayerName.isPresent()) {
if (finalLayerName.get().equals(layer.getName())) {
filesToKeep.add(entry);
} else {
FileEntriesLayer.Builder targetLayerBuilder = Verify.verifyNotNull(newToLayers.get(finalLayerName.get()));
targetLayerBuilder.addEntry(entry);
}
}
}
if (!filesToKeep.isEmpty()) {
newPlanBuilder.addLayer(layer.toBuilder().setEntries(filesToKeep).build());
}
}
// Add newly created non-empty to-layers (if any).
newToLayers.values().stream().map(FileEntriesLayer.Builder::build).filter(layer -> !layer.getEntries().isEmpty()).forEach(newPlanBuilder::addLayer);
return newPlanBuilder.build();
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib-extensions by GoogleContainerTools.
the class JibNativeImageExtensionTest method testExtendContainerBuildPlan.
@Test
public void testExtendContainerBuildPlan() throws JibPluginExtensionException, IOException {
Map<String, String> properties = Collections.singletonMap("imageName", "theExecutable");
tempFolder.newFile("theExecutable");
FileEntriesLayer layer = buildLayer("original layer", Paths.get("foo.txt"));
ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer).build();
ContainerBuildPlan newPlan = new JibNativeImageExtension().extendContainerBuildPlan(buildPlan, properties, Optional.empty(), mavenData, logger);
assertEquals(1, newPlan.getLayers().size());
FileEntriesLayer newLayer = (FileEntriesLayer) newPlan.getLayers().get(0);
assertEquals("native image", newLayer.getName());
assertEquals(1, newLayer.getEntries().size());
FileEntry fileEntry = newLayer.getEntries().get(0);
assertEquals(AbsoluteUnixPath.get("/app/theExecutable"), fileEntry.getExtractionPath());
assertEquals(FilePermissions.fromOctalString("755"), fileEntry.getPermissions());
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project geronimo-arthur by apache.
the class JibMojo method prepare.
private JibContainerBuilder prepare() {
try {
final JibContainerBuilder from = Jib.from(ImageReference.parse(this.from));
if (ports != null) {
from.setExposedPorts(Ports.parse(ports));
}
if (labels != null) {
from.setLabels(labels);
}
if (programArguments != null) {
from.setProgramArguments(programArguments);
}
from.setCreationTime(creationTimestamp < 0 ? Instant.now() : Instant.ofEpochMilli(creationTimestamp));
final boolean hasNatives = useLDD || (includeNatives != null && !includeNatives.isEmpty() && !singletonList("false").equals(includeNatives));
final Path source = ofNullable(binarySource).map(File::toPath).orElseGet(() -> Paths.get(requireNonNull(project.getProperties().getProperty(propertiesPrefix + "binary.path"), "No binary path found, ensure to run native-image before or set entrypoint")));
final Map<String, String> env = environment == null ? new TreeMap<>() : new TreeMap<>(environment);
final List<FileEntriesLayer> layers = new ArrayList<>(8);
if (includeCacerts) {
layers.add(findCertificates());
}
String ldLinux = null;
if (hasNatives) {
if (!singletonList("false").equals(includeNatives)) {
layers.add(findNatives());
}
if (useLDD) {
ldLinux = addLddLibsAndFindLdLinux(env, layers);
}
}
if (otherFiles != null && !otherFiles.isEmpty()) {
layers.add(createOthersLayer());
}
layers.add(FileEntriesLayer.builder().setName("Binary").addEntry(new FileEntry(source, AbsoluteUnixPath.get(entrypoint.iterator().next()), FilePermissions.fromOctalString("755"), getTimestamp(source))).build());
from.setFileEntriesLayers(layers);
if (!env.isEmpty()) {
from.setEnvironment(env);
}
if (entrypoint == null || entrypoint.size() < 1) {
throw new IllegalArgumentException("No entrypoint set");
}
from.setEntrypoint(Stream.concat(Stream.concat(Stream.concat(useLDD && ldLinux != null && !skipLdLinuxInEntrypoint ? Stream.of(ldLinux) : Stream.empty(), entrypoint.stream()), hasNatives ? Stream.of("-Djava.library.path=" + nativeRootDir) : Stream.empty()), includeCacerts ? Stream.of("-Djavax.net.ssl.trustStore=" + cacertsTarget) : Stream.empty()).collect(toList()));
return from;
} catch (final InvalidImageReferenceException | IOException e) {
throw new IllegalStateException(e);
}
}
Aggregations