Skip to content

Commit

Permalink
Merge pull request #38 from bouskaJ/add_new_integration_feature
Browse files Browse the repository at this point in the history
[ADD] Add new integration step
  • Loading branch information
christophd authored Feb 20, 2020
2 parents c107f5e + 2e45be4 commit ee7ed52
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 4 deletions.
37 changes: 35 additions & 2 deletions deploy/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ rules:
verbs:
- get
- create
- apiGroups:
- camel.apache.org
resources:
- integrations
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
- apiGroups:
- yaks.dev
resources:
Expand Down
13 changes: 13 additions & 0 deletions deploy/viewer_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ rules:
- get
- list
- watch
- apiGroups:
- camel.apache.org
resources:
- integrations
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
13 changes: 13 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<spring.version>5.0.8.RELEASE</spring.version>
<cucumber.version>3.0.2</cucumber.version>
<camel.version>2.23.4</camel.version>
<jackson.version>2.9.8</jackson.version>
<junit.version>4.12</junit.version>
<kubernetes-client.version>4.3.0</kubernetes-client.version>
<postgresql.version>9.4.1212</postgresql.version>
Expand Down Expand Up @@ -68,6 +69,17 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${kubernetes-client.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- Citrus -->
<dependency>
<groupId>com.consol.citrus</groupId>
Expand Down Expand Up @@ -129,6 +141,7 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
Expand Down
11 changes: 10 additions & 1 deletion java/yaks-testing-camel-k/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${kubernetes-client.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
package dev.yaks.testing.camel.k;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import dev.yaks.testing.camel.k.model.Integration;
import io.fabric8.kubernetes.api.model.DoneablePod;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;


public class CamelKSteps {

private static final int MAX_ATTEMPTS = 150;
private static final long DELAY_BETWEEN_ATTEMPTS = 2000;

private KubernetesClient client;
private ObjectMapper obj = new ObjectMapper();

/** Logger */
private static final Logger LOG = LoggerFactory.getLogger(CamelKSteps.class);

@Given("^new integration with name ([a-z0-9_]+\\.[a-z0-9_]+) with configuration:$")
public void createNewIntegration(String name, Map<String, String> configuration) throws IOException {
if(configuration.get("source") == null) {
throw new IllegalStateException("Specify 'source' parameter");
}
createIntegration(name, configuration.get("source"), configuration.get("dependencies"), configuration.get("traits"));

}

@Given("^new integration with name ([a-z0-9_]+\\.[a-z0-9_]+)$")
public void createNewIntegration(String name, String source) throws IOException {
createIntegration(name, source, null, null);

}

@Given("^integration ([a-z0-9-.]+) is running$")
@Then("^integration ([a-z0-9-.]+) should be running$")
Expand Down Expand Up @@ -89,8 +125,80 @@ private Pod getRunningIntegrationPod(String integration) {
return null;
}

private CustomResourceDefinitionContext getIntegrationCRD() {
return new CustomResourceDefinitionContext.Builder()
.withName(Integration.CRD_INTEGRATION_NAME)
.withGroup(Integration.CRD_GROUP)
.withVersion(Integration.CRD_VERSION)
.withPlural("integrations")
.withScope("Namespaced")
.build();
}

private void createIntegration(String name, String source, String dependencies, String traits) {
final Integration.Builder integrationBuilder = new Integration.Builder()
.name(name)
.source(source);

if(dependencies != null && !dependencies.isEmpty()) {
integrationBuilder.dependencies(Arrays.asList(dependencies.split(",")));
}

if (traits != null && !traits.isEmpty()) {
final Map<String, Integration.TraitConfig> traitConfigMap = new HashMap<>();
for(String t : traits.split(",")){
//traitName.key=value
if(!validateTraitFormat(t)) {
throw new IllegalArgumentException("Trait" + t + "does not match format traitName.key=value");
}
final String[] trait = t.split("\\.",2);
final String[] traitConfig = trait[1].split("=", 2);
if(traitConfigMap.containsKey(trait[0])) {
traitConfigMap.get(trait[0]).add(traitConfig[0], traitConfig[1]);
} else {
traitConfigMap.put(trait[0], new Integration.TraitConfig(traitConfig[0], traitConfig[1]));
}
}
integrationBuilder.traits(traitConfigMap);
}

final Integration i = integrationBuilder.build();

final CustomResourceDefinitionContext crdContext = getIntegrationCRD();

try {
Map<String, Object> result = client().customResource(crdContext).createOrReplace(namespace(), obj.writeValueAsString(i));
if(result.get("message") != null) {
throw new IllegalStateException(result.get("message").toString());
}
} catch (IOException e) {
throw new IllegalStateException("Can't create Integration JSON object", e);
}
}

private boolean validateTraitFormat(String trait) {
String patternString = "[A-Za-z-0-9]+\\.[A-Za-z-0-9]+=[A-Za-z-0-9]+";

Pattern pattern = Pattern.compile(patternString);

Matcher matcher = pattern.matcher(trait);
return matcher.matches();
}



private String namespace() {
return System.getenv("NAMESPACE");
String result = System.getenv("NAMESPACE");
final File namespace = new File("/var/run/secrets/kubernetes.io/serviceaccount/namespace");
if(result == null && namespace.exists()){
try {
result = new String(Files.readAllBytes(namespace.toPath()));
} catch (IOException e) {
LOG.warn("Can't read {}", namespace, e);
return null;
}
}
return result;
}

private KubernetesClient client() {
Expand Down
Loading

0 comments on commit ee7ed52

Please sign in to comment.