Skip to content

Commit df02564

Browse files
committed
refactor: move functionality out of ResourceConfiguration
Signed-off-by: Chris Laprun <claprun@redhat.com>
1 parent a6621c2 commit df02564

File tree

17 files changed

+247
-278
lines changed

17 files changed

+247
-278
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ public <P extends HasMetadata> RegisteredController<P> register(Reconciler<P> re
213213

214214
controllerManager.add(controller);
215215

216-
final var watchedNS = configuration.watchAllNamespaces() ? "[all namespaces]"
217-
: configuration.getEffectiveNamespaces();
216+
final var informerConfig = configuration.getInformerConfig();
217+
final var watchedNS = informerConfig.watchAllNamespaces() ? "[all namespaces]"
218+
: informerConfig.getEffectiveNamespaces(configuration);
218219

219220
log.info(
220221
"Registered reconciler: '{}' for resource: '{}' for namespace(s): {}",

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
@@ -86,7 +86,7 @@ default Class<P> getResourceClass() {
8686

8787
@SuppressWarnings("unused")
8888
default Set<String> getEffectiveNamespaces() {
89-
return ResourceConfiguration.super.getEffectiveNamespaces(this);
89+
return getInformerConfig().getEffectiveNamespaces(this);
9090
}
9191

9292
/**

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,8 @@ public class ControllerConfigurationOverrider<R extends HasMetadata> {
3535
private ControllerConfigurationOverrider(ControllerConfiguration<R> original) {
3636
this.finalizer = original.getFinalizerName();
3737
this.generationAware = original.isGenerationAware();
38-
this.config = InformerConfiguration.builder(original.getResourceClass())
39-
.withName(name)
40-
.withNamespaces(original.getNamespaces())
41-
.withLabelSelector(original.getLabelSelector())
42-
.withOnAddFilter(original.onAddFilter().orElse(null))
43-
.withOnUpdateFilter(original.onUpdateFilter().orElse(null))
44-
.withGenericFilter(original.genericFilter().orElse(null))
45-
.withInformerListLimit(original.getInformerListLimit().orElse(null))
46-
.withItemStore(original.getItemStore().orElse(null));
38+
final var informerConfig = original.getInformerConfig();
39+
this.config = InformerConfiguration.builder(informerConfig);
4740
this.retry = original.getRetry();
4841
this.reconciliationMaxInterval = original.maxReconciliationInterval().orElse(null);
4942
this.original = original;
Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
package io.javaoperatorsdk.operator.api.config;
22

3-
import java.util.Collection;
4-
import java.util.Collections;
5-
import java.util.Optional;
6-
import java.util.Set;
7-
import java.util.stream.Collectors;
83

94
import io.fabric8.kubernetes.api.model.HasMetadata;
10-
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
11-
import io.javaoperatorsdk.operator.OperatorException;
125
import io.javaoperatorsdk.operator.ReconcilerUtils;
136
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
14-
import io.javaoperatorsdk.operator.api.reconciler.Constants;
15-
import io.javaoperatorsdk.operator.processing.event.source.cache.BoundedItemStore;
16-
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
17-
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
18-
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
19-
20-
import static io.javaoperatorsdk.operator.api.reconciler.Constants.DEFAULT_NAMESPACES_SET;
21-
import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE_SET;
227

238
public interface ResourceConfiguration<R extends HasMetadata> {
249

@@ -28,134 +13,9 @@ default String getResourceTypeName() {
2813

2914
InformerConfiguration<R> getInformerConfig();
3015

31-
default Optional<OnAddFilter<? super R>> onAddFilter() {
32-
return Optional.ofNullable(getInformerConfig().getOnAddFilter());
33-
}
34-
35-
default Optional<OnUpdateFilter<? super R>> onUpdateFilter() {
36-
return Optional.ofNullable(getInformerConfig().getOnUpdateFilter());
37-
}
38-
39-
default Optional<GenericFilter<? super R>> genericFilter() {
40-
return Optional.ofNullable(getInformerConfig().getGenericFilter());
41-
}
42-
43-
/**
44-
* Retrieves the label selector that is used to filter which resources are actually watched by the
45-
* associated event source. See the official documentation on the
46-
* <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/">topic</a>
47-
* for more details on syntax.
48-
*
49-
* @return the label selector filtering watched resources
50-
*/
51-
default String getLabelSelector() {
52-
return getInformerConfig().getLabelSelector();
53-
}
54-
55-
static String ensureValidLabelSelector(String labelSelector) {
56-
// might want to implement validation here?
57-
return labelSelector;
58-
}
59-
6016
@SuppressWarnings("unchecked")
6117
default Class<R> getResourceClass() {
6218
return (Class<R>) Utils.getFirstTypeArgumentFromSuperClassOrInterface(getClass(),
6319
ResourceConfiguration.class);
6420
}
65-
66-
default Set<String> getNamespaces() {
67-
return getInformerConfig().getNamespaces();
68-
}
69-
70-
default boolean watchAllNamespaces() {
71-
return allNamespacesWatched(getNamespaces());
72-
}
73-
74-
static boolean allNamespacesWatched(Set<String> namespaces) {
75-
failIfNotValid(namespaces);
76-
return DEFAULT_NAMESPACES_SET.equals(namespaces);
77-
}
78-
79-
default boolean watchCurrentNamespace() {
80-
return currentNamespaceWatched(getNamespaces());
81-
}
82-
83-
static boolean currentNamespaceWatched(Set<String> namespaces) {
84-
failIfNotValid(namespaces);
85-
return WATCH_CURRENT_NAMESPACE_SET.equals(namespaces);
86-
}
87-
88-
static void failIfNotValid(Set<String> namespaces) {
89-
if (namespaces != null && !namespaces.isEmpty()) {
90-
final var present = namespaces.contains(Constants.WATCH_CURRENT_NAMESPACE)
91-
|| namespaces.contains(Constants.WATCH_ALL_NAMESPACES);
92-
if (!present || namespaces.size() == 1) {
93-
return;
94-
}
95-
}
96-
throw new IllegalArgumentException(
97-
"Must specify namespaces. To watch all namespaces, use only '"
98-
+ Constants.WATCH_ALL_NAMESPACES
99-
+ "'. To watch only the namespace in which the operator is deployed, use only '"
100-
+ Constants.WATCH_CURRENT_NAMESPACE + "'");
101-
}
102-
103-
static Set<String> ensureValidNamespaces(Collection<String> namespaces) {
104-
if (namespaces != null && !namespaces.isEmpty()) {
105-
return namespaces.stream().map(String::trim).collect(Collectors.toSet());
106-
} else {
107-
return Constants.DEFAULT_NAMESPACES_SET;
108-
}
109-
}
110-
111-
/**
112-
* Computes the effective namespaces based on the set specified by the user, in particular
113-
* retrieves the current namespace from the client when the user specified that they wanted to
114-
* watch the current namespace only.
115-
*
116-
* @return a Set of namespace names the associated controller will watch
117-
*/
118-
default Set<String> getEffectiveNamespaces(ControllerConfiguration<?> controllerConfiguration) {
119-
var targetNamespaces = getNamespaces();
120-
if (watchCurrentNamespace()) {
121-
final String namespace =
122-
controllerConfiguration.getConfigurationService().getKubernetesClient().getConfiguration()
123-
.getNamespace();
124-
if (namespace == null) {
125-
throw new OperatorException(
126-
"Couldn't retrieve the currently connected namespace. Make sure it's correctly set in your ~/.kube/config file, using, e.g. 'kubectl config set-context <your context> --namespace=<your namespace>'");
127-
}
128-
targetNamespaces = Collections.singleton(namespace);
129-
}
130-
return targetNamespaces;
131-
}
132-
133-
/**
134-
* Replaces the item store in informer. See underlying <a href=
135-
* "https://github.com/fabric8io/kubernetes-client/blob/43b67939fde91046ab7fb0c362f500c2b46eb59e/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/impl/DefaultSharedIndexInformer.java#L273">method</a>
136-
* in fabric8 client informer implementation.
137-
*
138-
* <p>
139-
* The main goal, is to be able to use limited caches or provide any custom implementation.
140-
* </p>
141-
*
142-
* <p>
143-
* See {@link BoundedItemStore} and <a href=
144-
* "https://github.com/operator-framework/java-operator-sdk/blob/main/caffeine-bounded-cache-support/src/main/java/io/javaoperatorsdk/operator/processing/event/source/cache/CaffeineBoundedCache.java">CaffeineBoundedCache</a>
145-
* </p>
146-
*
147-
* @return Optional {@link ItemStore} implementation. If present this item store will be used by
148-
* the informers.
149-
*/
150-
default Optional<ItemStore<R>> getItemStore() {
151-
return Optional.ofNullable(getInformerConfig().getItemStore());
152-
}
153-
154-
/**
155-
* The maximum amount of items to return for a single list call when starting an informer. If this
156-
* is a not null it will result in paginating for the initial load of the informer cache.
157-
*/
158-
default Optional<Long> getInformerListLimit() {
159-
return Optional.ofNullable(getInformerConfig().getInformerListLimit());
160-
}
16121
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
1414
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
1515

16-
import static io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration.DEFAULT_FOLLOW_CONTROLLER_NAMESPACES_ON_CHANGE;
16+
import static io.javaoperatorsdk.operator.api.reconciler.Constants.DEFAULT_FOLLOW_CONTROLLER_NAMESPACES_ON_CHANGE;
1717
import static io.javaoperatorsdk.operator.api.reconciler.Constants.NO_LONG_VALUE_SET;
1818
import static io.javaoperatorsdk.operator.api.reconciler.Constants.NO_VALUE_SET;
1919

0 commit comments

Comments
 (0)