From 2214d9d9b2931d17eaf4879b69376a9e8b50cd19 Mon Sep 17 00:00:00 2001 From: manusa Date: Thu, 23 Apr 2020 16:27:35 +0200 Subject: [PATCH] refactor: fix watch + standalone assemblies and defaults Signed-off-by: Marc Nuri --- CHANGELOG.md | 9 +- .../kit/build/core/assembly/BuildDirs.java | 3 +- .../core/assembly/DockerAssemblyManager.java | 8 +- .../JKubeAssemblyConfigurationUtils.java | 17 +- .../core/assembly/JKubeBuildTarArchiver.java | 2 +- .../build/service/docker/BuildService.java | 2 +- .../service/docker/DockerAccessFactory.java | 10 ++ .../service/docker/ServiceHubFactory.java | 31 ++-- .../docker/config/RunImageConfiguration.java | 2 +- .../assembly/DockerAssemblyManagerTest.java | 6 +- .../JKubeAssemblyConfigurationUtilsTest.java | 21 ++- .../kit/common/archive/JKubeTarArchiver.java | 19 +-- .../jkube/kit/common/util/FileUtil.java | 13 +- .../jkube/kit/common/util/FileUtilTest.java | 8 +- .../image/build/AssemblyConfiguration.java | 11 +- .../image/build/BuildConfiguration.java | 2 + .../config/image/build/DockerFileBuilder.java | 37 ++--- .../openshift/OpenshiftBuildServiceTest.java | 7 + jkube-kit/jkube-kit-openliberty/pom.xml | 14 -- .../plugin/mojo/build/AbstractDockerMojo.java | 4 +- .../maven/plugin/mojo/build/ResourceMojo.java | 16 +- quickstarts/kit/docker-image/README.md | 56 +++++++ quickstarts/kit/docker-image/pom.xml | 22 ++- .../jkube/quickstart/kit/docker/Main.java | 60 ++----- quickstarts/maven/vertx/pom.xml | 149 +++++++++--------- 25 files changed, 297 insertions(+), 232 deletions(-) create mode 100644 quickstarts/kit/docker-image/README.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 15c3ebe995..82d6ce7406 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,8 +23,11 @@ Usage: ### 1.0.0-SNAPSHOT * Fix #130: Updated HelmMojo documentation * Fix #138: dockerAccessRequired should be false in case of docker build strategy -* Fix #131: Extra-artifact wrongly generated -* Ported PR fabric8io/fabric8-mavne-plugin#1810, Update Fabric8 Images to latest version +* Fix #131: Extra-artifact wrongly generated +* Fix #152: kubernetes namespace is ignored in debug goal +* Ported PR fabric8io/fabric8-maven-plugin#1810, Update Fabric8 Images to latest version +* Fix #136: Refactored configuration model to provide fluent builders and defaults +* Fix #163: Added Quickstart for external Dockerfile ### 1.0.0-alpha-1 (2020-03-27) * Ported PR fabric8io/fabric8-maven-plugin#1792, NullCheck in FileDataSecretEnricher @@ -38,8 +41,6 @@ Usage: * Fix #120: Critical bugs reported by Sonar * Fix #88: Unreachable statement in DockerAssemblyManager * Fix #122: Bug 561261 - jkube-kit - insecure yaml load leading to RCE (CWE-502) -* Fix #152: kubernetes namespace is ignored in debug goal -* Fix #163: Added Quickstart for external Dockerfile ### 0.2.0 (2020-03-05) * Fix #71: script to extract changelog information for notifications diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/BuildDirs.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/BuildDirs.java index dd38653964..d396fa5d2c 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/BuildDirs.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/BuildDirs.java @@ -24,7 +24,6 @@ * directories. * * @author roland - * @since 27/02/15 */ class BuildDirs { @@ -35,7 +34,7 @@ class BuildDirs { * Constructor building up the the output directories * * @param imageName image name for the image to build - * @param params mojo params holding base and global outptput dir + * @param params mojo params holding base and global output dir */ BuildDirs(String imageName, JKubeConfiguration params) { this.params = params; diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManager.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManager.java index 499de0e571..b3fe343f78 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManager.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManager.java @@ -440,7 +440,13 @@ private Map processJKubeProjectAssemblyFileSet(JavaProject project } else { FileUtil.copy(sourceFile, destFile); } - fileToPermissionsMap.put(destFile, jkubeProjectAssemblyFileSet.getFileMode()); + final String fileMode = jkubeProjectAssemblyFileSet.getFileMode(); + if (destFile.isFile()) { + fileToPermissionsMap.put(destFile, fileMode); + } else { + final int directoryCanListPermission = Integer.parseInt(fileMode, 8) + 0111; + fileToPermissionsMap.put(destFile, Integer.toOctalString(directoryCanListPermission)); + } } } return fileToPermissionsMap; diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtils.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtils.java index b875e379a8..f808e9d247 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtils.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtils.java @@ -13,12 +13,14 @@ */ package org.eclipse.jkube.kit.build.core.assembly; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration; import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.eclipse.jkube.kit.common.AssemblyFile; import org.eclipse.jkube.kit.common.AssemblyFileSet; import org.eclipse.jkube.kit.common.Assembly; +import java.io.File; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -28,15 +30,24 @@ public class JKubeAssemblyConfigurationUtils { - private static final String DEFAULT_TARGET_DIR = "/maven"; + private static final String DEFAULT_NAME = "maven"; + private static final String DEFAULT_TARGET_DIR = File.separator.concat(DEFAULT_NAME); private static final String DEFAULT_USER = "root"; private JKubeAssemblyConfigurationUtils() {} static AssemblyConfiguration getAssemblyConfigurationOrCreateDefault(BuildConfiguration buildConfiguration) { - return Optional.ofNullable(buildConfiguration) + final AssemblyConfiguration ac = Optional.ofNullable(buildConfiguration) .map(BuildConfiguration::getAssemblyConfiguration) - .orElse(AssemblyConfiguration.builder().targetDir(DEFAULT_TARGET_DIR).user(DEFAULT_USER).build()); + .orElse(AssemblyConfiguration.builder().user(DEFAULT_USER).build()); + final AssemblyConfiguration.AssemblyConfigurationBuilder builder = ac.toBuilder(); + if (StringUtils.isBlank(ac.getName())) { + builder.name(DEFAULT_NAME); + } + if (StringUtils.isBlank(ac.getTargetDir())) { + builder.targetDir(DEFAULT_TARGET_DIR); + } + return builder.build(); } static List getJKubeAssemblyFileSets(AssemblyConfiguration configuration) { diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeBuildTarArchiver.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeBuildTarArchiver.java index 833fb7b199..35b17be872 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeBuildTarArchiver.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/core/assembly/JKubeBuildTarArchiver.java @@ -53,7 +53,7 @@ public List getFilesNamesToExcludeName() { public File createArchive(File inputDirectory, BuildDirs buildDirs, ArchiveCompression compression) throws IOException { File outputFile = new File(buildDirs.getTemporaryRootDirectory(), "docker-build." + (compression.equals(ArchiveCompression.none) ? "tar" : compression.getFileSuffix())); - List files = FileUtil.listFilesRecursivelyInDirectory(inputDirectory); + List files = FileUtil.listFilesAndDirsRecursivelyInDirectory(inputDirectory); if (!filesToIncludeNameMap.isEmpty()) { for (Map.Entry entry : filesToIncludeNameMap.entrySet()) { diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java index 2c8ee4bc77..d8b462f53b 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java @@ -103,7 +103,7 @@ public void tagImage(String imageName, ImageConfiguration imageConfig) throws Do * @throws IOException in case of any I/O exception */ protected void buildImage(ImageConfiguration imageConfig, JKubeConfiguration params, boolean noCache, Map buildArgs) - throws DockerAccessException, IOException { + throws IOException { String imageName = imageConfig.getName(); ImageName.validate(imageName); diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/DockerAccessFactory.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/DockerAccessFactory.java index d7bbcf7a71..01aed1fe6d 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/DockerAccessFactory.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/DockerAccessFactory.java @@ -121,6 +121,8 @@ public static class DockerAccessContext implements Serializable { private static final long serialVersionUID = -4768689117574797600L; + public static final int DEFAULT_MAX_CONNECTIONS = 100; + private Properties projectProperties; private DockerMachineConfiguration machine; private List dockerHostProviders; @@ -131,6 +133,14 @@ public static class DockerAccessContext implements Serializable { private int maxConnections; private KitLogger log; + public static DockerAccessContext getDefault(KitLogger kitLogger){ + return DockerAccessContext.builder() + .projectProperties(System.getProperties()) + .skipMachine(false) + .maxConnections(DEFAULT_MAX_CONNECTIONS) + .log(kitLogger) + .build(); + } } } diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/ServiceHubFactory.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/ServiceHubFactory.java index 9a96ebd35b..a58175341f 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/ServiceHubFactory.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/ServiceHubFactory.java @@ -23,19 +23,28 @@ */ public class ServiceHubFactory { - // Track started containers - private final ContainerTracker containerTracker = new ContainerTracker(); + private static final LogOutputSpecFactory DEFAULT_LOG_OUTPUT_SPEC_FACTORY = new LogOutputSpecFactory(true, true); + // Track started containers + private final ContainerTracker containerTracker = new ContainerTracker(); - private LogOutputSpecFactory logOutputSpecFactory; + private LogOutputSpecFactory logOutputSpecFactory; - public ServiceHub createServiceHub(DockerAccess access, KitLogger log, LogOutputSpecFactory logSpecFactory) { - this.logOutputSpecFactory = logSpecFactory; - return new ServiceHub(access, containerTracker, DockerAssemblyManager.getInstance(), - log, logSpecFactory); - } + public ServiceHub createServiceHub(KitLogger kitLogger) { + return createServiceHub( + new DockerAccessFactory().createDockerAccess(DockerAccessFactory.DockerAccessContext.getDefault(kitLogger)), + kitLogger, + DEFAULT_LOG_OUTPUT_SPEC_FACTORY + ); + } - public LogOutputSpecFactory getLogOutputSpecFactory() { - return logOutputSpecFactory; - } + public ServiceHub createServiceHub(DockerAccess access, KitLogger kitLogger, LogOutputSpecFactory logSpecFactory) { + this.logOutputSpecFactory = logSpecFactory; + return new ServiceHub(access, containerTracker, DockerAssemblyManager.getInstance(), + kitLogger, logSpecFactory); + } + + public LogOutputSpecFactory getLogOutputSpecFactory() { + return logOutputSpecFactory; + } } diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/config/RunImageConfiguration.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/config/RunImageConfiguration.java index 41fa684a80..0bf413d1c7 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/config/RunImageConfiguration.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/config/RunImageConfiguration.java @@ -213,7 +213,7 @@ public NetworkConfig getNetworkingConfig() { } else if (net != null) { return NetworkConfig.fromLegacyNetSpec(net); } else { - return NetworkConfig.builder().build(); + return new NetworkConfig(); } } diff --git a/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManagerTest.java b/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManagerTest.java index 2ea75f14a0..6b721d5cb8 100644 --- a/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManagerTest.java +++ b/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/DockerAssemblyManagerTest.java @@ -138,6 +138,8 @@ public void testCopyChownValidVerifyGivenDockerfile(@Injectable final KitLogger private BuildConfiguration createBuildConfig() { return BuildConfiguration.builder() .assembly(AssemblyConfiguration.builder() + .name("maven") + .targetDir("/maven") .descriptorRef("artifact") .build()) .build(); @@ -219,7 +221,7 @@ public void testCreateDockerTarArchiveWithoutDockerfile() throws IOException { // Then assertNotNull(dockerArchiveFile); assertTrue(dockerArchiveFile.exists()); - assertEquals(2560, dockerArchiveFile.length()); + assertEquals(3072, dockerArchiveFile.length()); assertTrue(outputDirectory.isDirectory() && outputDirectory.exists()); File buildOutputDir = new File(outputDirectory, "test-image"); assertTrue(buildOutputDir.isDirectory() && buildOutputDir.exists()); @@ -279,7 +281,7 @@ public void testCreateDockerTarArchiveWithDockerfile() throws IOException { // Then assertNotNull(dockerArchiveFile); assertTrue(dockerArchiveFile.exists()); - assertEquals(2560, dockerArchiveFile.length()); + assertEquals(3072, dockerArchiveFile.length()); assertTrue(dockerDirectory.isDirectory() && dockerDirectory.exists()); File buildOutputDir = new File(dockerDirectory, "test-image"); assertTrue(buildOutputDir.isDirectory() && buildOutputDir.exists()); diff --git a/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtilsTest.java b/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtilsTest.java index fd3544590c..87887c4e22 100644 --- a/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtilsTest.java +++ b/jkube-kit/build/service/docker/src/test/java/org/eclipse/jkube/kit/build/core/assembly/JKubeAssemblyConfigurationUtilsTest.java @@ -18,14 +18,14 @@ import java.util.Collections; import java.util.List; +import org.eclipse.jkube.kit.common.Assembly; import org.eclipse.jkube.kit.common.AssemblyFile; import org.eclipse.jkube.kit.common.AssemblyFileSet; -import org.eclipse.jkube.kit.common.Assembly; +import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration; +import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import mockit.Expectations; import mockit.Injectable; -import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration; -import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.junit.Test; import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault; @@ -35,12 +35,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class JKubeAssemblyConfigurationUtilsTest { - @Test public void getAssemblyConfigurationOrCreateDefaultNoConfigurationShouldReturnDefault( @Injectable final BuildConfiguration buildConfiguration) { @@ -53,29 +51,28 @@ public void getAssemblyConfigurationOrCreateDefaultNoConfigurationShouldReturnDe // When final AssemblyConfiguration result = getAssemblyConfigurationOrCreateDefault(buildConfiguration); // Then + assertEquals("maven", result.getName()); assertEquals("/maven", result.getTargetDir()); assertEquals("root", result.getUser()); } @Test public void getAssemblyConfigurationOrCreateDefaultWithConfigurationShouldReturnConfiguration( - @Injectable final BuildConfiguration buildConfiguration, @Injectable AssemblyConfiguration configuration) { + @Injectable final BuildConfiguration buildConfiguration) { // Given + final AssemblyConfiguration configuration = AssemblyConfiguration.builder().user("OtherUser").name("ImageName").build(); new Expectations() {{ buildConfiguration.getAssemblyConfiguration(); result = configuration; - configuration.getUser(); - result = "OtherUser"; - configuration.getName(); - result = "ImageName"; }}; // When final AssemblyConfiguration result = getAssemblyConfigurationOrCreateDefault(buildConfiguration); // Then - assertNull(result.getTargetDir()); - assertEquals("OtherUser", result.getUser()); + assertNotNull(result); assertEquals("ImageName", result.getName()); + assertEquals(File.separator + "maven", result.getTargetDir()); + assertEquals("OtherUser", result.getUser()); } diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/archive/JKubeTarArchiver.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/archive/JKubeTarArchiver.java index 459f0729e3..47dfbb609f 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/archive/JKubeTarArchiver.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/archive/JKubeTarArchiver.java @@ -36,15 +36,8 @@ private JKubeTarArchiver() {} public static File createTarBallOfDirectory( File outputFile, File inputDirectory, ArchiveCompression compression) throws IOException { - return JKubeTarArchiver.createTarBall(outputFile, inputDirectory, FileUtil.listFilesRecursivelyInDirectory(inputDirectory), Collections.emptyMap(), compression); - } - - public static File createTarBallOfDirectory( - File outputFile, File inputDirectory, Map fileToPermissionsMap, ArchiveCompression compression) - throws IOException { - - return JKubeTarArchiver.createTarBall(outputFile, inputDirectory, FileUtil.listFilesRecursivelyInDirectory(inputDirectory), - fileToPermissionsMap, compression); + return JKubeTarArchiver.createTarBall(outputFile, inputDirectory, + FileUtil.listFilesAndDirsRecursivelyInDirectory(inputDirectory), Collections.emptyMap(), compression); } public static File createTarBall( @@ -55,7 +48,7 @@ public static File createTarBall( try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) { - TarArchiveOutputStream tarArchiveOutputStream = null; + final TarArchiveOutputStream tarArchiveOutputStream; if (compression.equals(ArchiveCompression.gzip)) { tarArchiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(bufferedOutputStream)); } else if (compression.equals(ArchiveCompression.bzip2)) { @@ -75,11 +68,15 @@ public static File createTarBall( tarEntry.setSize(currentFile.length()); if (fileToPermissionsMap.containsKey(currentFile)) { tarEntry.setMode(Integer.parseInt(fileToPermissionsMap.get(currentFile), 8)); + } else if (currentFile.isDirectory()) { + tarEntry.setMode(TarArchiveEntry.DEFAULT_DIR_MODE); } tarArchiveOutputStream.putArchiveEntry(tarEntry); tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); - tarArchiveOutputStream.write(IOUtils.toByteArray(new FileInputStream(currentFile))); + if (currentFile.isFile()) { + tarArchiveOutputStream.write(IOUtils.toByteArray(new FileInputStream(currentFile))); + } tarArchiveOutputStream.closeArchiveEntry(); } tarArchiveOutputStream.close(); diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/FileUtil.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/FileUtil.java index a9a6952c4d..168bcec3df 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/FileUtil.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/FileUtil.java @@ -338,12 +338,13 @@ public static String trimWildcardCharactersFromPath(String filePath) { return filePath.substring(0, charIndex); } - public static List listFilesRecursivelyInDirectory(File directory) { - return new ArrayList<>(FileUtils.listFiles( - directory, - new RegexFileFilter("^(.*?)"), - DirectoryFileFilter.DIRECTORY - )); + public static List listFilesAndDirsRecursivelyInDirectory(File directory) { + return FileUtils.listFilesAndDirs(directory, new RegexFileFilter("^(.*?)"), + DirectoryFileFilter.DIRECTORY + ) + .stream() + .filter(f -> !f.equals(directory)) + .collect(Collectors.toList()); } public static void createDirectory(File directory) throws IOException { diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/FileUtilTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/FileUtilTest.java index c22c706b36..e7bf0c81a0 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/FileUtilTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/FileUtilTest.java @@ -43,16 +43,16 @@ public void testCreateDirectory() throws IOException { @Test public void testListFilesRecursively() throws IOException { prepareDirectory(); - List fileList = FileUtil.listFilesRecursivelyInDirectory(folder.getRoot()); + List fileList = FileUtil.listFilesAndDirsRecursivelyInDirectory(folder.getRoot()); assertNotNull(fileList); - assertEquals(4, fileList.size()); + assertEquals(6, fileList.size()); } @Test public void testCleanDirectory() throws IOException { prepareDirectory(); - List fileList = FileUtil.listFilesRecursivelyInDirectory(folder.getRoot()); - assertEquals(4, fileList.size()); + List fileList = FileUtil.listFilesAndDirsRecursivelyInDirectory(folder.getRoot()); + assertEquals(6, fileList.size()); FileUtil.cleanDirectory(folder.getRoot()); assertFalse(folder.getRoot().exists()); } diff --git a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/AssemblyConfiguration.java b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/AssemblyConfiguration.java index f158528ec3..3f9229a596 100644 --- a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/AssemblyConfiguration.java +++ b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/AssemblyConfiguration.java @@ -13,7 +13,6 @@ */ package org.eclipse.jkube.kit.config.image.build; -import java.io.File; import java.io.Serializable; import java.util.Objects; import java.util.Optional; @@ -26,7 +25,7 @@ import lombok.NoArgsConstructor; import org.eclipse.jkube.kit.common.Assembly; -@Builder +@Builder(toBuilder = true) @AllArgsConstructor @NoArgsConstructor @Getter @@ -77,14 +76,6 @@ public Boolean exportTargetDir() { } } - public String getName() { - return Optional.ofNullable(name).orElse("maven"); - } - - public String getTargetDir() { - return Optional.ofNullable(targetDir).orElse(File.separator.concat(getName())); - } - public AssemblyMode getMode() { return mode != null ? mode : AssemblyMode.dir; } diff --git a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java index 58d8c6130f..b21baa7b50 100644 --- a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java +++ b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java @@ -98,7 +98,9 @@ public class BuildConfiguration implements Serializable { private List volumes; @Singular private List tags; + @Singular("putEnv") private Map env; + @Singular private Map labels; private Map args; private Arguments entryPoint; diff --git a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/DockerFileBuilder.java b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/DockerFileBuilder.java index 6f93e0a2a0..4c219ee967 100644 --- a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/DockerFileBuilder.java +++ b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/DockerFileBuilder.java @@ -19,10 +19,11 @@ import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.StringJoiner; +import java.util.Optional; import java.util.UUID; import java.util.function.Function; import java.util.regex.Matcher; @@ -35,12 +36,9 @@ * Create a dockerfile * * @author roland - * @since 17.04.14 */ public class DockerFileBuilder { - private static final StringJoiner JOIN_ON_COMMA = new StringJoiner("\",\""); - // Base image to use as from private String baseImage; @@ -108,7 +106,7 @@ public File write(File destDir) throws IOException { * @return the dockerfile create * @throws IllegalArgumentException if no src/dest entries have been added */ - public String content() throws IllegalArgumentException { + public String content() { StringBuilder b = new StringBuilder(); @@ -263,8 +261,8 @@ private String createKeyValue(String key, String value) { if (value == null || value.isEmpty()) { return sb.append("\"\"").toString(); } - StringBuilder valBuf = new StringBuilder(); - boolean toBeQuoted = false; + StringBuilder valBuf = new StringBuilder(); + boolean toBeQuoted = false; for (int i = 0; i < value.length(); ++i) { char c = value.charAt(i); switch (c) { @@ -291,7 +289,7 @@ private String createKeyValue(String key, String value) { } private void addPorts(StringBuilder b) { - if (ports.size() > 0) { + if (!ports.isEmpty()) { String[] portsS = new String[ports.size()]; int i = 0; for(String port : ports) { @@ -301,7 +299,7 @@ private void addPorts(StringBuilder b) { } } - private String validatePortExposure(String input) throws IllegalArgumentException { + private String validatePortExposure(String input) { try { Matcher matcher = Pattern.compile("(.*?)(?:/(tcp|udp))?$", Pattern.CASE_INSENSITIVE).matcher(input); // Matches always. If there is a tcp/udp protocol, should end up in the second group @@ -324,11 +322,11 @@ private void addOptimisation() { } } - private void addRun(StringBuilder b) { - for (String run : runCmds) { + private void addRun(StringBuilder b) { + for (String run : runCmds) { DockerFileKeyword.RUN.addTo(b, run); - } - } + } + } private void addVolumes(StringBuilder b) { if (exportTargetDir != null ? exportTargetDir : baseImage == null) { @@ -425,13 +423,9 @@ public DockerFileBuilder expose(List ports) { * @return DockerFileBuilder docker file builder */ public DockerFileBuilder run(List runCmds) { - if (runCmds != null) { - for (String cmd : runCmds) { - if (!StringUtils.isEmpty(cmd)) { - this.runCmds.add(cmd); - } - } - } + Optional.ofNullable(runCmds).orElse(Collections.emptyList()).stream() + .filter(StringUtils::isNotEmpty) + .forEach(this.runCmds::add); return this; } @@ -478,7 +472,8 @@ private void validateMap(Map env) { // All entries required, destination is relative to exportDir private static final class CopyEntry { - private String source,destination; + private String source; + private String destination; private CopyEntry(String src, String dest) { source = src; diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceTest.java index 1098a3d4e2..ec8924f37f 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceTest.java @@ -113,6 +113,13 @@ public void init() throws Exception { ); result = dockerFile; minTimes = 0; + jKubeServiceHub.getDockerServiceHub().getArchiveService().createDockerBuildArchive( + withAny(null), + withAny(null), + withAny(null) + ); + result = dockerFile; + minTimes = 0; jKubeServiceHub.getConfiguration().getProject(); result = jKubeServiceHub.getConfiguration().getProject(); minTimes = 0; diff --git a/jkube-kit/jkube-kit-openliberty/pom.xml b/jkube-kit/jkube-kit-openliberty/pom.xml index f6f24fe3c2..2fbe9db1f2 100644 --- a/jkube-kit/jkube-kit-openliberty/pom.xml +++ b/jkube-kit/jkube-kit-openliberty/pom.xml @@ -28,44 +28,30 @@ JKube Kit :: Open Liberty - - org.eclipse.jkube - jkube-kit-enricher-api - - org.eclipse.jkube jkube-kit-enricher-specific - org.eclipse.jkube jkube-kit-generator-java-exec - - org.eclipse.jkube - jkube-kit-common - - junit junit test - org.hamcrest hamcrest-all test - org.jmockit jmockit test - org.assertj assertj-core diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java index efec6d2a93..18fb75fcae 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java @@ -96,6 +96,8 @@ import org.fusesource.jansi.Ansi; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; +import static org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory.DockerAccessContext.DEFAULT_MAX_CONNECTIONS; + public abstract class AbstractDockerMojo extends AbstractMojo implements ConfigHelper.Customizer, Contextualizable { public static final String DMP_PLUGIN_DESCRIPTOR = "META-INF/maven/org.eclipse.jkube/k8s-plugin"; public static final String DOCKER_EXTRA_DIR = "docker-extra"; @@ -157,7 +159,7 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements ConfigH protected boolean skipMachine; // maximum connection to use in parallel for connecting the docker host - @Parameter(property = "docker.maxConnections", defaultValue = "100") + @Parameter(property = "docker.maxConnections", defaultValue = "" + DEFAULT_MAX_CONNECTIONS) protected int maxConnections; // Whether to use color diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java index 47c2f61002..8a9082d352 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java @@ -455,23 +455,17 @@ private KubernetesList generateResources(PlatformMode platformMode, List pathname.isDirectory()); + File[] profileDirs = resourceDir.listFiles(File::isDirectory); if (profileDirs != null) { for (File profileDir : profileDirs) { - Profile profile = ProfileUtil.findProfile(profileDir.getName(), resourceDir); - if (profile == null) { - throw new MojoExecutionException(String.format("Invalid profile '%s' given as directory in %s. " + - "Please either define a profile of this name or move this directory away", - profileDir.getName(), resourceDir)); - } - - ProcessorConfig enricherConfig = profile.getEnricherConfig(); + Profile foundProfile = ProfileUtil.findProfile(profileDir.getName(), resourceDir); + ProcessorConfig enricherConfig = foundProfile.getEnricherConfig(); File[] resourceFiles = KubernetesResourceUtil.listResourceFragments(profileDir); if (resourceFiles.length > 0) { KubernetesListBuilder profileBuilder = readResourceFragments(platformMode, resourceFiles); diff --git a/quickstarts/kit/docker-image/README.md b/quickstarts/kit/docker-image/README.md new file mode 100644 index 0000000000..65c611c163 --- /dev/null +++ b/quickstarts/kit/docker-image/README.md @@ -0,0 +1,56 @@ +# JKube Kit - Docker Image Build Example + +This [quickstart](../../../quickstarts) will generate a Docker image using JKube Kit API. + +Image configuration can be altered programmatically in the following section: + +```java +final ImageConfiguration imageConfiguration = ImageConfiguration.builder() + .name("jkube-example") + .run(RunImageConfiguration.builder().user("1000").build()) + .build(BuildConfiguration.builder() + .putEnv("MY_VAR", "value") + .putEnv("MY_OTHER_VAR", "true") + .label("maintainer", "JKube Devs") + .port("80/tcp") + .maintainer("JKube Devs") + .from("busybox") + .cmd(Arguments.builder().shell("/bin/sh").build()) + .build()) + .build(); +``` + +## How to run + +Run the `mvn package` from your terminal. + +Once the image is built a success informative message will print the generated image id. + +You can now run the generated image by invoking `docker run -it ${imageId}`. + +```shell script +$ mvn clean package +# ... +[INFO] --- exec-maven-plugin:1.2.1:java (default) @ docker-image --- +Initiating default JKube configuration and required services... + - Creating Docker Service Hub + - Creating Docker Build Service Configuration + - Creating configuration for JKube +Creating configuration for example Docker Image +SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". +SLF4J: Defaulting to no-operation (NOP) logger implementation +SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. +Build args set {} +Build args set {} +[jkube-example:latest]: Created docker-build.tar in 73 milliseconds +[jkube-example:latest]: Built image sha256:1e648 +Deleted: "sha256:d51117140b834c19506d7ec8132861f23c5a8b19843f89f241522c3265e0c10f" +Deleted: "sha256:4a19816ec525a9975864f89e5a4349be9e18f5a75819758fc0ccfa741c111145" +Deleted: "sha256:b591658b980f4a906f173d86db212f32b2005a0bc06db346f5e5d66ddba88070" +Deleted: "sha256:14f2f99860068a0f58fd7ff861008e85a8ff6e9049577eb041868765e5553ec1" +Deleted: "sha256:bd42432d1bd00f27813542bf82226edfcb88718f4bf3b4b8bcaa9c6fd10f6d58" +[jkube-example:latest]: Removed old image sha256:d5111 +Docker image built successfully (sha256:1e648)! +$ docker run -it 1e648 +/ # +``` \ No newline at end of file diff --git a/quickstarts/kit/docker-image/pom.xml b/quickstarts/kit/docker-image/pom.xml index f0575ead25..8b7b3af344 100644 --- a/quickstarts/kit/docker-image/pom.xml +++ b/quickstarts/kit/docker-image/pom.xml @@ -37,6 +37,24 @@ ${project.version} - - + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + + java + + package + + + + org.eclipse.jkube.quickstart.kit.docker.Main + + + + diff --git a/quickstarts/kit/docker-image/src/main/java/org/eclipse/jkube/quickstart/kit/docker/Main.java b/quickstarts/kit/docker-image/src/main/java/org/eclipse/jkube/quickstart/kit/docker/Main.java index 1d3b3fcfbb..f02f2e415c 100644 --- a/quickstarts/kit/docker-image/src/main/java/org/eclipse/jkube/quickstart/kit/docker/Main.java +++ b/quickstarts/kit/docker-image/src/main/java/org/eclipse/jkube/quickstart/kit/docker/Main.java @@ -13,35 +13,27 @@ */ package org.eclipse.jkube.quickstart.kit.docker; -import org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory; +import java.io.File; + import org.eclipse.jkube.kit.build.service.docker.ImageConfiguration; import org.eclipse.jkube.kit.build.service.docker.ServiceHub; import org.eclipse.jkube.kit.build.service.docker.ServiceHubFactory; -import org.eclipse.jkube.kit.build.service.docker.access.log.LogOutputSpecFactory; +import org.eclipse.jkube.kit.build.service.docker.config.RunImageConfiguration; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.config.JKubeConfiguration; +import org.eclipse.jkube.kit.config.image.build.Arguments; import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.eclipse.jkube.kit.config.resource.RuntimeMode; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; -import org.eclipse.jkube.kit.config.service.JKubeServiceException; import org.eclipse.jkube.kit.config.service.JKubeServiceHub; -import java.io.File; - public class Main { public static void main(String[] args) { final KitLogger kitLogger = new KitLogger.StdoutLogger(); kitLogger.info("Initiating default JKube configuration and required services..."); - kitLogger.info(" - Creating DockerAccessContext"); - final DockerAccessFactory.DockerAccessContext dac = DockerAccessFactory.DockerAccessContext.builder() - .projectProperties(System.getProperties()).skipMachine(false).maxConnections(100).log(kitLogger) - .build(); kitLogger.info(" - Creating Docker Service Hub"); - final ServiceHub serviceHub = new ServiceHubFactory().createServiceHub( - new DockerAccessFactory().createDockerAccess(dac), - kitLogger, - new LogOutputSpecFactory(false, true)); + final ServiceHub serviceHub = new ServiceHubFactory().createServiceHub(kitLogger); kitLogger.info(" - Creating Docker Build Service Configuration"); final BuildServiceConfig dockerBuildServiceConfig = BuildServiceConfig.builder().build(); kitLogger.info(" - Creating configuration for JKube"); @@ -53,8 +45,15 @@ public static void main(String[] args) { kitLogger.info("Creating configuration for example Docker Image"); final ImageConfiguration imageConfiguration = ImageConfiguration.builder() .name("jkube-example") + .run(RunImageConfiguration.builder().user("1000").build()) .build(BuildConfiguration.builder() - .from("containous/whoami") + .putEnv("MY_VAR", "value") + .putEnv("MY_OTHER_VAR", "true") + .label("maintainer", "JKube Devs") + .port("80/tcp") + .maintainer("JKube Devs") + .from("busybox") + .cmd(Arguments.builder().shell("/bin/sh").build()) .build()) .build(); try ( @@ -66,36 +65,11 @@ public static void main(String[] args) { .buildServiceConfig(dockerBuildServiceConfig) .build()) { jKubeServiceHub.getBuildService().build(imageConfiguration); -// jKubeServiceHub.getApplyService().applyResource(); - } catch (JKubeServiceException ex) { + final String imageId = jKubeServiceHub.getDockerServiceHub().getDockerAccess().getImageId("jkube-example"); + kitLogger.info("Docker image built successfully (%s)!", imageId); + System.exit(0); + } catch (Exception ex) { kitLogger.error("Error occurred: '%s'", ex.getMessage()); - ex.printStackTrace(); } } - // - // - // private static BuildServiceConfig initBuildServiceConfig() throws DependencyResolutionRequiredException { - // return new BuildServiceConfig.Builder() - // .dockerBuildContext(getBuildContext()) - // .dockerMavenBuildContext(createMojoParameters()) - // .buildRecreateMode(BuildRecreateMode.fromParameter(buildRecreate)) - // .openshiftBuildStrategy(buildStrategy) - // .openshiftPullSecret(openshiftPullSecret) - // .s2iBuildNameSuffix(s2iBuildNameSuffix) - // .s2iImageStreamLookupPolicyLocal(s2iImageStreamLookupPolicyLocal) - // .forcePullEnabled(forcePull) - // .imagePullManager(getImagePullManager(imagePullPolicy, autoPull)) - // .buildDirectory(project.getBuild().getDirectory()) - // .attacher((classifier, destFile) -> { - // if (destFile.exists()) { - // projectHelper.attachArtifact(project, "yml", classifier, destFile); - // } - // }) - // .enricherTask(builder -> { - // EnricherManager enricherManager = new EnricherManager(resources, getEnricherContext(), MavenUtil.getCompileClasspathElementsIfRequested(project, useProjectClasspath)); - // enricherManager.enrich(PlatformMode.kubernetes, builder); - // enricherManager.enrich(PlatformMode.openshift, builder); - // }) - // .build(); - // } } diff --git a/quickstarts/maven/vertx/pom.xml b/quickstarts/maven/vertx/pom.xml index 6c2a20aca7..dedd663e87 100644 --- a/quickstarts/maven/vertx/pom.xml +++ b/quickstarts/maven/vertx/pom.xml @@ -89,79 +89,86 @@ kubernetes - - - org.eclipse.jkube - kubernetes-maven-plugin - ${project.version} - - - jkube - - resource - build - - - - - - - - io.vertx.core.Launcher - - - - - - - true - - - - - - - - - - - openshift - - - - org.eclipse.jkube - openshift-maven-plugin - ${project.version} - - - jkube - - resource - build - - - - - - - - io.vertx.core.Launcher - - - - - - - true - - - - - - - + + + org.eclipse.jkube + kubernetes-maven-plugin + ${project.version} + + + jkube + + resource + build + + + + + + + + io.vertx.core.Launcher + + + + + + + NodePort + + + + + + + true + + + + + + + + + openshift + + + + org.eclipse.jkube + openshift-maven-plugin + ${project.version} + + + jkube + + resource + build + + + + + + + + io.vertx.core.Launcher + + + + + + + true + + + + + + + + +