Skip to content

Commit 0fd5c03

Browse files
committed
refactor: remove InformerConfigSpec
Signed-off-by: Chris Laprun <claprun@redhat.com>
1 parent 5f6df70 commit 0fd5c03

File tree

11 files changed

+114
-231
lines changed

11 files changed

+114
-231
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,24 @@ private static List<DependentResourceSpec> dependentResources(
225225
Utils.instantiate(dependent.reconcilePrecondition(), Condition.class, context),
226226
Utils.instantiate(dependent.deletePostcondition(), Condition.class, context),
227227
Utils.instantiate(dependent.activationCondition(), Condition.class, context),
228-
eventSourceName);
228+
eventSourceName, resourceTypeFor(dependentType));
229229

230230
// extract potential configuration
231-
DependentResourceConfigurationResolver.configureSpecFromConfigured(spec, controllerConfiguration,
231+
DependentResourceConfigurationResolver.configureSpecFromConfigured(spec,
232+
controllerConfiguration,
232233
dependentType);
233234

234235
specsMap.put(dependentName, spec);
235236
}
236237
return specsMap.values().stream().toList();
237238
}
238239

240+
@SuppressWarnings({"unchecked", "rawtypes"})
241+
private static <R> Class<R> resourceTypeFor(
242+
Class<? extends DependentResource> dependentResourceClass) {
243+
return Utils.instantiate(dependentResourceClass, DependentResource.class, null).resourceType();
244+
}
245+
239246
protected boolean createIfNeeded() {
240247
return true;
241248
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,21 @@
1111
public class DependentResourceSpec<R, P extends HasMetadata, C> {
1212

1313
private final Class<? extends DependentResource<R, P>> dependentResourceClass;
14-
1514
private final String name;
16-
1715
private final Set<String> dependsOn;
18-
1916
private final Condition<?, ?> readyCondition;
20-
2117
private final Condition<?, ?> reconcileCondition;
22-
2318
private final Condition<?, ?> deletePostCondition;
24-
2519
private final Condition<?, ?> activationCondition;
26-
2720
private final String useEventSourceWithName;
21+
private final Class<R> resourceClass;
2822
private C nullableConfiguration;
2923

3024
public DependentResourceSpec(Class<? extends DependentResource<R, P>> dependentResourceClass,
3125
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
3226
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition,
33-
Condition<?, ?> activationCondition, String useEventSourceWithName) {
27+
Condition<?, ?> activationCondition, String useEventSourceWithName,
28+
Class<R> resourceClass) {
3429
this.dependentResourceClass = dependentResourceClass;
3530
this.name = name;
3631
this.dependsOn = dependsOn;
@@ -39,6 +34,7 @@ public DependentResourceSpec(Class<? extends DependentResource<R, P>> dependentR
3934
this.deletePostCondition = deletePostCondition;
4035
this.activationCondition = activationCondition;
4136
this.useEventSourceWithName = useEventSourceWithName;
37+
this.resourceClass = resourceClass;
4238
}
4339

4440
public Class<? extends DependentResource<R, P>> getDependentResourceClass() {
@@ -107,4 +103,8 @@ public Optional<C> getConfiguration() {
107103
protected void setNullableConfiguration(C configuration) {
108104
this.nullableConfiguration = configuration;
109105
}
106+
107+
public Class<R> getResourceClass() {
108+
return resourceClass;
109+
}
110110
}

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
1313
import io.javaoperatorsdk.operator.api.config.Utils;
1414
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
15-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.GenericKubernetesDependentResource;
16-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.InformerConfigSpec;
1715
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
1816
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
1917
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
@@ -193,27 +191,6 @@ public InformerConfigurationBuilder<R> withName(String name) {
193191
return this;
194192
}
195193

196-
@SuppressWarnings("unchecked")
197-
public InformerConfigurationBuilder<R> withConfig(InformerConfigSpec<R> spec) {
198-
groupVersionKind = spec.groupVersionKind();
199-
if (groupVersionKind != null
200-
&& !GenericKubernetesResource.class.isAssignableFrom(resourceClass)) {
201-
throw new IllegalStateException("If GroupVersionKind is set, the resource type must be "
202-
+ GenericKubernetesDependentResource.class.getSimpleName());
203-
}
204-
205-
return withName(spec.name())
206-
.withGenericFilter(spec.genericFilter())
207-
.withSecondaryToPrimaryMapper(spec.secondaryToPrimaryMapper())
208-
.withPrimaryToSecondaryMapper(spec.primaryToSecondaryMapper())
209-
.withLabelSelector(spec.labelSelector())
210-
.withNamespaces(spec.namespaces())
211-
.withOnAddFilter(spec.onAddFilter())
212-
.withOnUpdateFilter(spec.onUpdateFilter())
213-
.withOnDeleteFilter(spec.onDeleteFilter())
214-
.followControllerNamespacesOnChange(spec.followControllerNamespacesOnChange());
215-
}
216-
217194
public <P extends HasMetadata> InformerConfigurationBuilder<R> withPrimaryToSecondaryMapper(
218195
PrimaryToSecondaryMapper<P> primaryToSecondaryMapper) {
219196
this.primaryToSecondaryMapper = primaryToSecondaryMapper;

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

Lines changed: 0 additions & 103 deletions
This file was deleted.

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

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8+
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
89
import io.fabric8.kubernetes.api.model.HasMetadata;
910
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1011
import io.javaoperatorsdk.operator.api.config.Utils;
1112
import io.javaoperatorsdk.operator.api.config.dependent.ConfigurationConverter;
1213
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
14+
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
1315
import io.javaoperatorsdk.operator.api.reconciler.Constants;
1416
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
1517
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
@@ -43,7 +45,7 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
4345
useSSA = configAnnotation.useSSA().asBoolean();
4446
}
4547

46-
var informerConfiguration = createInformerConfiguration(configAnnotation,
48+
var informerConfiguration = createInformerConfig(configAnnotation,
4749
(DependentResourceSpec<R, P, KubernetesDependentResourceConfig<R>>) spec,
4850
controllerConfig);
4951

@@ -52,76 +54,98 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
5254
}
5355

5456
@SuppressWarnings({"unchecked", "rawtypes"})
55-
private InformerConfigSpec<R> createInformerConfiguration(KubernetesDependent configAnnotation,
57+
private InformerConfiguration<R> createInformerConfig(KubernetesDependent configAnnotation,
5658
DependentResourceSpec<R, P, KubernetesDependentResourceConfig<R>> spec,
57-
ControllerConfiguration<?> controllerConfig) {
58-
final var dependentResourceClass = (Class<? extends KubernetesDependentResource<R, P>>) spec.getDependentResourceClass();
59+
ControllerConfiguration<? extends HasMetadata> controllerConfig) {
60+
Class<? extends KubernetesDependentResource<?, ?>> dependentResourceClass =
61+
(Class<? extends KubernetesDependentResource<?, ?>>) spec.getDependentResourceClass();
62+
final var resourceType = spec.getResourceClass();
63+
64+
InformerConfiguration.InformerConfigurationBuilder informerConfig;
65+
if (configAnnotation != null && configAnnotation.informerConfig() != null &&
66+
!Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().groupVersionKind())) {
67+
68+
if (!GenericKubernetesResource.class.isAssignableFrom(resourceType)) {
69+
throw new IllegalStateException(
70+
"If GroupVersionKind is set the resource type must be GenericKubernetesDependentResource for: "
71+
+ dependentResourceClass.getName());
72+
}
73+
74+
informerConfig = InformerConfiguration.from(
75+
GroupVersionKind.fromString(configAnnotation.informerConfig().groupVersionKind()),
76+
controllerConfig.getResourceClass());
77+
} else {
78+
informerConfig =
79+
InformerConfiguration.from(resourceType, controllerConfig.getResourceClass());
80+
}
81+
5982
// default name should be set even if there's no explicit informer configuration
60-
var name = DependentResource.defaultNameFor(dependentResourceClass);
83+
informerConfig.withName(DependentResource.defaultNameFor(dependentResourceClass));
84+
6185
if (configAnnotation != null && configAnnotation.informerConfig() != null) {
6286
// override default name if more specific one is provided
63-
final var nameFromAnnotation = configAnnotation.informerConfig().name();
64-
if (!Constants.NO_VALUE_SET.equals(nameFromAnnotation)) {
65-
name = nameFromAnnotation;
66-
} else {
67-
final var nameFromDependentAnnotation = spec.getName();
68-
if (nameFromDependentAnnotation != null
69-
&& !Constants.NO_VALUE_SET.equals(nameFromDependentAnnotation)) {
70-
name = nameFromDependentAnnotation;
71-
}
87+
if (!Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().name())) {
88+
informerConfig.withName(configAnnotation.informerConfig().name());
89+
} else if (spec.getName() != null && !Constants.NO_VALUE_SET.equals(spec.getName())) {
90+
informerConfig.withName(spec.getName());
7291
}
7392

7493
var namespaces = Set.of(configAnnotation.informerConfig().namespaces());
94+
informerConfig.withNamespaces(namespaces);
7595

76-
final var labelFromAnnotation = configAnnotation.informerConfig().labelSelector();
77-
var labelSelector =
78-
Constants.NO_VALUE_SET.equals(labelFromAnnotation) ? null : labelFromAnnotation;
96+
final var fromAnnotation = configAnnotation.informerConfig().labelSelector();
97+
var labelSelector = Constants.NO_VALUE_SET.equals(fromAnnotation) ? null : fromAnnotation;
98+
informerConfig.withLabelSelector(labelSelector);
7999

80100
final var context = Utils.contextFor(controllerConfig, dependentResourceClass,
81101
configAnnotation.annotationType());
102+
82103
var onAddFilter = Utils.instantiate(configAnnotation.informerConfig().onAddFilter(),
83104
OnAddFilter.class, context);
105+
informerConfig.withOnAddFilter((OnAddFilter<? super R>) onAddFilter);
106+
84107
var onUpdateFilter =
85108
Utils.instantiate(configAnnotation.informerConfig().onUpdateFilter(),
86109
OnUpdateFilter.class, context);
110+
informerConfig.withOnUpdateFilter((OnUpdateFilter<? super R>) onUpdateFilter);
111+
87112
var onDeleteFilter =
88113
Utils.instantiate(configAnnotation.informerConfig().onDeleteFilter(),
89114
OnDeleteFilter.class, context);
115+
informerConfig.withOnDeleteFilter((OnDeleteFilter<? super R>) onDeleteFilter);
116+
90117
var genericFilter =
91118
Utils.instantiate(configAnnotation.informerConfig().genericFilter(),
92119
GenericFilter.class,
93120
context);
94121

95-
final var gvkFromAnnotation = configAnnotation.informerConfig().groupVersionKind();
96-
var gvk = !Constants.NO_VALUE_SET.equals(gvkFromAnnotation)
97-
? GroupVersionKind.fromString(gvkFromAnnotation)
98-
: null;
122+
informerConfig.withGenericFilter((GenericFilter<? super R>) genericFilter);
123+
124+
informerConfig.followControllerNamespacesOnChange(
125+
configAnnotation.informerConfig().followControllerNamespacesOnChange());
99126

100127
var primaryToSecondaryMapper =
101128
Utils.instantiate(configAnnotation.informerConfig().primaryToSecondaryMapper(),
102129
PrimaryToSecondaryMapper.class, context);
130+
informerConfig.withPrimaryToSecondaryMapper(primaryToSecondaryMapper);
103131

104132
var secondaryToPrimaryMapper =
105133
Utils.instantiate(configAnnotation.informerConfig().secondaryToPrimaryMapper(),
106134
SecondaryToPrimaryMapper.class, context);
107-
108-
return new InformerConfigSpec<>(name, namespaces, labelSelector,
109-
configAnnotation.informerConfig().followControllerNamespacesOnChange(),
110-
onAddFilter, onUpdateFilter, onDeleteFilter, genericFilter,
111-
gvk, secondaryToPrimaryMapper, primaryToSecondaryMapper);
112-
} else {
113-
var secondaryToPrimaryMapper =
114-
(SecondaryToPrimaryMapper<R>) getSecondaryToPrimaryMapper(dependentResourceClass,
115-
controllerConfig.getResourceClass())
116-
.orElse(null);
117-
if (secondaryToPrimaryMapper == null) {
118-
if (spec.getUseEventSourceWithName().isEmpty()) {
119-
log.warn("No SecondaryToPrimaryMapper set for dependent resource "
120-
+ dependentResourceClass.getSimpleName() +
121-
". This might be an issue with the setup of the dependent resource");
122-
}
135+
if (secondaryToPrimaryMapper != null) {
136+
informerConfig.withSecondaryToPrimaryMapper(secondaryToPrimaryMapper);
123137
}
124-
return new InformerConfigSpec<>(name, secondaryToPrimaryMapper);
138+
} else {
139+
getSecondaryToPrimaryMapper(dependentResourceClass,
140+
controllerConfig.getResourceClass())
141+
.ifPresentOrElse(informerConfig::withSecondaryToPrimaryMapper, () -> {
142+
if (spec.getUseEventSourceWithName().isEmpty()) {
143+
log.warn("No SecondaryToPrimaryMapper set for dependent resource "
144+
+ dependentResourceClass.getSimpleName() +
145+
". This might be an issue with the setup of the dependent resource");
146+
}
147+
});
125148
}
149+
return informerConfig.build();
126150
}
127151
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,8 @@ protected void addReferenceHandlingMetadata(R desired, P primary) {
209209
protected InformerEventSource<R, P> createEventSource(EventSourceContext<P> context) {
210210
InformerConfiguration<R> config;
211211
if (kubernetesDependentResourceConfig != null
212-
&& kubernetesDependentResourceConfig.informerConfigSpec() != null) {
213-
config = informerConfigurationBuilder()
214-
.withConfig(kubernetesDependentResourceConfig.informerConfigSpec())
215-
.build();
212+
&& kubernetesDependentResourceConfig.informerConfiguration() != null) {
213+
config = kubernetesDependentResourceConfig.informerConfiguration();
216214
} else {
217215
config = informerConfigurationBuilder()
218216
.withName(name())

0 commit comments

Comments
 (0)