|
1 | 1 | package io.javaoperatorsdk.operator.springboot.starter;
|
2 | 2 |
|
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | + |
3 | 9 | import io.fabric8.kubernetes.client.ConfigBuilder;
|
| 10 | +import io.fabric8.kubernetes.client.CustomResource; |
4 | 11 | import io.fabric8.kubernetes.client.DefaultKubernetesClient;
|
5 | 12 | import io.fabric8.kubernetes.client.KubernetesClient;
|
6 | 13 | import io.fabric8.openshift.client.DefaultOpenShiftClient;
|
7 | 14 | import io.javaoperatorsdk.operator.Operator;
|
8 | 15 | import io.javaoperatorsdk.operator.api.ResourceController;
|
| 16 | +import io.javaoperatorsdk.operator.config.AnnotationConfiguration; |
| 17 | +import io.javaoperatorsdk.operator.config.ClientConfiguration; |
| 18 | +import io.javaoperatorsdk.operator.config.ConfigurationService; |
| 19 | +import io.javaoperatorsdk.operator.config.ControllerConfiguration; |
| 20 | +import io.javaoperatorsdk.operator.config.RetryConfiguration; |
9 | 21 | import io.javaoperatorsdk.operator.processing.retry.GenericRetry;
|
10 | 22 | import io.javaoperatorsdk.operator.processing.retry.Retry;
|
11 |
| -import java.util.List; |
12 | 23 | import org.apache.commons.lang3.StringUtils;
|
13 | 24 | import org.slf4j.Logger;
|
14 | 25 | import org.slf4j.LoggerFactory;
|
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
15 | 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
16 | 28 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
17 | 29 | import org.springframework.context.annotation.Bean;
|
18 | 30 | import org.springframework.context.annotation.Configuration;
|
19 | 31 |
|
20 | 32 | @Configuration
|
21 |
| -@EnableConfigurationProperties({OperatorProperties.class, RetryProperties.class}) |
22 |
| -public class OperatorAutoConfiguration { |
23 |
| - private static final Logger log = LoggerFactory.getLogger(OperatorAutoConfiguration.class); |
24 |
| - |
25 |
| - @Bean |
26 |
| - @ConditionalOnMissingBean |
27 |
| - public KubernetesClient kubernetesClient(OperatorProperties operatorProperties) { |
| 33 | +@EnableConfigurationProperties({ConfigurationProperties.class}) |
| 34 | +public class OperatorAutoConfiguration implements ConfigurationService { |
| 35 | + private static final Logger log = LoggerFactory.getLogger(OperatorAutoConfiguration.class); |
| 36 | + @Autowired |
| 37 | + private ConfigurationProperties configuration; |
| 38 | + private final Map<String, ControllerConfiguration> controllers = new ConcurrentHashMap<>(); |
| 39 | + |
| 40 | + @Bean |
| 41 | + @ConditionalOnMissingBean |
| 42 | + public KubernetesClient kubernetesClient() { |
| 43 | + final var clientCfg = getClientConfiguration(); |
28 | 44 | ConfigBuilder config = new ConfigBuilder();
|
29 |
| - config.withTrustCerts(operatorProperties.isTrustSelfSignedCertificates()); |
30 |
| - if (StringUtils.isNotBlank(operatorProperties.getUsername())) { |
31 |
| - config.withUsername(operatorProperties.getUsername()); |
| 45 | + config.withTrustCerts(clientCfg.isTrustSelfSignedCertificates()); |
| 46 | + clientCfg.getMasterUrl().ifPresent(config::withMasterUrl); |
| 47 | + clientCfg.getUsername().ifPresent(config::withUsername); |
| 48 | + clientCfg.getPassword().ifPresent(config::withPassword); |
| 49 | + return clientCfg.isOpenshift() ? new DefaultOpenShiftClient(config.build()) : new DefaultKubernetesClient(config.build()); |
32 | 50 | }
|
33 |
| - if (StringUtils.isNotBlank(operatorProperties.getPassword())) { |
34 |
| - config.withUsername(operatorProperties.getPassword()); |
| 51 | + |
| 52 | + @Bean |
| 53 | + @ConditionalOnMissingBean(Operator.class) |
| 54 | + public Operator operator(KubernetesClient kubernetesClient, ConfigurationProperties config, List<ResourceController> resourceControllers) { |
| 55 | + Operator operator = new Operator(kubernetesClient); |
| 56 | + // todo: create register method that takes the controller's configuration into account |
| 57 | + resourceControllers.forEach(r -> operator.registerController(processController(r))); |
| 58 | + return operator; |
35 | 59 | }
|
36 |
| - if (StringUtils.isNotBlank(operatorProperties.getMasterUrl())) { |
37 |
| - config.withMasterUrl(operatorProperties.getMasterUrl()); |
| 60 | + |
| 61 | + private ResourceController processController(ResourceController controller) { |
| 62 | + final var controllerPropertiesMap = configuration.getControllers(); |
| 63 | + var controllerProps = controllerPropertiesMap.get(controller.getName()); |
| 64 | + final var cfg = new ConfigurationWrapper(controller, controllerProps); |
| 65 | + this.controllers.put(controller.getName(), cfg); |
| 66 | + return controller; |
38 | 67 | }
|
39 |
| - return operatorProperties.isOpenshift() |
40 |
| - ? new DefaultOpenShiftClient(config.build()) |
41 |
| - : new DefaultKubernetesClient(config.build()); |
42 |
| - } |
43 |
| - |
44 |
| - @Bean |
45 |
| - @ConditionalOnMissingBean(Operator.class) |
46 |
| - public Operator operator( |
47 |
| - KubernetesClient kubernetesClient, List<ResourceController> resourceControllers) { |
48 |
| - Operator operator = new Operator(kubernetesClient); |
49 |
| - resourceControllers.forEach(r -> operator.registerControllerForAllNamespaces(r)); |
50 |
| - return operator; |
51 |
| - } |
52 |
| - |
53 |
| - @Bean |
54 |
| - @ConditionalOnMissingBean |
55 |
| - public Retry retry(RetryProperties retryProperties) { |
56 |
| - GenericRetry retry = new GenericRetry(); |
57 |
| - if (retryProperties.getInitialInterval() != null) { |
58 |
| - retry.setInitialInterval(retryProperties.getInitialInterval()); |
| 68 | + |
| 69 | + @Override |
| 70 | + public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(ResourceController<R> controller) { |
| 71 | + return controllers.get(controller.getName()); |
59 | 72 | }
|
60 |
| - if (retryProperties.getIntervalMultiplier() != null) { |
61 |
| - retry.setIntervalMultiplier(retryProperties.getIntervalMultiplier()); |
| 73 | + |
| 74 | + @Override |
| 75 | + public ClientConfiguration getClientConfiguration() { |
| 76 | + return configuration.getClient(); |
62 | 77 | }
|
63 |
| - if (retryProperties.getMaxAttempts() != null) { |
64 |
| - retry.setMaxAttempts(retryProperties.getMaxAttempts()); |
| 78 | + |
| 79 | + private static class ConfigurationWrapper<R extends CustomResource> extends AnnotationConfiguration<R> { |
| 80 | + private final Optional<ControllerProperties> properties; |
| 81 | + |
| 82 | + private ConfigurationWrapper(ResourceController<R> controller, ControllerProperties properties) { |
| 83 | + super(controller); |
| 84 | + this.properties = Optional.ofNullable(properties); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getName() { |
| 89 | + return super.getName(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getCRDName() { |
| 94 | + return properties.map(ControllerProperties::getCRDName).orElse(super.getCRDName()); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String getFinalizer() { |
| 99 | + return properties.map(ControllerProperties::getFinalizer).orElse(super.getFinalizer()); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean isGenerationAware() { |
| 104 | + return properties.map(ControllerProperties::isGenerationAware).orElse(super.isGenerationAware()); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Class<R> getCustomResourceClass() { |
| 109 | + return super.getCustomResourceClass(); |
65 | 110 | }
|
66 |
| - if (retryProperties.getMaxInterval() != null) { |
67 |
| - retry.setInitialInterval(retryProperties.getMaxInterval()); |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean isClusterScoped() { |
| 114 | + return properties.map(ControllerProperties::isClusterScoped).orElse(super.isClusterScoped()); |
68 | 115 | }
|
69 |
| - return retry; |
| 116 | + |
| 117 | + @Override |
| 118 | + public Set<String> getNamespaces() { |
| 119 | + return properties.map(ControllerProperties::getNamespaces).orElse(super.getNamespaces()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean watchAllNamespaces() { |
| 124 | + return super.watchAllNamespaces(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public RetryConfiguration getRetryConfiguration() { |
| 129 | + return properties.map(ControllerProperties::getRetry).map(RetryProperties::asRetryConfiguration).orElse(RetryConfiguration.DEFAULT); |
| 130 | + } |
70 | 131 | }
|
71 | 132 | }
|
0 commit comments