Skip to content

Commit 7d92eaf

Browse files
authored
Merge pull request #198 from jenkinsci/substitute
Use Util.replaceMacro instead of our custom replacement logic
2 parents c71a20f + 6c442a7 commit 7d92eaf

File tree

3 files changed

+15
-28
lines changed

3 files changed

+15
-28
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtils.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.csanchez.jenkins.plugins.kubernetes;
22

3+
import static hudson.Util.*;
34
import static java.util.stream.Collectors.*;
45
import static org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate.*;
56

@@ -10,8 +11,6 @@
1011
import java.util.List;
1112
import java.util.Map;
1213
import java.util.Set;
13-
import java.util.regex.Matcher;
14-
import java.util.regex.Pattern;
1514

1615
import javax.annotation.CheckForNull;
1716
import javax.annotation.Nonnull;
@@ -23,17 +22,13 @@
2322
import com.google.common.base.Preconditions;
2423
import com.google.common.base.Strings;
2524

25+
import hudson.Util;
2626
import hudson.model.Label;
2727
import hudson.model.Node;
2828
import hudson.tools.ToolLocationNodeProperty;
2929

3030
public class PodTemplateUtils {
3131

32-
private static final String PLACEHOLDER_KEY = "key";
33-
private static final String PLACEHOLDER_FORMAT = "\\$\\{%s\\}";
34-
private static final String PLACEHOLDER_REGEX = String.format(PLACEHOLDER_FORMAT, "(?<" + PLACEHOLDER_KEY + ">[a-zA-Z0-9_]+)");
35-
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile(PLACEHOLDER_REGEX);
36-
3732
/**
3833
* Combines a {@link ContainerTemplate} with its parent.
3934
* @param parent The parent container template (nullable).
@@ -221,52 +216,44 @@ public static PodTemplate getTemplateByName(@CheckForNull String name, Collectio
221216
* @return The substituted value if found, or the input value otherwise.
222217
*/
223218
public static String substituteEnv(String s) {
224-
return substitute(s, System.getenv());
219+
return replaceMacro(s, System.getenv());
225220
}
226221

227222
/**
228223
* Substitutes a placeholder with a value found in the environment.
224+
* @deprecated check if it is null or empty in the caller method, then use {@link #substituteEnv(String)}
229225
* @param s The placeholder. Should be use the format: ${placeholder}.
230226
* @param defaultValue The default value to return if no match is found.
231227
* @return The substituted value if found, or the default value otherwise.
232228
*/
229+
@Deprecated
233230
public static String substituteEnv(String s, String defaultValue) {
234231
return substitute(s, System.getenv(), defaultValue);
235232
}
236233

237234
/**
238235
* Substitutes a placeholder with a value found in the specified map.
236+
* @deprecated use {@link Util#replaceMacro(String, Map)}
239237
* @param s The placeholder. Should be use the format: ${placeholder}.
240238
* @param properties The map with the key value pairs to use for substitution.
241239
* @return The substituted value if found, or the input value otherwise.
242240
*/
241+
@Deprecated
243242
public static String substitute(String s, Map<String, String> properties) {
244-
return substitute(s, properties, null);
243+
return replaceMacro(s, properties);
245244
}
246245

247246
/**
248247
* Substitutes a placeholder with a value found in the specified map.
248+
* @deprecated check if it is null or empty in the caller method, then use {@link #substitute(String,Map)}
249249
* @param s The placeholder. Should be use the format: ${placeholder}.
250250
* @param properties The map with the key value pairs to use for substitution.
251251
* @param defaultValue The default value to return if no match is found.
252252
* @return The substituted value if found, or the default value otherwise.
253253
*/
254+
@Deprecated
254255
public static String substitute(String s, Map<String, String> properties, String defaultValue) {
255-
if (Strings.isNullOrEmpty(s)) {
256-
return defaultValue;
257-
}
258-
259-
Matcher m = PLACEHOLDER_PATTERN.matcher(s);
260-
while (m.find()) {
261-
String key = m.group(PLACEHOLDER_KEY);
262-
String val = properties.get(key);
263-
if (val != null) {
264-
s = s.replaceAll(String.format(PLACEHOLDER_FORMAT, key), Matcher.quoteReplacement(val));
265-
} else if (defaultValue != null) {
266-
s = s.replaceAll(String.format(PLACEHOLDER_FORMAT, key), defaultValue);
267-
}
268-
}
269-
return s;
256+
return Strings.isNullOrEmpty(s) ? defaultValue : replaceMacro(s, properties);
270257
}
271258

272259
private static List<TemplateEnvVar> combineEnvVars(ContainerTemplate parent, ContainerTemplate template) {

src/main/java/org/csanchez/jenkins/plugins/kubernetes/ProvisioningCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ private Pod getPodTemplate(KubernetesSlave slave, PodTemplate template) {
428428

429429
private Map<String, Quantity> getResourcesMap(String memory, String cpu) {
430430
ImmutableMap.Builder<String, Quantity> builder = ImmutableMap.<String, Quantity> builder();
431-
String actualMemory = substituteEnv(memory, null);
432-
String actualCpu = substituteEnv(cpu, null);
431+
String actualMemory = substituteEnv(memory);
432+
String actualCpu = substituteEnv(cpu);
433433
if (StringUtils.isNotBlank(actualMemory)) {
434434
Quantity memoryQuantity = new Quantity(actualMemory);
435435
builder.put("memory", memoryQuantity);

src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ public void shouldSubstituteMultipleEnvVarsAndIgnoreMissing() {
363363
}
364364

365365
@Test
366-
public void shouldSubstituteMultipleEnvVarsAndUseDefaultsForMissing() {
366+
public void shouldSubstituteMultipleEnvVarsAndNotUseDefaultsForMissing() {
367367
Map<String, String> properties = new HashMap<>();
368368
properties.put("key1", "value1");
369369
properties.put("key2", "value2");
370-
assertEquals("value1 or value2 or defaultValue", substitute("${key1} or ${key2} or ${key3}", properties, "defaultValue"));
370+
assertEquals("value1 or value2 or ${key3}", substitute("${key1} or ${key2} or ${key3}", properties, "defaultValue"));
371371
}
372372
}

0 commit comments

Comments
 (0)