This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathEnricherManager.java
107 lines (89 loc) · 4.12 KB
/
EnricherManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.plugin.enricher;
import java.util.List;
import java.util.Optional;
import com.google.common.base.Function;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.maven.core.config.PlatformMode;
import io.fabric8.maven.core.config.ProcessorConfig;
import io.fabric8.maven.core.config.ResourceConfig;
import io.fabric8.maven.core.util.ClassUtil;
import io.fabric8.maven.core.util.PluginServiceFactory;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.enricher.api.Enricher;
import io.fabric8.maven.enricher.api.EnricherContext;
import static io.fabric8.maven.enricher.api.util.Misc.filterEnrichers;
/**
* @author roland
* @since 08/04/16
*/
public class EnricherManager {
// List of enrichers used for customizing the generated deployment descriptors
private List<Enricher> enrichers;
// context used by enrichers
private final ProcessorConfig defaultEnricherConfig;
private Logger log;
public EnricherManager(ResourceConfig resourceConfig, EnricherContext enricherContext, Optional<List<String>> extraClasspathElements) {
PluginServiceFactory<EnricherContext> pluginFactory = new PluginServiceFactory<>(enricherContext);
extraClasspathElements.ifPresent(
cpElements -> pluginFactory.addAdditionalClassLoader(ClassUtil.createProjectClassLoader(cpElements, enricherContext.getLog())));
this.log = enricherContext.getLog();
this.defaultEnricherConfig = enricherContext.getConfiguration().getProcessorConfig().orElse(ProcessorConfig.EMPTY);
this.enrichers = pluginFactory.createServiceObjects("META-INF/fabric8-enricher-default",
"META-INF/fabric8/enricher-default",
"META-INF/fabric8-enricher",
"META-INF/fabric8/enricher");
logEnrichers(filterEnrichers(defaultEnricherConfig, enrichers));
}
public void createDefaultResources(PlatformMode platformMode, final KubernetesListBuilder builder) {
createDefaultResources(platformMode, defaultEnricherConfig, builder);
}
public void createDefaultResources(PlatformMode platformMode, ProcessorConfig enricherConfig, final KubernetesListBuilder builder) {
// Add default resources
loop(enricherConfig, enricher -> {
enricher.create(platformMode, builder);
return null;
});
}
public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) {
enrich(platformMode, defaultEnricherConfig, builder);
}
/**
* Allow enricher to add Metadata to the resources.
* @param platformMode
* @param enricherConfig
* @param builder builder to customize
*/
public void enrich(PlatformMode platformMode, final ProcessorConfig enricherConfig, final KubernetesListBuilder builder) {
loop(enricherConfig, enricher -> {
enricher.enrich(platformMode, builder);
return null;
});
}
// =============================================================================================
private void logEnrichers(List<Enricher> enrichers) {
log.verbose(Logger.LogVerboseCategory.BUILD, "Enrichers:");
for (Enricher enricher : enrichers) {
log.verbose(Logger.LogVerboseCategory.BUILD, "- %s", enricher.getName());
}
}
private void loop(ProcessorConfig config, Function<Enricher, Void> function) {
for (Enricher enricher : filterEnrichers(config, enrichers)) {
function.apply(enricher);
}
}
}