Skip to content

Commit 9289426

Browse files
csvirimetacosm
andcommitted
feat: use fabric8 client json mapper by default (#1382)
Also remove constants to avoid mismatched configurations Co-authored-by: Chris Laprun <[email protected]>
1 parent 0c5233f commit 9289426

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AbstractConfigurationService.java

+14
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@
1313
public class AbstractConfigurationService implements ConfigurationService {
1414
private final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
1515
private final Version version;
16+
private final Cloner cloner;
1617

1718
public AbstractConfigurationService(Version version) {
1819
this.version = version;
20+
this.cloner = ConfigurationService.super.getResourceCloner();
21+
}
22+
23+
public AbstractConfigurationService(Version version, Cloner cloner) {
24+
this.version = version;
25+
this.cloner = cloner;
1926
}
2027

2128
protected <R extends HasMetadata> void register(ControllerConfiguration<R> config) {
@@ -77,10 +84,12 @@ protected <R extends HasMetadata> String keyFor(Reconciler<R> reconciler) {
7784
return ReconcilerUtils.getNameFor(reconciler);
7885
}
7986

87+
@SuppressWarnings("unused")
8088
protected ControllerConfiguration getFor(String reconcilerName) {
8189
return configurations.get(reconcilerName);
8290
}
8391

92+
@SuppressWarnings("unused")
8493
protected Stream<ControllerConfiguration> controllerConfigurations() {
8594
return configurations.values().stream();
8695
}
@@ -94,4 +103,9 @@ public Set<String> getKnownReconcilerNames() {
94103
public Version getVersion() {
95104
return version;
96105
}
106+
107+
@Override
108+
public Cloner getResourceCloner() {
109+
return cloner;
110+
}
97111
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/BaseConfigurationService.java

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public BaseConfigurationService(Version version) {
1515
super(version);
1616
}
1717

18+
public BaseConfigurationService(Version version, Cloner cloner) {
19+
super(version, cloner);
20+
}
21+
1822
public BaseConfigurationService() {
1923
this(Utils.loadFromProperties());
2024
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.fabric8.kubernetes.api.model.HasMetadata;
88
import io.fabric8.kubernetes.client.Config;
99
import io.fabric8.kubernetes.client.CustomResource;
10+
import io.fabric8.kubernetes.client.utils.Serialization;
1011
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
1112
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
1213
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory;
@@ -17,20 +18,6 @@
1718
/** An interface from which to retrieve configuration information. */
1819
public interface ConfigurationService {
1920

20-
ObjectMapper OBJECT_MAPPER = new ObjectMapper();
21-
22-
Cloner DEFAULT_CLONER = new Cloner() {
23-
@SuppressWarnings("unchecked")
24-
@Override
25-
public HasMetadata clone(HasMetadata object) {
26-
try {
27-
return OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(object), object.getClass());
28-
} catch (JsonProcessingException e) {
29-
throw new IllegalStateException(e);
30-
}
31-
}
32-
};
33-
3421
/**
3522
* Retrieves the configuration associated with the specified reconciler
3623
*
@@ -93,12 +80,25 @@ default int concurrentReconciliationThreads() {
9380
}
9481

9582
/**
96-
* Used to clone custom resources.
83+
* Used to clone custom resources. It is strongly suggested that implementors override this method
84+
* since the default implementation creates a new {@link Cloner} instance each time this method is
85+
* called.
9786
*
98-
* @return the ObjectMapper to use
87+
* @return the configured {@link Cloner}
9988
*/
10089
default Cloner getResourceCloner() {
101-
return DEFAULT_CLONER;
90+
return new Cloner() {
91+
@SuppressWarnings("unchecked")
92+
@Override
93+
public HasMetadata clone(HasMetadata object) {
94+
try {
95+
final var mapper = getObjectMapper();
96+
return mapper.readValue(mapper.writeValueAsString(object), object.getClass());
97+
} catch (JsonProcessingException e) {
98+
throw new IllegalStateException(e);
99+
}
100+
}
101+
};
102102
}
103103

104104
int DEFAULT_TERMINATION_TIMEOUT_SECONDS = 10;
@@ -126,7 +126,7 @@ default boolean closeClientOnStop() {
126126
}
127127

128128
default ObjectMapper getObjectMapper() {
129-
return OBJECT_MAPPER;
129+
return Serialization.jsonMapper();
130130
}
131131

132132
default DependentResourceFactory dependentResourceFactory() {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public ConfigurationServiceOverrider withObjectMapper(ObjectMapper objectMapper)
8181
}
8282

8383
public ConfigurationService build() {
84-
return new BaseConfigurationService(original.getVersion()) {
84+
return new BaseConfigurationService(original.getVersion(), cloner) {
8585
@Override
8686
public Set<String> getKnownReconcilerNames() {
8787
return original.getKnownReconcilerNames();
@@ -102,11 +102,6 @@ public int concurrentReconciliationThreads() {
102102
return threadNumber;
103103
}
104104

105-
@Override
106-
public Cloner getResourceCloner() {
107-
return cloner;
108-
}
109-
110105
@Override
111106
public int getTerminationTimeoutSeconds() {
112107
return timeoutSeconds;

operator-framework/src/test/java/io/javaoperatorsdk/operator/StandaloneDependentResourceIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import io.fabric8.kubernetes.api.model.ObjectMeta;
99
import io.fabric8.kubernetes.api.model.apps.Deployment;
10-
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
10+
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
1111
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
1212
import io.javaoperatorsdk.operator.sample.standalonedependent.StandaloneDependentTestCustomResource;
1313
import io.javaoperatorsdk.operator.sample.standalonedependent.StandaloneDependentTestCustomResourceSpec;
@@ -52,7 +52,7 @@ void executeUpdateForTestingCacheUpdateForGetResource() {
5252

5353
awaitForDeploymentReadyReplicas(1);
5454

55-
var clonedCr = ConfigurationService.DEFAULT_CLONER.clone(createdCR);
55+
var clonedCr = ConfigurationServiceProvider.instance().getResourceCloner().clone(createdCR);
5656
clonedCr.getSpec().setReplicaCount(2);
5757
operator.replace(clonedCr);
5858

0 commit comments

Comments
 (0)