Skip to content

Commit

Permalink
refactor: fix watch + standalone assemblies and defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Apr 24, 2020
1 parent 13f5571 commit 2214d9d
Show file tree
Hide file tree
Showing 25 changed files with 297 additions and 232 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* directories.
*
* @author roland
* @since 27/02/15
*/
class BuildDirs {

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,13 @@ private Map<File, String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AssemblyFileSet> getJKubeAssemblyFileSets(AssemblyConfiguration configuration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<String> 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<File> files = FileUtil.listFilesRecursivelyInDirectory(inputDirectory);
List<File> files = FileUtil.listFilesAndDirsRecursivelyInDirectory(inputDirectory);

if (!filesToIncludeNameMap.isEmpty()) {
for (Map.Entry<File, String> entry : filesToIncludeNameMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> buildArgs)
throws DockerAccessException, IOException {
throws IOException {

String imageName = imageConfig.getName();
ImageName.validate(imageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DockerConnectionDetector.DockerHostProvider> dockerHostProviders;
Expand All @@ -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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public NetworkConfig getNetworkingConfig() {
} else if (net != null) {
return NetworkConfig.fromLegacyNetSpec(net);
} else {
return NetworkConfig.builder().build();
return new NetworkConfig();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<File, String> 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(
Expand All @@ -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)) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,13 @@ public static String trimWildcardCharactersFromPath(String filePath) {
return filePath.substring(0, charIndex);
}

public static List<File> listFilesRecursivelyInDirectory(File directory) {
return new ArrayList<>(FileUtils.listFiles(
directory,
new RegexFileFilter("^(.*?)"),
DirectoryFileFilter.DIRECTORY
));
public static List<File> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public void testCreateDirectory() throws IOException {
@Test
public void testListFilesRecursively() throws IOException {
prepareDirectory();
List<File> fileList = FileUtil.listFilesRecursivelyInDirectory(folder.getRoot());
List<File> fileList = FileUtil.listFilesAndDirsRecursivelyInDirectory(folder.getRoot());
assertNotNull(fileList);
assertEquals(4, fileList.size());
assertEquals(6, fileList.size());
}

@Test
public void testCleanDirectory() throws IOException {
prepareDirectory();
List<File> fileList = FileUtil.listFilesRecursivelyInDirectory(folder.getRoot());
assertEquals(4, fileList.size());
List<File> fileList = FileUtil.listFilesAndDirsRecursivelyInDirectory(folder.getRoot());
assertEquals(6, fileList.size());
FileUtil.cleanDirectory(folder.getRoot());
assertFalse(folder.getRoot().exists());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +25,7 @@
import lombok.NoArgsConstructor;
import org.eclipse.jkube.kit.common.Assembly;

@Builder
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@Getter
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 2214d9d

Please sign in to comment.