diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/BaseConfigurationService.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/BaseConfigurationService.java index 2e8895ad87..e294e0babf 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/BaseConfigurationService.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/BaseConfigurationService.java @@ -105,9 +105,10 @@ protected

ControllerConfiguration

configFor(Reconcile + " annotation for reconciler: " + reconciler); } + Class> reconcilerClass = (Class>) reconciler.getClass(); + final var resourceClass = ConfigurationServiceProvider.instance().getResourceClassResolver() + .getResourceClass(reconcilerClass); - final var resourceClass = (Class

) Utils.getFirstTypeArgumentFromSuperClassOrInterface( - reconciler.getClass(), Reconciler.class); final var name = ReconcilerUtils.getNameFor(reconciler); final var generationAware = valueOrDefault( annotation, diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java index e9249505ab..d7dccd5bb4 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java @@ -206,4 +206,8 @@ default Optional getInformerStoppedHandler() { default ManagedWorkflowFactory getWorkflowFactory() { return ManagedWorkflowFactory.DEFAULT; } + + default ResourceClassResolver getResourceClassResolver() { + return new DefaultResourceClassResolver(); + } } diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java index 2b8aee9708..22a4b6e6bd 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java @@ -30,6 +30,7 @@ public class ConfigurationServiceOverrider { private InformerStoppedHandler informerStoppedHandler; private Boolean stopOnInformerErrorDuringStartup; private Duration cacheSyncTimeout; + private ResourceClassResolver resourceClassResolver; ConfigurationServiceOverrider(ConfigurationService original) { this.original = original; @@ -113,6 +114,12 @@ public ConfigurationServiceOverrider withCacheSyncTimeout(Duration cacheSyncTime return this; } + public ConfigurationServiceOverrider withResourceClassResolver( + ResourceClassResolver resourceClassResolver) { + this.resourceClassResolver = resourceClassResolver; + return this; + } + public ConfigurationService build() { return new BaseConfigurationService(original.getVersion(), cloner, objectMapper) { @Override @@ -196,6 +203,12 @@ public boolean stopOnInformerErrorDuringStartup() { public Duration cacheSyncTimeout() { return cacheSyncTimeout != null ? cacheSyncTimeout : super.cacheSyncTimeout(); } + + @Override + public ResourceClassResolver getResourceClassResolver() { + return resourceClassResolver != null ? resourceClassResolver + : super.getResourceClassResolver(); + } }; } diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfiguration.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfiguration.java index b2640777a7..1363ef0101 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfiguration.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfiguration.java @@ -114,6 +114,9 @@ default ConfigurationService getConfigurationService() { @SuppressWarnings("unchecked") @Override default Class

getResourceClass() { + // note that this implementation at the end not used within the boundaries of the core + // framework, should be removed in the future, (and marked as an API changed, or behavior + // change) return (Class

) Utils.getFirstTypeArgumentFromSuperClassOrInterface(getClass(), ControllerConfiguration.class); } diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverrider.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverrider.java index 4a3efbc4a6..74a1d78a07 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverrider.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverrider.java @@ -36,20 +36,22 @@ public class ControllerConfigurationOverrider { private GenericFilter genericFilter; private RateLimiter rateLimiter; private Map configurations; + private String name; private ControllerConfigurationOverrider(ControllerConfiguration original) { - finalizer = original.getFinalizerName(); - generationAware = original.isGenerationAware(); - namespaces = new HashSet<>(original.getNamespaces()); - retry = original.getRetry(); - labelSelector = original.getLabelSelector(); - customResourcePredicate = original.getEventFilter(); - reconciliationMaxInterval = original.maxReconciliationInterval().orElse(null); + this.finalizer = original.getFinalizerName(); + this.generationAware = original.isGenerationAware(); + this.namespaces = new HashSet<>(original.getNamespaces()); + this.retry = original.getRetry(); + this.labelSelector = original.getLabelSelector(); + this.customResourcePredicate = original.getEventFilter(); + this.reconciliationMaxInterval = original.maxReconciliationInterval().orElse(null); this.onAddFilter = original.onAddFilter().orElse(null); this.onUpdateFilter = original.onUpdateFilter().orElse(null); this.genericFilter = original.genericFilter().orElse(null); this.original = original; this.rateLimiter = original.getRateLimiter(); + this.name = original.getName(); } public ControllerConfigurationOverrider withFinalizer(String finalizer) { @@ -152,6 +154,11 @@ public ControllerConfigurationOverrider withGenericFilter(GenericFilter ge return this; } + public ControllerConfigurationOverrider withName(String name) { + this.name = name; + return this; + } + public ControllerConfigurationOverrider replacingNamedDependentResourceConfig(String name, Object dependentResourceConfig) { @@ -165,13 +172,12 @@ public ControllerConfigurationOverrider replacingNamedDependentResourceConfig configurations = new HashMap<>(specs.size()); } configurations.put(spec, dependentResourceConfig); - return this; } public ControllerConfiguration build() { - final var overridden = new ResolvedControllerConfiguration<>( - original.getResourceClass(), original.getName(), + final var overridden = new ResolvedControllerConfiguration<>(original.getResourceClass(), + name, generationAware, original.getAssociatedReconcilerClassName(), retry, rateLimiter, reconciliationMaxInterval, onAddFilter, onUpdateFilter, genericFilter, original.getDependentResources(), diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/DefaultResourceClassResolver.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/DefaultResourceClassResolver.java new file mode 100644 index 0000000000..c038e7d966 --- /dev/null +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/DefaultResourceClassResolver.java @@ -0,0 +1,15 @@ +package io.javaoperatorsdk.operator.api.config; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.javaoperatorsdk.operator.api.reconciler.Reconciler; + +public class DefaultResourceClassResolver implements ResourceClassResolver { + + @SuppressWarnings("unchecked") + @Override + public Class getResourceClass( + Class> reconcilerClass) { + return (Class) Utils.getFirstTypeArgumentFromSuperClassOrInterface(reconcilerClass, + Reconciler.class); + } +} diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ResourceClassResolver.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ResourceClassResolver.java new file mode 100644 index 0000000000..e15d53016a --- /dev/null +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ResourceClassResolver.java @@ -0,0 +1,10 @@ +package io.javaoperatorsdk.operator.api.config; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.javaoperatorsdk.operator.api.reconciler.Reconciler; + +public interface ResourceClassResolver { + + Class getResourceClass(Class> reconcilerClass); + +} diff --git a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationTest.java b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationTest.java deleted file mode 100644 index 1e78288232..0000000000 --- a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.javaoperatorsdk.operator.api.config; - -import org.junit.jupiter.api.Test; - -import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class ControllerConfigurationTest { - - @Test - void getCustomResourceClass() { - final ControllerConfiguration lambdasCannotBeUsedToExtractGenericParam = - () -> null; - assertThrows(RuntimeException.class, - lambdasCannotBeUsedToExtractGenericParam::getResourceClass); - - final ControllerConfiguration conf = new ControllerConfiguration<>() { - @Override - public String getAssociatedReconcilerClassName() { - return null; - } - }; - assertEquals(TestCustomResource.class, conf.getResourceClass()); - } -}