Skip to content

Commit 2042d20

Browse files
authored
Merge pull request #1 from java-operator-sdk/master
update fork
2 parents a4d5507 + 2ef6c8f commit 2042d20

File tree

14 files changed

+300
-78
lines changed

14 files changed

+300
-78
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,28 @@ public class Application {
204204
}
205205
}
206206
```
207+
208+
#### Spring Boot test support
209+
210+
Adding the following dependency would let you mock the operator for the
211+
tests where loading the spring container is necessary,
212+
but it doesn't need real access to a Kubernetes cluster.
213+
214+
```xml
215+
<dependency>
216+
<groupId>io.javaoperatorsdk</groupId>
217+
<artifactId>operator-framework-spring-boot-starter-test</artifactId>
218+
<version>{see https://search.maven.org/search?q=a:operator-framework-spring-boot-starter for latest version}</version>
219+
</dependency>
220+
```
221+
222+
Mock the operator:
223+
```java
224+
@SpringBootTest
225+
@EnableMockOperator
226+
public class SpringBootStarterSampleApplicationTest {
227+
228+
@Test
229+
void contextLoads() {}
230+
}
231+
```

operator-framework-quarkus-extension/deployment/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
<groupId>io.quarkus</groupId>
3232
<artifactId>quarkus-kubernetes-client-deployment</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>io.quarkus</groupId>
36+
<artifactId>quarkus-kubernetes-deployment</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>io.quarkus</groupId>
40+
<artifactId>quarkus-container-image-jib-deployment</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>io.quarkus</groupId>
44+
<artifactId>quarkus-smallrye-health-deployment</artifactId>
45+
</dependency>
3446
</dependencies>
3547

3648
<build>

operator-framework-quarkus-extension/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2020
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2121
<maven.compiler.parameters>true</maven.compiler.parameters>
22-
<quarkus.version>1.10.3.Final</quarkus.version>
22+
<quarkus.version>1.10.5.Final</quarkus.version>
2323
<compiler-plugin.version>3.8.1</compiler-plugin.version>
2424
<maven.surefire.version>3.0.0-M5</maven.surefire.version>
2525
</properties>

operator-framework-quarkus-extension/runtime/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
<groupId>io.quarkus</groupId>
3333
<artifactId>quarkus-kubernetes-client</artifactId>
3434
</dependency>
35+
<dependency>
36+
<groupId>io.quarkus</groupId>
37+
<artifactId>quarkus-kubernetes</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>io.quarkus</groupId>
41+
<artifactId>quarkus-container-image-jib</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.quarkus</groupId>
45+
<artifactId>quarkus-smallrye-health</artifactId>
46+
</dependency>
3547
</dependencies>
3648

3749
<build>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.javaoperatorsdk</groupId>
8+
<artifactId>operator-framework-spring-boot-starter-test</artifactId>
9+
10+
<parent>
11+
<groupId>io.javaoperatorsdk</groupId>
12+
<artifactId>java-operator-sdk</artifactId>
13+
<version>1.6.2-SNAPSHOT</version>
14+
</parent>
15+
16+
<properties>
17+
<java.version>11</java.version>
18+
<maven.compiler.source>11</maven.compiler.source>
19+
<maven.compiler.target>11</maven.compiler.target>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-dependencies</artifactId>
27+
<version>2.3.4.RELEASE</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>io.fabric8</groupId>
37+
<artifactId>kubernetes-server-mock</artifactId>
38+
<version>4.12.0</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.javaoperatorsdk</groupId>
47+
<artifactId>operator-framework</artifactId>
48+
<version>${project.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.fabric8</groupId>
52+
<artifactId>kubernetes-server-mock</artifactId>
53+
<version>4.12.0</version>
54+
</dependency>
55+
</dependencies>
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.javaoperatorsdk.operator.springboot.starter.test;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
import org.springframework.context.annotation.Import;
6+
7+
@Retention(RetentionPolicy.RUNTIME)
8+
@Import(TestConfiguration.class)
9+
public @interface EnableMockOperator {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.javaoperatorsdk.operator.springboot.starter.test;
2+
3+
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
4+
import io.fabric8.kubernetes.client.KubernetesClient;
5+
import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
6+
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
7+
import io.fabric8.kubernetes.client.utils.Serialization;
8+
import io.fabric8.mockwebserver.Context;
9+
import java.io.FileInputStream;
10+
import java.io.FileNotFoundException;
11+
import java.util.Collections;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import okhttp3.mockwebserver.MockWebServer;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
import org.springframework.beans.factory.annotation.Value;
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.util.ResourceUtils;
21+
22+
@Configuration
23+
public class TestConfiguration {
24+
25+
private static final Logger log = LoggerFactory.getLogger(TestConfiguration.class);
26+
27+
@Value("${io.javaoperatorsdk.test.crdPaths}")
28+
private List<String> crdPaths;
29+
30+
@Bean
31+
public KubernetesMockServer k8sMockServer() {
32+
final var server =
33+
new KubernetesMockServer(
34+
new Context(),
35+
new MockWebServer(),
36+
new HashMap<>(),
37+
new KubernetesCrudDispatcher(Collections.emptyList()),
38+
true);
39+
server.init();
40+
return server;
41+
}
42+
43+
@Bean
44+
public KubernetesClient kubernetesClient(KubernetesMockServer server) {
45+
final var client = server.createClient();
46+
47+
crdPaths.forEach(
48+
crdPath -> {
49+
CustomResourceDefinition crd = null;
50+
try {
51+
crd = Serialization.unmarshal(new FileInputStream(ResourceUtils.getFile(crdPath)));
52+
} catch (FileNotFoundException e) {
53+
log.warn("CRD with path {} not found!", crdPath);
54+
e.printStackTrace();
55+
return;
56+
}
57+
58+
client.apiextensions().v1().customResourceDefinitions().create(crd);
59+
});
60+
61+
return client;
62+
}
63+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<module>operator-framework</module>
4646
<module>operator-framework-quarkus-extension</module>
4747
<module>operator-framework-spring-boot-starter</module>
48+
<module>operator-framework-spring-boot-starter-test</module>
4849
<module>samples</module>
4950
</modules>
5051

samples/quarkus/pom.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<properties>
1818
<maven.compiler.source>11</maven.compiler.source>
1919
<maven.compiler.target>11</maven.compiler.target>
20-
<quarkus.version>1.10.3.Final</quarkus.version>
20+
<quarkus.version>1.10.5.Final</quarkus.version>
2121
<compiler-plugin.version>3.8.1</compiler-plugin.version>
2222
<maven.compiler.parameters>true</maven.compiler.parameters>
2323
</properties>
@@ -45,10 +45,6 @@
4545
<artifactId>operator-framework-samples-common</artifactId>
4646
<version>${project.version}</version>
4747
</dependency>
48-
<dependency>
49-
<groupId>io.quarkus</groupId>
50-
<artifactId>quarkus-core</artifactId>
51-
</dependency>
5248
</dependencies>
5349

5450
<build>

samples/quarkus/src/main/java/io/javaoperatorsdk/operator/sample/QuarkusOperator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public class QuarkusOperator implements QuarkusApplication {
1717

1818
@Inject ConfigurationService configuration;
1919

20+
@Inject CustomServiceController controller;
21+
2022
public static void main(String... args) {
2123
Quarkus.run(QuarkusOperator.class, args);
2224
}
2325

2426
@Override
2527
public int run(String... args) throws Exception {
26-
final var config = configuration.getConfigurationFor(new CustomServiceController(client));
28+
final var config = configuration.getConfigurationFor(controller);
2729
System.out.println("CR class: " + config.getCustomResourceClass());
2830
System.out.println("Doneable class = " + config.getDoneableClass());
2931
Quarkus.waitForExit();

0 commit comments

Comments
 (0)