Skip to content

Fix generics #636

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 8 commits into from
Oct 28, 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
Expand Up @@ -5,6 +5,7 @@
import io.javaoperatorsdk.operator.api.Controller;
import io.javaoperatorsdk.operator.api.ResourceController;

@SuppressWarnings("rawtypes")
public class ControllerUtils {

private static final String FINALIZER_NAME_SUFFIX = "/finalizer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void close() {
* @param <R> the {@code CustomResource} type associated with the controller
* @throws OperatorException if a problem occurred during the registration process
*/
public <R extends CustomResource> void register(ResourceController<R> controller)
public <R extends CustomResource<?, ?>> void register(ResourceController<R> controller)
throws OperatorException {
register(controller, null);
}
Expand All @@ -132,7 +132,7 @@ public <R extends CustomResource> void register(ResourceController<R> controller
* @param <R> the {@code CustomResource} type associated with the controller
* @throws OperatorException if a problem occurred during the registration process
*/
public <R extends CustomResource> void register(
public <R extends CustomResource<?, ?>> void register(
ResourceController<R> controller, ControllerConfiguration<R> configuration)
throws OperatorException {
final var existing = configurationService.getConfigurationFor(controller);
Expand All @@ -148,7 +148,7 @@ public <R extends CustomResource> void register(
configuration = existing;
}
final var configuredController =
new ConfiguredController(controller, configuration, kubernetesClient);
new ConfiguredController<>(controller, configuration, kubernetesClient);
controllers.add(configuredController);

final var watchedNS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;

public abstract class BaseControl<T extends BaseControl> {
public abstract class BaseControl<T extends BaseControl<T>> {

private Long scheduleDelay = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.util.Optional;

import io.fabric8.kubernetes.client.CustomResource;

public interface Context<T extends CustomResource> {
public interface Context {

Optional<RetryInfo> getRetryInfo();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.util.Optional;

import io.fabric8.kubernetes.client.CustomResource;

public class DefaultContext<T extends CustomResource> implements Context<T> {
public class DefaultContext implements Context {

private final RetryInfo retryInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public boolean isRemoveFinalizer() {

@Override
public DeleteControl rescheduleAfter(long delay) {
if (removeFinalizer == true) {
if (removeFinalizer) {
throw new IllegalStateException("Cannot reschedule deleteResource if removing finalizer");
}
return super.rescheduleAfter(delay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.fabric8.kubernetes.client.CustomResource;

public interface ResourceController<R extends CustomResource> {
public interface ResourceController<R extends CustomResource<?, ?>> {

/**
* Note that this method is used in combination of finalizers. If automatic finalizer handling is
Expand All @@ -28,7 +28,7 @@ public interface ResourceController<R extends CustomResource> {
* finalizer to indicate that the resource should not be deleted after all, in which case
* the controller should restore the resource's state appropriately.
*/
default DeleteControl deleteResource(R resource, Context<R> context) {
default DeleteControl deleteResource(R resource, Context context) {
return DeleteControl.defaultDelete();
}

Expand All @@ -46,6 +46,6 @@ default DeleteControl deleteResource(R resource, Context<R> context) {
* be skipped. <b>However we will always call an update if there is no finalizer on object
* and it's not marked for deletion.</b>
*/
UpdateControl<R> createOrUpdateResource(R resource, Context<R> context);
UpdateControl<R> createOrUpdateResource(R resource, Context context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.fabric8.kubernetes.client.CustomResource;

@SuppressWarnings("rawtypes")
public class UpdateControl<T extends CustomResource> extends BaseControl<UpdateControl<T>> {

private final T customResource;
Expand Down Expand Up @@ -30,16 +31,16 @@ public static <T extends CustomResource> UpdateControl<T> updateStatusSubResourc
/**
* As a results of this there will be two call to K8S API. First the custom resource will be
* updates then the status sub-resource.
*
*
* @param customResource - custom resource to use in both API calls
* @return UpdateControl instance
*/
public static <T extends CustomResource> UpdateControl<T> updateCustomResourceAndStatus(
public static <T extends CustomResource<?, ?>> UpdateControl<T> updateCustomResourceAndStatus(
T customResource) {
return new UpdateControl<>(customResource, true, true);
}

public static <T extends CustomResource> UpdateControl<T> noUpdate() {
public static <T extends CustomResource<?, ?>> UpdateControl<T> noUpdate() {
return new UpdateControl<>(null, false, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.javaoperatorsdk.operator.ControllerUtils;
import io.javaoperatorsdk.operator.api.ResourceController;

@SuppressWarnings("rawtypes")
public class AbstractConfigurationService implements ConfigurationService {
private final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
private final Version version;
Expand All @@ -17,15 +18,15 @@ public AbstractConfigurationService(Version version) {
this.version = version;
}

protected <R extends CustomResource> void register(ControllerConfiguration<R> config) {
protected <R extends CustomResource<?, ?>> void register(ControllerConfiguration<R> config) {
put(config, true);
}

protected <R extends CustomResource> void replace(ControllerConfiguration<R> config) {
protected <R extends CustomResource<?, ?>> void replace(ControllerConfiguration<R> config) {
put(config, false);
}

private <R extends CustomResource> void put(
private <R extends CustomResource<?, ?>> void put(
ControllerConfiguration<R> config, boolean failIfExisting) {
final var name = config.getName();
if (failIfExisting) {
Expand All @@ -38,8 +39,8 @@ private <R extends CustomResource> void put(
config.setConfigurationService(this);
}

protected void throwExceptionOnNameCollision(
String newControllerClassName, ControllerConfiguration existing) {
protected <R extends CustomResource<?, ?>> void throwExceptionOnNameCollision(
String newControllerClassName, ControllerConfiguration<R> existing) {
throw new IllegalArgumentException(
"Controller name '"
+ existing.getName()
Expand All @@ -50,7 +51,7 @@ protected void throwExceptionOnNameCollision(
}

@Override
public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
public <R extends CustomResource<?, ?>> ControllerConfiguration<R> getConfigurationFor(
ResourceController<R> controller) {
final var key = keyFor(controller);
final var configuration = configurations.get(key);
Expand All @@ -72,7 +73,7 @@ private String getControllersNameMessage() {
+ ".";
}

protected String keyFor(ResourceController controller) {
protected <R extends CustomResource<?, ?>> String keyFor(ResourceController<R> controller) {
return ControllerUtils.getNameFor(controller);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public interface ConfigurationService {
* @param controller the controller we want the configuration of
* @param <R> the {@code CustomResource} type associated with the specified controller
* @return the {@link ControllerConfiguration} associated with the specified controller or {@code
* null} if no configuration exists for the controller
* null} if no configuration exists for the controller
*/
<R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
<R extends CustomResource<?, ?>> ControllerConfiguration<R> getConfigurationFor(
ResourceController<R> controller);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ConfigurationServiceOverrider withMetrics(Metrics metrics) {
public ConfigurationService build() {
return new ConfigurationService() {
@Override
public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
public <R extends CustomResource<?, ?>> ControllerConfiguration<R> getConfigurationFor(
ResourceController<R> controller) {
return original.getConfigurationFor(controller);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEventFilter;
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEventFilters;

public interface ControllerConfiguration<R extends CustomResource> {
public interface ControllerConfiguration<R extends CustomResource<?, ?>> {

default String getName() {
return ControllerUtils.getDefaultResourceControllerName(getAssociatedControllerClassName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ControllerConfigurationOverrider<R> addingNamespaces(String... namespaces
}

public ControllerConfigurationOverrider<R> removingNamespaces(String... namespaces) {
this.namespaces.removeAll(List.of(namespaces));
List.of(namespaces).forEach(this.namespaces::remove);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ default void reconcileCustomResource(CustomResourceID customResourceID,
default void failedReconciliation(CustomResourceID customResourceID,
RuntimeException exception) {}

default void cleanupDoneFor(CustomResourceID customResourceUid) {};
default void cleanupDoneFor(CustomResourceID customResourceUid) {}

default void finishedReconciliation(CustomResourceID resourceID) {};
default void finishedReconciliation(CustomResourceID resourceID) {}


interface ControllerExecution<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;

public class ConfiguredController<R extends CustomResource<?, ?>> implements ResourceController<R>,
LifecycleAware, EventSourceInitializer {
LifecycleAware, EventSourceInitializer<R> {
private final ResourceController<R> controller;
private final ControllerConfiguration<R> configuration;
private final KubernetesClient kubernetesClient;
private DefaultEventSourceManager eventSourceManager;
private DefaultEventSourceManager<R> eventSourceManager;

public ConfiguredController(ResourceController<R> controller,
ControllerConfiguration<R> configuration,
Expand All @@ -38,7 +38,7 @@ public ConfiguredController(ResourceController<R> controller,
}

@Override
public DeleteControl deleteResource(R resource, Context<R> context) {
public DeleteControl deleteResource(R resource, Context context) {
return configuration.getConfigurationService().getMetrics().timeControllerExecution(
new ControllerExecution<>() {
@Override
Expand All @@ -64,7 +64,7 @@ public DeleteControl execute() {
}

@Override
public UpdateControl<R> createOrUpdateResource(R resource, Context<R> context) {
public UpdateControl<R> createOrUpdateResource(R resource, Context context) {
return configuration.getConfigurationService().getMetrics().timeControllerExecution(
new ControllerExecution<>() {
@Override
Expand Down Expand Up @@ -97,7 +97,7 @@ public UpdateControl<R> execute() {
}

@Override
public void prepareEventSources(EventSourceManager eventSourceManager) {
public void prepareEventSources(EventSourceManager<R> eventSourceManager) {
throw new UnsupportedOperationException("This method should never be called directly");
}

Expand Down Expand Up @@ -170,7 +170,7 @@ public void start() throws OperatorException {
try {
eventSourceManager = new DefaultEventSourceManager<>(this);
if (controller instanceof EventSourceInitializer) {
((EventSourceInitializer) controller).prepareEventSources(eventSourceManager);
((EventSourceInitializer<R>) controller).prepareEventSources(eventSourceManager);
}
} catch (MissingCRDException e) {
throwMissingCRDException(crdName, specVersion, controllerName);
Expand Down Expand Up @@ -213,7 +213,7 @@ private boolean failOnMissingCurrentNS() {
return false;
}

public EventSourceManager getEventSourceManager() {
public EventSourceManager<R> getEventSourceManager() {
return eventSourceManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.javaoperatorsdk.operator.api.*;
import io.javaoperatorsdk.operator.api.BaseControl;
import io.javaoperatorsdk.operator.api.Context;
import io.javaoperatorsdk.operator.api.DefaultContext;
import io.javaoperatorsdk.operator.api.DeleteControl;
import io.javaoperatorsdk.operator.api.ObservedGenerationAware;
import io.javaoperatorsdk.operator.api.ResourceController;
import io.javaoperatorsdk.operator.api.UpdateControl;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;

import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getName;
Expand Down Expand Up @@ -64,8 +70,8 @@ private PostExecutionControl<R> handleDispatch(ExecutionScope<R> executionScope)
return PostExecutionControl.defaultDispatch();
}

Context<R> context =
new DefaultContext<>(executionScope.getRetryInfo());
Context context =
new DefaultContext(executionScope.getRetryInfo());
if (markedForDeletion) {
return handleDelete(resource, context);
} else {
Expand All @@ -92,7 +98,7 @@ private boolean shouldNotDispatchToDelete(R resource) {
}

private PostExecutionControl<R> handleCreateOrUpdate(
ExecutionScope<R> executionScope, R resource, Context<R> context) {
ExecutionScope<R> executionScope, R resource, Context context) {
if (configuration().useFinalizer() && !resource.hasFinalizer(configuration().getFinalizer())) {
/*
* We always add the finalizer if missing and the controller is configured to use a finalizer.
Expand Down Expand Up @@ -161,7 +167,7 @@ private void updatePostExecutionControlWithReschedule(
baseControl.getScheduleDelay().ifPresent(postExecutionControl::withReSchedule);
}

private PostExecutionControl<R> handleDelete(R resource, Context<R> context) {
private PostExecutionControl<R> handleDelete(R resource, Context context) {
log.debug(
"Executing delete for resource: {} with version: {}",
getName(resource),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @param <T> the type of custom resources handled by this filter
*/
@FunctionalInterface
public interface CustomResourceEventFilter<T extends CustomResource> {
public interface CustomResourceEventFilter<T extends CustomResource<?, ?>> {

/**
* Determines whether the change between the old version of the resource and the new one needs to
Expand Down
Loading