-
Notifications
You must be signed in to change notification settings - Fork 218
feat: decouple from ObjectMapper #1953
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,23 +11,25 @@ | |
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.ConfigBuilder; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.client.utils.Serialization; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
import io.fabric8.kubernetes.client.utils.KubernetesSerialization; | ||
import io.javaoperatorsdk.operator.api.monitoring.Metrics; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory; | ||
import io.javaoperatorsdk.operator.processing.dependent.workflow.ManagedWorkflowFactory; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import static io.javaoperatorsdk.operator.api.config.ExecutorServiceManager.newThreadPoolExecutor; | ||
|
||
/** An interface from which to retrieve configuration information. */ | ||
public interface ConfigurationService { | ||
|
||
Logger log = LoggerFactory.getLogger(ConfigurationService.class); | ||
|
||
int DEFAULT_MAX_CONCURRENT_REQUEST = 512; | ||
|
||
/** | ||
* Retrieves the configuration associated with the specified reconciler | ||
* | ||
|
@@ -38,14 +40,30 @@ public interface ConfigurationService { | |
*/ | ||
<R extends HasMetadata> ControllerConfiguration<R> getConfigurationFor(Reconciler<R> reconciler); | ||
|
||
|
||
/** | ||
* Retrieves the Kubernetes client configuration | ||
* Used to clone custom resources. It is strongly suggested that implementors override this method | ||
* since the default implementation creates a new {@link Cloner} instance each time this method is | ||
* called. | ||
* | ||
* @return the configuration of the Kubernetes client, defaulting to the provided | ||
* auto-configuration | ||
* @return the configured {@link Cloner} | ||
*/ | ||
default Config getClientConfiguration() { | ||
return Config.autoConfigure(null); | ||
default Cloner getResourceCloner() { | ||
return new Cloner() { | ||
@Override | ||
public <R extends HasMetadata> R clone(R object) { | ||
return getKubernetesClient().getKubernetesSerialization().clone(object); | ||
} | ||
}; | ||
} | ||
|
||
default KubernetesClient getKubernetesClient() { | ||
return new KubernetesClientBuilder() | ||
.withConfig(new ConfigBuilder(Config.autoConfigure(null)) | ||
.withMaxConcurrentRequests(DEFAULT_MAX_CONCURRENT_REQUEST) | ||
.build()) | ||
.withKubernetesSerialization(new KubernetesSerialization()) | ||
.build(); | ||
} | ||
|
||
/** | ||
|
@@ -120,28 +138,6 @@ default int minConcurrentWorkflowExecutorThreads() { | |
return MIN_DEFAULT_WORKFLOW_EXECUTOR_THREAD_NUMBER; | ||
} | ||
|
||
/** | ||
* Used to clone custom resources. It is strongly suggested that implementors override this method | ||
* since the default implementation creates a new {@link Cloner} instance each time this method is | ||
* called. | ||
* | ||
* @return the configured {@link Cloner} | ||
*/ | ||
default Cloner getResourceCloner() { | ||
return new Cloner() { | ||
@SuppressWarnings("unchecked") | ||
@Override | ||
public HasMetadata clone(HasMetadata object) { | ||
try { | ||
final var mapper = getObjectMapper(); | ||
return mapper.readValue(mapper.writeValueAsString(object), object.getClass()); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
int DEFAULT_TERMINATION_TIMEOUT_SECONDS = 10; | ||
|
||
/** | ||
|
@@ -176,10 +172,6 @@ default boolean closeClientOnStop() { | |
return true; | ||
} | ||
|
||
default ObjectMapper getObjectMapper() { | ||
return Serialization.jsonMapper(); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
default DependentResourceFactory dependentResourceFactory() { | ||
return DependentResourceFactory.DEFAULT; | ||
|
@@ -261,6 +253,11 @@ static ConfigurationService newOverriddenConfigurationService( | |
return baseConfiguration; | ||
} | ||
|
||
static ConfigurationService newOverriddenConfigurationService( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels little smelly, since in an abstraction we creating an overrode of specific implementation, is this really needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, just noticing that the overriding method is almost always called passing it a new BaseConfigurationService instance. Also, I think it makes it easier for users because they might not know which instance to use to override the defaults. Open to cleaning this up further. |
||
Consumer<ConfigurationServiceOverrider> overrider) { | ||
return newOverriddenConfigurationService(new BaseConfigurationService(), overrider); | ||
} | ||
|
||
default ExecutorServiceManager getExecutorServiceManager() { | ||
return new ExecutorServiceManager(this); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have unit/integration tests for these setting parts. Also shouldn't we deprecate the client as param if it is also in ConfigurationService?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand your last point. Regarding unit tests, this is far from finished. I'm not going to move forward with this unless we agree on the approach to take first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, np, just a remark