8000 Merge pull request #6 from ContainerSolutions/main · java-operator-sdk/samples@17a18d7 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit 17a18d7

Browse files
authored
Merge pull request #6 from ContainerSolutions/main
Merge results of ContainerSolutions mob programming sessions: GH Actions pipeline running integration test for TomcatOperator
2 parents dfe8d83 + 5ea00bd commit 17a18d7

File tree

7 files changed

+128
-5
lines changed

7 files changed

+128
-5
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Tomcat integration test
2+
on:
3+
push:
4+
branches:
5+
- "*"
6+
jobs:
7+
tomcat_integration_test:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
13+
- name: Set up Helm
14+
uses: azure/setup-helm@v1
15+
with:
16+
version: v3.4.0
17+
18+
- uses: actions/setup-python@v2
19+
with:
20+
python-version: 3.7
21+
22+
- name: Create kind cluster
23+
uses: helm/kind-action@v1.2.0
24+
25+
- name: Apply CRDs
26+
run: kubectl apply -f tomcat/k8s/crd.yaml
27+
28+
- name: Set up Java and Maven
29+
uses: actions/setup-java@v1
30+
with:
31+
# java-version: ${{ matrix.java }}
32+
java-version: 15
33+
- uses: actions/cache@v2
34+
with:
35+
path: ~/.m2/repository
36+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
37+
restore-keys: |
38+
${{ runner.os }}-maven-
39+
- name: Run unit tests
40+
run: mvn -B test --file tomcat/pom.xml

tomcat/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
<artifactId>takes</artifactId>
3939
<version>1.19</version>
4040
</dependency>
41+
<dependency>
42+
<groupId>junit</groupId>
43+
<artifactId>junit</artifactId>
44+
<version>4.13.1</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.awaitility</groupId>
49+
<artifactId>awaitility</artifactId>
50+
<version>4.1.0</version>
51+
<scope>test</scope>
52+
</dependency>
4153
</dependencies>
4254

4355
<build>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.javaoperatorsdk.operator.sample;
22

3+
import io.fabric8.kubernetes.api.model.Namespaced;
34
import io.fabric8.kubernetes.client.CustomResource;
45
import io.fabric8.kubernetes.model.annotation.Group;
56
import io.fabric8.kubernetes.model.annotation.Version;
67

78
@Group("tomcatoperator.io")
89
@Version("v1")
9-
public class Webapp extends CustomResource<WebappSpec, WebappStatus> {}
10+
public class Webapp extends CustomResource<WebappSpec, WebappStatus> implements Namespaced {}

tomcat/src/main/java/io/javaoperatorsdk/operator/sample/WebappController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public WebappController(KubernetesClient kubernetesClient) {
2424

2525
@Override
2626
public UpdateControl<Webapp> createOrUpdateResource(Webapp webapp, Context<Webapp> context) {
27-
if (Objects.equals(webapp.getSpec().getUrl(), webapp.getStatus().getDeployedArtifact())) {
27+
if (webapp.getStatus() != null && Objects.equals(webapp.getSpec().getUrl(), webapp.getStatus().getDeployedArtifact())) {
2828
return UpdateControl.noUpdate();
2929
}
3030

3131
String[] command = new String[] {"wget", "-O", "/data/" + webapp.getSpec().getContextPath() + ".war", webapp.getSpec().getUrl()};
3232

3333
executeCommandInAllPods(kubernetesClient, webapp, command);
3434

35-
webapp.getStatus().setDeployedArtifact(webapp.getSpec().getUrl());
35+
//webapp.getStatus().setDeployedArtifact(webapp.getSpec().getUrl());
3636
return UpdateControl.updateStatusSubResource(webapp);
3737
}
3838

tomcat/src/main/resources/io/javaoperatorsdk/operator/sample/service.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ spec:
99
- protocol: TCP
1010
port: 80
1111
targetPort: 8080
12-
nodePort: 30000
1312
type: NodePort

tomcat/src/main/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Configuration status="WARN">
33
<Appenders>
44
<Console name="Console" target="SYSTEM_OUT">
5-
<PatternLayout pattern="%style{%d}{yellow} %style{%-30c{1.}}{cyan} %highlight{[%-5level] %msg%n%throwable}{INFO=black}"/>
5+
<!-- <PatternLayout pattern="%style{%d}{yellow} %style{%-30c{1.}}{cyan} %highlight{[%-5level] %msg%n%throwable}{INFO=black}"/>-->
66
</Console>
77
</Appenders>
88
<Loggers>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package sample;
2+
3+
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
4+
import io.fabric8.kubernetes.api.model.Namespace;
5+
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
6+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
7+
import io.fabric8.kubernetes.client.Config;
8+
import io.fabric8.kubernetes.client.ConfigBuilder;
9+
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
10+
import io.fabric8.kubernetes.client.KubernetesClient;
11+
import io.fabric8.kubernetes.client.dsl.MixedOperation;
12+
import io.fabric8.kubernetes.client.dsl.Resource;
13+
import io.fabric8.kubernetes.client.utils.Serialization;
14+
import io.javaoperatorsdk.operator.Operator;
15+
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
16+
import io.javaoperatorsdk.operator.sample.Tomcat;
17+
import io.javaoperatorsdk.operator.sample.TomcatController;
18+
import io.javaoperatorsdk.operator.sample.WebappController;
19+
import org.junit.Test;
20+
21+
import java.io.FileInputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
25+
import static java.util.concurrent.TimeUnit.SECONDS;
26+
import static org.awaitility.Awaitility.await;
27+
import static org.junit.Assert.*;
28+
29+
public class IntegrationTest {
30+
@Test
31+
public void test() throws InterruptedException {
32+
Config config = new ConfigBuilder().withNamespace(null).build();
33+
KubernetesClient client = new DefaultKubernetesClient(config);
34+
Operator operator = new Operator(client, DefaultConfigurationService.instance());
35+
36+
TomcatController tomcatController = new TomcatController(client);
37+
operator.register(tomcatController);
38+
39+
operator.register(new WebappController(client));
40+
41+
Tomcat tomcat = loadYaml(Tomcat.class, "tomcat-sample1.yaml");
42+
43+
tomcat.getSpec().setReplicas(3);
44+
tomcat.getMetadata().setNamespace("tomcat-test");
45+
46+
MixedOperation<Tomcat, KubernetesResourceList<Tomcat>, Resource<Tomcat>> tomcatClient = client.customResources(Tomcat.class);
47+
48+
Namespace tt_ns = new NamespaceBuilder().withMetadata(new ObjectMetaBuilder().withName("tomcat-test").build()).build();
49+
50+
client.namespaces().delete(tt_ns);
51+
52+
await().atMost(300, SECONDS).until(() -> client.namespaces().withName("tomcat-test").get() == null);
53+
54+
client.namespaces().createOrReplace(tt_ns);
55+
56+
tomcatClient.inNamespace("tomcat-test").create(tomcat);
57+
58+
await().atMost(60, SECONDS).until(() -> {
59+
Tomcat updatedTomcat = tomcatClient.inNamespace("tomcat-test").withName("test-tomcat1").get();
60+
return updatedTomcat.getStatus() != null && (int) updatedTomcat.getStatus().getReadyReplicas() == 3;
61+
});
62+
}
63+
64+
private <T> T loadYaml(Class<T> clazz, String yaml) {
65+
try (InputStream is = new FileInputStream("k8s/" + yaml)) {
66+
return Serialization.unmarshal(is, clazz);
67+
} catch (IOException ex) {
68+
throw new IllegalStateException("Cannot find yaml on classpath: " + yaml);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)
0