Skip to content

Commit 57e3f0f

Browse files
authored
Move xpack template resources to an isolated module (elastic#97763)
Xpack core contains dozens of templates. These files are loaded via a shared template utils method. While they should ideally be moved to their appropriate modules, that would be large undertaking. This commit moves these resources to a single module for xpack core (which may be overriden by serverless).
1 parent 52565a3 commit 57e3f0f

File tree

132 files changed

+114
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+114
-110
lines changed

x-pack/plugin/core/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ dependencies {
4141
api "org.apache.httpcomponents:httpcore:${versions.httpcore}"
4242
api "org.apache.httpcomponents:httpcore-nio:${versions.httpcore}"
4343
api "org.apache.httpcomponents:httpasyncclient:${versions.httpasyncclient}"
44-
4544
api "commons-logging:commons-logging:${versions.commonslogging}"
4645
api "org.apache.logging.log4j:log4j-1.2-api:${versions.log4j}"
4746
api "commons-codec:commons-codec:${versions.commonscodec}"
4847

4948
// security deps
5049
api 'com.unboundid:unboundid-ldapsdk:6.0.3'
5150

51+
implementation project(":x-pack:plugin:core:template-resources")
52+
5253
testImplementation "org.elasticsearch:mocksocket:${versions.mocksocket}"
5354
testImplementation "org.apache.logging.log4j:log4j-slf4j-impl:${versions.log4j}" // this might suffer from https://github.com/elastic/elasticsearch/issues/93714
5455
testImplementation "org.slf4j:slf4j-api:${versions.slf4j}"

x-pack/plugin/core/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
requires org.apache.lucene.join;
2222
requires unboundid.ldapsdk;
2323
requires org.elasticsearch.tdigest;
24+
requires org.elasticsearch.xcore.templates;
2425

2526
exports org.elasticsearch.index.engine.frozen;
2627
exports org.elasticsearch.license;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtils.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@
1414
import org.elasticsearch.cluster.metadata.ItemUsage;
1515
import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService;
1616
import org.elasticsearch.common.bytes.BytesArray;
17-
import org.elasticsearch.common.bytes.BytesReference;
1817
import org.elasticsearch.common.compress.NotXContentException;
1918
import org.elasticsearch.common.settings.Settings;
2019
import org.elasticsearch.common.xcontent.XContentHelper;
21-
import org.elasticsearch.core.Streams;
2220
import org.elasticsearch.xcontent.NamedXContentRegistry;
2321
import org.elasticsearch.xcontent.XContentParser;
2422
import org.elasticsearch.xcontent.XContentParserConfiguration;
2523
import org.elasticsearch.xcontent.XContentType;
24+
import org.elasticsearch.xpack.core.template.resources.TemplateResources;
2625

27-
import java.io.ByteArrayOutputStream;
28-
import java.io.IOException;
29-
import java.io.InputStream;
3026
import java.util.List;
3127
import java.util.Map;
3228
import java.util.regex.Pattern;
@@ -49,13 +45,13 @@ public static LifecyclePolicy loadPolicy(
4945
NamedXContentRegistry xContentRegistry
5046
) {
5147
try {
52-
BytesReference source = load(resource);
48+
String source = TemplateResources.load(resource);
5349
source = replaceVariables(source, variables);
5450
validate(source);
5551

5652
try (
5753
XContentParser parser = XContentType.JSON.xContent()
58-
.createParser(XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry), source.utf8ToString())
54+
.createParser(XContentParserConfiguration.EMPTY.withRegistry(xContentRegistry), source)
5955
) {
6056
LifecyclePolicy policy = LifecyclePolicy.parse(parser, name);
6157
policy.validate();
@@ -66,24 +62,11 @@ public static LifecyclePolicy loadPolicy(
6662
}
6763
}
6864

69-
/**
70-
* Loads a resource from the classpath and returns it as a {@link BytesReference}
71-
*/
72-
private static BytesReference load(String name) throws IOException {
73-
try (InputStream is = LifecyclePolicyUtils.class.getResourceAsStream(name)) {
74-
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
75-
Streams.copy(is, out);
76-
return new BytesArray(out.toByteArray());
77-
}
78-
}
79-
}
80-
81-
private static BytesReference replaceVariables(BytesReference input, Map<String, String> variables) {
82-
String template = input.utf8ToString();
65+
private static String replaceVariables(String template, Map<String, String> variables) {
8366
for (Map.Entry<String, String> variable : variables.entrySet()) {
8467
template = replaceVariable(template, variable.getKey(), variable.getValue());
8568
}
86-
return new BytesArray(template);
69+
return template;
8770
}
8871

8972
/**
@@ -96,13 +79,13 @@ public static String replaceVariable(String input, String variable, String value
9679
/**
9780
* Parses and validates that the source is not empty.
9881
*/
99-
private static void validate(BytesReference source) {
82+
private static void validate(String source) {
10083
if (source == null) {
10184
throw new ElasticsearchParseException("policy must not be null");
10285
}
10386

10487
try {
105-
XContentHelper.convertToMap(source, false, XContentType.JSON).v2();
88+
XContentHelper.convertToMap(new BytesArray(source), false, XContentType.JSON).v2();
10689
} catch (NotXContentException e) {
10790
throw new ElasticsearchParseException("policy must not be empty");
10891
} catch (Exception e) {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlConfigIndex.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ public static String indexName() {
2929
}
3030

3131
public static String mapping() {
32-
return TemplateUtils.loadTemplate(
33-
"/org/elasticsearch/xpack/core/ml/config_index_mappings.json",
34-
Version.CURRENT.toString(),
35-
MAPPINGS_VERSION_VARIABLE
36-
);
32+
return TemplateUtils.loadTemplate("/ml/config_index_mappings.json", Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE);
3733
}
3834

3935
public static Settings settings() {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetaIndex.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ public static String indexName() {
2727
}
2828

2929
public static String mapping() {
30-
return TemplateUtils.loadTemplate(
31-
"/org/elasticsearch/xpack/core/ml/meta_index_mappings.json",
32-
Version.CURRENT.toString(),
33-
MAPPINGS_VERSION_VARIABLE
34-
);
30+
return TemplateUtils.loadTemplate("/ml/meta_index_mappings.json", Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE);
3531
}
3632

3733
public static Settings settings() {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ public static String wrappedMapping() {
3636
}
3737

3838
public static String mapping() {
39-
return TemplateUtils.loadTemplate(
40-
"/org/elasticsearch/xpack/core/ml/stats_index_mappings.json",
41-
Version.CURRENT.toString(),
42-
MAPPINGS_VERSION_VARIABLE
43-
);
39+
return TemplateUtils.loadTemplate("/ml/stats_index_mappings.json", Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE);
4440
}
4541

4642
public static String indexPattern() {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ public static void createAnnotationsIndexIfNecessary(
208208
}
209209

210210
public static String annotationsMapping() {
211-
return TemplateUtils.loadTemplate(
212-
"/org/elasticsearch/xpack/core/ml/annotations_index_mappings.json",
213-
Version.CURRENT.toString(),
214-
MAPPINGS_VERSION_VARIABLE
215-
);
211+
return TemplateUtils.loadTemplate("/ml/annotations_index_mappings.json", Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE);
216212
}
217213
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/persistence/InferenceIndexConstants.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ public final class InferenceIndexConstants {
4646
private static final String MAPPINGS_VERSION_VARIABLE = "xpack.ml.version";
4747

4848
public static String mapping() {
49-
return TemplateUtils.loadTemplate(
50-
"/org/elasticsearch/xpack/core/ml/inference_index_mappings.json",
51-
Version.CURRENT.toString(),
52-
MAPPINGS_VERSION_VARIABLE
53-
);
49+
return TemplateUtils.loadTemplate("/ml/inference_index_mappings.json", Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE);
5450
}
5551

5652
public static String nativeDefinitionStore() {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public final class AnomalyDetectorsIndex {
2929

3030
private static final String RESULTS_MAPPINGS_VERSION_VARIABLE = "xpack.ml.version";
31-
private static final String RESOURCE_PATH = "/org/elasticsearch/xpack/core/ml/anomalydetection/";
31+
private static final String RESOURCE_PATH = "/ml/anomalydetection/";
3232

3333
private AnomalyDetectorsIndex() {}
3434

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/NotificationsIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class NotificationsIndex {
1313

1414
public static final String NOTIFICATIONS_INDEX = ".ml-notifications-000002";
1515

16-
private static final String RESOURCE_PATH = "/org/elasticsearch/xpack/core/ml/";
16+
private static final String RESOURCE_PATH = "/ml/";
1717
private static final String MAPPINGS_VERSION_VARIABLE = "xpack.ml.version";
1818

1919
private NotificationsIndex() {}

0 commit comments

Comments
 (0)