Skip to content

Commit 4a6080b

Browse files
committed
refactor: more generification
1 parent 67ea87c commit 4a6080b

File tree

7 files changed

+43
-46
lines changed

7 files changed

+43
-46
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package io.javaoperatorsdk.operator.api.config;
22

3-
import io.fabric8.kubernetes.client.CustomResource;
43
import java.util.Collections;
54
import java.util.Set;
65

7-
public abstract class AbstractControllerConfiguration<R extends CustomResource>
6+
import io.fabric8.kubernetes.client.CustomResource;
7+
8+
public abstract class AbstractControllerConfiguration<R extends CustomResource<?, ?>>
89
implements ControllerConfiguration<R> {
910

1011
private final String associatedControllerClassName;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.javaoperatorsdk.operator.api.config;
22

3-
import io.fabric8.kubernetes.client.CustomResource;
43
import java.util.HashSet;
54
import java.util.List;
65
import java.util.Set;
76

8-
public class ControllerConfigurationOverrider<R extends CustomResource> {
7+
import io.fabric8.kubernetes.client.CustomResource;
8+
9+
public class ControllerConfigurationOverrider<R extends CustomResource<?, ?>> {
910

1011
private String finalizer;
1112
private boolean generationAware;
12-
private Set<String> namespaces;
13+
private final Set<String> namespaces;
1314
private RetryConfiguration retry;
1415
private String labelSelector;
1516
private final ControllerConfiguration<R> original;
@@ -65,7 +66,7 @@ public ControllerConfigurationOverrider<R> withLabelSelector(String labelSelecto
6566
}
6667

6768
public ControllerConfiguration<R> build() {
68-
return new AbstractControllerConfiguration<R>(
69+
return new AbstractControllerConfiguration<>(
6970
original.getAssociatedControllerClassName(),
7071
original.getName(),
7172
original.getCRDName(),
@@ -86,7 +87,7 @@ public ConfigurationService getConfigurationService() {
8687
};
8788
}
8889

89-
public static <R extends CustomResource> ControllerConfigurationOverrider<R> override(
90+
public static <R extends CustomResource<?, ?>> ControllerConfigurationOverrider<R> override(
9091
ControllerConfiguration<R> original) {
9192
return new ControllerConfigurationOverrider<>(original);
9293
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getName;
55
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getVersion;
66

7-
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
87
import java.util.HashMap;
98
import java.util.HashSet;
109
import java.util.Map;
@@ -21,6 +20,7 @@
2120
import io.fabric8.kubernetes.client.CustomResource;
2221
import io.javaoperatorsdk.operator.api.RetryInfo;
2322
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
23+
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
2424
import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager;
2525
import io.javaoperatorsdk.operator.processing.event.Event;
2626
import io.javaoperatorsdk.operator.processing.event.EventHandler;
@@ -46,7 +46,7 @@ public class DefaultEventHandler<R extends CustomResource<?, ?>> implements Even
4646
private final int terminationTimeout;
4747
private final ReentrantLock lock = new ReentrantLock();
4848
private DefaultEventSourceManager<R> eventSourceManager;
49-
private final ControllerConfiguration configuration;
49+
private final ControllerConfiguration<R> configuration;
5050

5151
public DefaultEventHandler(ConfiguredController<R> controller) {
5252
this(
@@ -62,7 +62,7 @@ public DefaultEventHandler(ConfiguredController<R> controller) {
6262
EventDispatcher<R> eventDispatcher,
6363
String relatedControllerName,
6464
Retry retry,
65-
int concurrentReconciliationThreads, ControllerConfiguration configuration) {
65+
int concurrentReconciliationThreads, ControllerConfiguration<R> configuration) {
6666
this(
6767
eventDispatcher,
6868
relatedControllerName,
@@ -76,7 +76,7 @@ private DefaultEventHandler(
7676
String relatedControllerName,
7777
Retry retry,
7878
int concurrentReconciliationThreads,
79-
int terminationTimeout, ControllerConfiguration configuration) {
79+
int terminationTimeout, ControllerConfiguration<R> configuration) {
8080
this.eventDispatcher = eventDispatcher;
8181
this.retry = retry;
8282
this.controllerName = relatedControllerName;
@@ -157,7 +157,7 @@ private RetryInfo retryInfo(String customResourceUid) {
157157
}
158158

159159
void eventProcessingFinished(
160-
ExecutionScope<R> executionScope, PostExecutionControl postExecutionControl) {
160+
ExecutionScope<R> executionScope, PostExecutionControl<R> postExecutionControl) {
161161
try {
162162
lock.lock();
163163
log.debug(
@@ -194,7 +194,7 @@ void eventProcessingFinished(
194194
* events (received meanwhile retry is in place or already in buffer) instantly or always wait
195195
* according to the retry timing if there was an exception.
196196
*/
197-
private void handleRetryOnException(ExecutionScope executionScope) {
197+
private void handleRetryOnException(ExecutionScope<R> executionScope) {
198198
RetryExecution execution = getOrInitRetryExecution(executionScope);
199199
boolean newEventsExists = eventBuffer.newEventsExists(executionScope.getCustomResourceUid());
200200
eventBuffer.putBackEvents(executionScope.getCustomResourceUid(), executionScope.getEvents());
@@ -216,9 +216,7 @@ private void handleRetryOnException(ExecutionScope executionScope) {
216216
.getRetryTimerEventSource()
217217
.scheduleOnce(executionScope.getCustomResource(), delay);
218218
},
219-
() -> {
220-
log.error("Exhausted retries for {}", executionScope);
221-
});
219+
() -> log.error("Exhausted retries for {}", executionScope));
222220
}
223221

224222
private void markSuccessfulExecutionRegardingRetry(ExecutionScope<R> executionScope) {
@@ -257,11 +255,10 @@ private RetryExecution getOrInitRetryExecution(ExecutionScope<R> executionScope)
257255
* would override an additional change coming from a different client.
258256
*/
259257
private void cacheUpdatedResourceIfChanged(
260-
ExecutionScope<R> executionScope, PostExecutionControl postExecutionControl) {
258+
ExecutionScope<R> executionScope, PostExecutionControl<R> postExecutionControl) {
261259
if (postExecutionControl.customResourceUpdatedDuringExecution()) {
262260
R originalCustomResource = executionScope.getCustomResource();
263-
CustomResource customResourceAfterExecution =
264-
postExecutionControl.getUpdatedCustomResource().get();
261+
R customResourceAfterExecution = postExecutionControl.getUpdatedCustomResource().get();
265262
String originalResourceVersion = getVersion(originalCustomResource);
266263

267264
log.debug(

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public EventDispatcher(ConfiguredController<R> controller) {
4141
this(controller, new CustomResourceFacade<>(controller.getCRClient()));
4242
}
4343

44-
public PostExecutionControl handleExecution(ExecutionScope<R> executionScope) {
44+
public PostExecutionControl<R> handleExecution(ExecutionScope<R> executionScope) {
4545
try {
4646
return handleDispatch(executionScope);
4747
} catch (KubernetesClientException e) {
@@ -57,7 +57,7 @@ public PostExecutionControl handleExecution(ExecutionScope<R> executionScope) {
5757
}
5858
}
5959

60-
private PostExecutionControl handleDispatch(ExecutionScope<R> executionScope) {
60+
private PostExecutionControl<R> handleDispatch(ExecutionScope<R> executionScope) {
6161
R resource = executionScope.getCustomResource();
6262
log.debug("Handling events: {} for resource {}", executionScope.getEvents(), getName(resource));
6363

@@ -106,7 +106,7 @@ private boolean shouldNotDispatchToDelete(R resource) {
106106
return configuration().useFinalizer() && !resource.hasFinalizer(configuration().getFinalizer());
107107
}
108108

109-
private PostExecutionControl handleCreateOrUpdate(
109+
private PostExecutionControl<R> handleCreateOrUpdate(
110110
ExecutionScope<R> executionScope, R resource, Context<R> context) {
111111
if (configuration().useFinalizer() && !resource.hasFinalizer(configuration().getFinalizer())) {
112112
/*
@@ -153,7 +153,7 @@ private PostExecutionControl handleCreateOrUpdate(
153153
}
154154
}
155155

156-
private PostExecutionControl handleDelete(R resource, Context<R> context) {
156+
private PostExecutionControl<R> handleDelete(R resource, Context<R> context) {
157157
log.debug(
158158
"Executing delete for resource: {} with version: {}",
159159
getName(resource),
@@ -213,7 +213,7 @@ private R replace(R resource) {
213213
}
214214

215215
// created to support unit testing
216-
static class CustomResourceFacade<R extends CustomResource> {
216+
static class CustomResourceFacade<R extends CustomResource<?, ?>> {
217217

218218
private final MixedOperation<R, KubernetesResourceList<R>, Resource<R>> resourceOperation;
219219

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
package io.javaoperatorsdk.operator.processing;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
3+
import io.fabric8.kubernetes.client.CustomResource;
54

6-
class ExecutionConsumer implements Runnable {
5+
class ExecutionConsumer<R extends CustomResource<?, ?>> implements Runnable {
76

8-
private static final Logger log = LoggerFactory.getLogger(ExecutionConsumer.class);
9-
10-
private final ExecutionScope executionScope;
11-
private final EventDispatcher eventDispatcher;
12-
private final DefaultEventHandler defaultEventHandler;
7+
private final ExecutionScope<R> executionScope;
8+
private final EventDispatcher<R> eventDispatcher;
9+
private final DefaultEventHandler<R> defaultEventHandler;
1310

1411
ExecutionConsumer(
15-
ExecutionScope executionScope,
16-
EventDispatcher eventDispatcher,
17-
DefaultEventHandler defaultEventHandler) {
12+
ExecutionScope<R> executionScope,
13+
EventDispatcher<R> eventDispatcher,
14+
DefaultEventHandler<R> defaultEventHandler) {
1815
this.executionScope = executionScope;
1916
this.eventDispatcher = eventDispatcher;
2017
this.defaultEventHandler = defaultEventHandler;
2118
}
2219

2320
@Override
2421
public void run() {
25-
PostExecutionControl postExecutionControl = eventDispatcher.handleExecution(executionScope);
22+
PostExecutionControl<R> postExecutionControl = eventDispatcher.handleExecution(executionScope);
2623
defaultEventHandler.eventProcessingFinished(executionScope, postExecutionControl);
2724
}
2825
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package io.javaoperatorsdk.operator.processing;
22

3-
import io.fabric8.kubernetes.client.CustomResource;
43
import java.util.Optional;
54

6-
public final class PostExecutionControl {
5+
import io.fabric8.kubernetes.client.CustomResource;
6+
7+
public final class PostExecutionControl<R extends CustomResource<?, ?>> {
78

89
private final boolean onlyFinalizerHandled;
910

10-
private final CustomResource updatedCustomResource;
11+
private final R updatedCustomResource;
1112

1213
private final RuntimeException runtimeException;
1314

1415
private PostExecutionControl(
1516
boolean onlyFinalizerHandled,
16-
CustomResource updatedCustomResource,
17+
R updatedCustomResource,
1718
RuntimeException runtimeException) {
1819
this.onlyFinalizerHandled = onlyFinalizerHandled;
1920
this.updatedCustomResource = updatedCustomResource;
@@ -28,8 +29,9 @@ public static PostExecutionControl defaultDispatch() {
2829
return new PostExecutionControl(false, null, null);
2930
}
3031

31-
public static PostExecutionControl customResourceUpdated(CustomResource updatedCustomResource) {
32-
return new PostExecutionControl(false, updatedCustomResource, null);
32+
public static <R extends CustomResource<?, ?>> PostExecutionControl<R> customResourceUpdated(
33+
R updatedCustomResource) {
34+
return new PostExecutionControl<>(false, updatedCustomResource, null);
3335
}
3436

3537
public static PostExecutionControl exceptionDuringExecution(RuntimeException exception) {
@@ -40,7 +42,7 @@ public boolean isOnlyFinalizerHandled() {
4042
return onlyFinalizerHandled;
4143
}
4244

43-
public Optional<CustomResource> getUpdatedCustomResource() {
45+
public Optional<R> getUpdatedCustomResource() {
4446
return Optional.ofNullable(updatedCustomResource);
4547
}
4648

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ public class DefaultEventSourceManager<R extends CustomResource<?, ?>>
4646

4747
public DefaultEventSourceManager(ConfiguredController<R> controller) {
4848
this(new DefaultEventHandler<>(controller), true);
49-
registerEventSource(
50-
CUSTOM_RESOURCE_EVENT_SOURCE_NAME,
51-
new CustomResourceEventSource<>(controller.getCRClient(), controller.getConfiguration()));
49+
registerEventSource(CUSTOM_RESOURCE_EVENT_SOURCE_NAME,
50+
new CustomResourceEventSource<>(controller));
5251
}
5352

5453
@Override

0 commit comments

Comments
 (0)