Skip to content

Commit

Permalink
chore(camel-k): Support resource modeline instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Jun 18, 2024
1 parent 61d4cbc commit b14700e
Showing 1 changed file with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class CreateIntegrationAction extends AbstractCamelKAction {
private final List<String> propertyFiles;
private final List<String> traits;
private final List<String> openApis;
private final List<String> resources;
private final boolean supportVariables;

/**
Expand All @@ -95,6 +96,7 @@ public CreateIntegrationAction(Builder builder) {
this.propertyFiles = builder.propertyFiles;
this.traits = builder.traits;
this.openApis = builder.openApis;
this.resources = builder.resources;
this.supportVariables = builder.supportVariables;
}

Expand Down Expand Up @@ -124,8 +126,9 @@ public void doExecute(TestContext context) {
specBuilder.addAllToDependencies(resolvedDependencies);
}
Map<String, Map<String, Object>> traitConfigMap = new HashMap<>();
addPropertyConfigurationSpec(specBuilder, context);
addPropertyConfigurationSpec(specBuilder, resolvedSource, context);
addRuntimeConfigurationSpec(specBuilder, resolvedSource, context);
addResourcesSpec(traitConfigMap, resolvedSource, context);
addBuildPropertyConfigurationSpec(traitConfigMap, resolvedSource, context);
addEnvVarConfigurationSpec(traitConfigMap, resolvedSource, context);
addOpenApiSpec(traitConfigMap, resolvedSource, context);
Expand All @@ -145,6 +148,20 @@ public void doExecute(TestContext context) {
LOG.info(String.format("Successfully created Camel K integration '%s'", integration.getMetadata().getName()));
}

private void addResourcesSpec(Map<String, Map<String, Object>> traitConfigMap, String source, TestContext context) {
String traitName = "mount.resources";
Pattern pattern = getModelinePattern("resource");
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
String resource = matcher.group(1);
addTraitSpec("%s=%s".formatted(traitName, context.replaceDynamicContentInString(resource)), traitConfigMap);
}

for (String resource : resources) {
addTraitSpec("%s=%s".formatted(traitName, context.replaceDynamicContentInString(resource)), traitConfigMap);
}
}

/**
* Creates the Camel K integration as a custom resource in given namespace.
* @param k8sClient
Expand Down Expand Up @@ -230,9 +247,9 @@ private static String[] camelRunArgs(Integration integration) {
private void addOpenApiSpec(Map<String, Map<String, Object>> traitConfigMap, String source, TestContext context) {
String traitName = "openapi.configmaps";
Pattern pattern = getModelinePattern("open-api");
Matcher depMatcher = pattern.matcher(source);
while (depMatcher.find()) {
String openApiSpecFile = depMatcher.group(1);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
String openApiSpecFile = matcher.group(1);
addTraitSpec("%s=%s".formatted(traitName, context.replaceDynamicContentInString(openApiSpecFile)), traitConfigMap);
}

Expand All @@ -249,9 +266,9 @@ private void addTraitSpec(Map<String, Map<String, Object>> traitConfigMap, Strin
}

Pattern pattern = getModelinePattern("trait");
Matcher depMatcher = pattern.matcher(source);
while (depMatcher.find()) {
addTraitSpec(depMatcher.group(1), traitConfigMap);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
addTraitSpec(matcher.group(1), traitConfigMap);
}
}

Expand Down Expand Up @@ -321,7 +338,15 @@ private Object resolveTraitValue(String traitKey, String value) {
}
}

private void addPropertyConfigurationSpec(IntegrationSpecBuilder specBuilder, TestContext context) {
private void addPropertyConfigurationSpec(IntegrationSpecBuilder specBuilder, String source, TestContext context) {
Pattern pattern = getModelinePattern("property");
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
final String[] property = matcher.group(1).split("=",2);
specBuilder.addToConfiguration(
new ConfigurationBuilder().withType("property").withValue(createPropertySpec(property[0], property[1], context)).build());
}

if (properties != null && !properties.isEmpty()) {
for (String p : context.resolveDynamicValuesInList(properties)){
//key=value
Expand Down Expand Up @@ -379,9 +404,9 @@ private void addBuildPropertyConfigurationSpec(Map<String, Map<String, Object>>
}

Pattern pattern = getModelinePattern("build-property");
Matcher depMatcher = pattern.matcher(source);
while (depMatcher.find()) {
addTraitSpec(String.format("%s=%s", traitName, depMatcher.group(1)), traitConfigMap);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
addTraitSpec(String.format("%s=%s", traitName, matcher.group(1)), traitConfigMap);
}
}

Expand Down Expand Up @@ -415,21 +440,21 @@ private void addEnvVarConfigurationSpec(Map<String, Map<String, Object>> traitCo
}

Pattern pattern = getModelinePattern("env");
Matcher depMatcher = pattern.matcher(source);
while (depMatcher.find()) {
addTraitSpec(String.format("%s=%s", traitName, depMatcher.group(1)), traitConfigMap);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
addTraitSpec(String.format("%s=%s", traitName, matcher.group(1)), traitConfigMap);
}
}

private void addRuntimeConfigurationSpec(IntegrationSpecBuilder specBuilder, String source, TestContext context) {
Pattern pattern = getModelinePattern("config");
Matcher depMatcher = pattern.matcher(source);
while (depMatcher.find()) {
String[] config = depMatcher.group(1).split(":", 2);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
String[] config = matcher.group(1).split(":", 2);
if (config.length == 2) {
specBuilder.addToConfiguration(new ConfigurationBuilder().withType(config[0]).withValue(context.replaceDynamicContentInString(config[1])).build());
} else {
specBuilder.addToConfiguration(new ConfigurationBuilder().withType("property").withValue(context.replaceDynamicContentInString(depMatcher.group(1))).build());
specBuilder.addToConfiguration(new ConfigurationBuilder().withType("property").withValue(context.replaceDynamicContentInString(matcher.group(1))).build());
}
}
}
Expand Down Expand Up @@ -466,9 +491,9 @@ private static List<String> resolveDependencies(String source, List<String> depe
List<String> resolved = new ArrayList<>(dependencies);

Pattern pattern = getModelinePattern("dependency");
Matcher depMatcher = pattern.matcher(source);
while (depMatcher.find()) {
String dependency = depMatcher.group(1);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
String dependency = matcher.group(1);

if (dependency.startsWith("camel-quarkus-")) {
dependency = "camel:" + dependency.substring("camel-quarkus-".length());
Expand Down Expand Up @@ -528,6 +553,7 @@ public static final class Builder extends AbstractCamelKAction.Builder<CreateInt
private final List<String> propertyFiles = new ArrayList<>();
private final List<String> traits = new ArrayList<>();
private final List<String> openApis = new ArrayList<>();
private final List<String> resources = new ArrayList<>();
private boolean supportVariables = true;

public Builder integration(String integrationName) {
Expand Down Expand Up @@ -566,8 +592,13 @@ public Builder openApi(String configMap) {
return this;
}

public Builder resource(String resource) {
this.resources.add(resource);
return this;
}

public Builder dependencies(String dependencies) {
if (dependencies != null && dependencies.length() > 0) {
if (dependencies != null && !dependencies.isEmpty()) {
dependencies(Arrays.asList(dependencies.split(",")));
}

Expand All @@ -585,7 +616,7 @@ public Builder propertyFiles(List<String> propertyFiles) {
}

public Builder properties(String properties) {
if (properties != null && properties.length() > 0) {
if (properties != null && !properties.isEmpty()) {
properties(Arrays.asList(properties.split(",")));
}

Expand Down Expand Up @@ -684,7 +715,7 @@ public Builder envVar(String name, String value) {
}

public Builder traits(String traits) {
if (traits != null && traits.length() > 0) {
if (traits != null && !traits.isEmpty()) {
traits(Arrays.asList(traits.split(",")));
}

Expand Down

0 comments on commit b14700e

Please sign in to comment.