11package io .javaoperatorsdk .operator .api .config .informer ;
22
3- import java .util .Collections ;
43import java .util .Objects ;
54import java .util .Set ;
65
76import io .fabric8 .kubernetes .api .model .HasMetadata ;
87import io .javaoperatorsdk .operator .api .config .DefaultResourceConfiguration ;
98import io .javaoperatorsdk .operator .api .config .ResourceConfiguration ;
9+ import io .javaoperatorsdk .operator .api .reconciler .EventSourceContext ;
1010import io .javaoperatorsdk .operator .processing .event .source .SecondaryToPrimaryMapper ;
1111import io .javaoperatorsdk .operator .processing .event .source .informer .Mappers ;
1212
13- @ SuppressWarnings ("rawtypes" )
1413public interface InformerConfiguration <R extends HasMetadata >
1514 extends ResourceConfiguration <R > {
1615
1716 class DefaultInformerConfiguration <R extends HasMetadata > extends
1817 DefaultResourceConfiguration <R > implements InformerConfiguration <R > {
1918
2019 private final SecondaryToPrimaryMapper <R > secondaryToPrimaryMapper ;
20+ private final boolean followControllerNamespaceChanges ;
2121
2222 protected DefaultInformerConfiguration (String labelSelector ,
2323 Class <R > resourceClass ,
2424 SecondaryToPrimaryMapper <R > secondaryToPrimaryMapper ,
25- Set <String > namespaces ) {
25+ Set <String > namespaces , boolean followControllerNamespaceChanges ) {
2626 super (labelSelector , resourceClass , namespaces );
27+ this .followControllerNamespaceChanges = followControllerNamespaceChanges ;
2728 this .secondaryToPrimaryMapper =
2829 Objects .requireNonNullElse (secondaryToPrimaryMapper ,
2930 Mappers .fromOwnerReference ());
3031 }
3132
33+ public boolean followControllerNamespaceChanges () {
34+ return followControllerNamespaceChanges ;
35+ }
3236
3337 public SecondaryToPrimaryMapper <R > getSecondaryToPrimaryMapper () {
3438 return secondaryToPrimaryMapper ;
3539 }
3640
3741 }
3842
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+
3951 SecondaryToPrimaryMapper <R > getSecondaryToPrimaryMapper ();
4052
4153 @ SuppressWarnings ("unused" )
@@ -45,6 +57,7 @@ class InformerConfigurationBuilder<R extends HasMetadata> {
4557 private Set <String > namespaces ;
4658 private String labelSelector ;
4759 private final Class <R > resourceClass ;
60+ private boolean inheritControllerNamespacesOnChange = false ;
4861
4962 private InformerConfigurationBuilder (Class <R > resourceClass ) {
5063 this .resourceClass = resourceClass ;
@@ -57,15 +70,61 @@ public InformerConfigurationBuilder<R> withSecondaryToPrimaryMapper(
5770 }
5871
5972 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 ) ;
6275 }
6376
6477 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 ;
66113 return this ;
67114 }
68115
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+ }
69128
70129 public InformerConfigurationBuilder <R > withLabelSelector (String labelSelector ) {
71130 this .labelSelector = labelSelector ;
@@ -75,7 +134,7 @@ public InformerConfigurationBuilder<R> withLabelSelector(String labelSelector) {
75134 public InformerConfiguration <R > build () {
76135 return new DefaultInformerConfiguration <>(labelSelector , resourceClass ,
77136 secondaryToPrimaryMapper ,
78- namespaces );
137+ namespaces , inheritControllerNamespacesOnChange );
79138 }
80139 }
81140
@@ -84,12 +143,19 @@ static <R extends HasMetadata> InformerConfigurationBuilder<R> from(
84143 return new InformerConfigurationBuilder <>(resourceClass );
85144 }
86145
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+ */
88155 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 );
94159 }
160+
95161}
0 commit comments