Skip to content

Commit 55a903a

Browse files
committed
feat: add resource type to DependentResourceSpec
1 parent 179d627 commit 55a903a

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

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

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

33
import java.time.Duration;
44
import java.util.ArrayList;
5+
import java.util.Arrays;
56
import java.util.Collections;
67
import java.util.List;
78
import java.util.Optional;
@@ -26,7 +27,7 @@ public class AnnotationControllerConfiguration<R extends HasMetadata>
2627

2728
protected final Reconciler<R> reconciler;
2829
private final ControllerConfiguration annotation;
29-
private List<DependentResourceSpec<?, ?>> specs;
30+
private List<DependentResourceSpec<?, ?, ?>> specs;
3031

3132
public AnnotationControllerConfiguration(Reconciler<R> reconciler) {
3233
this.reconciler = reconciler;
@@ -135,7 +136,7 @@ public static <T> T valueOrDefault(
135136

136137
@SuppressWarnings({"rawtypes", "unchecked"})
137138
@Override
138-
public List<DependentResourceSpec<?, ?>> getDependentResources() {
139+
public List<DependentResourceSpec<?, ?, ?>> getDependentResources() {
139140
if (specs == null) {
140141
final var dependents =
141142
valueOrDefault(annotation, ControllerConfiguration::dependents, new Dependent[] {});
@@ -147,6 +148,11 @@ public static <T> T valueOrDefault(
147148
specs = new ArrayList<>(dependents.length);
148149
for (Dependent dependent : dependents) {
149150
final Class<? extends DependentResource> dependentType = dependent.type();
151+
final Class<?> resourceType =
152+
Arrays.asList(dependentType.getInterfaces()).contains(DependentResource.class)
153+
? Utils.getFirstTypeArgumentFromInterface(dependentType)
154+
: Utils.getFirstTypeArgumentFromExtendedClass(dependentType);
155+
150156
if (KubernetesDependentResource.class.isAssignableFrom(dependentType)) {
151157
final var kubeDependent = dependentType.getAnnotation(KubernetesDependent.class);
152158
final var namespaces =
@@ -164,7 +170,7 @@ public static <T> T valueOrDefault(
164170
config =
165171
new KubernetesDependentResourceConfig(addOwnerReference, namespaces, labelSelector);
166172
}
167-
specs.add(new DependentResourceSpec(dependentType, config));
173+
specs.add(new DependentResourceSpec(dependentType, resourceType, config));
168174
}
169175
}
170176
return specs;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ default ObjectMapper getObjectMapper() {
131131
return OBJECT_MAPPER;
132132
}
133133

134-
default <T extends DependentResource<?, ?>> T createFrom(DependentResourceSpec<T, ?> spec) {
134+
default <T extends DependentResource<?, ?>> T createFrom(DependentResourceSpec<T, ?, ?> spec) {
135135
try {
136136
return spec.getDependentResourceClass().getConstructor().newInstance();
137137
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException

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
@@ -51,7 +51,7 @@ default ResourceEventFilter<R> getEventFilter() {
5151
return ResourceEventFilters.passthrough();
5252
}
5353

54-
default List<DependentResourceSpec<?, ?>> getDependentResources() {
54+
default List<DependentResourceSpec<?, ?, ?>> getDependentResources() {
5555
return Collections.emptyList();
5656
}
5757

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ControllerConfigurationOverrider<R extends HasMetadata> {
2222
private ResourceEventFilter<R> customResourcePredicate;
2323
private final ControllerConfiguration<R> original;
2424
private Duration reconciliationMaxInterval;
25-
private final List<DependentResourceSpec<?, ?>> dependentResourceSpecs;
25+
private final List<DependentResourceSpec<?, ?, ?>> dependentResourceSpecs;
2626

2727
private ControllerConfigurationOverrider(ControllerConfiguration<R> original) {
2828
finalizer = original.getFinalizer();
@@ -91,6 +91,7 @@ public ControllerConfigurationOverrider<R> withReconciliationMaxInterval(
9191

9292
public void replaceDependentResourceConfig(
9393
Class<? extends DependentResource<?, R>> dependentResourceClass,
94+
Class<?> resourceType,
9495
Object dependentResourceConfig) {
9596

9697
var currentConfig =
@@ -101,7 +102,8 @@ public void replaceDependentResourceConfig(
101102
}
102103
dependentResourceSpecs.remove(currentConfig.get());
103104
dependentResourceSpecs
104-
.add(new DependentResourceSpec(dependentResourceClass, dependentResourceConfig));
105+
.add(new DependentResourceSpec(dependentResourceClass, resourceType,
106+
dependentResourceConfig));
105107
}
106108

107109
public void addNewDependentResourceConfig(DependentResourceSpec dependentResourceSpec) {
@@ -115,7 +117,7 @@ public void addNewDependentResourceConfig(DependentResourceSpec dependentResourc
115117
dependentResourceSpecs.add(dependentResourceSpec);
116118
}
117119

118-
private Optional<DependentResourceSpec<?, ?>> findConfigForDependentResourceClass(
120+
private Optional<DependentResourceSpec<?, ?, ?>> findConfigForDependentResourceClass(
119121
Class<? extends DependentResource> dependentResourceClass) {
120122
return dependentResourceSpecs.stream()
121123
.filter(dc -> dc.getDependentResourceClass().equals(dependentResourceClass))

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
1111
import io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEventFilter;
1212

13-
@SuppressWarnings("rawtypes")
1413
public class DefaultControllerConfiguration<R extends HasMetadata>
1514
extends DefaultResourceConfiguration<R>
1615
implements ControllerConfiguration<R> {
@@ -22,7 +21,7 @@ public class DefaultControllerConfiguration<R extends HasMetadata>
2221
private final boolean generationAware;
2322
private final RetryConfiguration retryConfiguration;
2423
private final ResourceEventFilter<R> resourceEventFilter;
25-
private final List<DependentResourceSpec<?, ?>> dependents;
24+
private final List<DependentResourceSpec<?, ?, ?>> dependents;
2625
private final Duration reconciliationMaxInterval;
2726

2827
// NOSONAR constructor is meant to provide all information
@@ -38,7 +37,7 @@ public DefaultControllerConfiguration(
3837
ResourceEventFilter<R> resourceEventFilter,
3938
Class<R> resourceClass,
4039
Duration reconciliationMaxInterval,
41-
List<DependentResourceSpec<?, ?>> dependents) {
40+
List<DependentResourceSpec<?, ?, ?>> dependents) {
4241
super(labelSelector, resourceClass, namespaces);
4342
this.associatedControllerClassName = associatedControllerClassName;
4443
this.name = name;
@@ -91,7 +90,7 @@ public ResourceEventFilter<R> getEventFilter() {
9190
}
9291

9392
@Override
94-
public List<DependentResourceSpec<?, ?>> getDependentResources() {
93+
public List<DependentResourceSpec<?, ?, ?>> getDependentResources() {
9594
return dependents;
9695
}
9796

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
66

7-
public class DependentResourceSpec<T extends DependentResource<?, ?>, C> {
7+
public class DependentResourceSpec<T extends DependentResource<?, ?>, R, C> {
88

99
private final Class<T> dependentResourceClass;
1010

1111
private final C dependentResourceConfig;
1212

13-
public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig) {
13+
private final Class<R> resourceType;
14+
15+
public DependentResourceSpec(Class<T> dependentResourceClass, Class<R> resourceType,
16+
C dependentResourceConfig) {
1417
this.dependentResourceClass = dependentResourceClass;
1518
this.dependentResourceConfig = dependentResourceConfig;
19+
this.resourceType = resourceType;
1620
}
1721

1822
public Class<T> getDependentResourceClass() {
@@ -22,4 +26,8 @@ public Class<T> getDependentResourceClass() {
2226
public Optional<C> getDependentResourceConfiguration() {
2327
return Optional.ofNullable(dependentResourceConfig);
2428
}
29+
30+
public Class<R> getResourceType() {
31+
return resourceType;
32+
}
2533
}

0 commit comments

Comments
 (0)