Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging(template-mcp): adding more logging around templating #11786

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -109,13 +110,29 @@ static List<ObjectNode> resolveMCPTemplate(
AuditStamp auditStamp)
throws IOException {

String template = loadTemplate(mcpTemplate.getMcps_location());
Mustache mustache = MUSTACHE_FACTORY.compile(new StringReader(template), mcpTemplate.getName());
final String template = loadTemplate(mcpTemplate.getMcps_location());
Map<String, Object> scopeValues = resolveValues(opContext, mcpTemplate, auditStamp);

StringWriter writer = new StringWriter();
mustache.execute(writer, scopeValues);
try {
Mustache mustache =
MUSTACHE_FACTORY.compile(new StringReader(template), mcpTemplate.getName());
mustache.execute(writer, scopeValues);
} catch (Exception e) {
log.error(
"Failed to apply mustache template. Template: {} Values: {}",
template,
resolveEnv(mcpTemplate));
throw e;
}

return opContext.getYamlMapper().readValue(writer.toString(), new TypeReference<>() {});
final String yaml = writer.toString();
try {
return opContext.getYamlMapper().readValue(yaml, new TypeReference<>() {});
} catch (Exception e) {
log.error("Failed to parse rendered MCP bootstrap yaml: {}", yaml);
throw e;
}
}

static Map<String, Object> resolveValues(
Expand All @@ -128,13 +145,21 @@ static Map<String, Object> resolveValues(
// built-in
scopeValues.put("auditStamp", RecordUtils.toJsonString(auditStamp));

String envValue = resolveEnv(mcpTemplate);
if (envValue != null) {
scopeValues.putAll(opContext.getObjectMapper().readValue(envValue, new TypeReference<>() {}));
}
return scopeValues;
}

@Nullable
private static String resolveEnv(BootstrapMCPConfigFile.MCPTemplate mcpTemplate) {
if (mcpTemplate.getValues_env() != null
&& !mcpTemplate.getValues_env().isEmpty()
&& System.getenv().containsKey(mcpTemplate.getValues_env())) {
String envValue = System.getenv(mcpTemplate.getValues_env());
scopeValues.putAll(opContext.getObjectMapper().readValue(envValue, new TypeReference<>() {}));
return System.getenv(mcpTemplate.getValues_env());
}
return scopeValues;
return null;
}

private static String loadTemplate(String source) throws IOException {
Expand Down
Loading