-
Notifications
You must be signed in to change notification settings - Fork 218
feat: support for graceful shutdown based on configuration #2479
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
Merged
metacosm
merged 4 commits into
operator-framework:next
from
10000-ki:settingbased-gracefulshutdown
Aug 6, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,8 +101,8 @@ static ConfigurationService newOverriddenConfigurationService( | |
* | ||
* @param reconciler the reconciler we want the configuration of | ||
* @param <R> the {@code CustomResource} type associated with the specified reconciler | ||
* @return the {@link ControllerConfiguration} associated with the specified reconciler or {@code | ||
* null} if no configuration exists for the reconciler | ||
* @return the {@link ControllerConfiguration} associated with the specified reconciler or | ||
* {@code null} if no configuration exists for the reconciler | ||
*/ | ||
<R extends HasMetadata> ControllerConfiguration<R> getConfigurationFor(Reconciler<R> reconciler); | ||
|
||
|
@@ -211,7 +211,7 @@ default int concurrentWorkflowExecutorThreads() { | |
|
||
/** | ||
* Override to provide a custom {@link Metrics} implementation | ||
* | ||
* | ||
* @return the {@link Metrics} implementation | ||
*/ | ||
default Metrics getMetrics() { | ||
|
@@ -221,7 +221,7 @@ default Metrics getMetrics() { | |
/** | ||
* Override to provide a custom {@link ExecutorService} implementation to change how threads | ||
* handle concurrent reconciliations | ||
* | ||
* | ||
* @return the {@link ExecutorService} implementation to use for concurrent reconciliation | ||
* processing | ||
*/ | ||
|
@@ -232,7 +232,7 @@ default ExecutorService getExecutorService() { | |
/** | ||
* Override to provide a custom {@link ExecutorService} implementation to change how dependent | ||
* workflows are processed in parallel | ||
* | ||
* | ||
* @return the {@link ExecutorService} implementation to use for dependent workflow processing | ||
*/ | ||
default ExecutorService getWorkflowExecutorService() { | ||
|
@@ -242,7 +242,7 @@ default ExecutorService getWorkflowExecutorService() { | |
/** | ||
* Determines whether the associated Kubernetes client should be closed when the associated | ||
* {@link io.javaoperatorsdk.operator.Operator} is stopped. | ||
* | ||
* | ||
* @return {@code true} if the Kubernetes should be closed on stop, {@code false} otherwise | ||
*/ | ||
default boolean closeClientOnStop() { | ||
|
@@ -252,7 +252,7 @@ default boolean closeClientOnStop() { | |
/** | ||
* Override to provide a custom {@link DependentResourceFactory} implementation to change how | ||
* {@link io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource} are instantiated | ||
* | ||
* | ||
* @return the custom {@link DependentResourceFactory} implementation | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
|
@@ -264,7 +264,7 @@ default DependentResourceFactory dependentResourceFactory() { | |
* Retrieves the optional {@link LeaderElectionConfiguration} to specify how the associated | ||
* {@link io.javaoperatorsdk.operator.Operator} handles leader election to ensure only one | ||
* instance of the operator runs on the cluster at any given time | ||
* | ||
* | ||
* @return the {@link LeaderElectionConfiguration} | ||
*/ | ||
default Optional<LeaderElectionConfiguration> getLeaderElectionConfiguration() { | ||
|
@@ -299,6 +299,17 @@ default Duration cacheSyncTimeout() { | |
return Duration.ofMinutes(2); | ||
} | ||
|
||
/** | ||
* This is the timeout value that allows the reconciliation threads to gracefully shut down. If no | ||
* value is set, the default is immediate shutdown. | ||
* | ||
* @return The duration of time to wait before terminating the reconciliation threads | ||
* @since 5.0.0 | ||
*/ | ||
metacosm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default Duration reconciliationTerminationTimeout() { | ||
return Duration.ZERO; | ||
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. The default is immediate shutdown |
||
} | ||
|
||
/** | ||
* Handler for an informer stop. Informer stops if there is a non-recoverable error. Like received | ||
* a resource that cannot be deserialized. | ||
|
@@ -326,7 +337,7 @@ default Optional<InformerStoppedHandler> getInformerStoppedHandler() { | |
* Override to provide a custom {@link ManagedWorkflowFactory} implementation to change how | ||
* {@link io.javaoperatorsdk.operator.processing.dependent.workflow.ManagedWorkflow} are | ||
* instantiated | ||
* | ||
* | ||
* @return the custom {@link ManagedWorkflowFactory} implementation | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
|
@@ -336,7 +347,7 @@ default ManagedWorkflowFactory getWorkflowFactory() { | |
|
||
/** | ||
* Override to provide a custom {@link ExecutorServiceManager} implementation | ||
* | ||
* | ||
* @return the custom {@link ExecutorServiceManager} implementation | ||
*/ | ||
default ExecutorServiceManager getExecutorServiceManager() { | ||
|
@@ -353,9 +364,8 @@ default ExecutorServiceManager getExecutorServiceManager() { | |
* SSA based create/update can be still used with the legacy matching, just overriding the match | ||
* method of Kubernetes Dependent Resource. | ||
* | ||
* @since 4.4.0 | ||
* | ||
* @return if SSA should be used for dependent resources | ||
* @since 4.4.0 | ||
*/ | ||
default boolean ssaBasedCreateUpdateMatchForDependentResources() { | ||
return true; | ||
|
@@ -383,9 +393,8 @@ default Set<Class<? extends HasMetadata>> defaultNonSSAResource() { | |
* <p> | ||
* Disable this if you want to react to your own dependent resource updates | ||
* | ||
* @since 4.5.0 | ||
* | ||
* @return if special annotation should be used for dependent resource to filter events | ||
* @since 4.5.0 | ||
*/ | ||
default boolean previousAnnotationForDependentResourcesEventFiltering() { | ||
return true; | ||
|
@@ -400,9 +409,8 @@ default boolean previousAnnotationForDependentResourcesEventFiltering() { | |
* logic, and you want to further minimize the amount of work done / updates issued by the | ||
* operator. | ||
* | ||
* @since 4.5.0 | ||
* | ||
* @return if resource version should be parsed (as integer) | ||
* @since 4.5.0 | ||
*/ | ||
default boolean parseResourceVersionsForEventFilteringAndCaching() { | ||
return false; | ||
|
@@ -415,8 +423,8 @@ default boolean parseResourceVersionsForEventFilteringAndCaching() { | |
* | ||
* @return {@code true} if Server-Side Apply (SSA) should be used when patching the primary | ||
* resources, {@code false} otherwise | ||
* @since 5.0.0 | ||
* @see ConfigurationServiceOverrider#withUseSSAToPatchPrimaryResource(boolean) | ||
* @since 5.0.0 | ||
*/ | ||
default boolean useSSAToPatchPrimaryResource() { | ||
return true; | ||
|
@@ -427,18 +435,17 @@ default boolean useSSAToPatchPrimaryResource() { | |
* Determines whether resources retrieved from caches such as via calls to | ||
* {@link Context#getSecondaryResource(Class)} should be defensively cloned first. | ||
* </p> | ||
* | ||
* | ||
* <p> | ||
* Defensive cloning to prevent problematic cache modifications (modifying the resource would | ||
* otherwise modify the stored copy in the cache) was transparently done in previous JOSDK | ||
* versions. This might have performance consequences and, with the more prevalent use of | ||
* Server-Side Apply, where you should create a new copy of your resource with only modified | ||
* fields, such modifications of these resources are less likely to occur. | ||
* </p> | ||
* | ||
* | ||
* @return {@code true} if resources should be defensively cloned before returning them from | ||
* caches, {@code false} otherwise | ||
* | ||
* @since 5.0.0 | ||
*/ | ||
default boolean cloneSecondaryResourcesWhenGettingFromCache() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,26 +18,21 @@ | |
public class GracefulStopIT { | ||
|
||
public static final String TEST_1 = "test1"; | ||
public static final String TEST_2 = "test2"; | ||
|
||
@RegisterExtension | ||
LocallyRunOperatorExtension operator = | ||
LocallyRunOperatorExtension.builder() | ||
.withConfigurationService(o -> o.withCloseClientOnStop(false)) | ||
.withConfigurationService(o -> o.withCloseClientOnStop(false) | ||
.withReconciliationTerminationTimeout(Duration.ofMillis(RECONCILER_SLEEP))) | ||
.withReconciler(new GracefulStopTestReconciler()) | ||
.build(); | ||
|
||
@Test | ||
void stopsGracefullyWIthTimeout() { | ||
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 also corrected a typo in the method name while I was at it. |
||
testGracefulStop(TEST_1, RECONCILER_SLEEP, 2); | ||
void stopsGracefullyWithTimeoutConfiguration() { | ||
testGracefulStop(TEST_1, 2); | ||
} | ||
|
||
@Test | ||
void stopsGracefullyWithExpiredTimeout() { | ||
testGracefulStop(TEST_2, RECONCILER_SLEEP / 5, 1); | ||
} | ||
|
||
private void testGracefulStop(String resourceName, int stopTimeout, int expectedFinalGeneration) { | ||
private void testGracefulStop(String resourceName, int expectedFinalGeneration) { | ||
var testRes = operator.create(testResource(resourceName)); | ||
await().untilAsserted(() -> { | ||
var r = operator.get(GracefulStopTestCustomResource.class, resourceName); | ||
|
@@ -54,7 +49,7 @@ private void testGracefulStop(String resourceName, int stopTimeout, int expected | |
() -> assertThat(operator.getReconcilerOfType(GracefulStopTestReconciler.class) | ||
.getNumberOfExecutions()).isEqualTo(2)); | ||
|
||
operator.getOperator().stop(Duration.ofMillis(stopTimeout)); | ||
operator.getOperator().stop(); | ||
|
||
await().untilAsserted(() -> { | ||
var r = operator.get(GracefulStopTestCustomResource.class, resourceName); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, it is beneficial for users to be aware of this feature, so I have documented it.
However, in most users' cases, the graceful shutdown feature may not be necessary.
If you think this documentation is unnecessary, please let me know and I will remove it.