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

Commit cfb79a1

Browse files
committed
Refactor: Move XML configuration code from Mojo to enrichers
Right now there is lot of code in ResourceMojo which should ideally be in enrichers, Mojo should stay as slim as possible.
1 parent 66cb664 commit cfb79a1

File tree

8 files changed

+251
-317
lines changed

8 files changed

+251
-317
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
1818
* Fix 690: Removes deprecated _legacyPortMapping_ property.
1919
* Fix 1458: Support for from Image configuration in openshift docker build strategy
2020
* Fix 732: Added 'skip' options to goals.
21+
* Refactor 1520: Move XML configuration code from Mojo to enrichers
2122
* Fix 1486: Remove Kompose Support
2223
* Fix 1467: Wait timeout for build pod is too small
2324
* Fix 1466: Allow to configure noCache option for openshift docker build strategy

core/src/main/java/io/fabric8/maven/core/util/FileUtil.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
*/
1616
package io.fabric8.maven.core.util;
1717

18+
import org.apache.maven.plugin.MojoExecutionException;
19+
import io.fabric8.maven.docker.util.Logger;
20+
1821
import java.io.File;
1922
import java.io.IOException;
23+
import java.net.MalformedURLException;
2024
import java.net.URISyntaxException;
2125
import java.net.URL;
2226
import java.nio.file.Files;
2327
import java.nio.file.Path;
2428
import java.nio.file.Paths;
29+
import java.util.List;
2530

2631
/**
2732
* File related methods which cannot be found elsewhere
@@ -80,4 +85,43 @@ public static String getAbsolutePath(URL url) {
8085
throw new RuntimeException(e);
8186
}
8287
}
88+
89+
public static void downloadRemotes(final File outputDirectory, List<String> remotes, Logger log) {
90+
91+
if (!outputDirectory.exists()) {
92+
try {
93+
Files.createDirectories(outputDirectory.toPath());
94+
} catch (IOException e) {
95+
throw new IllegalArgumentException(e);
96+
}
97+
}
98+
99+
remotes.stream()
100+
.map(remote -> {
101+
try {
102+
return new URL(remote);
103+
} catch (MalformedURLException e) {
104+
throw new IllegalArgumentException(e);
105+
}
106+
})
107+
.forEach(url -> {
108+
try {
109+
IoUtil.download(log, url, new File(outputDirectory, getOutputName(url)));
110+
} catch (MojoExecutionException e) {
111+
throw new IllegalArgumentException(e);
112+
}
113+
});
114+
}
115+
116+
private static String getOutputName(URL url) {
117+
final String path = url.getPath();
118+
119+
final int slashIndex = path.lastIndexOf('/');
120+
if (slashIndex >= 0) {
121+
return path.substring(slashIndex + 1);
122+
} else {
123+
throw new IllegalArgumentException(String.format("URL %s should contain a name file to be downloaded.", url.toString()));
124+
}
125+
126+
}
83127
}

enricher/api/src/main/java/io/fabric8/maven/enricher/api/MavenEnricherContext.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Optional;
2323
import java.util.Set;
2424

25+
import com.google.gson.JsonObject;
26+
import com.google.gson.JsonPrimitive;
2527
import io.fabric8.maven.core.config.ProcessorConfig;
2628
import io.fabric8.maven.core.config.ResourceConfig;
2729
import io.fabric8.maven.core.model.Configuration;
@@ -53,6 +55,8 @@ public class MavenEnricherContext implements EnricherContext {
5355
// overall configuration for the build
5456
private Configuration configuration;
5557

58+
private Settings settings;
59+
5660
private MavenProject project;
5761
private Logger log;
5862

@@ -66,6 +70,10 @@ public Configuration getConfiguration() {
6670
return configuration;
6771
}
6872

73+
public Settings getSettings() {
74+
return settings;
75+
}
76+
6977
@Override
7078
public Logger getLog() {
7179
return log;
@@ -133,6 +141,48 @@ public MavenProject getProject() {
133141
return project;
134142
}
135143

144+
//Method used in MOJO
145+
public String getDockerJsonConfigString(final Settings settings, final String serverId) {
146+
Server server = getServer(settings, serverId);
147+
if (server == null) {
148+
return "";
149+
}
150+
151+
JsonObject auth = new JsonObject();
152+
auth.add("username", new JsonPrimitive(server.getUsername()));
153+
auth.add("password", new JsonPrimitive(server.getPassword()));
154+
155+
String mail = getConfigurationValue(server, "email");
156+
if (!StringUtils.isBlank(mail)) {
157+
auth.add("email", new JsonPrimitive(mail));
158+
}
159+
160+
JsonObject json = new JsonObject();
161+
json.add(serverId, auth);
162+
return json.toString();
163+
}
164+
165+
public Server getServer(final Settings settings, final String serverId) {
166+
if (settings == null || StringUtils.isBlank(serverId)) {
167+
return null;
168+
}
169+
return settings.getServer(serverId);
170+
}
171+
172+
private String getConfigurationValue(final Server server, final String key) {
173+
174+
final Xpp3Dom configuration = (Xpp3Dom) server.getConfiguration();
175+
if (configuration == null) {
176+
return null;
177+
}
178+
179+
final Xpp3Dom node = configuration.getChild(key);
180+
if (node == null) {
181+
return null;
182+
}
183+
184+
return node.getValue();
185+
}
136186
// =======================================================================================================
137187
public static class Builder {
138188

@@ -177,6 +227,11 @@ public Builder openshiftDependencyResources(OpenShiftDependencyResources openShi
177227
return this;
178228
}
179229

230+
public Builder settings(Settings settings) {
231+
ctx.settings = settings;
232+
return this;
233+
}
234+
180235
public MavenEnricherContext build() {
181236
ctx.configuration =
182237
new Configuration.Builder()

enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import io.fabric8.kubernetes.api.builder.TypedVisitor;
1919
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
2020
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
21+
import io.fabric8.maven.core.config.ConfigMapEntry;
22+
import io.fabric8.maven.core.config.ResourceConfig;
2123
import io.fabric8.maven.enricher.api.BaseEnricher;
2224
import io.fabric8.maven.enricher.api.MavenEnricherContext;
2325
import java.io.IOException;
@@ -39,6 +41,7 @@ public ConfigMapEnricher(MavenEnricherContext enricherContext) {
3941
@Override
4042
public void addMissingResources(KubernetesListBuilder builder) {
4143
addAnnotations(builder);
44+
addConfigMapFromXmlConfigurations(builder);
4245
}
4346

4447
private void addAnnotations(KubernetesListBuilder builder) {
@@ -82,4 +85,52 @@ private String getOutput(String key) {
8285
return key.substring(PREFIX_ANNOTATION.length());
8386
}
8487

88+
private void addConfigMapFromXmlConfigurations(KubernetesListBuilder builder) {
89+
io.fabric8.maven.core.config.ConfigMap configMap = getConfigMapFromXmlConfiguration();
90+
final Map<String, String> configMapFromConfiguration;
91+
try {
92+
configMapFromConfiguration = createConfigMapFromConfiguration(configMap);
93+
if(!configMapFromConfiguration.isEmpty()) {
94+
ConfigMapBuilder element = new ConfigMapBuilder();
95+
element.withNewMetadata().withName("xmlconfig").endMetadata();
96+
element.addToData(configMapFromConfiguration);
97+
98+
builder.addToConfigMapItems(element.build());
99+
}
100+
} catch (IOException e) {
101+
throw new IllegalArgumentException(e);
102+
}
103+
}
104+
105+
private io.fabric8.maven.core.config.ConfigMap getConfigMapFromXmlConfiguration() {
106+
ResourceConfig resourceConfig = getConfiguration().getResource().orElse(null);
107+
if(resourceConfig != null && resourceConfig.getConfigMap() != null) {
108+
return resourceConfig.getConfigMap();
109+
}
110+
return null;
111+
}
112+
113+
private Map<String, String> createConfigMapFromConfiguration(io.fabric8.maven.core.config.ConfigMap configMap) throws IOException {
114+
final Map<String, String> configMapData = new HashMap<>();
115+
116+
if (configMap != null) {
117+
for (ConfigMapEntry configMapEntry : configMap.getEntries()) {
118+
String name = configMapEntry.getName();
119+
final String value = configMapEntry.getValue();
120+
if (name != null && value != null) {
121+
configMapData.put(name, value);
122+
} else {
123+
final String file = configMapEntry.getFile();
124+
if (file != null) {
125+
if (name == null) {
126+
name = Paths.get(file).getFileName().toString();
127+
}
128+
configMapData.put(name, readContent(file));
129+
}
130+
}
131+
}
132+
}
133+
return configMapData;
134+
}
135+
85136
}

enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import io.fabric8.kubernetes.api.model.ServicePort;
2727
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
2828
import io.fabric8.kubernetes.api.model.ServiceSpec;
29+
import io.fabric8.maven.core.config.ResourceConfig;
30+
import io.fabric8.maven.core.config.ServiceConfig;
31+
import io.fabric8.maven.core.handler.ServiceHandler;
2932
import io.fabric8.maven.core.util.Configs;
3033
import io.fabric8.maven.core.util.MavenUtil;
3134
import io.fabric8.maven.core.util.kubernetes.KubernetesHelper;
@@ -104,10 +107,39 @@ public void addMissingResources(KubernetesListBuilder builder) {
104107
} else {
105108
addDefaultService(builder, defaultService);
106109
}
110+
111+
// Add Services configured via XML
112+
addServices(builder);
107113
}
108114

109115
// =======================================================================================================
110116

117+
private void addServices(KubernetesListBuilder builder) {
118+
ResourceConfig resources = new ResourceConfig();
119+
120+
if (resources != null && resources.getServices() != null) {
121+
List<ServiceConfig> serviceConfig = resources.getServices();
122+
ServiceHandler serviceHandler = new ServiceHandler();
123+
builder.addToServiceItems(toArray(serviceHandler.getServices(serviceConfig)));
124+
}
125+
}
126+
127+
// convert list to array, never returns null.
128+
private Service[] toArray(List<Service> services) {
129+
if (services == null) {
130+
return new Service[0];
131+
}
132+
if (services instanceof ArrayList) {
133+
return ((ArrayList<Service>) services).toArray(new Service[services.size()]);
134+
} else {
135+
Service[] ret = new Service[services.size()];
136+
for (int i = 0; i < services.size(); i++) {
137+
ret[i] = services.get(i);
138+
}
139+
return ret;
140+
}
141+
}
142+
111143
private Service getDefaultService() {
112144

113145
// No image config, no service

enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,23 @@
1515
*/
1616
package io.fabric8.maven.enricher.standard;
1717

18+
import java.util.HashMap;
19+
import java.util.List;
1820
import java.util.Map;
1921

2022
import io.fabric8.kubernetes.api.builder.TypedVisitor;
2123
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
24+
import io.fabric8.kubernetes.api.model.ObjectMeta;
25+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
26+
import io.fabric8.kubernetes.api.model.Secret;
2227
import io.fabric8.kubernetes.api.model.SecretBuilder;
28+
import io.fabric8.maven.core.config.ResourceConfig;
29+
import io.fabric8.maven.core.config.SecretConfig;
2330
import io.fabric8.maven.core.util.Base64Util;
31+
import io.fabric8.maven.core.util.SecretConstants;
2432
import io.fabric8.maven.enricher.api.BaseEnricher;
2533
import io.fabric8.maven.enricher.api.MavenEnricherContext;
34+
import org.apache.commons.lang3.StringUtils;
2635

2736
public abstract class SecretEnricher extends BaseEnricher {
2837

@@ -58,6 +67,61 @@ public void visit(SecretBuilder secretBuilder) {
5867
secretBuilder.addToData(data);
5968
}
6069
});
70+
71+
addSecretsFromXmlConfiguration(builder);
72+
}
73+
74+
private void addSecretsFromXmlConfiguration(KubernetesListBuilder builder) {
75+
log.verbose("Adding secrets resources from plugin configuration");
76+
List<SecretConfig> secrets = getSecretsFromXmlConfig();
77+
if (secrets == null || secrets.isEmpty()) {
78+
return;
79+
}
80+
for (int i = 0; i < secrets.size(); i++) {
81+
SecretConfig secretConfig = secrets.get(i);
82+
if (StringUtils.isBlank(secretConfig.getName())) {
83+
log.warn("Secret name is empty. You should provide a proper name for the secret");
84+
continue;
85+
}
86+
87+
Map<String, String> data = new HashMap<>();
88+
String type = "";
89+
ObjectMeta metadata = new ObjectMetaBuilder()
90+
.withNamespace(secretConfig.getNamespace())
91+
.withName(secretConfig.getName())
92+
.build();
93+
94+
// docker-registry
95+
if (secretConfig.getDockerServerId() != null) {
96+
MavenEnricherContext mavenContext = ((MavenEnricherContext)getContext());
97+
String dockerSecret = (mavenContext).getDockerJsonConfigString(mavenContext.getSettings(), secretConfig.getDockerServerId());
98+
if (StringUtils.isBlank(dockerSecret)) {
99+
log.warn("Docker secret with id "
100+
+ secretConfig.getDockerServerId()
101+
+ " cannot be found in maven settings");
102+
continue;
103+
}
104+
data.put(SecretConstants.DOCKER_DATA_KEY, Base64Util.encodeToString(dockerSecret));
105+
type = SecretConstants.DOCKER_CONFIG_TYPE;
106+
}
107+
// TODO: generic secret (not supported for now)
108+
109+
if (StringUtils.isBlank(type) || data.isEmpty()) {
110+
log.warn("No data can be found for docker secret with id " + secretConfig.getDockerServerId());
111+
continue;
112+
}
113+
114+
Secret secret = new SecretBuilder().withData(data).withMetadata(metadata).withType(type).build();
115+
builder.addToSecretItems(i, secret);
116+
}
117+
}
118+
119+
private List<SecretConfig> getSecretsFromXmlConfig() {
120+
ResourceConfig resourceConfig = getConfiguration().getResource().orElse(null);
121+
if(resourceConfig != null && resourceConfig.getSecrets() != null) {
122+
return resourceConfig.getSecrets();
123+
}
124+
return null;
61125
}
62126

63127
protected abstract String getAnnotationKey();

0 commit comments

Comments
 (0)