1
1
package io .javaoperatorsdk .operator .api .config .informer ;
2
2
3
- import java .util .Collections ;
4
3
import java .util .Objects ;
5
4
import java .util .Set ;
6
5
7
6
import io .fabric8 .kubernetes .api .model .HasMetadata ;
8
7
import io .javaoperatorsdk .operator .api .config .DefaultResourceConfiguration ;
9
8
import io .javaoperatorsdk .operator .api .config .ResourceConfiguration ;
9
+ import io .javaoperatorsdk .operator .api .reconciler .EventSourceContext ;
10
10
import io .javaoperatorsdk .operator .processing .event .source .SecondaryToPrimaryMapper ;
11
11
import io .javaoperatorsdk .operator .processing .event .source .informer .Mappers ;
12
12
13
- @ SuppressWarnings ("rawtypes" )
14
13
public interface InformerConfiguration <R extends HasMetadata >
15
14
extends ResourceConfiguration <R > {
16
15
17
16
class DefaultInformerConfiguration <R extends HasMetadata > extends
18
17
DefaultResourceConfiguration <R > implements InformerConfiguration <R > {
19
18
20
19
private final SecondaryToPrimaryMapper <R > secondaryToPrimaryMapper ;
20
+ private final boolean followControllerNamespaceChanges ;
21
21
22
22
protected DefaultInformerConfiguration (String labelSelector ,
23
23
Class <R > resourceClass ,
24
24
SecondaryToPrimaryMapper <R > secondaryToPrimaryMapper ,
25
- Set <String > namespaces ) {
25
+ Set <String > namespaces , boolean followControllerNamespaceChanges ) {
26
26
super (labelSelector , resourceClass , namespaces );
27
+ this .followControllerNamespaceChanges = followControllerNamespaceChanges ;
27
28
this .secondaryToPrimaryMapper =
28
29
Objects .requireNonNullElse (secondaryToPrimaryMapper ,
29
30
Mappers .fromOwnerReference ());
30
31
}
31
32
33
+ public boolean followControllerNamespaceChanges () {
34
+ return followControllerNamespaceChanges ;
35
+ }
32
36
33
37
public SecondaryToPrimaryMapper <R > getSecondaryToPrimaryMapper () {
34
38
return secondaryToPrimaryMapper ;
35
39
}
36
40
37
41
}
38
42
43
+ /**
44
+ * Used in case the watched namespaces are changed dynamically, thus when operator is running (See
45
+ * {@link io.javaoperatorsdk.operator.RegisteredController}). If true, changing the target
46
+ * namespaces of a controller would result to change target namespaces for the
47
+ * InformerEventSource.
48
+ */
49
+ boolean followControllerNamespaceChanges ();
50
+
39
51
SecondaryToPrimaryMapper <R > getSecondaryToPrimaryMapper ();
40
52
41
53
@ SuppressWarnings ("unused" )
@@ -45,6 +57,7 @@ class InformerConfigurationBuilder<R extends HasMetadata> {
45
57
private Set <String > namespaces ;
46
58
private String labelSelector ;
47
59
private final Class <R > resourceClass ;
60
+ private boolean inheritControllerNamespacesOnChange = false ;
48
61
49
62
private InformerConfigurationBuilder (Class <R > resourceClass ) {
50
63
this .resourceClass = resourceClass ;
@@ -57,15 +70,61 @@ public InformerConfigurationBuilder<R> withSecondaryToPrimaryMapper(
57
70
}
58
71
59
72
public InformerConfigurationBuilder <R > withNamespaces (String ... namespaces ) {
60
- this . namespaces = namespaces != null ? Set . of ( namespaces ) : Collections . emptySet ();
61
- return this ;
73
+ return withNamespaces (
74
+ namespaces != null ? Set . of ( namespaces ) : ResourceConfiguration . DEFAULT_NAMESPACES ) ;
62
75
}
63
76
64
77
public InformerConfigurationBuilder <R > withNamespaces (Set <String > namespaces ) {
65
- this .namespaces = namespaces != null ? namespaces : Collections .emptySet ();
78
+ return withNamespaces (namespaces , false );
79
+ }
80
+
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 ;
94
+ this .inheritControllerNamespacesOnChange = true ;
95
+ return this ;
96
+ }
97
+
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 (
110
+ EventSourceContext <P > context ) {
111
+ namespaces = context .getControllerConfiguration ().getEffectiveNamespaces ();
112
+ this .inheritControllerNamespacesOnChange = true ;
66
113
return this ;
67
114
}
68
115
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
+ }
69
128
70
129
public InformerConfigurationBuilder <R > withLabelSelector (String labelSelector ) {
71
130
this .labelSelector = labelSelector ;
@@ -75,7 +134,7 @@ public InformerConfigurationBuilder<R> withLabelSelector(String labelSelector) {
75
134
public InformerConfiguration <R > build () {
76
135
return new DefaultInformerConfiguration <>(labelSelector , resourceClass ,
77
136
secondaryToPrimaryMapper ,
78
- namespaces );
137
+ namespaces , inheritControllerNamespacesOnChange );
79
138
}
80
139
}
81
140
@@ -84,12 +143,19 @@ static <R extends HasMetadata> InformerConfigurationBuilder<R> from(
84
143
return new InformerConfigurationBuilder <>(resourceClass );
85
144
}
86
145
87
-
146
+ /**
147
+ * Creates a configuration builder that inherits namespaces from the controller and follows
148
+ * namespaces changes.
149
+ *
150
+ * @param resourceClass secondary resource class
151
+ * @param eventSourceContext of the initializer
152
+ * @return builder
153
+ * @param <R> secondary resource type
154
+ */
88
155
static <R extends HasMetadata > InformerConfigurationBuilder <R > from (
89
- InformerConfiguration <R > configuration ) {
90
- return new InformerConfigurationBuilder <R >(configuration .getResourceClass ())
91
- .withNamespaces (configuration .getNamespaces ())
92
- .withLabelSelector (configuration .getLabelSelector ())
93
- .withSecondaryToPrimaryMapper (configuration .getSecondaryToPrimaryMapper ());
156
+ Class <R > resourceClass , EventSourceContext <?> eventSourceContext ) {
157
+ return new InformerConfigurationBuilder <>(resourceClass )
158
+ .withNamespacesInheritedFromController (eventSourceContext );
94
159
}
160
+
95
161
}
0 commit comments