-
Notifications
You must be signed in to change notification settings - Fork 220
dynamic namespace config #1187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamic namespace config #1187
Changes from all commits
b8b9e2f
97332bf
a6a0b44
96703be
91f9699
5aeaccd
facfadf
a8df89b
55b8fc7
58d2b0b
6cee3c5
6f2e55f
59c3191
00ec65d
0ae3aab
3022ac4
a38c354
99aa13b
4370755
534274f
9b95037
8b4bfa5
abc4da2
4aed6fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import io.javaoperatorsdk.operator.api.config.NamespaceChangeable; | ||
|
||
public interface RegisteredController extends NamespaceChangeable { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.javaoperatorsdk.operator.api.config; | ||
|
||
import java.util.Set; | ||
|
||
public interface NamespaceChangeable { | ||
|
||
/** | ||
* If the controller and possibly registered | ||
* {@link io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource} | ||
* watches a set of namespaces this set can be adjusted dynamically, this when the operator is | ||
* running. | ||
* | ||
* @param namespaces target namespaces to watch | ||
*/ | ||
void changeNamespaces(Set<String> namespaces); | ||
|
||
default void changeNamespaces(String... namespaces) { | ||
changeNamespaces( | ||
namespaces != null ? Set.of(namespaces) : ResourceConfiguration.DEFAULT_NAMESPACES); | ||
} | ||
|
||
default boolean allowsNamespaceChanges() { | ||
return true; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,53 @@ | ||
package io.javaoperatorsdk.operator.api.config.informer; | ||
|
||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.api.config.DefaultResourceConfiguration; | ||
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext; | ||
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public interface InformerConfiguration<R extends HasMetadata> | ||
extends ResourceConfiguration<R> { | ||
|
||
class DefaultInformerConfiguration<R extends HasMetadata> extends | ||
DefaultResourceConfiguration<R> implements InformerConfiguration<R> { | ||
|
||
private final SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper; | ||
private final boolean followControllerNamespaceChanges; | ||
|
||
protected DefaultInformerConfiguration(String labelSelector, | ||
Class<R> resourceClass, | ||
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper, | ||
Set<String> namespaces) { | ||
Set<String> namespaces, boolean followControllerNamespaceChanges) { | ||
metacosm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super(labelSelector, resourceClass, namespaces); | ||
this.followControllerNamespaceChanges = followControllerNamespaceChanges; | ||
this.secondaryToPrimaryMapper = | ||
Objects.requireNonNullElse(secondaryToPrimaryMapper, | ||
Mappers.fromOwnerReference()); | ||
} | ||
|
||
public boolean followControllerNamespaceChanges() { | ||
return followControllerNamespaceChanges; | ||
} | ||
|
||
public SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper() { | ||
return secondaryToPrimaryMapper; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Used in case the watched namespaces are changed dynamically, thus when operator is running (See | ||
* {@link io.javaoperatorsdk.operator.RegisteredController}). If true, changing the target | ||
* namespaces of a controller would result to change target namespaces for the | ||
* InformerEventSource. | ||
*/ | ||
boolean followControllerNamespaceChanges(); | ||
|
||
SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper(); | ||
|
||
@SuppressWarnings("unused") | ||
|
@@ -45,6 +57,7 @@ class InformerConfigurationBuilder<R extends HasMetadata> { | |
private Set<String> namespaces; | ||
private String labelSelector; | ||
private final Class<R> resourceClass; | ||
private boolean inheritControllerNamespacesOnChange = false; | ||
|
||
private InformerConfigurationBuilder(Class<R> resourceClass) { | ||
this.resourceClass = resourceClass; | ||
|
@@ -57,15 +70,61 @@ public InformerConfigurationBuilder<R> withSecondaryToPrimaryMapper( | |
} | ||
|
||
public InformerConfigurationBuilder<R> withNamespaces(String... namespaces) { | ||
this.namespaces = namespaces != null ? Set.of(namespaces) : Collections.emptySet(); | ||
return this; | ||
return withNamespaces( | ||
namespaces != null ? Set.of(namespaces) : ResourceConfiguration.DEFAULT_NAMESPACES); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Since it is already defined as: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used I do agree that it's a little confusing but I think it's better to keep this semantics to make it explicit that the default value is used at these spots. This way, the code using the value should also not change if the default value were to change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that makes sense, I just I think found some places where it was not used, just the value directly, will rather then create PR for that if I find it again (can't remember where it was) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will take a look There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's possible, unfortunately, ran into a couple of such occurrences myself as well and tried to fix them when I could. |
||
} | ||
|
||
public InformerConfigurationBuilder<R> withNamespaces(Set<String> namespaces) { | ||
this.namespaces = namespaces != null ? namespaces : Collections.emptySet(); | ||
return withNamespaces(namespaces, false); | ||
} | ||
|
||
/** | ||
* Sets the initial set of namespaces to watch (typically extracted from the parent | ||
* {@link io.javaoperatorsdk.operator.processing.Controller}'s configuration), specifying | ||
* whether changes made to the parent controller configured namespaces should be tracked or not. | ||
* | ||
* @param namespaces the initial set of namespaces to watch | ||
* @param followChanges {@code true} to follow the changes made to the parent controller | ||
* namespaces, {@code false} otherwise | ||
* @return the builder instance so that calls can be chained fluently | ||
*/ | ||
public InformerConfigurationBuilder<R> withNamespaces(Set<String> namespaces, | ||
boolean followChanges) { | ||
this.namespaces = namespaces != null ? namespaces : ResourceConfiguration.DEFAULT_NAMESPACES; | ||
this.inheritControllerNamespacesOnChange = true; | ||
return this; | ||
} | ||
|
||
/** | ||
* Configures the informer to watch and track the same namespaces as the parent | ||
* {@link io.javaoperatorsdk.operator.processing.Controller}, meaning that the informer will be | ||
* restarted to watch the new namespaces if the parent controller's namespace configuration | ||
* changes. | ||
* | ||
* @param context {@link EventSourceContext} from which the parent | ||
* {@link io.javaoperatorsdk.operator.processing.Controller}'s configuration is retrieved | ||
* @param <P> the primary resource type associated with the parent controller | ||
* @return the builder instance so that calls can be chained fluently | ||
*/ | ||
public <P extends HasMetadata> InformerConfigurationBuilder<R> withNamespacesInheritedFromController( | ||
metacosm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
EventSourceContext<P> context) { | ||
namespaces = context.getControllerConfiguration().getEffectiveNamespaces(); | ||
this.inheritControllerNamespacesOnChange = true; | ||
return this; | ||
} | ||
|
||
/** | ||
* Whether or not the associated informer should track changes made to the parent | ||
* {@link io.javaoperatorsdk.operator.processing.Controller}'s namespaces configuration. | ||
* | ||
* @param followChanges {@code true} to reconfigure the associated informer when the parent | ||
* controller's namespaces are reconfigured, {@code false} otherwise | ||
* @return the builder instance so that calls can be chained fluently | ||
*/ | ||
public InformerConfigurationBuilder<R> followNamespaceChanges(boolean followChanges) { | ||
this.inheritControllerNamespacesOnChange = followChanges; | ||
return this; | ||
} | ||
|
||
public InformerConfigurationBuilder<R> withLabelSelector(String labelSelector) { | ||
this.labelSelector = labelSelector; | ||
|
@@ -75,7 +134,7 @@ public InformerConfigurationBuilder<R> withLabelSelector(String labelSelector) { | |
public InformerConfiguration<R> build() { | ||
return new DefaultInformerConfiguration<>(labelSelector, resourceClass, | ||
secondaryToPrimaryMapper, | ||
namespaces); | ||
namespaces, inheritControllerNamespacesOnChange); | ||
} | ||
} | ||
|
||
|
@@ -84,12 +143,19 @@ static <R extends HasMetadata> InformerConfigurationBuilder<R> from( | |
return new InformerConfigurationBuilder<>(resourceClass); | ||
} | ||
|
||
|
||
/** | ||
* Creates a configuration builder that inherits namespaces from the controller and follows | ||
* namespaces changes. | ||
* | ||
* @param resourceClass secondary resource class | ||
* @param eventSourceContext of the initializer | ||
* @return builder | ||
* @param <R> secondary resource type | ||
*/ | ||
static <R extends HasMetadata> InformerConfigurationBuilder<R> from( | ||
InformerConfiguration<R> configuration) { | ||
return new InformerConfigurationBuilder<R>(configuration.getResourceClass()) | ||
.withNamespaces(configuration.getNamespaces()) | ||
.withLabelSelector(configuration.getLabelSelector()) | ||
.withSecondaryToPrimaryMapper(configuration.getSecondaryToPrimaryMapper()); | ||
Class<R> resourceClass, EventSourceContext<?> eventSourceContext) { | ||
return new InformerConfigurationBuilder<>(resourceClass) | ||
.withNamespacesInheritedFromController(eventSourceContext); | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.