diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java index d0af5ee441..d6ee17a383 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java @@ -102,11 +102,15 @@ private void startLeading() { } private void stopLeading() { - log.info("Stopped leading for identity: {}. Exiting.", identity); - // When leader stops leading the process ends immediately to prevent multiple reconciliations - // running parallel. - // Note that some reconciliations might run for a very long time. - System.exit(1); + if (configurationService.getLeaderElectionConfiguration().orElseThrow().isExitOnStopLeading()) { + log.info("Stopped leading for identity: {}. Exiting.", identity); + // When leader stops leading the process ends immediately to prevent multiple reconciliations + // running parallel. + // Note that some reconciliations might run for a very long time. + System.exit(1); + } else { + log.info("Stopped leading, configured not to exit"); + } } private String identity(LeaderElectionConfiguration config) { diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfiguration.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfiguration.java index 0ab72ff165..cfce453e14 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfiguration.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfiguration.java @@ -20,6 +20,7 @@ public class LeaderElectionConfiguration { private final Duration retryPeriod; private final LeaderCallbacks leaderCallbacks; + private final boolean exitOnStopLeading; public LeaderElectionConfiguration(String leaseName, String leaseNamespace, String identity) { this( @@ -27,7 +28,7 @@ public LeaderElectionConfiguration(String leaseName, String leaseNamespace, Stri leaseNamespace, LEASE_DURATION_DEFAULT_VALUE, RENEW_DEADLINE_DEFAULT_VALUE, - RETRY_PERIOD_DEFAULT_VALUE, identity, null); + RETRY_PERIOD_DEFAULT_VALUE, identity, null, true); } public LeaderElectionConfiguration(String leaseName, String leaseNamespace) { @@ -36,7 +37,7 @@ public LeaderElectionConfiguration(String leaseName, String leaseNamespace) { leaseNamespace, LEASE_DURATION_DEFAULT_VALUE, RENEW_DEADLINE_DEFAULT_VALUE, - RETRY_PERIOD_DEFAULT_VALUE, null, null); + RETRY_PERIOD_DEFAULT_VALUE, null, null, true); } public LeaderElectionConfiguration(String leaseName) { @@ -45,7 +46,7 @@ public LeaderElectionConfiguration(String leaseName) { null, LEASE_DURATION_DEFAULT_VALUE, RENEW_DEADLINE_DEFAULT_VALUE, - RETRY_PERIOD_DEFAULT_VALUE, null, null); + RETRY_PERIOD_DEFAULT_VALUE, null, null, true); } public LeaderElectionConfiguration( @@ -54,7 +55,7 @@ public LeaderElectionConfiguration( Duration leaseDuration, Duration renewDeadline, Duration retryPeriod) { - this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null); + this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null, true); } public LeaderElectionConfiguration( @@ -64,7 +65,8 @@ public LeaderElectionConfiguration( Duration renewDeadline, Duration retryPeriod, String identity, - LeaderCallbacks leaderCallbacks) { + LeaderCallbacks leaderCallbacks, + boolean exitOnStopLeading) { this.leaseName = leaseName; this.leaseNamespace = leaseNamespace; this.leaseDuration = leaseDuration; @@ -72,6 +74,7 @@ public LeaderElectionConfiguration( this.retryPeriod = retryPeriod; this.identity = identity; this.leaderCallbacks = leaderCallbacks; + this.exitOnStopLeading = exitOnStopLeading; } public Optional getLeaseNamespace() { @@ -101,4 +104,8 @@ public Optional getIdentity() { public Optional getLeaderCallbacks() { return Optional.ofNullable(leaderCallbacks); } + + public boolean isExitOnStopLeading() { + return exitOnStopLeading; + } } diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.java index 494c9d8e66..eda262f9eb 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.java @@ -16,6 +16,7 @@ public final class LeaderElectionConfigurationBuilder { private Duration renewDeadline = RENEW_DEADLINE_DEFAULT_VALUE; private Duration retryPeriod = RETRY_PERIOD_DEFAULT_VALUE; private LeaderCallbacks leaderCallbacks; + private boolean exitOnStopLeading = true; private LeaderElectionConfigurationBuilder(String leaseName) { this.leaseName = leaseName; @@ -55,8 +56,13 @@ public LeaderElectionConfigurationBuilder withLeaderCallbacks(LeaderCallbacks le return this; } + public LeaderElectionConfigurationBuilder withExitOnStopLeading(boolean exitOnStopLeading) { + this.exitOnStopLeading = exitOnStopLeading; + return this; + } + public LeaderElectionConfiguration build() { return new LeaderElectionConfiguration(leaseName, leaseNamespace, leaseDuration, renewDeadline, - retryPeriod, identity, leaderCallbacks); + retryPeriod, identity, leaderCallbacks, exitOnStopLeading); } }