forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (jkube-kit/spring-boot) : SpringBootGenerator utilizes layered j…
…ar if present and use it as Docker layers (eclipse-jkube#1674) + Add LayeredJarGenerator for layered container assembly for spring boot + Add gradle integration test for spring boot layered image generation Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
017fae9
commit 03534bd
Showing
12 changed files
with
448 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...spring-boot/src/main/java/org/eclipse/jkube/springboot/SpringBootLayeredJarExecUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.springboot; | ||
|
||
import org.eclipse.jkube.kit.common.ExternalCommand; | ||
import org.eclipse.jkube.kit.common.KitLogger; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SpringBootLayeredJarExecUtils { | ||
private SpringBootLayeredJarExecUtils() { } | ||
|
||
public static List<String> listLayers(KitLogger kitLogger, File layeredJar) { | ||
LayerListCommand layerListCommand = new LayerListCommand(kitLogger, layeredJar); | ||
try { | ||
layerListCommand.execute(); | ||
return layerListCommand.getLayers(); | ||
} catch (IOException ioException) { | ||
throw new IllegalStateException("Failure in getting spring boot jar layers information", ioException); | ||
} | ||
} | ||
|
||
public static void extractLayers(KitLogger kitLogger, File extractionDir, File layeredJar) { | ||
LayerExtractorCommand layerExtractorCommand = new LayerExtractorCommand(kitLogger, extractionDir, layeredJar); | ||
try { | ||
layerExtractorCommand.execute(); | ||
} catch (IOException ioException) { | ||
throw new IllegalStateException("Failure in extracting spring boot jar layers", ioException); | ||
} | ||
} | ||
|
||
private static class LayerExtractorCommand extends ExternalCommand { | ||
private final File layeredJar; | ||
protected LayerExtractorCommand(KitLogger log, File workDir, File layeredJar) { | ||
super(log, workDir); | ||
this.layeredJar = layeredJar; | ||
} | ||
|
||
@Override | ||
protected String[] getArgs() { | ||
return new String[] { "java", "-Djarmode=layertools", "-jar", layeredJar.getAbsolutePath(), "extract"}; | ||
} | ||
} | ||
|
||
private static class LayerListCommand extends ExternalCommand { | ||
private final List<String> layers; | ||
private final File layeredJar; | ||
protected LayerListCommand(KitLogger log, File layeredJar) { | ||
super(log); | ||
layers = new ArrayList<>(); | ||
this.layeredJar = layeredJar; | ||
} | ||
|
||
@Override | ||
protected String[] getArgs() { | ||
return new String[] { "java", "-Djarmode=layertools", "-jar", layeredJar.getAbsolutePath(), "list"}; | ||
} | ||
|
||
@Override | ||
protected void processLine(String line) { | ||
layers.add(line); | ||
} | ||
|
||
public List<String> getLayers() { | ||
return layers; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...spring-boot/src/main/java/org/eclipse/jkube/springboot/generator/LayeredJarGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.springboot.generator; | ||
|
||
import org.eclipse.jkube.generator.api.GeneratorConfig; | ||
import org.eclipse.jkube.generator.api.GeneratorContext; | ||
import org.eclipse.jkube.generator.javaexec.FatJarDetector; | ||
import org.eclipse.jkube.kit.common.Arguments; | ||
import org.eclipse.jkube.kit.common.Assembly; | ||
import org.eclipse.jkube.kit.common.AssemblyConfiguration; | ||
import org.eclipse.jkube.kit.common.AssemblyFileSet; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.eclipse.jkube.kit.common.util.FileUtil.getRelativePath; | ||
import static org.eclipse.jkube.springboot.SpringBootLayeredJarExecUtils.extractLayers; | ||
import static org.eclipse.jkube.springboot.SpringBootLayeredJarExecUtils.listLayers; | ||
|
||
public class LayeredJarGenerator extends AbstractSpringBootNestedGenerator { | ||
private final FatJarDetector.Result fatJarDetectorResult; | ||
public LayeredJarGenerator(GeneratorContext generatorContext, GeneratorConfig generatorConfig, FatJarDetector.Result result) { | ||
super(generatorContext, generatorConfig); | ||
fatJarDetectorResult = result; | ||
} | ||
|
||
@Override | ||
public Arguments getBuildEntryPoint() { | ||
return Arguments.builder() | ||
.exec(Arrays.asList("java", "org.springframework.boot.loader.JarLauncher")) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getEnv() { | ||
return Collections.singletonMap("JAVA_MAIN_CLASS", "org.springframework.boot.loader.JarLauncher"); | ||
} | ||
|
||
@Override | ||
public AssemblyConfiguration createAssemblyConfiguration() { | ||
getLogger().info("Spring Boot layered jar detected"); | ||
|
||
List<String> layerNames = listLayers(getLogger(), fatJarDetectorResult.getArchiveFile()); | ||
List<Assembly> layerAssemblies = new ArrayList<>(); | ||
extractLayers(getLogger(), getProject().getBuildPackageDirectory(), fatJarDetectorResult.getArchiveFile()); | ||
|
||
for (String springBootLayer : layerNames) { | ||
File layerDir = new File(getProject().getBuildPackageDirectory(), springBootLayer); | ||
layerAssemblies.add(Assembly.builder() | ||
.id(springBootLayer) | ||
.fileSet(AssemblyFileSet.builder() | ||
.outputDirectory(new File(".")) | ||
.directory(getRelativePath(getProject().getBaseDirectory(), layerDir)) | ||
.exclude("*") | ||
.fileMode("0640") | ||
.build()) | ||
.build()); | ||
} | ||
|
||
return AssemblyConfiguration.builder() | ||
.targetDir(getTargetDir()) | ||
.excludeFinalOutputArtifact(true) | ||
.layers(layerAssemblies) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.