Skip to content

Commit 213ae2e

Browse files
committed
Change template processor
1 parent a179b14 commit 213ae2e

File tree

7 files changed

+70
-11
lines changed

7 files changed

+70
-11
lines changed

docs/chapters/ABOUT_ONEKO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ feature/my-new-feature").
5353
### Configuration template
5454

5555
A configuration template is a native Helm values .yaml file (e.g. a service definition).
56-
Variable placeholders like `${MY_VARIABLE_NAME}` can be used to insert O-Neko specific variables into these templates.
56+
Variable placeholders like `{{MY_VARIABLE_NAME}}` can be used to insert O-Neko specific variables into these templates.
5757
The variables can be overridden in project versions to e.g. achieve slightly different configurations of versions without
5858
having to modify the templates per version.
5959

docs/chapters/GETTING_STARTED.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ chart version. Then you can write your Helm values .yaml file in the editor.
4545
In order to deploy everything correctly, you need to replace some fixed entries in your files with a template variable syntax.
4646
The most important is the docker image tag in your deployment.
4747

48-
First and most importantly: You need to set your docker image tag to `${VERSION_NAME}`.
48+
First and most importantly: You need to set your docker image tag to `{{VERSION_NAME}}`.
4949

5050
`VERSION_NAME` will be replaced with the docker tag you want to deploy, which might be `latest`, `1.0.0`, `bugfix_user_auth`
5151
or basically every docker tag that exists in your project.
@@ -56,10 +56,10 @@ the cluster to pull your image every time. If your Helm chart does not allow to
5656
extend your Helm chart.
5757

5858
In order to get dynamic URLs to your app you'll have to configure the host name (e.g. in an ingress).
59-
In this template you should replace the host string with a string that contains the variable `${SAFE_VERSION_NAME}`, like this:
59+
In this template you should replace the host string with a string that contains the variable `{{SAFE_VERSION_NAME}}`, like this:
6060

6161
```yaml
62-
- host: my_app-${SAFE_VERSION_NAME}.my-k8s-cluster.my-company.com
62+
- host: my_app-{{SAFE_VERSION_NAME}}.my-k8s-cluster.my-company.com
6363
```
6464
6565
`SAFE_VERSION_NAME` should be used, because this will make sure that the string replaced there will result in a valid URL.

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@
187187
<version>3.18.1</version>
188188
<scope>test</scope>
189189
</dependency>
190+
<dependency>
191+
<groupId>io.marioslab.basis</groupId>
192+
<artifactId>template</artifactId>
193+
<version>1.7</version>
194+
</dependency>
190195
</dependencies>
191196

192197
<build>

src/main/java/io/oneko/project/ProjectVersion.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import java.util.UUID;
99
import java.util.stream.Collectors;
1010

11-
import org.apache.commons.lang.StringUtils;
11+
import org.apache.commons.lang3.StringUtils;
1212
import org.apache.commons.text.StringSubstitutor;
1313

14+
import io.marioslab.basis.template.Template;
15+
import io.marioslab.basis.template.TemplateContext;
16+
import io.marioslab.basis.template.TemplateLoader;
1417
import io.oneko.automations.LifetimeBehaviour;
1518
import io.oneko.deployable.DeploymentBehaviour;
1619
import io.oneko.kubernetes.deployments.DesiredState;
@@ -113,13 +116,22 @@ default String calculateConfiguration() {
113116
* template or a modified version template and the effective template variables.
114117
*/
115118
default List<WritableConfigurationTemplate> getCalculatedConfigurationTemplates() {
116-
StringSubstitutor sub = new StringSubstitutor(this.calculateEffectiveTemplateVariables());
117-
119+
final Map<String, String> variables = this.calculateEffectiveTemplateVariables();
118120
//somehow java does not properly figure out the list type here
119121
final List<ConfigurationTemplate> unifiedTemplates = ConfigurationTemplates.unifyTemplateSets(getProject().getDefaultConfigurationTemplates(), getConfigurationTemplates());
122+
123+
TemplateContext context = new TemplateContext();
124+
variables.forEach(context::set);
125+
context.set("fn", TemplateFunctions.class);
126+
120127
return unifiedTemplates.stream()
121128
.map(WritableConfigurationTemplate::clone)
122-
.peek(template -> template.setContent(sub.replace(template.getContent())))
129+
.peek(template -> {
130+
TemplateLoader.MapTemplateLoader loader = new TemplateLoader.MapTemplateLoader();
131+
loader.set("tpl", template.getContent());
132+
final Template tpl = loader.load("tpl");
133+
template.setContent(tpl.render(context));
134+
})
123135
.collect(Collectors.toList());
124136
}
125137

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.oneko.project;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import lombok.experimental.UtilityClass;
6+
7+
@UtilityClass
8+
public class TemplateFunctions {
9+
10+
public static String lowerCase(String str) {
11+
return StringUtils.lowerCase(str);
12+
}
13+
14+
public static String replace(String text, String searchString, String replacement) {
15+
return StringUtils.replace(text, searchString, replacement);
16+
}
17+
18+
public static String replace(String text, String searchString, String replacement, int max) {
19+
return StringUtils.replace(text, searchString, replacement, max);
20+
}
21+
22+
public static String trim(String str) {
23+
return StringUtils.trim(str);
24+
}
25+
26+
public static String upperCase(String str) {
27+
return StringUtils.upperCase(str);
28+
}
29+
30+
public static String truncate(String str, int maxWidth) {
31+
return StringUtils.truncate(str, maxWidth);
32+
}
33+
34+
public static String removeEnd(final String str, final String remove) {
35+
return StringUtils.removeEnd(str, remove);
36+
}
37+
38+
public static String removeStart(final String str, final String remove) {
39+
return StringUtils.removeStart(str, remove);
40+
}
41+
42+
}

src/test/java/io/oneko/project/ProjectVersionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void testCalculateConfiguration() {
2727
List<WritableConfigurationTemplate> templates = Collections.singletonList(
2828
WritableConfigurationTemplate.builder()
2929
.content(
30-
"This is a template with implicit variables (${PROJECT_NAME}, ${VERSION_NAME}), " +
31-
"default variables (${TEST1}), overwritten default variables (${TEST2}) and child variables (${TEST3})"
30+
"This is a template with implicit variables ({{PROJECT_NAME}}, {{VERSION_NAME}}), " +
31+
"default variables ({{TEST1}}), overwritten default variables ({{TEST2}}) and child variables ({{TEST3}})"
3232
)
3333
.id(UUID.randomUUID())
3434
.name("name")

src/test/java/io/oneko/project/rest/DeployableConfigurationDTOMapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void configShouldHaveReplacesVariables() {
3535
.build();
3636
final WritableProjectVersion version = project1.createVersion("Name");
3737
version.setConfigurationTemplates(Collections.singletonList(
38-
WritableConfigurationTemplate.builder().content("${VAR_1} ${VAR_2}").build()
38+
WritableConfigurationTemplate.builder().content("{{VAR_1}} {{VAR_2}}").build()
3939
));
4040
version.setTemplateVariables(templateVariables);
4141

0 commit comments

Comments
 (0)