Skip to content

Commit 402eabd

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

File tree

76 files changed

+156
-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.

76 files changed

+156
-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/api/config/Utils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,27 @@ public static Class<?> getTypeArgumentFromExtendedClassByIndex(Class<?> clazz, i
134134
}
135135
}
136136

137+
public static Class<?> getTypeArgumentFromHierarchyByIndex(
138+
Class<?> clazz, Class<?> expectedImplementedInterface, int index) {
139+
Class<?> c = clazz;
140+
while (!(c.getGenericSuperclass() instanceof ParameterizedType)) {
141+
c = c.getSuperclass();
142+
}
143+
Class<?> actualTypeArgument =
144+
(Class<?>) ((ParameterizedType) c.getGenericSuperclass()).getActualTypeArguments()[index];
145+
if (!expectedImplementedInterface.isAssignableFrom(actualTypeArgument)) {
146+
throw new IllegalArgumentException(
147+
GENERIC_PARAMETER_TYPE_ERROR_PREFIX
148+
+ clazz.getName()
149+
+ "because it doesn't extend a class that is parametrized with the type that"
150+
+ " implements "
151+
+ expectedImplementedInterface.getSimpleName()
152+
+ ". Please provide the resource type in the constructor (e.g.,"
153+
+ " super(Deployment.class).");
154+
}
155+
return actualTypeArgument;
156+
}
157+
137158
public static Class<?> getFirstTypeArgumentFromInterface(
138159
Class<?> clazz, Class<?> expectedImplementedInterface) {
139160
return getTypeArgumentFromInterfaceByIndex(clazz, expectedImplementedInterface, 0);

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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;
7+
import io.javaoperatorsdk.operator.api.config.Utils;
68
import io.javaoperatorsdk.operator.api.reconciler.Context;
79
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
810
import io.javaoperatorsdk.operator.api.reconciler.Ignore;
@@ -23,13 +25,23 @@ public abstract class AbstractEventSourceHolderDependentResource<
2325
private boolean isCacheFillerEventSource;
2426
protected String eventSourceNameToUse;
2527

28+
@SuppressWarnings("unchecked")
29+
protected AbstractEventSourceHolderDependentResource() {
30+
this(null, null);
31+
}
32+
2633
protected AbstractEventSourceHolderDependentResource(Class<R> resourceType) {
2734
this(resourceType, null);
2835
}
2936

3037
protected AbstractEventSourceHolderDependentResource(Class<R> resourceType, String name) {
3138
super(name);
32-
this.resourceType = resourceType;
39+
if (resourceType == null) {
40+
this.resourceType =
41+
(Class<R>) Utils.getTypeArgumentFromHierarchyByIndex(getClass(), HasMetadata.class, 0);
42+
} else {
43+
this.resourceType = resourceType;
44+
}
3345
}
3446

3547
/**
@@ -116,4 +128,23 @@ protected void onUpdated(P primary, R updated, R actual, Context<P> context) {
116128
private RecentOperationCacheFiller<R> recentOperationCacheFiller() {
117129
return (RecentOperationCacheFiller<R>) eventSource;
118130
}
131+
132+
@SuppressWarnings("unchecked")
133+
private Class<R> findFirstGenericSuperclass(
134+
Class<? extends AbstractEventSourceHolderDependentResource> clazz) {
135+
Class<? extends AbstractEventSourceHolderDependentResource> c = clazz;
136+
while (!(c.getGenericSuperclass() instanceof ParameterizedType)) {
137+
c = (Class<? extends AbstractEventSourceHolderDependentResource>) c.getSuperclass();
138+
}
139+
Class<R> actualTypeArgument =
140+
(Class<R>) ((ParameterizedType) c.getGenericSuperclass()).getActualTypeArguments()[0];
141+
if (!HasMetadata.class.isAssignableFrom(actualTypeArgument)) {
142+
throw new IllegalStateException(
143+
"Not possible to automatically derive the the resource type for "
144+
+ clazz.getName()
145+
+ ". Please provide the resource type in the constructor (e.g.,"
146+
+ " super(Deployment.class).");
147+
}
148+
return actualTypeArgument;
149+
}
119150
}

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
}

0 commit comments

Comments
 (0)