Skip to content

Commit e03d15f

Browse files
committed
feat: automatically derive the the dependent resource type if not specified
Signed-off-by: xstefank <[email protected]>
1 parent 4225645 commit e03d15f

File tree

75 files changed

+79
-315
lines changed

Some content is hidden

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

75 files changed

+79
-315
lines changed

bootstrapper-maven-plugin/src/main/resources/templates/ConfigMapDependentResource.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public class ConfigMapDependentResource
1717

1818
public static final String KEY = "key";
1919

20-
public ConfigMapDependentResource() {
21-
super(ConfigMap.class);
22-
}
23-
2420
@Override
2521
protected ConfigMap desired({{artifactClassId}}CustomResource primary,
2622
Context<{{artifactClassId}}CustomResource> context) {

docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ Deleted (or set to be garbage collected). The following example shows how to cre
136136
@KubernetesDependent(labelSelector = WebPageManagedDependentsReconciler.SELECTOR)
137137
class DeploymentDependentResource extends CRUDKubernetesDependentResource<Deployment, WebPage> {
138138

139-
public DeploymentDependentResource() {
140-
super(Deployment.class);
141-
}
142-
143139
@Override
144140
protected Deployment desired(WebPage webPage, Context<WebPage> context) {
145141
var deploymentName = deploymentName(webPage);

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator.processing.dependent;
22

3+
import java.lang.reflect.ParameterizedType;
34
import java.util.Optional;
45

56
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -23,13 +24,22 @@ public abstract class AbstractEventSourceHolderDependentResource<
2324
private boolean isCacheFillerEventSource;
2425
protected String eventSourceNameToUse;
2526

27+
@SuppressWarnings("unchecked")
28+
protected AbstractEventSourceHolderDependentResource() {
29+
this(null, null);
30+
}
31+
2632
protected AbstractEventSourceHolderDependentResource(Class<R> resourceType) {
2733
this(resourceType, null);
2834
}
2935

3036
protected AbstractEventSourceHolderDependentResource(Class<R> resourceType, String name) {
3137
super(name);
32-
this.resourceType = resourceType;
38+
if (resourceType == null) {
39+
this.resourceType = findFirstGenericSuperclass(getClass());
40+
} else {
41+
this.resourceType = resourceType;
42+
}
3343
}
3444

3545
/**
@@ -116,4 +126,14 @@ protected void onUpdated(P primary, R updated, R actual, Context<P> context) {
116126
private RecentOperationCacheFiller<R> recentOperationCacheFiller() {
117127
return (RecentOperationCacheFiller<R>) eventSource;
118128
}
129+
130+
@SuppressWarnings("unchecked")
131+
private Class<R> findFirstGenericSuperclass(
132+
Class<? extends AbstractEventSourceHolderDependentResource> clazz) {
133+
while (!(clazz.getGenericSuperclass() instanceof ParameterizedType)) {
134+
clazz = (Class<? extends AbstractEventSourceHolderDependentResource>) clazz.getSuperclass();
135+
}
136+
return (Class<R>)
137+
((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments()[0];
138+
}
119139
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public abstract class AbstractExternalDependentResource<
2121

2222
private InformerEventSource<?, P> externalStateEventSource;
2323

24+
protected AbstractExternalDependentResource() {}
25+
2426
@SuppressWarnings("unchecked")
2527
protected AbstractExternalDependentResource(Class<R> resourceType) {
2628
super(resourceType);

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/external/AbstractPollingDependentResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public abstract class AbstractPollingDependentResource<R, P extends HasMetadata>
1616
public static final Duration DEFAULT_POLLING_PERIOD = Duration.ofMillis(5000);
1717
private Duration pollingPeriod;
1818

19+
protected AbstractPollingDependentResource() {}
20+
1921
protected AbstractPollingDependentResource(Class<R> resourceType) {
2022
this(resourceType, DEFAULT_POLLING_PERIOD);
2123
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/external/PerResourcePollingDependentResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public abstract class PerResourcePollingDependentResource<R, P extends HasMetada
1414
extends AbstractPollingDependentResource<R, P>
1515
implements PerResourcePollingEventSource.ResourceFetcher<R, P> {
1616

17+
public PerResourcePollingDependentResource() {}
18+
1719
public PerResourcePollingDependentResource(Class<R> resourceType) {
1820
super(resourceType);
1921
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/CRUDKubernetesDependentResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public abstract class CRUDKubernetesDependentResource<R extends HasMetadata, P e
1818
extends KubernetesDependentResource<R, P>
1919
implements Creator<R, P>, Updater<R, P>, GarbageCollected<P> {
2020

21+
public CRUDKubernetesDependentResource() {}
22+
2123
public CRUDKubernetesDependentResource(Class<R> resourceType) {
2224
super(resourceType);
2325
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/CRUDNoGCKubernetesDependentResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
public class CRUDNoGCKubernetesDependentResource<R extends HasMetadata, P extends HasMetadata>
2121
extends KubernetesDependentResource<R, P> implements Creator<R, P>, Updater<R, P>, Deleter<P> {
2222

23+
public CRUDNoGCKubernetesDependentResource() {}
24+
2325
public CRUDNoGCKubernetesDependentResource(Class<R> resourceType) {
2426
super(resourceType);
2527
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
4141
private KubernetesDependentResourceConfig<R> kubernetesDependentResourceConfig;
4242
private volatile Boolean useSSA;
4343

44+
public KubernetesDependentResource() {}
45+
4446
public KubernetesDependentResource(Class<R> resourceType) {
4547
this(resourceType, null);
4648
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,11 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
359359
}
360360

361361
public static class ReadOnlyDependent extends KubernetesDependentResource<ConfigMap, ConfigMap>
362-
implements GarbageCollected<ConfigMap> {
363-
364-
public ReadOnlyDependent() {
365-
super(ConfigMap.class);
366-
}
367-
}
362+
implements GarbageCollected<ConfigMap> {}
368363

369364
@KubernetesDependent(informer = @Informer(namespaces = Constants.WATCH_ALL_NAMESPACES))
370365
public static class WatchAllNSDependent extends KubernetesDependentResource<ConfigMap, ConfigMap>
371-
implements GarbageCollected<ConfigMap> {
372-
373-
public WatchAllNSDependent() {
374-
super(ConfigMap.class);
375-
}
376-
}
366+
implements GarbageCollected<ConfigMap> {}
377367

378368
@Workflow(dependents = @Dependent(type = OverriddenNSDependent.class))
379369
@ControllerConfiguration(
@@ -394,10 +384,6 @@ public static class OverriddenNSDependent
394384
implements GarbageCollected<ConfigMap> {
395385

396386
private static final String DEP_NS = "dependentNS";
397-
398-
public OverriddenNSDependent() {
399-
super(ConfigMap.class);
400-
}
401387
}
402388

403389
@Workflow(
@@ -415,12 +401,7 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
415401

416402
private static class NamedDependentResource
417403
extends KubernetesDependentResource<ConfigMap, ConfigMap>
418-
implements GarbageCollected<ConfigMap> {
419-
420-
public NamedDependentResource() {
421-
super(ConfigMap.class);
422-
}
423-
}
404+
implements GarbageCollected<ConfigMap> {}
424405

425406
private static class ExternalDependentResource
426407
implements DependentResource<Object, ConfigMap>,

0 commit comments

Comments
 (0)