Skip to content

Commit 35688e6

Browse files
authored
Merge pull request operator-framework#385 from i8r/object-mapper
Make ObjectMapper of CustomResourceCache configurable
2 parents 71fcef4 + 4985c59 commit 35688e6

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
45
import io.fabric8.kubernetes.client.CustomResource;
56
import io.fabric8.kubernetes.client.KubernetesClient;
@@ -24,10 +25,19 @@ public class Operator {
2425
private static final Logger log = LoggerFactory.getLogger(Operator.class);
2526
private final KubernetesClient k8sClient;
2627
private final ConfigurationService configurationService;
28+
private final ObjectMapper objectMapper;
2729

2830
public Operator(KubernetesClient k8sClient, ConfigurationService configurationService) {
31+
this(k8sClient, configurationService, new ObjectMapper());
32+
}
33+
34+
public Operator(
35+
KubernetesClient k8sClient,
36+
ConfigurationService configurationService,
37+
ObjectMapper objectMapper) {
2938
this.k8sClient = k8sClient;
3039
this.configurationService = configurationService;
40+
this.objectMapper = objectMapper;
3141
}
3242

3343
/**
@@ -131,7 +141,7 @@ public <R extends CustomResource> void register(
131141
new EventDispatcher(
132142
controller, finalizer, new EventDispatcher.CustomResourceFacade(client));
133143

134-
CustomResourceCache customResourceCache = new CustomResourceCache();
144+
CustomResourceCache customResourceCache = new CustomResourceCache(objectMapper);
135145
DefaultEventHandler defaultEventHandler =
136146
new DefaultEventHandler(customResourceCache, dispatcher, controllerName, retry);
137147
DefaultEventSourceManager eventSourceManager =

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/CustomResourceCache.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import io.fabric8.kubernetes.client.CustomResource;
6-
import java.util.Map;
76
import java.util.Optional;
87
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ConcurrentMap;
99
import java.util.concurrent.locks.Lock;
1010
import java.util.concurrent.locks.ReentrantLock;
1111
import java.util.function.Predicate;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15+
@SuppressWarnings("rawtypes")
1516
public class CustomResourceCache {
1617

1718
private static final Logger log = LoggerFactory.getLogger(CustomResourceCache.class);
1819

19-
private ObjectMapper objectMapper = new ObjectMapper();
20-
private final Map<String, CustomResource> resources = new ConcurrentHashMap<>();
20+
private final ObjectMapper objectMapper;
21+
private final ConcurrentMap<String, CustomResource> resources = new ConcurrentHashMap<>();
2122
private final Lock lock = new ReentrantLock();
2223

24+
public CustomResourceCache() {
25+
this(new ObjectMapper());
26+
}
27+
28+
public CustomResourceCache(ObjectMapper objectMapper) {
29+
this.objectMapper = objectMapper;
30+
}
31+
2332
public void cacheResource(CustomResource resource) {
2433
try {
2534
lock.lock();
@@ -49,18 +58,13 @@ public void cacheResource(CustomResource resource, Predicate<CustomResource> pre
4958
* @return
5059
*/
5160
public Optional<CustomResource> getLatestResource(String uuid) {
52-
return Optional.ofNullable(clone(resources.get(uuid)));
61+
return Optional.ofNullable(resources.get(uuid)).map(this::clone);
5362
}
5463

5564
private CustomResource clone(CustomResource customResource) {
5665
try {
57-
if (customResource == null) {
58-
return null;
59-
}
60-
CustomResource clonedObject =
61-
objectMapper.readValue(
62-
objectMapper.writeValueAsString(customResource), customResource.getClass());
63-
return clonedObject;
66+
return objectMapper.readValue(
67+
objectMapper.writeValueAsString(customResource), customResource.getClass());
6468
} catch (JsonProcessingException e) {
6569
throw new IllegalStateException(e);
6670
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator.springboot.starter;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import io.fabric8.kubernetes.client.Config;
45
import io.fabric8.kubernetes.client.ConfigBuilder;
56
import io.fabric8.kubernetes.client.CustomResource;
@@ -59,8 +60,13 @@ public boolean checkCRDAndValidateLocalModel() {
5960
@Bean
6061
@ConditionalOnMissingBean(Operator.class)
6162
public Operator operator(
62-
KubernetesClient kubernetesClient, List<ResourceController<?>> resourceControllers) {
63-
Operator operator = new Operator(kubernetesClient, this);
63+
KubernetesClient kubernetesClient,
64+
List<ResourceController<?>> resourceControllers,
65+
Optional<ObjectMapper> objectMapper) {
66+
Operator operator =
67+
objectMapper
68+
.map(x -> new Operator(kubernetesClient, this, x))
69+
.orElse(new Operator(kubernetesClient, this));
6470
resourceControllers.forEach(r -> operator.register(processController(r)));
6571
return operator;
6672
}

0 commit comments

Comments
 (0)