Skip to content

Commit

Permalink
improve: double bracket in object template
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed Jan 5, 2025
1 parent 0a8d185 commit 7c0accc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public GenericBulkDependentResource(GenericTemplateHandler genericTemplateHandle
public Map<String, GenericKubernetesResource> desiredResources(Glue primary,
Context<Glue> context) {

var res = genericTemplateHandler.processTemplate(desiredTemplate, primary, context);
var res = genericTemplateHandler.processTemplate(desiredTemplate, primary, false, context);
var desiredList = Serialization.unmarshal(res, GenericKubernetesResourceList.class).getItems();
desiredList.forEach(r -> {
r.getMetadata().getAnnotations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public GenericDependentResource(GenericTemplateHandler genericTemplateHandler,
@Override
protected GenericKubernetesResource desired(Glue primary,
Context<Glue> context) {
boolean objectTemplate = desired != null;
var template = objectTemplate ? Serialization.asYaml(desired) : desiredTemplate;

var template = desired == null ? desiredTemplate : Serialization.asYaml(desired);

var res = genericTemplateHandler.processTemplate(template, primary, context);
var res = genericTemplateHandler.processTemplate(template, primary, objectTemplate, context);
var resultDesired = Serialization.unmarshal(res, GenericKubernetesResource.class);

resultDesired.getMetadata().getAnnotations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private void createAndAddDependentToWorkflow(Glue primary, Context<Glue> context
// todo test processing ns as template
// name can reference related resources todo doc
var targetNamespace = Utils.getNamespace(spec).map(ns -> genericTemplateHandler
.processTemplate(ns, primary, context));
.processTemplate(ns, primary, false, context));
var resourceInSameNamespaceAsPrimary =
targetNamespace.map(n -> n.trim().equals(primary.getMetadata().getNamespace().trim()))
.orElse(true);
Expand All @@ -205,7 +205,7 @@ private void createAndAddDependentToWorkflow(Glue primary, Context<Glue> context

if (!(dr instanceof BulkDependentResource<?, ?>)) {
dr.setResourceDiscriminator(new GenericResourceDiscriminator(dr.getGroupVersionKind(),
genericTemplateHandler.processTemplate(Utils.getName(spec), primary, context),
genericTemplateHandler.processTemplate(Utils.getName(spec), primary, false, context),
targetNamespace.orElse(null)));
}

Expand Down Expand Up @@ -267,10 +267,11 @@ private void patchRelatedResourcesStatus(Context<Glue> context,
targetRelatedResources.forEach(r -> {
var relatedResources = Utils.getRelatedResources(primary, r, context);

var template = r.getStatusPatchTemplate() != null ? r.getStatusPatchTemplate()
: Serialization.asYaml(r.getStatusPatch());
var objectTemplate = r.getStatusPatch() != null;
var template =
objectTemplate ? Serialization.asYaml(r.getStatusPatch()) : r.getStatusPatchTemplate();
var resultTemplate =
genericTemplateHandler.processTemplate(actualData, template);
genericTemplateHandler.processTemplate(actualData, template, objectTemplate);
var statusObjectMap = GenericTemplateHandler.parseTemplateToMapObject(resultTemplate);
relatedResources.forEach((n, kr) -> {
kr.setAdditionalProperty("status", statusObjectMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ private ObjectMeta glueMetadata(GlueOperator glueOperator,
if (glueMeta != null) {
// optimize
var data = Map.of(PARENT_RELATED_RESOURCE_NAME, parent);
var glueName = genericTemplateHandler.processInputAndTemplate(data, glueMeta.getName());
var glueName =
genericTemplateHandler.processInputAndTemplate(data, glueMeta.getName(), false);
var glueNamespace =
genericTemplateHandler.processInputAndTemplate(data, glueMeta.getNamespace());
genericTemplateHandler.processInputAndTemplate(data, glueMeta.getNamespace(), false);
objectMetaBuilder.withName(glueName);
objectMetaBuilder.withNamespace(glueNamespace);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@ public class GenericTemplateHandler {
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Engine engine = Engine.builder().addDefaults().build();

public String processTemplate(Map<String, Map<?, ?>> data, String template) {
public String processTemplate(Map<String, Map<?, ?>> data, String template,
boolean objectTemplate) {
if (objectTemplate) {
template = handleDoubleCurlyBrackets(template);
}
Template parsedTemplate = engine.parse(template);
return parsedTemplate.data(data).render();
}

private String handleDoubleCurlyBrackets(String template) {
template = template.replace("\"{{", "{");
return template.replace("}}\n", "}");
}

public String processInputAndTemplate(Map<String, GenericKubernetesResource> data,
String template) {
String template, boolean objectTemplate) {
Map<String, Map<?, ?>> res =
genericKubernetesResourceDataToGenericData(data);
return processTemplate(res, template);
return processTemplate(res, template, objectTemplate);
}

public String processTemplate(String template, Glue primary,
public String processTemplate(String template, Glue primary, boolean objectTemplate,
Context<Glue> context) {
var data = createDataWithResources(primary, context);
return processTemplate(data, template);
return processTemplate(data, template, objectTemplate);
}

private static Map<String, Map<?, ?>> genericKubernetesResourceDataToGenericData(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.javaoperatorsdk.operator.glue.templating;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class GenericTemplateHandlerTest {

@Test
void testDoubleCurlyBrackets() {
fail();
}

}

0 comments on commit 7c0accc

Please sign in to comment.