Skip to content

feat: configure resource class and name for controller #1781

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 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
Expand Up @@ -105,9 +105,10 @@ protected <P extends HasMetadata> ControllerConfiguration<P> configFor(Reconcile
+
" annotation for reconciler: " + reconciler);
}
Class<Reconciler<P>> reconcilerClass = (Class<Reconciler<P>>) reconciler.getClass();
final var resourceClass = ConfigurationServiceProvider.instance().getResourceClassResolver()
.getResourceClass(reconcilerClass);

final var resourceClass = (Class<P>) Utils.getFirstTypeArgumentFromSuperClassOrInterface(
reconciler.getClass(), Reconciler.class);
final var name = ReconcilerUtils.getNameFor(reconciler);
final var generationAware = valueOrDefault(
annotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,8 @@ default Optional<InformerStoppedHandler> getInformerStoppedHandler() {
default ManagedWorkflowFactory getWorkflowFactory() {
return ManagedWorkflowFactory.DEFAULT;
}

default ResourceClassResolver getResourceClassResolver() {
return new DefaultResourceClassResolver();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,5 @@ default ConfigurationService getConfigurationService() {

@SuppressWarnings("unchecked")
@Override
default Class<P> getResourceClass() {
return (Class<P>) Utils.getFirstTypeArgumentFromSuperClassOrInterface(getClass(),
ControllerConfiguration.class);
}
Class<P> getResourceClass();
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should keep a default implementation to avoid breaking the API.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, yes, althoug not really the API, it's the implementation. But we should remove it in the future, to make it more explicit that this is not used

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is ok to do ti for next? (I mean a separate PR, wdyt?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, this method should stay as it's needed by some implementations, Quarkus in particular. And it is breaking the API because implementations that were relying on the provided default implementation won't compile anymore with this change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reverted and commented it for now, but I guess we can adjust also quarkus in the future if it will be removed.

For now fine to have it there IMO.

This PR should be ready now, also the adjusted spring boot part.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think that this will change in Quarkus because the resource class is resolved at build time and is therefore fixed then, not resolved at runtime, which is why we need this method to stay as-is.

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ public class ControllerConfigurationOverrider<R extends HasMetadata> {
private GenericFilter<R> genericFilter;
private RateLimiter rateLimiter;
private Map<DependentResourceSpec, Object> configurations;
private String name;

private ControllerConfigurationOverrider(ControllerConfiguration<R> 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<R> withFinalizer(String finalizer) {
Expand Down Expand Up @@ -152,6 +154,11 @@ public ControllerConfigurationOverrider<R> withGenericFilter(GenericFilter<R> ge
return this;
}

public ControllerConfigurationOverrider<R> withName(String name) {
this.name = name;
return this;
}

public ControllerConfigurationOverrider<R> replacingNamedDependentResourceConfig(String name,
Object dependentResourceConfig) {

Expand All @@ -165,13 +172,12 @@ public ControllerConfigurationOverrider<R> replacingNamedDependentResourceConfig
configurations = new HashMap<>(specs.size());
}
configurations.put(spec, dependentResourceConfig);

return this;
}

public ControllerConfiguration<R> 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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <R extends HasMetadata> Class<R> getResourceClass(
Class<? extends Reconciler<R>> reconcilerClass) {
return (Class<R>) Utils.getFirstTypeArgumentFromSuperClassOrInterface(reconcilerClass,
Reconciler.class);
}
}
Original file line number Diff line number Diff line change
@@ -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 {

<R extends HasMetadata> Class<R> getResourceClass(Class<? extends Reconciler<R>> reconcilerClass);

}

This file was deleted.