Skip to content

Commit 0ae3aab

Browse files
metacosmcsviri
authored andcommitted
refactor: follow existing pattern to make method names more discoverable
1 parent 00ec65d commit 0ae3aab

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
public interface ResourceConfiguration<R extends HasMetadata> {
1212

13-
Set<String> DEFAULT_NAMESPACES = Set.of(Constants.WATCH_ALL_NAMESPACES);
14-
Set<String> CURRENT_NAMESPACE_ONLY = Set.of(Constants.WATCH_CURRENT_NAMESPACE);
13+
Set<String> DEFAULT_NAMESPACES = Collections.singleton(Constants.WATCH_ALL_NAMESPACES);
14+
Set<String> CURRENT_NAMESPACE_ONLY = Collections.singleton(Constants.WATCH_CURRENT_NAMESPACE);
1515

1616
default String getResourceTypeName() {
1717
return ReconcilerUtils.getResourceTypeName(getResourceClass());

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

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.javaoperatorsdk.operator.api.config.informer;
22

3-
import java.util.Collections;
43
import java.util.Objects;
54
import java.util.Set;
65

@@ -11,7 +10,6 @@
1110
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
1211
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;
1312

14-
@SuppressWarnings("rawtypes")
1513
public interface InformerConfiguration<R extends HasMetadata>
1614
extends ResourceConfiguration<R> {
1715

@@ -72,34 +70,67 @@ public InformerConfigurationBuilder<R> withSecondaryToPrimaryMapper(
7270
}
7371

7472
public InformerConfigurationBuilder<R> withNamespaces(String... namespaces) {
75-
this.namespaces = namespaces != null ? Set.of(namespaces) : Collections.emptySet();
76-
return this;
73+
return withNamespaces(
74+
namespaces != null ? Set.of(namespaces) : ResourceConfiguration.DEFAULT_NAMESPACES);
7775
}
7876

7977
public InformerConfigurationBuilder<R> withNamespaces(Set<String> namespaces) {
80-
this.namespaces = namespaces != null ? namespaces : Collections.emptySet();
81-
return this;
82-
}
83-
84-
public InformerConfigurationBuilder<R> withLabelSelector(String labelSelector) {
85-
this.labelSelector = labelSelector;
86-
return this;
78+
return withNamespaces(namespaces, false);
8779
}
8880

89-
public <P extends HasMetadata> InformerConfigurationBuilder<R> setAndFollowControllerNamespaceChanges(
90-
Set<String> namespaces) {
91-
this.namespaces = namespaces;
81+
/**
82+
* Sets the initial set of namespaces to watch (typically extracted from the parent
83+
* {@link io.javaoperatorsdk.operator.processing.Controller}'s configuration), specifying
84+
* whether changes made to the parent controller configured namespaces should be tracked or not.
85+
*
86+
* @param namespaces the initial set of namespaces to watch
87+
* @param followChanges {@code true} to follow the changes made to the parent controller
88+
* namespaces, {@code false} otherwise
89+
* @return the builder instance so that calls can be chained fluently
90+
*/
91+
public InformerConfigurationBuilder<R> withNamespaces(Set<String> namespaces,
92+
boolean followChanges) {
93+
this.namespaces = namespaces != null ? namespaces : ResourceConfiguration.DEFAULT_NAMESPACES;
9294
this.inheritControllerNamespacesOnChange = true;
9395
return this;
9496
}
9597

96-
public <P extends HasMetadata> InformerConfigurationBuilder<R> setAndFollowControllerNamespaceChanges(
98+
/**
99+
* Configures the informer to watch and track the same namespaces as the parent
100+
* {@link io.javaoperatorsdk.operator.processing.Controller}, meaning that the informer will be
101+
* restarted to watch the new namespaces if the parent controller's namespace configuration
102+
* changes.
103+
*
104+
* @param context {@link EventSourceContext} from which the parent
105+
* {@link io.javaoperatorsdk.operator.processing.Controller}'s configuration is retrieved
106+
* @param <P> the primary resource type associated with the parent controller
107+
* @return the builder instance so that calls can be chained fluently
108+
*/
109+
public <P extends HasMetadata> InformerConfigurationBuilder<R> withNamespacesInheritedFromController(
97110
EventSourceContext<P> context) {
98111
namespaces = context.getControllerConfiguration().getEffectiveNamespaces();
99112
this.inheritControllerNamespacesOnChange = true;
100113
return this;
101114
}
102115

116+
/**
117+
* Whether or not the associated informer should track changes made to the parent
118+
* {@link io.javaoperatorsdk.operator.processing.Controller}'s namespaces configuration.
119+
*
120+
* @param followChanges {@code true} to reconfigure the associated informer when the parent
121+
* controller's namespaces are reconfigured, {@code false} otherwise
122+
* @return the builder instance so that calls can be chained fluently
123+
*/
124+
public InformerConfigurationBuilder<R> followNamespaceChanges(boolean followChanges) {
125+
this.inheritControllerNamespacesOnChange = followChanges;
126+
return this;
127+
}
128+
129+
public InformerConfigurationBuilder<R> withLabelSelector(String labelSelector) {
130+
this.labelSelector = labelSelector;
131+
return this;
132+
}
133+
103134
public InformerConfiguration<R> build() {
104135
return new DefaultInformerConfiguration<>(labelSelector, resourceClass,
105136
secondaryToPrimaryMapper,

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,13 @@ private void configureWith(String labelSelector, Set<String> namespaces,
5858
final SecondaryToPrimaryMapper<R> primaryResourcesRetriever =
5959
(this instanceof SecondaryToPrimaryMapper) ? (SecondaryToPrimaryMapper<R>) this
6060
: Mappers.fromOwnerReference();
61-
InformerConfiguration.InformerConfigurationBuilder<R> ic =
62-
InformerConfiguration.from(resourceType())
63-
.withLabelSelector(labelSelector)
64-
.withSecondaryToPrimaryMapper(primaryResourcesRetriever);
65-
if (inheritNamespacesOnChange) {
66-
ic.setAndFollowControllerNamespaceChanges(namespaces);
67-
} else {
68-
ic.withNamespaces(namespaces);
69-
}
61+
var ic = InformerConfiguration.from(resourceType())
62+
.withLabelSelector(labelSelector)
63+
.withSecondaryToPrimaryMapper(primaryResourcesRetriever)
64+
.withNamespaces(namespaces, inheritNamespacesOnChange)
65+
.build();
7066

71-
configureWith(new InformerEventSource<>(ic.build(), client));
67+
configureWith(new InformerEventSource<>(ic, client));
7268
}
7369

7470
/**
@@ -83,11 +79,9 @@ public void configureWith(InformerEventSource<R, P> informerEventSource) {
8379

8480
protected R handleCreate(R desired, P primary, Context<P> context) {
8581
ResourceID resourceID = ResourceID.fromResource(desired);
86-
R created = null;
8782
try {
8883
prepareEventFiltering(desired, resourceID);
89-
created = super.handleCreate(desired, primary, context);
90-
return created;
84+
return super.handleCreate(desired, primary, context);
9185
} catch (RuntimeException e) {
9286
cleanupAfterEventFiltering(resourceID);
9387
throw e;
@@ -96,11 +90,9 @@ protected R handleCreate(R desired, P primary, Context<P> context) {
9690

9791
protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
9892
ResourceID resourceID = ResourceID.fromResource(desired);
99-
R updated = null;
10093
try {
10194
prepareEventFiltering(desired, resourceID);
102-
updated = super.handleUpdate(actual, desired, primary, context);
103-
return updated;
95+
return super.handleUpdate(actual, desired, primary, context);
10496
} catch (RuntimeException e) {
10597
cleanupAfterEventFiltering(resourceID);
10698
throw e;

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/changenamespace/ChangeNamespaceTestReconciler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Map<String, EventSource> prepareEventSources(
2727

2828
InformerEventSource<ConfigMap, ChangeNamespaceTestCustomResource> configMapES =
2929
new InformerEventSource<>(InformerConfiguration.from(ConfigMap.class)
30-
.setAndFollowControllerNamespaceChanges(context)
30+
.withNamespacesInheritedFromController(context)
3131
.build(), context);
3232

3333
return EventSourceInitializer.nameEventSources(configMapES);

0 commit comments

Comments
 (0)