Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
feat(#1375): Abstracts Enrichers logic to not depend on Maven interna…
Browse files Browse the repository at this point in the history
…ls (#1411)
  • Loading branch information
lordofthejars authored and rhuss committed Oct 11, 2018
1 parent 9dd0e15 commit 5026685
Show file tree
Hide file tree
Showing 95 changed files with 1,929 additions and 1,112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,17 +36,17 @@ public class ExternalEnvVarHandler {
public static final String ENVIRONMENT_SCHEMA_FILE = "io/fabric8/environment/schema.json";
private static ObjectMapper objectMapper = createObjectMapper();

public static Map<String, String> getExportedEnvironmentVariables(MavenProject project, Map<String, String> envVars) {
Map<String, String> ret = getEnvironmentVarsFromJsonSchema(project, envVars);
public static Map<String, String> getExportedEnvironmentVariables(URLClassLoader compileClassloader, String outputDirectory, Map<String, String> envVars) {
Map<String, String> ret = getEnvironmentVarsFromJsonSchema(compileClassloader, outputDirectory, envVars);
ret.putAll(envVars);
return ret;
}

// ==================================================================================================

private static Map<String, String> getEnvironmentVarsFromJsonSchema(MavenProject project, Map<String, String> envVars) {
private static Map<String, String> getEnvironmentVarsFromJsonSchema(URLClassLoader compileClassloader, String outputDirectory, Map<String, String> envVars) {
Map<String, String> ret = new TreeMap<>();
JsonSchema schema = getEnvironmentVariableJsonSchema(project, envVars);
JsonSchema schema = getEnvironmentVariableJsonSchema(compileClassloader, outputDirectory, envVars);
Map<String, JsonSchemaProperty> properties = schema.getProperties();
Set<Map.Entry<String, JsonSchemaProperty>> entries = properties.entrySet();
for (Map.Entry<String, JsonSchemaProperty> entry : entries) {
Expand All @@ -56,10 +57,10 @@ private static Map<String, String> getEnvironmentVarsFromJsonSchema(MavenProject
return ret;
}

private static JsonSchema getEnvironmentVariableJsonSchema(MavenProject project, Map<String, String> envVars) {
private static JsonSchema getEnvironmentVariableJsonSchema(URLClassLoader compileClassloader, String outputDirectory, Map<String, String> envVars) {
try {
JsonSchema schema = ExternalEnvVarHandler.loadEnvironmentSchemas(MavenUtil.getCompileClassLoader(project),
project.getBuild().getOutputDirectory());
JsonSchema schema = ExternalEnvVarHandler.loadEnvironmentSchemas(compileClassloader,
outputDirectory);
if (schema == null) {
schema = new JsonSchema();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
package io.fabric8.maven.core.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ContainerPort;
Expand All @@ -30,14 +26,17 @@
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import io.fabric8.maven.core.config.ResourceConfig;
import io.fabric8.maven.core.config.VolumeConfig;
import io.fabric8.maven.core.model.Artifact;
import io.fabric8.maven.core.util.kubernetes.KubernetesResourceUtil;
import io.fabric8.maven.docker.access.PortMapping;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.project.MavenProject;
import org.json.JSONArray;
import org.json.JSONObject;

Expand All @@ -49,12 +48,14 @@ class ContainerHandler {

private final EnvVarHandler envVarHandler;
private final ProbeHandler probeHandler;
private final MavenProject project;
private final Properties configurationProperties;
private final Artifact artifact;

public ContainerHandler(MavenProject project, EnvVarHandler envVarHandler, ProbeHandler probeHandler) {
public ContainerHandler(Properties configurationProperties, Artifact artifact, EnvVarHandler envVarHandler, ProbeHandler probeHandler) {
this.envVarHandler = envVarHandler;
this.probeHandler = probeHandler;
this.project = project;
this.configurationProperties = configurationProperties;
this.artifact = artifact;
}

List<Container> getContainers(ResourceConfig config, List<ImageConfiguration> images) {
Expand All @@ -66,7 +67,7 @@ List<Container> getContainers(ResourceConfig config, List<ImageConfiguration> im
Probe readinessProbe = probeHandler.getProbe(config.getReadiness());

Container container = new ContainerBuilder()
.withName(KubernetesResourceUtil.extractContainerName(project, imageConfig))
.withName(KubernetesResourceUtil.extractContainerName(this.artifact, imageConfig))
.withImage(getImageName(imageConfig))
.withImagePullPolicy(getImagePullPolicy(config))
.withEnv(envVarHandler.getEnvironmentVariables(config.getEnv()))
Expand All @@ -85,9 +86,8 @@ List<Container> getContainers(ResourceConfig config, List<ImageConfiguration> im

private String getImagePullPolicy(ResourceConfig config) {
String pullPolicy = config.getImagePullPolicy();
String version = project.getVersion();
if (StringUtils.isBlank(pullPolicy) &&
version != null && version.endsWith("SNAPSHOT")) {
this.artifact.getVersion() != null && this.artifact.getVersion().endsWith("SNAPSHOT")) {
// TODO: Is that what we want ?
return "PullAlways";
}
Expand All @@ -98,7 +98,7 @@ private String getImageName(ImageConfiguration imageConfiguration) {
if (StringUtils.isBlank(imageConfiguration.getName())) {
return null;
}
Properties props = EnvUtil.getPropertiesWithSystemOverrides(project);
Properties props = getPropertiesWithSystemOverrides(this.configurationProperties);
String configuredRegistry = EnvUtil.fistRegistryOf(
imageConfiguration.getRegistry(),
props.getProperty("docker.pull.registry"),
Expand All @@ -107,6 +107,16 @@ private String getImageName(ImageConfiguration imageConfiguration) {
return new ImageName(imageConfiguration.getName()).getFullName(configuredRegistry);
}

private Properties getPropertiesWithSystemOverrides(Properties configurationProperties) {

if (configurationProperties == null) {
configurationProperties = new Properties();
}

configurationProperties.putAll(System.getProperties());
return configurationProperties;
}

private SecurityContext createSecurityContext(ResourceConfig config) {
return new SecurityContextBuilder()
.withPrivileged(config.isContainerPrivileged())
Expand Down Expand Up @@ -139,7 +149,7 @@ private List<ContainerPort> getContainerPorts(ImageConfiguration imageConfig) {
List<String> ports = buildConfig.getPorts();
if (!ports.isEmpty()) {
List<ContainerPort> ret = new ArrayList<>();
PortMapping portMapping = new PortMapping(ports, project.getProperties());
PortMapping portMapping = new PortMapping(ports, configurationProperties);
JSONArray portSpecs = portMapping.toJson();
for (int i = 0; i < portSpecs.length(); i ++) {
JSONObject portSpec = portSpecs.getJSONObject(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,34 @@
*/
package io.fabric8.maven.core.handler;

import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.maven.core.extenvvar.ExternalEnvVarHandler;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.maven.core.extenvvar.ExternalEnvVarHandler;
import org.apache.maven.project.MavenProject;

/**
* @author roland
* @since 08/04/16
*/
class EnvVarHandler {

private MavenProject project;
private URLClassLoader urlClassLoader;
private String outputDirectory;

EnvVarHandler(MavenProject project) {
this.project = project;
EnvVarHandler(URLClassLoader compileClassloader, String outputDirectory) {
this.urlClassLoader = compileClassloader;
this.outputDirectory = outputDirectory;
}

List<EnvVar> getEnvironmentVariables(Map<String, String> envVars) {

List<EnvVar> ret = new ArrayList<>();

Map<String, String> envs = ExternalEnvVarHandler.getExportedEnvironmentVariables(project, envVars);
Map<String, String> envs = ExternalEnvVarHandler.getExportedEnvironmentVariables(this.urlClassLoader, this.outputDirectory, envVars);
Map<String, EnvVar> envMap = convertToEnvVarMap(envs);
ret.addAll(envMap.values());

Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/io/fabric8/maven/core/handler/HandlerHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package io.fabric8.maven.core.handler;

import org.apache.maven.project.MavenProject;
import io.fabric8.maven.core.model.Artifact;
import java.net.URLClassLoader;
import java.util.Properties;

/**
* @author roland
Expand All @@ -31,10 +33,10 @@ public class HandlerHub {
private final DaemonSetHandler daemonSetHandler;
private final JobHandler jobHandler;

public HandlerHub(MavenProject project) {
public HandlerHub(URLClassLoader compileClassloader, String outputDirectory, Artifact artifact, Properties configuration) {
ProbeHandler probeHandler = new ProbeHandler();
EnvVarHandler envVarHandler = new EnvVarHandler(project);
ContainerHandler containerHandler = new ContainerHandler(project, envVarHandler, probeHandler);
EnvVarHandler envVarHandler = new EnvVarHandler(compileClassloader, outputDirectory);
ContainerHandler containerHandler = new ContainerHandler(configuration, artifact, envVarHandler, probeHandler);
PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);

deploymentHandler = new DeploymentHandler(podTemplateHandler);
Expand Down
36 changes: 36 additions & 0 deletions core/src/main/java/io/fabric8/maven/core/model/Artifact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.fabric8.maven.core.model;

public class Artifact {

private String groupId;
private String artifactId;
private String version;

public Artifact() {
this("unknown", "empty-project", "0");
}

public Artifact(String version) {
this();
this.version = version;
}

public Artifact(String groupId, String artifactId, String version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
}

public String getGroupId() {
return groupId;
}

public String getArtifactId() {
return artifactId;
}

public String getVersion() {
return version;
}

}
4 changes: 2 additions & 2 deletions core/src/main/java/io/fabric8/maven/core/util/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ private static String convertToClass(String name, String prefix) {
}


public static URLClassLoader createProjectClassLoader(final MavenProject project, Logger log) {
public static URLClassLoader createProjectClassLoader(List<String> elements, Logger log) {

try {

List<URL> compileJars = new ArrayList<>();

for (String element : project.getCompileClasspathElements()) {
for (String element : elements) {
compileJars.add(new File(element).toURI().toURL());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,28 @@
*/
package io.fabric8.maven.core.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.json.JSONObject;

public class DockerServerUtil {
public static Server getServer(final Settings settings, final String serverId) {
if (settings == null || StringUtils.isBlank(serverId)) {
return null;
}
return settings.getServer(serverId);
}

public static String getDockerJsonConfigString(final Settings settings, final String serverId) {
Server server = getServer(settings, serverId);
if (server == null) {
return new String();
public static final String EMAIL = "email";

public static String getDockerJsonConfigString(final String serverId, final Map<String, String> auth) {

if (auth == null || auth.isEmpty()) {
return "";
}

Map<String, String> auth = new HashMap();
auth.put("username", server.getUsername());
auth.put("password", server.getPassword());

String mail = getConfigurationValue(server, "email");
if (StringUtils.isBlank(mail)) {
mail = "foo@foo.com";
if (!auth.containsKey(EMAIL) || StringUtils.isBlank(auth.get(EMAIL))) {
auth.put(EMAIL, "foo@foo.com");
}
auth.put("email", mail);

JSONObject json = new JSONObject()
.put(serverId, auth);
return json.toString();
}

private static String getConfigurationValue(final Server server, final String key) {

final Xpp3Dom configuration = (Xpp3Dom) server.getConfiguration();
if (configuration == null) {
return null;
}

final Xpp3Dom node = configuration.getChild(key);
if (node == null) {
return null;
}

return node.getValue();
}

}
14 changes: 5 additions & 9 deletions core/src/main/java/io/fabric8/maven/core/util/GitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@
public class GitUtil {


public static Repository getGitRepository(MavenProject project) throws IOException {
MavenProject rootProject = MavenUtil.getRootProject(project);
File baseDir = rootProject.getBasedir();
if (baseDir == null) {
baseDir = project.getBasedir();
}
if (baseDir == null) {
public static Repository getGitRepository(File currentDir) throws IOException {

if (currentDir == null) {
// TODO: Why is this check needed ?
baseDir = new File(System.getProperty("basedir", "."));
currentDir = new File(System.getProperty("basedir", "."));
}
File gitFolder = findGitFolder(baseDir);
File gitFolder = findGitFolder(currentDir);
if (gitFolder == null) {
// No git repository found
return null;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/fabric8/maven/core/util/MavenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public static URLClassLoader getTestClassLoader(MavenProject project) {
}
}

public static String createDefaultResourceName(MavenProject project, String ... suffixes) {
public static String createDefaultResourceName(String artifactId, String ... suffixes) {
String suffix = StringUtils.join(suffixes, "-");
String ret = project.getArtifactId() + (suffix.length() > 0 ? "-" + suffix : "");
String ret = artifactId + (suffix.length() > 0 ? "-" + suffix : "");
if (ret.length() > 63) {
ret = ret.substring(0,63);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class SpringBootUtil {
* Returns the spring boot configuration (supports `application.properties` and `application.yml`)
* or an empty properties object if not found
*/
public static Properties getSpringBootApplicationProperties(MavenProject project) {
URLClassLoader compileClassLoader = MavenUtil.getCompileClassLoader(project);
public static Properties getSpringBootApplicationProperties(URLClassLoader compileClassLoader) {
URL ymlResource = compileClassLoader.findResource("application.yml");
URL propertiesResource = compileClassLoader.findResource("application.properties");

Expand Down
Loading

0 comments on commit 5026685

Please sign in to comment.