1
1
package io .javaoperatorsdk .operator .api .config ;
2
2
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 ;
8
3
9
4
import io .fabric8 .kubernetes .api .model .HasMetadata ;
10
- import io .fabric8 .kubernetes .client .informers .cache .ItemStore ;
11
- import io .javaoperatorsdk .operator .OperatorException ;
12
5
import io .javaoperatorsdk .operator .ReconcilerUtils ;
13
6
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 ;
22
7
23
8
public interface ResourceConfiguration <R extends HasMetadata > {
24
9
@@ -28,134 +13,9 @@ default String getResourceTypeName() {
28
13
29
14
InformerConfiguration <R > getInformerConfig ();
30
15
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
-
60
16
@ SuppressWarnings ("unchecked" )
61
17
default Class <R > getResourceClass () {
62
18
return (Class <R >) Utils .getFirstTypeArgumentFromSuperClassOrInterface (getClass (),
63
19
ResourceConfiguration .class );
64
20
}
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
- }
161
21
}
0 commit comments