Skip to content

Make ObjectMapper of CustomResourceCache configurable #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.javaoperatorsdk.operator;

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

public Operator(KubernetesClient k8sClient, ConfigurationService configurationService) {
this(k8sClient, configurationService, new ObjectMapper());
}

public Operator(
KubernetesClient k8sClient,
ConfigurationService configurationService,
ObjectMapper objectMapper) {
this.k8sClient = k8sClient;
this.configurationService = configurationService;
this.objectMapper = objectMapper;
}

/**
Expand Down Expand Up @@ -131,7 +141,7 @@ public <R extends CustomResource> void register(
new EventDispatcher(
controller, finalizer, new EventDispatcher.CustomResourceFacade(client));

CustomResourceCache customResourceCache = new CustomResourceCache();
CustomResourceCache customResourceCache = new CustomResourceCache(objectMapper);
DefaultEventHandler defaultEventHandler =
new DefaultEventHandler(customResourceCache, dispatcher, controllerName, retry);
DefaultEventSourceManager eventSourceManager =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.client.CustomResource;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("rawtypes")
public class CustomResourceCache {

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

private ObjectMapper objectMapper = new ObjectMapper();
private final Map<String, CustomResource> resources = new ConcurrentHashMap<>();
private final ObjectMapper objectMapper;
private final ConcurrentMap<String, CustomResource> resources = new ConcurrentHashMap<>();
private final Lock lock = new ReentrantLock();

public CustomResourceCache() {
this(new ObjectMapper());
}

public CustomResourceCache(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public void cacheResource(CustomResource resource) {
try {
lock.lock();
Expand Down Expand Up @@ -49,18 +58,13 @@ public void cacheResource(CustomResource resource, Predicate<CustomResource> pre
* @return
*/
public Optional<CustomResource> getLatestResource(String uuid) {
return Optional.ofNullable(clone(resources.get(uuid)));
return Optional.ofNullable(resources.get(uuid)).map(this::clone);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking this is still unrelated. I'll undo it as well if you prefer.

}

private CustomResource clone(CustomResource customResource) {
try {
if (customResource == null) {
return null;
}
CustomResource clonedObject =
objectMapper.readValue(
objectMapper.writeValueAsString(customResource), customResource.getClass());
return clonedObject;
return objectMapper.readValue(
objectMapper.writeValueAsString(customResource), customResource.getClass());
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.javaoperatorsdk.operator.springboot.starter;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.CustomResource;
Expand Down Expand Up @@ -59,8 +60,13 @@ public boolean checkCRDAndValidateLocalModel() {
@Bean
@ConditionalOnMissingBean(Operator.class)
public Operator operator(
KubernetesClient kubernetesClient, List<ResourceController<?>> resourceControllers) {
Operator operator = new Operator(kubernetesClient, this);
KubernetesClient kubernetesClient,
List<ResourceController<?>> resourceControllers,
Optional<ObjectMapper> objectMapper) {
Operator operator =
objectMapper
.map(x -> new Operator(kubernetesClient, this, x))
.orElse(new Operator(kubernetesClient, this));
resourceControllers.forEach(r -> operator.register(processController(r)));
return operator;
}
Expand Down