Skip to content

Commit

Permalink
fix: KarafGenerator update
Browse files Browse the repository at this point in the history
- Created Karaf Quickstart
- Fixed problems with FileSet assembly config to include complete directories
- Upgraded images to use quay.io/jkube/jkube-karaf-binary-s2i

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jun 1, 2020
1 parent 2076887 commit 1a5ae1e
Show file tree
Hide file tree
Showing 39 changed files with 1,407 additions and 225 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Usage:
* Fix #210: WebAppGenerator\>JettyAppSeverHandler uses quay.io/jkube/jkube-jetty9-binary-s2i as base image
* Fix #211: pom.xml configured runtime mode `<mode>` is considered instead of `<configuredRuntimeMode>`
* Fix #209: WildFlySwarmGenerator includes required env variables + java options

* Fix #285: fix: KarafGenerator update (Created Karaf Quickstart #188, fix FileSet problems, upgraded base images)

### 1.0.0-alpha-3 (2020-05-06)
* Fix #167: Add CMD for wildfly based applications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.eclipse.jkube.kit.config.JKubeConfiguration;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;

/**
* @author roland
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,38 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import org.eclipse.jkube.kit.config.JKubeConfiguration;
import org.eclipse.jkube.kit.build.service.docker.helper.DockerFileUtil;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyFile;
import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.AssemblyMode;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.archive.ArchiveCompression;
import org.eclipse.jkube.kit.common.archive.JKubeTarArchiver;
import org.eclipse.jkube.kit.common.util.FileUtil;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.build.AssemblyMode;
import org.eclipse.jkube.kit.config.JKubeConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.image.build.DockerFileBuilder;

import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault;
import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getJKubeAssemblyFileSets;
import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getJKubeAssemblyFileSetsExcludes;
import static org.eclipse.jkube.kit.build.core.assembly.JKubeAssemblyConfigurationUtils.getJKubeAssemblyFiles;
import static org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet;

/**
* Tool for creating a docker image tar ball including a Dockerfile for building
* a docker image.
*
* @author roland
* @since 08.05.14
*/
public class DockerAssemblyManager {

Expand Down Expand Up @@ -404,56 +403,14 @@ private Map<File, String> copyFilesToFinalTarballDirectory(
final Map<File, String> filesToPermissionsMap = new HashMap<>();
FileUtil.createDirectory(new File(buildDirs.getOutputDirectory(), assemblyConfiguration.getName()));
for (AssemblyFileSet fileSet : getJKubeAssemblyFileSets(assemblyConfiguration)) {
filesToPermissionsMap.putAll(processJKubeProjectAssemblyFileSet(project, fileSet, buildDirs, assemblyConfiguration));
filesToPermissionsMap.putAll(processAssemblyFileSet(project.getBaseDirectory(), buildDirs.getOutputDirectory(), fileSet, assemblyConfiguration));
}
for (AssemblyFile file : getJKubeAssemblyFiles(assemblyConfiguration)) {
processJKubeProjectAssemblyFile(project, file, buildDirs, assemblyConfiguration);
}
return filesToPermissionsMap;
}

private Map<File, String> processJKubeProjectAssemblyFileSet(JavaProject project,
AssemblyFileSet jkubeProjectAssemblyFileSet, BuildDirs buildDirs,
AssemblyConfiguration jkubeProjectAssemblyConfiguration) throws IOException {

final Map<File, String> fileToPermissionsMap = new HashMap<>();

final List<String> includes = Optional.ofNullable(jkubeProjectAssemblyFileSet.getIncludes())
.orElse(Collections.singletonList(""));
for (String relativePathInclude : includes) {
final File sourceDirectory = project.getBaseDirectory().toPath()
.resolve(jkubeProjectAssemblyFileSet.getDirectory().toPath())
.toFile();
final File sourceFile = new File(sourceDirectory, FileUtil.trimWildcardCharactersFromPath(relativePathInclude));
File destParentFile = new File(buildDirs.getOutputDirectory(), jkubeProjectAssemblyConfiguration.getName());
if (jkubeProjectAssemblyFileSet.getOutputDirectory() != null
&& !jkubeProjectAssemblyFileSet.getOutputDirectory().getName().equals(".")) {
destParentFile = buildDirs.getOutputDirectory().toPath()
.resolve(jkubeProjectAssemblyConfiguration.getName())
.resolve(jkubeProjectAssemblyFileSet.getOutputDirectory().toPath())
.toFile();
}
FileUtil.createDirectory(destParentFile);
File destFile = new File(destParentFile, sourceFile.getName());

if (sourceFile.exists()) {
if (sourceFile.isDirectory()) {
FileUtil.copyDirectoryIfNotExists(sourceFile, destFile);
} else {
FileUtil.copy(sourceFile, destFile);
}
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;
}

private void processJKubeProjectAssemblyFile(
JavaProject project, AssemblyFile assemblyFile, BuildDirs buildDirs, AssemblyConfiguration assemblyConfiguration)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.common.AssemblyFile;
import org.eclipse.jkube.kit.common.AssemblyFileSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.config.image.ImageName;
import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.image.build.CleanupMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.common.util.MapUtil;
import org.eclipse.jkube.kit.config.image.build.Arguments;
import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.image.build.HealthCheckConfiguration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.eclipse.jkube.kit.config.JKubeConfiguration;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Properties;

import org.eclipse.jkube.kit.config.JKubeConfiguration;
import org.eclipse.jkube.kit.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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.config.image.build.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;

import mockit.Expectations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.config.image.build;
package org.eclipse.jkube.kit.common;

import java.io.Serializable;
import java.util.Optional;

import org.eclipse.jkube.kit.common.Assembly;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("JavaDoc")
Expand Down Expand Up @@ -58,7 +57,7 @@ public class AssemblyFileSet implements Serializable {
*/
private boolean filtered;
/**
* A set of files and directory to include.
* A set of files and directory to include. <b>NO WILDCARDS SUPPORTED</b>
*
* <p> If none is present, then everything is included
*
Expand Down Expand Up @@ -88,20 +87,17 @@ public class AssemblyFileSet implements Serializable {
* @return The assembly fileSet file mode value.
*/
private String fileMode;

public void addInclude(String item) {
if (includes == null) {
includes = new ArrayList<>();
}
includes.add(item);
}

public void addExclude(String item) {
if (excludes == null) {
excludes = new ArrayList<>();
}
excludes.add(item);
}
/**
* Similar to a UNIX permission, sets the directory mode of the directories included.
*
* <p> Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1.
*
* <p> For example, the value 0755 translates to User read-write, Group and Other read-only.
*
* @param directoryMode New file mode value for the assembly fileSet.
* @return The assembly fileSet directory mode value.
*/
private String directoryMode;

// Plexus deserialization specific setters
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.config.image.build;
package org.eclipse.jkube.kit.common;

/**
* Various modes how to add file for the tarball for "docker:build".
*
* @author roland
* @since 18/05/15
*/
public enum AssemblyMode {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* 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.kit.common.archive;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.AssemblyConfiguration;
import org.eclipse.jkube.kit.common.AssemblyFileSet;
import org.eclipse.jkube.kit.common.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class AssemblyFileSetUtils {

private static final String PATH_TO_SELF = ".";
private static final String DIRECTORY_CAN_LIST_PERMISSION = "040111";

private AssemblyFileSetUtils() {}

public static Map<File, String> calculateFilePermissions(File destFile, AssemblyFileSet assemblyFileSet) {
final Map<File, String> ret = new HashMap<>();
if (destFile.isDirectory()) {
final String directoryMode = Optional.ofNullable(assemblyFileSet.getDirectoryMode())
.orElse(DIRECTORY_CAN_LIST_PERMISSION);
ret.put(destFile, directoryMode);
FileUtil.listFilesAndDirsRecursivelyInDirectory(destFile).forEach(f -> {
if (f.isDirectory()) {
ret.put(f, directoryMode);
} else if(f.isFile() && assemblyFileSet.getFileMode() != null) {
ret.put(f, assemblyFileSet.getFileMode());
}
});
} else if (destFile.isFile() && assemblyFileSet.getFileMode() != null) {
ret.put(destFile, assemblyFileSet.getFileMode());
}
return ret;
}

/**
* Will copy files from the provided <code>baseDirectory</code> into <code>outputDirectory/assemblyConfiguration.name</code>
* considering the inclusion and exclusion rules defined in the provided {@link AssemblyFileSet}.
*
* <p> <b>NO WILDCARDS ARE SUPPORTED</b>
*
* @param baseDirectory directory from where to source files.
* @param outputDirectory directory where files should be output.
* @param assemblyFileSet fileSet to process.
* @param assemblyConfiguration configuration for assembly.
* @return Map containing the calculated permissions.
* @throws IOException in case something goes wrong when performing File operations.
*
* @see <a href="https://books.sonatype.com/mvnref-book/reference/assemblies-sect-controlling-contents.html">Maven Assemblies</a> (Spec <b>partially</b> compliant)
*/
public static Map<File, String> processAssemblyFileSet(
File baseDirectory, File outputDirectory, AssemblyFileSet assemblyFileSet,
AssemblyConfiguration assemblyConfiguration) throws IOException {

final Map<File, String> fileToPermissionsMap = new HashMap<>();
final File sourceDirectory = resolveSourceDirectory(baseDirectory, assemblyFileSet);
final File targetDirectory = outputDirectory.toPath()
.resolve(Objects.requireNonNull(assemblyConfiguration.getName(), "Assembly Configuration name is required"))
.toFile();
final File destinationDirectory;
if (assemblyFileSet.getOutputDirectory() == null) {
destinationDirectory = new File(targetDirectory, sourceDirectory.getName());
} else if (assemblyFileSet.getOutputDirectory().isAbsolute()) {
destinationDirectory = assemblyFileSet.getOutputDirectory();
} else if (assemblyFileSet.getOutputDirectory().getPath().equals(PATH_TO_SELF)) {
destinationDirectory = targetDirectory;
} else {
destinationDirectory = targetDirectory.toPath().resolve(assemblyFileSet.getOutputDirectory().getPath()).toFile();
}
final List<String> includes = Optional.ofNullable(assemblyFileSet.getIncludes())
.filter(i -> !i.isEmpty())
.orElse(Collections.singletonList(PATH_TO_SELF));
for (String include : includes) {
if (isSelfPath(include)) {
fileToPermissionsMap.putAll(copy(sourceDirectory, destinationDirectory, assemblyFileSet));
} else {
final File sourceFile = new File(sourceDirectory, include);
final File destFile = destinationDirectory.toPath().resolve(sourceDirectory.toPath().relativize(sourceFile.toPath())).toFile();
FileUtil.createDirectory(destFile.getParentFile());
fileToPermissionsMap.putAll(copy(sourceFile, destFile, assemblyFileSet));
}
}
return fileToPermissionsMap;
}


static File resolveSourceDirectory(File baseDirectory, AssemblyFileSet assemblyFileSet) {
if (Objects.requireNonNull(assemblyFileSet.getDirectory(), "Assembly FileSet directory is required").isAbsolute()) {
return assemblyFileSet.getDirectory();
} else {
return baseDirectory.toPath()
.resolve(assemblyFileSet.getDirectory().toPath())
.toFile();
}
}

private static boolean isSelfPath(String path) {
return StringUtils.isBlank(path) || path.equals(PATH_TO_SELF);
}

private static Map<File, String> copy(File source, File target, AssemblyFileSet assemblyFileSet) throws IOException {
if (source.exists()) {
if (source.isDirectory()) {
FileUtil.copyDirectoryIfNotExists(source, target);
} else {
FileUtil.copy(source, target);
}
return calculateFilePermissions(target, assemblyFileSet);
}
return Collections.emptyMap();
}
}
Loading

0 comments on commit 1a5ae1e

Please sign in to comment.