Skip to content

Commit 7fec740

Browse files
authored
refactor: fix generics and minor clean-ups (#636)
1 parent cbfd993 commit 7fec740

File tree

44 files changed

+174
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+174
-176
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.javaoperatorsdk.operator.api.Controller;
66
import io.javaoperatorsdk.operator.api.ResourceController;
77

8+
@SuppressWarnings("rawtypes")
89
public class ControllerUtils {
910

1011
private static final String FINALIZER_NAME_SUFFIX = "/finalizer";

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void close() {
114114
* @param <R> the {@code CustomResource} type associated with the controller
115115
* @throws OperatorException if a problem occurred during the registration process
116116
*/
117-
public <R extends CustomResource> void register(ResourceController<R> controller)
117+
public <R extends CustomResource<?, ?>> void register(ResourceController<R> controller)
118118
throws OperatorException {
119119
register(controller, null);
120120
}
@@ -132,7 +132,7 @@ public <R extends CustomResource> void register(ResourceController<R> controller
132132
* @param <R> the {@code CustomResource} type associated with the controller
133133
* @throws OperatorException if a problem occurred during the registration process
134134
*/
135-
public <R extends CustomResource> void register(
135+
public <R extends CustomResource<?, ?>> void register(
136136
ResourceController<R> controller, ControllerConfiguration<R> configuration)
137137
throws OperatorException {
138138
final var existing = configurationService.getConfigurationFor(controller);
@@ -148,7 +148,7 @@ public <R extends CustomResource> void register(
148148
configuration = existing;
149149
}
150150
final var configuredController =
151-
new ConfiguredController(controller, configuration, kubernetesClient);
151+
new ConfiguredController<>(controller, configuration, kubernetesClient);
152152
controllers.add(configuredController);
153153

154154
final var watchedNS =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Optional;
44
import java.util.concurrent.TimeUnit;
55

6-
public abstract class BaseControl<T extends BaseControl> {
6+
public abstract class BaseControl<T extends BaseControl<T>> {
77

88
private Long scheduleDelay = null;
99

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import java.util.Optional;
44

5-
import io.fabric8.kubernetes.client.CustomResource;
6-
7-
public interface Context<T extends CustomResource> {
5+
public interface Context {
86

97
Optional<RetryInfo> getRetryInfo();
108

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import java.util.Optional;
44

5-
import io.fabric8.kubernetes.client.CustomResource;
6-
7-
public class DefaultContext<T extends CustomResource> implements Context<T> {
5+
public class DefaultContext implements Context {
86

97
private final RetryInfo retryInfo;
108

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public boolean isRemoveFinalizer() {
2222

2323
@Override
2424
public DeleteControl rescheduleAfter(long delay) {
25-
if (removeFinalizer == true) {
25+
if (removeFinalizer) {
2626
throw new IllegalStateException("Cannot reschedule deleteResource if removing finalizer");
2727
}
2828
return super.rescheduleAfter(delay);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import io.fabric8.kubernetes.client.CustomResource;
44

5-
public interface ResourceController<R extends CustomResource> {
5+
public interface ResourceController<R extends CustomResource<?, ?>> {
66

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

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

5151
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.fabric8.kubernetes.client.CustomResource;
44

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

78
private final T customResource;
@@ -30,16 +31,16 @@ public static <T extends CustomResource> UpdateControl<T> updateStatusSubResourc
3031
/**
3132
* As a results of this there will be two call to K8S API. First the custom resource will be
3233
* updates then the status sub-resource.
33-
*
34+
*
3435
* @param customResource - custom resource to use in both API calls
3536
* @return UpdateControl instance
3637
*/
37-
public static <T extends CustomResource> UpdateControl<T> updateCustomResourceAndStatus(
38+
public static <T extends CustomResource<?, ?>> UpdateControl<T> updateCustomResourceAndStatus(
3839
T customResource) {
3940
return new UpdateControl<>(customResource, true, true);
4041
}
4142

42-
public static <T extends CustomResource> UpdateControl<T> noUpdate() {
43+
public static <T extends CustomResource<?, ?>> UpdateControl<T> noUpdate() {
4344
return new UpdateControl<>(null, false, false);
4445
}
4546

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.javaoperatorsdk.operator.ControllerUtils;
1010
import io.javaoperatorsdk.operator.api.ResourceController;
1111

12+
@SuppressWarnings("rawtypes")
1213
public class AbstractConfigurationService implements ConfigurationService {
1314
private final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
1415
private final Version version;
@@ -17,15 +18,15 @@ public AbstractConfigurationService(Version version) {
1718
this.version = version;
1819
}
1920

20-
protected <R extends CustomResource> void register(ControllerConfiguration<R> config) {
21+
protected <R extends CustomResource<?, ?>> void register(ControllerConfiguration<R> config) {
2122
put(config, true);
2223
}
2324

24-
protected <R extends CustomResource> void replace(ControllerConfiguration<R> config) {
25+
protected <R extends CustomResource<?, ?>> void replace(ControllerConfiguration<R> config) {
2526
put(config, false);
2627
}
2728

28-
private <R extends CustomResource> void put(
29+
private <R extends CustomResource<?, ?>> void put(
2930
ControllerConfiguration<R> config, boolean failIfExisting) {
3031
final var name = config.getName();
3132
if (failIfExisting) {
@@ -38,8 +39,8 @@ private <R extends CustomResource> void put(
3839
config.setConfigurationService(this);
3940
}
4041

41-
protected void throwExceptionOnNameCollision(
42-
String newControllerClassName, ControllerConfiguration existing) {
42+
protected <R extends CustomResource<?, ?>> void throwExceptionOnNameCollision(
43+
String newControllerClassName, ControllerConfiguration<R> existing) {
4344
throw new IllegalArgumentException(
4445
"Controller name '"
4546
+ existing.getName()
@@ -50,7 +51,7 @@ protected void throwExceptionOnNameCollision(
5051
}
5152

5253
@Override
53-
public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
54+
public <R extends CustomResource<?, ?>> ControllerConfiguration<R> getConfigurationFor(
5455
ResourceController<R> controller) {
5556
final var key = keyFor(controller);
5657
final var configuration = configurations.get(key);
@@ -72,7 +73,7 @@ private String getControllersNameMessage() {
7273
+ ".";
7374
}
7475

75-
protected String keyFor(ResourceController controller) {
76+
protected <R extends CustomResource<?, ?>> String keyFor(ResourceController<R> controller) {
7677
return ControllerUtils.getNameFor(controller);
7778
}
7879

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public interface ConfigurationService {
3434
* @param controller the controller we want the configuration of
3535
* @param <R> the {@code CustomResource} type associated with the specified controller
3636
* @return the {@link ControllerConfiguration} associated with the specified controller or {@code
37-
* null} if no configuration exists for the controller
37+
* null} if no configuration exists for the controller
3838
*/
39-
<R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
39+
<R extends CustomResource<?, ?>> ControllerConfiguration<R> getConfigurationFor(
4040
ResourceController<R> controller);
4141

4242
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public ConfigurationServiceOverrider withMetrics(Metrics metrics) {
6161
public ConfigurationService build() {
6262
return new ConfigurationService() {
6363
@Override
64-
public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor(
64+
public <R extends CustomResource<?, ?>> ControllerConfiguration<R> getConfigurationFor(
6565
ResourceController<R> controller) {
6666
return original.getConfigurationFor(controller);
6767
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEventFilter;
1111
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEventFilters;
1212

13-
public interface ControllerConfiguration<R extends CustomResource> {
13+
public interface ControllerConfiguration<R extends CustomResource<?, ?>> {
1414

1515
default String getName() {
1616
return ControllerUtils.getDefaultResourceControllerName(getAssociatedControllerClassName());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ControllerConfigurationOverrider<R> addingNamespaces(String... namespaces
4848
}
4949

5050
public ControllerConfigurationOverrider<R> removingNamespaces(String... namespaces) {
51-
this.namespaces.removeAll(List.of(namespaces));
51+
List.of(namespaces).forEach(this.namespaces::remove);
5252
return this;
5353
}
5454

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/monitoring/Metrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ default void reconcileCustomResource(CustomResourceID customResourceID,
1717
default void failedReconciliation(CustomResourceID customResourceID,
1818
RuntimeException exception) {}
1919

20-
default void cleanupDoneFor(CustomResourceID customResourceUid) {};
20+
default void cleanupDoneFor(CustomResourceID customResourceUid) {}
2121

22-
default void finishedReconciliation(CustomResourceID resourceID) {};
22+
default void finishedReconciliation(CustomResourceID resourceID) {}
2323

2424

2525
interface ControllerExecution<T> {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/ConfiguredController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
2424

2525
public class ConfiguredController<R extends CustomResource<?, ?>> implements ResourceController<R>,
26-
LifecycleAware, EventSourceInitializer {
26+
LifecycleAware, EventSourceInitializer<R> {
2727
private final ResourceController<R> controller;
2828
private final ControllerConfiguration<R> configuration;
2929
private final KubernetesClient kubernetesClient;
30-
private DefaultEventSourceManager eventSourceManager;
30+
private DefaultEventSourceManager<R> eventSourceManager;
3131

3232
public ConfiguredController(ResourceController<R> controller,
3333
ControllerConfiguration<R> configuration,
@@ -38,7 +38,7 @@ public ConfiguredController(ResourceController<R> controller,
3838
}
3939

4040
@Override
41-
public DeleteControl deleteResource(R resource, Context<R> context) {
41+
public DeleteControl deleteResource(R resource, Context context) {
4242
return configuration.getConfigurationService().getMetrics().timeControllerExecution(
4343
new ControllerExecution<>() {
4444
@Override
@@ -64,7 +64,7 @@ public DeleteControl execute() {
6464
}
6565

6666
@Override
67-
public UpdateControl<R> createOrUpdateResource(R resource, Context<R> context) {
67+
public UpdateControl<R> createOrUpdateResource(R resource, Context context) {
6868
return configuration.getConfigurationService().getMetrics().timeControllerExecution(
6969
new ControllerExecution<>() {
7070
@Override
@@ -97,7 +97,7 @@ public UpdateControl<R> execute() {
9797
}
9898

9999
@Override
100-
public void prepareEventSources(EventSourceManager eventSourceManager) {
100+
public void prepareEventSources(EventSourceManager<R> eventSourceManager) {
101101
throw new UnsupportedOperationException("This method should never be called directly");
102102
}
103103

@@ -170,7 +170,7 @@ public void start() throws OperatorException {
170170
try {
171171
eventSourceManager = new DefaultEventSourceManager<>(this);
172172
if (controller instanceof EventSourceInitializer) {
173-
((EventSourceInitializer) controller).prepareEventSources(eventSourceManager);
173+
((EventSourceInitializer<R>) controller).prepareEventSources(eventSourceManager);
174174
}
175175
} catch (MissingCRDException e) {
176176
throwMissingCRDException(crdName, specVersion, controllerName);
@@ -213,7 +213,7 @@ private boolean failOnMissingCurrentNS() {
213213
return false;
214214
}
215215

216-
public EventSourceManager getEventSourceManager() {
216+
public EventSourceManager<R> getEventSourceManager() {
217217
return eventSourceManager;
218218
}
219219

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/EventDispatcher.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
import io.fabric8.kubernetes.client.KubernetesClientException;
99
import io.fabric8.kubernetes.client.dsl.MixedOperation;
1010
import io.fabric8.kubernetes.client.dsl.Resource;
11-
import io.javaoperatorsdk.operator.api.*;
11+
import io.javaoperatorsdk.operator.api.BaseControl;
12+
import io.javaoperatorsdk.operator.api.Context;
13+
import io.javaoperatorsdk.operator.api.DefaultContext;
14+
import io.javaoperatorsdk.operator.api.DeleteControl;
15+
import io.javaoperatorsdk.operator.api.ObservedGenerationAware;
16+
import io.javaoperatorsdk.operator.api.ResourceController;
17+
import io.javaoperatorsdk.operator.api.UpdateControl;
1218
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1319

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

67-
Context<R> context =
68-
new DefaultContext<>(executionScope.getRetryInfo());
73+
Context context =
74+
new DefaultContext(executionScope.getRetryInfo());
6975
if (markedForDeletion) {
7076
return handleDelete(resource, context);
7177
} else {
@@ -92,7 +98,7 @@ private boolean shouldNotDispatchToDelete(R resource) {
9298
}
9399

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

164-
private PostExecutionControl<R> handleDelete(R resource, Context<R> context) {
170+
private PostExecutionControl<R> handleDelete(R resource, Context context) {
165171
log.debug(
166172
"Executing delete for resource: {} with version: {}",
167173
getName(resource),

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/internal/CustomResourceEventFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @param <T> the type of custom resources handled by this filter
1212
*/
1313
@FunctionalInterface
14-
public interface CustomResourceEventFilter<T extends CustomResource> {
14+
public interface CustomResourceEventFilter<T extends CustomResource<?, ?>> {
1515

1616
/**
1717
* Determines whether the change between the old version of the resource and the new one needs to

0 commit comments

Comments
 (0)