Skip to content

Commit 131b7cd

Browse files
committed
refactor: make configuration handling reusable in non-Spring contexts
1 parent 2c90655 commit 131b7cd

File tree

6 files changed

+116
-140
lines changed

6 files changed

+116
-140
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/Configuration.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package io.javaoperatorsdk.operator.config;
22

3+
import java.util.Optional;
4+
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.openshift.client.DefaultOpenShiftClient;
9+
import io.javaoperatorsdk.operator.processing.retry.GenericRetry;
10+
import io.javaoperatorsdk.operator.processing.retry.Retry;
11+
312
public class Configuration {
4-
private ClientConfiguration client = new ClientConfiguration();
5-
private OperatorConfiguration operator = new OperatorConfiguration();
13+
private final ClientConfiguration client = new ClientConfiguration();
14+
private final OperatorConfiguration operator = new OperatorConfiguration();
15+
private final RetryConfiguration retry = new RetryConfiguration();
616

717
public ClientConfiguration getClientConfiguration() {
818
return client;
@@ -12,6 +22,39 @@ public OperatorConfiguration getOperatorConfiguration() {
1222
return operator;
1323
}
1424

25+
public RetryConfiguration getRetryConfiguration() {
26+
return retry;
27+
}
28+
29+
public KubernetesClient getConfiguredClient() {
30+
return getClientFor(getClientConfiguration(), getOperatorConfiguration());
31+
}
32+
33+
public static KubernetesClient getClientFor(ClientConfiguration clientCfg, OperatorConfiguration operatorCfg) {
34+
ConfigBuilder cb = new ConfigBuilder();
35+
36+
cb.withTrustCerts(clientCfg.isTrustSelfSignedCertificates());
37+
trimmedPropertyIfPresent(clientCfg.getUsername()).ifPresent(cb::withUsername);
38+
trimmedPropertyIfPresent(clientCfg.getPassword()).ifPresent(cb::withPassword);
39+
trimmedPropertyIfPresent(clientCfg.getMasterUrl()).ifPresent(cb::withMasterUrl);
40+
41+
operatorCfg.getWatchedNamespaceIfUnique().ifPresent(cb::withNamespace);
42+
return clientCfg.isOpenshift() ? new DefaultOpenShiftClient(cb.build()) : new DefaultKubernetesClient(cb.build());
43+
}
44+
45+
public static Retry getRetryFor(RetryConfiguration retryCfg) {
46+
GenericRetry retry = new GenericRetry();
47+
Optional.ofNullable(retryCfg.getInitialInterval()).ifPresent(retry::setInitialInterval);
48+
Optional.ofNullable(retryCfg.getIntervalMultiplier()).ifPresent(retry::setIntervalMultiplier);
49+
Optional.ofNullable(retryCfg.getMaxAttempts()).ifPresent(retry::setMaxAttempts);
50+
Optional.ofNullable(retryCfg.getMaxInterval()).ifPresent(retry::setMaxInterval);
51+
return retry;
52+
}
53+
54+
private static Optional<String> trimmedPropertyIfPresent(String string) {
55+
return Optional.ofNullable(string).map(String::trim);
56+
}
57+
1558
public static Configuration defaultConfiguration() {
1659
return new Configuration();
1760
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.javaoperatorsdk.operator.config;
2+
3+
public class RetryConfiguration {
4+
private Integer maxAttempts;
5+
private Long initialInterval;
6+
private Double intervalMultiplier;
7+
private Long maxInterval;
8+
private Long maxElapsedTime;
9+
10+
public Integer getMaxAttempts() {
11+
return maxAttempts;
12+
}
13+
14+
public RetryConfiguration setMaxAttempts(Integer maxAttempts) {
15+
this.maxAttempts = maxAttempts;
16+
return this;
17+
}
18+
19+
public Long getInitialInterval() {
20+
return initialInterval;
21+
}
22+
23+
public RetryConfiguration setInitialInterval(Long initialInterval) {
24+
this.initialInterval = initialInterval;
25+
return this;
26+
}
27+
28+
public Double getIntervalMultiplier() {
29+
return intervalMultiplier;
30+
}
31+
32+
public RetryConfiguration setIntervalMultiplier(Double intervalMultiplier) {
33+
this.intervalMultiplier = intervalMultiplier;
34+
return this;
35+
}
36+
37+
public Long getMaxInterval() {
38+
return maxInterval;
39+
}
40+
41+
public RetryConfiguration setMaxInterval(Long maxInterval) {
42+
this.maxInterval = maxInterval;
43+
return this;
44+
}
45+
46+
public Long getMaxElapsedTime() {
47+
return maxElapsedTime;
48+
}
49+
50+
public RetryConfiguration setMaxElapsedTime(Long maxElapsedTime) {
51+
this.maxElapsedTime = maxElapsedTime;
52+
return this;
53+
}
54+
}
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,8 @@
11
package io.javaoperatorsdk.operator.springboot.starter;
22

3+
import io.javaoperatorsdk.operator.config.ClientConfiguration;
34
import org.springframework.boot.context.properties.ConfigurationProperties;
45

56
@ConfigurationProperties(prefix = "operator.kubernetes.client")
6-
public class ClientProperties {
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 ClientProperties setOpenshift(boolean openshift) {
19-
this.openshift = openshift;
20-
return this;
21-
}
22-
23-
public String getUsername() {
24-
return username;
25-
}
26-
27-
public ClientProperties setUsername(String username) {
28-
this.username = username;
29-
return this;
30-
}
31-
32-
public String getPassword() {
33-
return password;
34-
}
35-
36-
public ClientProperties setPassword(String password) {
37-
this.password = password;
38-
return this;
39-
}
40-
41-
public String getMasterUrl() {
42-
return masterUrl;
43-
}
44-
45-
public ClientProperties setMasterUrl(String masterUrl) {
46-
this.masterUrl = masterUrl;
47-
return this;
48-
}
49-
50-
public boolean isTrustSelfSignedCertificates() {
51-
return trustSelfSignedCertificates;
52-
}
53-
54-
public ClientProperties setTrustSelfSignedCertificates(boolean trustSelfSignedCertificates) {
55-
this.trustSelfSignedCertificates = trustSelfSignedCertificates;
56-
return this;
57-
}
7+
public class ClientProperties extends ClientConfiguration {
588
}

spring-boot-starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/OperatorAutoConfiguration.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
import java.util.List;
44

5-
import io.fabric8.kubernetes.client.ConfigBuilder;
6-
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
75
import io.fabric8.kubernetes.client.KubernetesClient;
8-
import io.fabric8.openshift.client.DefaultOpenShiftClient;
96
import io.javaoperatorsdk.operator.Operator;
107
import io.javaoperatorsdk.operator.api.ResourceController;
11-
import io.javaoperatorsdk.operator.processing.retry.GenericRetry;
128
import io.javaoperatorsdk.operator.processing.retry.Retry;
13-
import org.apache.commons.lang3.StringUtils;
149
import org.slf4j.Logger;
1510
import org.slf4j.LoggerFactory;
1611
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -25,45 +20,21 @@ public class OperatorAutoConfiguration {
2520

2621
@Bean
2722
@ConditionalOnMissingBean
28-
public KubernetesClient kubernetesClient(ClientProperties clientProperties) {
29-
ConfigBuilder config = new ConfigBuilder();
30-
config.withTrustCerts(clientProperties.isTrustSelfSignedCertificates());
31-
if (StringUtils.isNotBlank(clientProperties.getUsername())) {
32-
config.withUsername(clientProperties.getUsername());
33-
}
34-
if (StringUtils.isNotBlank(clientProperties.getPassword())) {
35-
config.withUsername(clientProperties.getPassword());
36-
}
37-
if (StringUtils.isNotBlank(clientProperties.getMasterUrl())) {
38-
config.withMasterUrl(clientProperties.getMasterUrl());
39-
}
40-
return clientProperties.isOpenshift() ? new DefaultOpenShiftClient(config.build()) : new DefaultKubernetesClient(config.build());
23+
public KubernetesClient kubernetesClient(ClientProperties clientProperties, OperatorProperties operatorProperties) {
24+
return io.javaoperatorsdk.operator.config.Configuration.getClientFor(clientProperties, operatorProperties);
4125
}
42-
26+
4327
@Bean
4428
@ConditionalOnMissingBean(Operator.class)
4529
public Operator operator(KubernetesClient kubernetesClient, Retry retry, List<ResourceController> resourceControllers) {
4630
Operator operator = new Operator(kubernetesClient);
4731
resourceControllers.forEach(r -> operator.registerController(r, retry));
4832
return operator;
4933
}
50-
34+
5135
@Bean
5236
@ConditionalOnMissingBean
5337
public Retry retry(RetryProperties retryProperties) {
54-
GenericRetry retry = new GenericRetry();
55-
if (retryProperties.getInitialInterval() != null) {
56-
retry.setInitialInterval(retryProperties.getInitialInterval());
57-
}
58-
if (retryProperties.getIntervalMultiplier() != null) {
59-
retry.setIntervalMultiplier(retryProperties.getIntervalMultiplier());
60-
}
61-
if (retryProperties.getMaxAttempts() != null) {
62-
retry.setMaxAttempts(retryProperties.getMaxAttempts());
63-
}
64-
if (retryProperties.getMaxInterval() != null) {
65-
retry.setInitialInterval(retryProperties.getMaxInterval());
66-
}
67-
return retry;
38+
return io.javaoperatorsdk.operator.config.Configuration.getRetryFor(retryProperties);
6839
}
6940
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.javaoperatorsdk.operator.springboot.starter;
2+
3+
import io.javaoperatorsdk.operator.config.OperatorConfiguration;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@ConfigurationProperties(prefix = "operator")
7+
public class OperatorProperties extends OperatorConfiguration {
8+
}
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,8 @@
11
package io.javaoperatorsdk.operator.springboot.starter;
22

3+
import io.javaoperatorsdk.operator.config.RetryConfiguration;
34
import org.springframework.boot.context.properties.ConfigurationProperties;
45

56
@ConfigurationProperties(prefix = "operator.controller.retry")
6-
public class RetryProperties {
7-
8-
private Integer maxAttempts;
9-
private Long initialInterval;
10-
private Double intervalMultiplier;
11-
private Long maxInterval;
12-
private Long maxElapsedTime;
13-
14-
public Integer getMaxAttempts() {
15-
return maxAttempts;
16-
}
17-
18-
public RetryProperties setMaxAttempts(Integer maxAttempts) {
19-
this.maxAttempts = maxAttempts;
20-
return this;
21-
}
22-
23-
public Long getInitialInterval() {
24-
return initialInterval;
25-
}
26-
27-
public RetryProperties setInitialInterval(Long initialInterval) {
28-
this.initialInterval = initialInterval;
29-
return this;
30-
}
31-
32-
public Double getIntervalMultiplier() {
33-
return intervalMultiplier;
34-
}
35-
36-
public RetryProperties setIntervalMultiplier(Double intervalMultiplier) {
37-
this.intervalMultiplier = intervalMultiplier;
38-
return this;
39-
}
40-
41-
public Long getMaxInterval() {
42-
return maxInterval;
43-
}
44-
45-
public RetryProperties setMaxInterval(Long maxInterval) {
46-
this.maxInterval = maxInterval;
47-
return this;
48-
}
49-
50-
public Long getMaxElapsedTime() {
51-
return maxElapsedTime;
52-
}
53-
54-
public RetryProperties setMaxElapsedTime(Long maxElapsedTime) {
55-
this.maxElapsedTime = maxElapsedTime;
56-
return this;
57-
}
7+
public class RetryProperties extends RetryConfiguration {
588
}

0 commit comments

Comments
 (0)