Skip to content

Commit 01cd323

Browse files
committed
Merge remote-tracking branch 'remotes/origin/master' into spring-properties-naming
# Conflicts: # operator-framework-spring-boot-starter-test/src/main/java/io/javaoperatorsdk/operator/springboot/starter/test/TestConfiguration.java
2 parents 265b75b + af6bd88 commit 01cd323

File tree

10 files changed

+156
-22
lines changed

10 files changed

+156
-22
lines changed

.github/workflows/master-snapshot-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Run unit tests
2525
run: mvn -B test -P no-integration-tests --file pom.xml
2626
- name: Set up Minikube
27-
uses: manusa/actions-setup-minikube@v2.0.1
27+
uses: manusa/actions-setup-minikube@v2.3.0
2828
with:
2929
minikube version: 'v1.15.1'
3030
kubernetes version: ${{ matrix.kubernetes }}

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Run unit tests
2727
run: mvn -B test -P no-integration-tests --file pom.xml
2828
- name: Set up Minikube
29-
uses: manusa/actions-setup-minikube@v2.0.1
29+
uses: manusa/actions-setup-minikube@v2.3.0
3030
with:
3131
minikube version: 'v1.15.1'
3232
kubernetes version: ${{ matrix.kubernetes }}

operator-framework-spring-boot-starter-test/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,19 @@
5252
<artifactId>kubernetes-server-mock</artifactId>
5353
<version>4.12.0</version>
5454
</dependency>
55+
<dependency>
56+
<groupId>io.javaoperatorsdk</groupId>
57+
<artifactId>operator-framework-spring-boot-starter</artifactId>
58+
<version>${project.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-test-autoconfigure</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-test</artifactId>
67+
<scope>test</scope>
68+
</dependency>
5569
</dependencies>
5670
</project>

operator-framework-spring-boot-starter-test/src/main/java/io/javaoperatorsdk/operator/springboot/starter/test/EnableMockOperator.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22

33
import java.lang.annotation.Retention;
44
import java.lang.annotation.RetentionPolicy;
5+
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
56
import org.springframework.context.annotation.Import;
67

78
@Retention(RetentionPolicy.RUNTIME)
89
@Import(TestConfiguration.class)
9-
public @interface EnableMockOperator {}
10+
@PropertyMapping("io.javaoperatorsdk.test")
11+
public @interface EnableMockOperator {
12+
13+
/**
14+
* Define a list of files that contain CustomResourceDefinitions for the tested operator. If the
15+
* file to be loaded is shall be loaded from the classpath prefix it with 'classpath', otherwise
16+
* provide a path relative to the current working directory.
17+
*
18+
* @return List of files
19+
*/
20+
@PropertyMapping("crd-paths")
21+
String[] crdPaths() default {};
22+
}

operator-framework-spring-boot-starter-test/src/main/java/io/javaoperatorsdk/operator/springboot/starter/test/TestConfiguration.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
77
import io.fabric8.kubernetes.client.utils.Serialization;
88
import io.fabric8.mockwebserver.Context;
9+
import io.javaoperatorsdk.operator.springboot.starter.OperatorAutoConfiguration;
910
import java.io.FileInputStream;
1011
import java.io.FileNotFoundException;
1112
import java.util.Collections;
1213
import java.util.HashMap;
13-
import java.util.List;
14+
import java.util.stream.Stream;
1415
import okhttp3.mockwebserver.MockWebServer;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
17-
import org.springframework.beans.factory.annotation.Value;
18+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
19+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1820
import org.springframework.context.annotation.Bean;
1921
import org.springframework.context.annotation.Configuration;
2022
import org.springframework.util.ResourceUtils;
2123

2224
@Configuration
25+
@ImportAutoConfiguration(OperatorAutoConfiguration.class)
26+
@EnableConfigurationProperties(TestConfigurationProperties.class)
2327
public class TestConfiguration {
2428

2529
private static final Logger log = LoggerFactory.getLogger(TestConfiguration.class);
2630

27-
@Value("${javaoperatorsdk.test.crdPaths}")
28-
private List<String> crdPaths;
29-
3031
@Bean
3132
public KubernetesMockServer k8sMockServer() {
3233
final var server =
@@ -41,22 +42,24 @@ public KubernetesMockServer k8sMockServer() {
4142
}
4243

4344
@Bean
44-
public KubernetesClient kubernetesClient(KubernetesMockServer server) {
45+
public KubernetesClient kubernetesClient(
46+
KubernetesMockServer server, TestConfigurationProperties properties) {
4547
final var client = server.createClient();
4648

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-
});
49+
Stream.concat(properties.getCrdPaths().stream(), properties.getGlobalCrdPaths().stream())
50+
.forEach(
51+
crdPath -> {
52+
CustomResourceDefinition crd;
53+
try {
54+
crd = Serialization.unmarshal(new FileInputStream(ResourceUtils.getFile(crdPath)));
55+
} catch (FileNotFoundException e) {
56+
log.warn("CRD with path {} not found!", crdPath);
57+
e.printStackTrace();
58+
return;
59+
}
60+
61+
client.apiextensions().v1().customResourceDefinitions().create(crd);
62+
});
6063

6164
return client;
6265
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.javaoperatorsdk.operator.springboot.starter.test;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
7+
@ConfigurationProperties("io.javaoperatorsdk.test")
8+
public class TestConfigurationProperties {
9+
10+
private List<String> globalCrdPaths = new ArrayList<>();
11+
12+
private List<String> crdPaths = new ArrayList<>();
13+
14+
public List<String> getCrdPaths() {
15+
return crdPaths;
16+
}
17+
18+
public void setCrdPaths(List<String> crdPaths) {
19+
this.crdPaths = crdPaths;
20+
}
21+
22+
public List<String> getGlobalCrdPaths() {
23+
return globalCrdPaths;
24+
}
25+
26+
public void setGlobalCrdPaths(List<String> globalCrdPaths) {
27+
this.globalCrdPaths = globalCrdPaths;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.javaoperatorsdk.operator.springboot.starter.test;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.fabric8.kubernetes.client.KubernetesClient;
6+
import io.javaoperatorsdk.operator.Operator;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.test.autoconfigure.json.JsonTest;
11+
import org.springframework.context.ApplicationContext;
12+
13+
@JsonTest
14+
@EnableMockOperator(crdPaths = "classpath:crd.yml")
15+
class EnableMockOperatorTests {
16+
17+
@Autowired KubernetesClient client;
18+
19+
@Autowired ApplicationContext applicationContext;
20+
21+
@Test
22+
void testCrdLoaded() {
23+
assertThat(applicationContext.getBean(Operator.class)).isNotNull();
24+
assertThat(
25+
client
26+
.customResourceDefinitions()
27+
.withName("customservices.sample.javaoperatorsdk")
28+
.get())
29+
.isNotNull();
30+
assertThat(
31+
client
32+
.customResourceDefinitions()
33+
.withName("customservices.global.sample.javaoperatorsdk")
34+
.get())
35+
.isNotNull();
36+
}
37+
38+
@SpringBootApplication
39+
static class SpringBootTestApplication {}
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.javaoperatorsdk.test.global-crd-paths=classpath:global-crd.yml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: customservices.sample.javaoperatorsdk
5+
spec:
6+
group: sample.javaoperatorsdk
7+
scope: Namespaced
8+
names:
9+
plural: customservices
10+
singular: customservice
11+
kind: CustomService
12+
shortNames:
13+
- cs
14+
versions:
15+
- name: v1
16+
served: true
17+
storage: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: customservices.global.sample.javaoperatorsdk
5+
spec:
6+
group: sample.javaoperatorsdk
7+
scope: Namespaced
8+
names:
9+
plural: customservices
10+
singular: customservice
11+
kind: CustomService
12+
shortNames:
13+
- cs
14+
versions:
15+
- name: v1
16+
served: true
17+
storage: true

0 commit comments

Comments
 (0)