Skip to content

Commit 0764a07

Browse files
committed
refactor to builder
Signed-off-by: Attila Mészáros <csviri@gmail.com>
1 parent 9076148 commit 0764a07

File tree

17 files changed

+147
-178
lines changed

17 files changed

+147
-178
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ 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, resourceTypeFor(dependentType));
228+
eventSourceName);
229229

230230
// extract potential configuration
231231
DependentResourceConfigurationResolver.configureSpecFromConfigured(spec,

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ public class DependentResourceSpec<R, P extends HasMetadata, C> {
1818
private final Condition<?, ?> deletePostCondition;
1919
private final Condition<?, ?> activationCondition;
2020
private final String useEventSourceWithName;
21-
private final Class<R> resourceClass;
2221
private C nullableConfiguration;
2322

2423
public DependentResourceSpec(Class<? extends DependentResource<R, P>> dependentResourceClass,
2524
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
2625
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition,
27-
Condition<?, ?> activationCondition, String useEventSourceWithName,
28-
Class<R> resourceClass) {
26+
Condition<?, ?> activationCondition, String useEventSourceWithName) {
2927
this.dependentResourceClass = dependentResourceClass;
3028
this.name = name;
3129
this.dependsOn = dependsOn;
@@ -34,7 +32,6 @@ public DependentResourceSpec(Class<? extends DependentResource<R, P>> dependentR
3432
this.deletePostCondition = deletePostCondition;
3533
this.activationCondition = activationCondition;
3634
this.useEventSourceWithName = useEventSourceWithName;
37-
this.resourceClass = resourceClass;
3835
}
3936

4037
public Class<? extends DependentResource<R, P>> getDependentResourceClass() {
@@ -104,7 +101,4 @@ protected void setNullableConfiguration(C configuration) {
104101
this.nullableConfiguration = configuration;
105102
}
106103

107-
public Class<R> getResourceClass() {
108-
return resourceClass;
109-
}
110104
}

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ static boolean inheritsNamespacesFromController(Set<String> namespaces) {
151151
@SuppressWarnings("unused")
152152
class InformerConfigurationBuilder<R extends HasMetadata> {
153153

154-
private final Class<R> resourceClass;
155-
private final GroupVersionKind groupVersionKind;
156-
private final Class<? extends HasMetadata> primaryResourceClass;
154+
private Class<R> resourceClass;
155+
protected GroupVersionKind groupVersionKind;
156+
protected Class<? extends HasMetadata> primaryResourceClass;
157157
private String name;
158158
private PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
159159
private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
@@ -179,6 +179,8 @@ private InformerConfigurationBuilder(GroupVersionKind groupVersionKind,
179179
this((Class<R>) GenericKubernetesResource.class, primaryResourceClass, groupVersionKind);
180180
}
181181

182+
protected InformerConfigurationBuilder() {}
183+
182184
private InformerConfigurationBuilder(Class<R> resourceClass,
183185
Class<? extends HasMetadata> primaryResourceClass, GroupVersionKind groupVersionKind) {
184186
this.resourceClass = resourceClass;
@@ -295,6 +297,11 @@ public InformerConfigurationBuilder<R> withItemStore(ItemStore<R> itemStore) {
295297
return this;
296298
}
297299

300+
public InformerConfigurationBuilder<R> withResourceClass(Class<R> resourceClass) {
301+
this.resourceClass = resourceClass;
302+
return this;
303+
}
304+
298305
/**
299306
* Sets a max page size limit when starting the informer. This will result in pagination while
300307
* populating the cache. This means that longer lists will take multiple requests to fetch. See
@@ -307,7 +314,21 @@ public InformerConfigurationBuilder<R> withInformerListLimit(Long informerListLi
307314
return this;
308315
}
309316

317+
public String getName() {
318+
return name;
319+
}
320+
321+
public SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper() {
322+
return secondaryToPrimaryMapper;
323+
}
324+
310325
public InformerConfiguration<R> build() {
326+
if (groupVersionKind != null
327+
&& !GenericKubernetesResource.class.isAssignableFrom(resourceClass)) {
328+
throw new IllegalStateException(
329+
"If GroupVersionKind is set the resource type must be GenericKubernetesDependentResource");
330+
}
331+
311332
return new DefaultInformerConfiguration<>(name, labelSelector, resourceClass,
312333
groupVersionKind,
313334
primaryToSecondaryMapper,

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import java.lang.annotation.Target;
77

88
import io.javaoperatorsdk.operator.api.reconciler.Constants;
9-
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
10-
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
119
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
1210
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
1311
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
@@ -79,21 +77,6 @@
7977
*/
8078
Class<? extends GenericFilter> genericFilter() default GenericFilter.class;
8179

82-
/**
83-
* Optional {@link SecondaryToPrimaryMapper} class, to override the default.
84-
*
85-
* @return class of a {@link SecondaryToPrimaryMapper}
86-
*
87-
*/
88-
Class<? extends SecondaryToPrimaryMapper> secondaryToPrimaryMapper() default SecondaryToPrimaryMapper.class;
89-
90-
/**
91-
* Optionally set a {@link PrimaryToSecondaryMapper}.
92-
*
93-
* @return PrimaryToSecondaryMapper to set
94-
*/
95-
Class<? extends PrimaryToSecondaryMapper> primaryToSecondaryMapper() default PrimaryToSecondaryMapper.class;
96-
9780
/**
9881
* Set that in case of a runtime controller namespace changes, the informer should also follow the
9982
* new namespace set.

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

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8-
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
98
import io.fabric8.kubernetes.api.model.HasMetadata;
109
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1110
import io.javaoperatorsdk.operator.api.config.Utils;
1211
import io.javaoperatorsdk.operator.api.config.dependent.ConfigurationConverter;
1312
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
1413
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
1514
import io.javaoperatorsdk.operator.api.reconciler.Constants;
16-
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
1715
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
18-
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
19-
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
2016
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
2117
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
2218
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
2319
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
2420

25-
import static io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource.getSecondaryToPrimaryMapper;
2621
import static io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig.DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA;
2722

2823
public class KubernetesDependentConverter<R extends HasMetadata, P extends HasMetadata> implements
@@ -54,98 +49,75 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
5449
}
5550

5651
@SuppressWarnings({"unchecked", "rawtypes"})
57-
private InformerConfiguration<R> createInformerConfig(KubernetesDependent configAnnotation,
52+
private InformerConfiguration.InformerConfigurationBuilder<R> createInformerConfig(
53+
KubernetesDependent configAnnotation,
5854
DependentResourceSpec<R, P, KubernetesDependentResourceConfig<R>> spec,
5955
ControllerConfiguration<? extends HasMetadata> controllerConfig) {
6056
Class<? extends KubernetesDependentResource<?, ?>> dependentResourceClass =
6157
(Class<? extends KubernetesDependentResource<?, ?>>) spec.getDependentResourceClass();
62-
final var resourceType = spec.getResourceClass();
6358

64-
InformerConfiguration.InformerConfigurationBuilder informerConfig;
59+
InformerConfiguration.InformerConfigurationBuilder informerBuilder;
6560
if (configAnnotation != null && configAnnotation.informerConfig() != null &&
6661
!Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().groupVersionKind())) {
6762

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(
63+
informerBuilder = InformerConfiguration.from(
7564
GroupVersionKind.fromString(configAnnotation.informerConfig().groupVersionKind()),
7665
controllerConfig.getResourceClass());
7766
} else {
78-
informerConfig =
79-
InformerConfiguration.from(resourceType, controllerConfig.getResourceClass());
67+
informerBuilder = new InternalInformerConfigBuilder(controllerConfig.getResourceClass());
8068
}
8169

82-
// default name should be set even if there's no explicit informer configuration
83-
informerConfig.withName(DependentResource.defaultNameFor(dependentResourceClass));
84-
8570
if (configAnnotation != null && configAnnotation.informerConfig() != null) {
8671
// override default name if more specific one is provided
8772
if (!Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().name())) {
88-
informerConfig.withName(configAnnotation.informerConfig().name());
73+
informerBuilder.withName(configAnnotation.informerConfig().name());
8974
} else if (spec.getName() != null && !Constants.NO_VALUE_SET.equals(spec.getName())) {
90-
informerConfig.withName(spec.getName());
75+
informerBuilder.withName(spec.getName());
9176
}
9277

9378
var namespaces = Set.of(configAnnotation.informerConfig().namespaces());
94-
informerConfig.withNamespaces(namespaces);
79+
informerBuilder.withNamespaces(namespaces);
9580

9681
final var fromAnnotation = configAnnotation.informerConfig().labelSelector();
9782
var labelSelector = Constants.NO_VALUE_SET.equals(fromAnnotation) ? null : fromAnnotation;
98-
informerConfig.withLabelSelector(labelSelector);
83+
informerBuilder.withLabelSelector(labelSelector);
9984

10085
final var context = Utils.contextFor(controllerConfig, dependentResourceClass,
10186
configAnnotation.annotationType());
10287

10388
var onAddFilter = Utils.instantiate(configAnnotation.informerConfig().onAddFilter(),
10489
OnAddFilter.class, context);
105-
informerConfig.withOnAddFilter((OnAddFilter<? super R>) onAddFilter);
90+
informerBuilder.withOnAddFilter((OnAddFilter<? super R>) onAddFilter);
10691

10792
var onUpdateFilter =
10893
Utils.instantiate(configAnnotation.informerConfig().onUpdateFilter(),
10994
OnUpdateFilter.class, context);
110-
informerConfig.withOnUpdateFilter((OnUpdateFilter<? super R>) onUpdateFilter);
95+
informerBuilder.withOnUpdateFilter((OnUpdateFilter<? super R>) onUpdateFilter);
11196

11297
var onDeleteFilter =
11398
Utils.instantiate(configAnnotation.informerConfig().onDeleteFilter(),
11499
OnDeleteFilter.class, context);
115-
informerConfig.withOnDeleteFilter((OnDeleteFilter<? super R>) onDeleteFilter);
100+
informerBuilder.withOnDeleteFilter((OnDeleteFilter<? super R>) onDeleteFilter);
116101

117102
var genericFilter =
118103
Utils.instantiate(configAnnotation.informerConfig().genericFilter(),
119104
GenericFilter.class,
120105
context);
121106

122-
informerConfig.withGenericFilter((GenericFilter<? super R>) genericFilter);
107+
informerBuilder.withGenericFilter((GenericFilter<? super R>) genericFilter);
123108

124-
informerConfig.followControllerNamespacesOnChange(
109+
informerBuilder.followControllerNamespacesOnChange(
125110
configAnnotation.informerConfig().followControllerNamespacesOnChange());
126111

127-
var primaryToSecondaryMapper =
128-
Utils.instantiate(configAnnotation.informerConfig().primaryToSecondaryMapper(),
129-
PrimaryToSecondaryMapper.class, context);
130-
informerConfig.withPrimaryToSecondaryMapper(primaryToSecondaryMapper);
112+
}
113+
return informerBuilder;
114+
}
131115

132-
var secondaryToPrimaryMapper =
133-
Utils.instantiate(configAnnotation.informerConfig().secondaryToPrimaryMapper(),
134-
SecondaryToPrimaryMapper.class, context);
135-
if (secondaryToPrimaryMapper != null) {
136-
informerConfig.withSecondaryToPrimaryMapper(secondaryToPrimaryMapper);
137-
}
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-
});
116+
private class InternalInformerConfigBuilder
117+
extends InformerConfiguration.InformerConfigurationBuilder<R> {
118+
public InternalInformerConfigBuilder(Class<? extends HasMetadata> primaryResourceClass) {
119+
this.primaryResourceClass = primaryResourceClass;
148120
}
149-
return informerConfig.build();
150121
}
122+
151123
}

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected;
2222
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ConfiguredDependentResource;
2323
import io.javaoperatorsdk.operator.processing.dependent.AbstractEventSourceHolderDependentResource;
24-
import io.javaoperatorsdk.operator.processing.dependent.Creator;
2524
import io.javaoperatorsdk.operator.processing.dependent.Matcher.Result;
2625
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.updatermatcher.GenericResourceUpdaterMatcher;
2726
import io.javaoperatorsdk.operator.processing.event.ResourceID;
@@ -209,14 +208,21 @@ protected void addReferenceHandlingMetadata(R desired, P primary) {
209208
protected InformerEventSource<R, P> createEventSource(EventSourceContext<P> context) {
210209
InformerConfiguration<R> config;
211210
if (kubernetesDependentResourceConfig != null
212-
&& kubernetesDependentResourceConfig.informerConfiguration() != null) {
213-
config = kubernetesDependentResourceConfig.informerConfiguration();
211+
&& kubernetesDependentResourceConfig.informerConfigurationBuilder() != null) {
212+
var configBuilder = kubernetesDependentResourceConfig.informerConfigurationBuilder();
213+
configBuilder.withResourceClass(resourceType());
214+
if (configBuilder.getName() == null) {
215+
configBuilder.withName(name());
216+
}
217+
if (configBuilder.getSecondaryToPrimaryMapper() == null) {
218+
configBuilder.withSecondaryToPrimaryMapper(getSecondaryToPrimaryMapper().orElseThrow());
219+
}
220+
config = configBuilder.build();
214221
} else {
215222
config = informerConfigurationBuilder()
216223
.withName(name())
217224
.withSecondaryToPrimaryMapper(
218-
(SecondaryToPrimaryMapper<R>) getSecondaryToPrimaryMapper(this.getClass(),
219-
getPrimaryResourceType()).orElseThrow())
225+
getSecondaryToPrimaryMapper().orElseThrow())
220226
.build();
221227
}
222228
var es = new InformerEventSource<>(config, context);
@@ -292,19 +298,18 @@ public boolean isDeletable() {
292298
return super.isDeletable() && !garbageCollected;
293299
}
294300

295-
@SuppressWarnings("rawtypes")
296-
public static Optional<SecondaryToPrimaryMapper<?>> getSecondaryToPrimaryMapper(
297-
Class<? extends KubernetesDependentResource> drClass,
298-
Class<? extends HasMetadata> primaryResourceClass) {
299-
300-
if (GarbageCollected.class.isAssignableFrom(drClass)) {
301-
var clustered = !Namespaced.class.isAssignableFrom(primaryResourceClass);
302-
return Optional.of(Mappers.fromOwnerReferences(primaryResourceClass, clustered));
303-
} else if (!GarbageCollected.class.isAssignableFrom(drClass)
304-
&& Creator.class.isAssignableFrom(drClass)) {
305-
return Optional.of(Mappers.fromDefaultAnnotations());
301+
@SuppressWarnings("unchecked")
302+
protected Optional<SecondaryToPrimaryMapper<R>> getSecondaryToPrimaryMapper() {
303+
if (this instanceof SecondaryToPrimaryMapper<?>) {
304+
return Optional.of((SecondaryToPrimaryMapper<R>) this);
306305
} else {
307-
return Optional.empty();
306+
var clustered = !Namespaced.class.isAssignableFrom(getPrimaryResourceType());
307+
if (garbageCollected) {
308+
return Optional.of(Mappers.fromOwnerReferences(getPrimaryResourceType(), clustered));
309+
} else if (isCreatable()) {
310+
return Optional.of(Mappers.fromDefaultAnnotations());
311+
}
308312
}
313+
return Optional.empty();
309314
}
310315
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ public class KubernetesDependentResourceConfig<R extends HasMetadata> {
1111

1212
private final Boolean useSSA;
1313
private final boolean createResourceOnlyIfNotExistingWithSSA;
14-
private final InformerConfiguration<R> spec;
14+
// here is informer builder instead of a config this has mostly technical related to Quarkus
15+
// extension
16+
private final InformerConfiguration.InformerConfigurationBuilder<R> informerConfigBuilder;
1517

1618
public KubernetesDependentResourceConfig(
1719
Boolean useSSA,
1820
boolean createResourceOnlyIfNotExistingWithSSA,
19-
InformerConfiguration<R> spec) {
21+
InformerConfiguration.InformerConfigurationBuilder<R> informerConfigBuilder) {
2022
this.useSSA = useSSA;
2123
this.createResourceOnlyIfNotExistingWithSSA = createResourceOnlyIfNotExistingWithSSA;
22-
this.spec = spec;
24+
this.informerConfigBuilder = informerConfigBuilder;
2325
}
2426

2527
public boolean createResourceOnlyIfNotExistingWithSSA() {
@@ -30,7 +32,7 @@ public Boolean useSSA() {
3032
return useSSA;
3133
}
3234

33-
public InformerConfiguration<R> informerConfiguration() {
34-
return spec;
35+
public InformerConfiguration.InformerConfigurationBuilder<R> informerConfigurationBuilder() {
36+
return informerConfigBuilder;
3537
}
3638
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public final class KubernetesDependentResourceConfigBuilder<R extends HasMetadat
88

99
private boolean createResourceOnlyIfNotExistingWithSSA;
1010
private Boolean useSSA = null;
11-
private InformerConfiguration<R> informerConfiguration;
11+
private InformerConfiguration.InformerConfigurationBuilder<R> informerConfigurationBuilder;
1212

1313
public KubernetesDependentResourceConfigBuilder() {}
1414

@@ -24,15 +24,15 @@ public KubernetesDependentResourceConfigBuilder<R> withUseSSA(boolean useSSA) {
2424
return this;
2525
}
2626

27-
public KubernetesDependentResourceConfigBuilder<R> withInformerConfiguration(
28-
InformerConfiguration<R> informerConfiguration) {
29-
this.informerConfiguration = informerConfiguration;
27+
public KubernetesDependentResourceConfigBuilder<R> withInformerConfigurationBuilder(
28+
InformerConfiguration.InformerConfigurationBuilder<R> informerConfigurationBuilder) {
29+
this.informerConfigurationBuilder = informerConfigurationBuilder;
3030
return this;
3131
}
3232

3333
public KubernetesDependentResourceConfig<R> build() {
3434
return new KubernetesDependentResourceConfig<>(
3535
useSSA, createResourceOnlyIfNotExistingWithSSA,
36-
informerConfiguration);
36+
informerConfigurationBuilder);
3737
}
3838
}

0 commit comments

Comments
 (0)