Skip to content

fix: default stop handler exists only if informer started #1575

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
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ default Duration cacheSyncTimeout() {
*/
default Optional<InformerStoppedHandler> getInformerStoppedHandler() {
return Optional.of((informer, ex) -> {
if (ex != null) {
// hasSynced is checked to verify that informer already started. If not started, in case
// of a fatal error the operator will stop, no need for explicit exit.
if (ex != null && informer.hasSynced()) {
Logger log = LoggerFactory.getLogger(ConfigurationService.class);
log.error("Fatal error in informer: {}. Stopping the operator", informer, ex);
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class InformerRelatedBehaviorITS {
KubernetesClient adminClient = new KubernetesClientBuilder().build();
InformerRelatedBehaviorTestReconciler reconciler;
String actualNamespace;
volatile boolean stopHandlerCalled = false;
volatile boolean replacementStopHandlerCalled = false;

@BeforeEach
void beforeEach(TestInfo testInfo) {
Expand Down Expand Up @@ -136,7 +136,19 @@ void callsStopHandlerOnStartupFail() {

assertThrows(OperatorException.class, () -> startOperator(true));

await().untilAsserted(() -> assertThat(stopHandlerCalled).isTrue());
await().untilAsserted(() -> assertThat(replacementStopHandlerCalled).isTrue());
}

@Test
void notExitingWithDefaultStopHandlerIfErrorHappensOnStartup() {
setNoCustomResourceAccess();
adminClient.resource(testCustomResource()).createOrReplace();

assertThrows(OperatorException.class, () -> startOperator(true, false));

// note that we just basically check here that the default handler does not call system exit.
// Thus, the test does not terminate before to assert.
await().untilAsserted(() -> assertThat(replacementStopHandlerCalled).isFalse());
}

private static void waitForWatchReconnect() {
Expand Down Expand Up @@ -183,16 +195,20 @@ KubernetesClient clientUsingServiceAccount() {
}

Operator startOperator(boolean stopOnInformerErrorDuringStartup) {
return startOperator(stopOnInformerErrorDuringStartup, true);
}

Operator startOperator(boolean stopOnInformerErrorDuringStartup, boolean addStopHandler) {
ConfigurationServiceProvider.reset();
reconciler = new InformerRelatedBehaviorTestReconciler();

Operator operator = new Operator(clientUsingServiceAccount(),
co -> {
co.withStopOnInformerErrorDuringStartup(stopOnInformerErrorDuringStartup);
co.withCacheSyncTimeout(Duration.ofMillis(3000));
co.withInformerStoppedHandler((informer, ex) -> {
stopHandlerCalled = true;
});
if (addStopHandler) {
co.withInformerStoppedHandler((informer, ex) -> replacementStopHandlerCalled = true);
}
});
operator.register(reconciler);
operator.installShutdownHook();
Expand Down