Skip to content

Commit 865aab5

Browse files
committed
Revert "removed spring boot support, added spring boot sample"
This reverts commit cff8b86. # Conflicts: # README.md # pom.xml
1 parent daf79e5 commit 865aab5

File tree

16 files changed

+378
-36
lines changed

16 files changed

+378
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The Controller only contains the logic to create, update and delete the actual r
1212

1313
Feature we would like to implement and invite the community to help us implement in the future:
1414

15-
* ~~Spring Boot sample~~
15+
* ~~Spring Boot support~~
1616
* Class generation from CRD to POJO
1717
* GraalVM / Quarkus support
1818

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
<version>3.0.0</version>
7575
<scope>test</scope>
7676
</dependency>
77+
<dependency>
78+
<groupId>org.springframework</groupId>
79+
<artifactId>spring-core</artifactId>
80+
<version>5.1.8.RELEASE</version>
81+
<scope>compile</scope>
82+
</dependency>
7783
</dependencies>
7884
</dependencyManagement>
7985

@@ -89,7 +95,7 @@
8995
<plugin>
9096
<groupId>org.apache.maven.plugins</groupId>
9197
<artifactId>maven-surefire-plugin</artifactId>
92-
<version>2.22.2</version>
98+
<!--<version>2.19.1</version> -->
9399
<configuration>
94100
<includes>
95101
<include>**/*IT.java</include>

samples/basic/spring-boot/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-dependencies</artifactId>
41-
<version>2.1.6.RELEASE</version>
41+
<version>2.2.6.RELEASE</version>
4242
<type>pom</type>
4343
<scope>import</scope>
4444
</dependency>

samples/basic/spring-boot/src/main/java/com/github/containersolutions/operator/sample/Config.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.containersolutions.operator.sample;
2+
3+
import com.github.containersolutions.operator.Operator;
4+
import io.fabric8.kubernetes.client.KubernetesClient;
5+
import io.fabric8.kubernetes.client.dsl.internal.CustomResourceOperationsImpl;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* This component just showcases what beans are registered.
11+
*/
12+
@Component
13+
public class SampleComponent {
14+
15+
private final Operator operator;
16+
17+
private final KubernetesClient kubernetesClient;
18+
19+
private final CustomServiceController customServiceController;
20+
21+
/**
22+
* You can use qualifier for custom resource operation in case there are more custom resources for the operator
23+
*/
24+
private final CustomResourceOperationsImpl customResourceOperations;
25+
26+
public SampleComponent(Operator operator, KubernetesClient kubernetesClient,
27+
CustomServiceController customServiceController,
28+
@Qualifier(CustomServiceController.KIND) CustomResourceOperationsImpl customResourceOperations) {
29+
this.operator = operator;
30+
this.kubernetesClient = kubernetesClient;
31+
this.customServiceController = customServiceController;
32+
this.customResourceOperations = customResourceOperations;
33+
}
34+
}

spring-boot-starter/pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>com.github.containersolutions</groupId>
8+
<artifactId>java-operator-sdk</artifactId>
9+
<version>1.0.1-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>spring-boot-operator-framework-starter</artifactId>
13+
<name>Operator SDK - Spring Boot Starter</name>
14+
<description>Spring Boot starter for framework</description>
15+
<packaging>jar</packaging>
16+
17+
<properties>
18+
<junit-jupiter.version>5.3.2</junit-jupiter.version>
19+
<java.version>8</java.version>
20+
<maven.compiler.source>1.8</maven.compiler.source>
21+
<maven.compiler.target>1.8</maven.compiler.target>
22+
</properties>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-surefire-plugin</artifactId>
29+
<version>2.22.2</version>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-maven-plugin</artifactId>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
38+
<dependencyManagement>
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-dependencies</artifactId>
43+
<version>2.1.6.RELEASE</version>
44+
<type>pom</type>
45+
<scope>import</scope>
46+
</dependency>
47+
</dependencies>
48+
</dependencyManagement>
49+
50+
<dependencies>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-autoconfigure-processor</artifactId>
54+
<optional>true</optional>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-autoconfigure</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-starter-test</artifactId>
63+
<scope>test</scope>
64+
<exclusions>
65+
<exclusion>
66+
<groupId>junit</groupId>
67+
<artifactId>junit</artifactId>
68+
</exclusion>
69+
</exclusions>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.junit.jupiter</groupId>
73+
<artifactId>junit-jupiter-api</artifactId>
74+
<version>${junit-jupiter.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.junit.jupiter</groupId>
79+
<artifactId>junit-jupiter-engine</artifactId>
80+
<version>${junit-jupiter.version}</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.github.containersolutions</groupId>
85+
<artifactId>operator-framework</artifactId>
86+
<version>${project.version}</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.slf4j</groupId>
90+
<artifactId>slf4j-api</artifactId>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.mockito</groupId>
94+
<artifactId>mockito-core</artifactId>
95+
</dependency>
96+
</dependencies>
97+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.containersolutions.operator.spingboot.starter;
2+
3+
import com.github.containersolutions.operator.Operator;
4+
import com.github.containersolutions.operator.api.ResourceController;
5+
import io.fabric8.kubernetes.client.ConfigBuilder;
6+
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
7+
import io.fabric8.kubernetes.client.KubernetesClient;
8+
import io.fabric8.kubernetes.client.dsl.internal.CustomResourceOperationsImpl;
9+
import io.fabric8.openshift.client.DefaultOpenShiftClient;
10+
import org.apache.commons.lang3.StringUtils;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
15+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
16+
import org.springframework.context.annotation.Bean;
17+
import org.springframework.context.annotation.Configuration;
18+
import org.springframework.context.support.GenericApplicationContext;
19+
20+
import java.util.List;
21+
22+
@Configuration
23+
@EnableConfigurationProperties(OperatorProperties.class)
24+
@ConditionalOnMissingBean(Operator.class)
25+
public class OperatorAutoConfiguration {
26+
private static final Logger log = LoggerFactory.getLogger(OperatorAutoConfiguration.class);
27+
28+
@Autowired
29+
private GenericApplicationContext genericApplicationContext;
30+
31+
@Autowired
32+
private OperatorProperties operatorProperties;
33+
34+
@Autowired
35+
private List<ResourceController> resourceControllers;
36+
37+
@Bean
38+
@ConditionalOnMissingBean
39+
public KubernetesClient kubernetesClient() {
40+
ConfigBuilder config = new ConfigBuilder();
41+
config.withTrustCerts(operatorProperties.isTrustSelfSignedCertificates());
42+
if (StringUtils.isNotBlank(operatorProperties.getUsername())) {
43+
config.withUsername(operatorProperties.getUsername());
44+
}
45+
if (StringUtils.isNotBlank(operatorProperties.getPassword())) {
46+
config.withUsername(operatorProperties.getPassword());
47+
}
48+
if (StringUtils.isNotBlank(operatorProperties.getMasterUrl())) {
49+
config.withMasterUrl(operatorProperties.getMasterUrl());
50+
}
51+
KubernetesClient k8sClient = operatorProperties.isOpenshift() ? new DefaultOpenShiftClient(config.build()) : new DefaultKubernetesClient(config.build());
52+
return k8sClient;
53+
}
54+
55+
@Bean
56+
public Operator operator(KubernetesClient kubernetesClient) {
57+
Operator operator = new Operator(kubernetesClient);
58+
resourceControllers.forEach(r -> operator.registerController(r));
59+
return operator;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.containersolutions.operator.spingboot.starter;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
@ConfigurationProperties(prefix = "operator.kubernetes")
6+
public class OperatorProperties {
7+
8+
private boolean openshift = false;
9+
private String username;
10+
private String password;
11+
private String masterUrl;
12+
private boolean trustSelfSignedCertificates = false;
13+
14+
public boolean isOpenshift() {
15+
return openshift;
16+
}
17+
18+
public OperatorProperties setOpenshift(boolean openshift) {
19+
this.openshift = openshift;
20+
return this;
21+
}
22+
23+
public String getUsername() {
24+
return username;
25+
}
26+
27+
public OperatorProperties setUsername(String username) {
28+
this.username = username;
29+
return this;
30+
}
31+
32+
public String getPassword() {
33+
return password;
34+
}
35+
36+
public OperatorProperties setPassword(String password) {
37+
this.password = password;
38+
return this;
39+
}
40+
41+
public String getMasterUrl() {
42+
return masterUrl;
43+
}
44+
45+
public OperatorProperties setMasterUrl(String masterUrl) {
46+
this.masterUrl = masterUrl;
47+
return this;
48+
}
49+
50+
public boolean isTrustSelfSignedCertificates() {
51+
return trustSelfSignedCertificates;
52+
}
53+
54+
public OperatorProperties setTrustSelfSignedCertificates(boolean trustSelfSignedCertificates) {
55+
this.trustSelfSignedCertificates = trustSelfSignedCertificates;
56+
return this;
57+
}
58+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
com.github.containersolutions.operator.spingboot.starter.OperatorAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.containersolutions.operator.spingboot.starter;
2+
3+
import com.github.containersolutions.operator.Operator;
4+
import com.github.containersolutions.operator.api.ResourceController;
5+
import io.fabric8.kubernetes.client.KubernetesClient;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.mock.mockito.MockBean;
11+
import org.springframework.test.context.junit.jupiter.SpringExtension;
12+
13+
import java.util.List;
14+
15+
import static org.junit.jupiter.api.Assertions.*;
16+
17+
@ExtendWith(SpringExtension.class)
18+
@SpringBootTest
19+
public class AutoconfigurationTest {
20+
21+
@Autowired
22+
private OperatorProperties operatorProperties;
23+
24+
@MockBean
25+
private Operator operator;
26+
27+
@Autowired
28+
private KubernetesClient kubernetesClient;
29+
30+
@Autowired
31+
private List<ResourceController> resourceControllers;
32+
33+
@Test
34+
public void configurationsLoadedProperly() {
35+
assertEquals("user", operatorProperties.getUsername());
36+
assertEquals("password", operatorProperties.getPassword());
37+
assertEquals("http://master.url", operatorProperties.getMasterUrl());
38+
}
39+
40+
@Test
41+
public void beansCreated() {
42+
assertNotNull(kubernetesClient);
43+
}
44+
45+
@Test
46+
public void resourceControllersAreDiscovered() {
47+
assertEquals(1, resourceControllers.size());
48+
assertTrue(resourceControllers.get(0) instanceof TestController);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)