Skip to content

Commit 2faadb5

Browse files
csvirimetacosm
andcommitted
feat: config option to not exit when stops leading (#2500)
* feat: config option to not exit when stops leading Signed-off-by: Attila Mészáros <[email protected]> * Update operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java Co-authored-by: Chris Laprun <[email protected]> --------- Signed-off-by: Attila Mészáros <[email protected]> Co-authored-by: Chris Laprun <[email protected]>
1 parent 3801c4b commit 2faadb5

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ private void startLeading() {
102102
}
103103

104104
private void stopLeading() {
105-
log.info("Stopped leading for identity: {}. Exiting.", identity);
106-
// When leader stops leading the process ends immediately to prevent multiple reconciliations
107-
// running parallel.
108-
// Note that some reconciliations might run for a very long time.
109-
System.exit(1);
105+
if (configurationService.getLeaderElectionConfiguration().orElseThrow().isExitOnStopLeading()) {
106+
log.info("Stopped leading for identity: {}. Exiting.", identity);
107+
// When leader stops leading the process ends immediately to prevent multiple reconciliations
108+
// running parallel.
109+
// Note that some reconciliations might run for a very long time.
110+
System.exit(1);
111+
} else {
112+
log.info("Stopped leading, configured not to exit");
113+
}
110114
}
111115

112116
private String identity(LeaderElectionConfiguration config) {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfiguration.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ public class LeaderElectionConfiguration {
2020
private final Duration retryPeriod;
2121

2222
private final LeaderCallbacks leaderCallbacks;
23+
private final boolean exitOnStopLeading;
2324

2425
public LeaderElectionConfiguration(String leaseName, String leaseNamespace, String identity) {
2526
this(
2627
leaseName,
2728
leaseNamespace,
2829
LEASE_DURATION_DEFAULT_VALUE,
2930
RENEW_DEADLINE_DEFAULT_VALUE,
30-
RETRY_PERIOD_DEFAULT_VALUE, identity, null);
31+
RETRY_PERIOD_DEFAULT_VALUE, identity, null, true);
3132
}
3233

3334
public LeaderElectionConfiguration(String leaseName, String leaseNamespace) {
@@ -36,7 +37,7 @@ public LeaderElectionConfiguration(String leaseName, String leaseNamespace) {
3637
leaseNamespace,
3738
LEASE_DURATION_DEFAULT_VALUE,
3839
RENEW_DEADLINE_DEFAULT_VALUE,
39-
RETRY_PERIOD_DEFAULT_VALUE, null, null);
40+
RETRY_PERIOD_DEFAULT_VALUE, null, null, true);
4041
}
4142

4243
public LeaderElectionConfiguration(String leaseName) {
@@ -45,7 +46,7 @@ public LeaderElectionConfiguration(String leaseName) {
4546
null,
4647
LEASE_DURATION_DEFAULT_VALUE,
4748
RENEW_DEADLINE_DEFAULT_VALUE,
48-
RETRY_PERIOD_DEFAULT_VALUE, null, null);
49+
RETRY_PERIOD_DEFAULT_VALUE, null, null, true);
4950
}
5051

5152
public LeaderElectionConfiguration(
@@ -54,7 +55,7 @@ public LeaderElectionConfiguration(
5455
Duration leaseDuration,
5556
Duration renewDeadline,
5657
Duration retryPeriod) {
57-
this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null);
58+
this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null, true);
5859
}
5960

6061
public LeaderElectionConfiguration(
@@ -64,14 +65,16 @@ public LeaderElectionConfiguration(
6465
Duration renewDeadline,
6566
Duration retryPeriod,
6667
String identity,
67-
LeaderCallbacks leaderCallbacks) {
68+
LeaderCallbacks leaderCallbacks,
69+
boolean exitOnStopLeading) {
6870
this.leaseName = leaseName;
6971
this.leaseNamespace = leaseNamespace;
7072
this.leaseDuration = leaseDuration;
7173
this.renewDeadline = renewDeadline;
7274
this.retryPeriod = retryPeriod;
7375
this.identity = identity;
7476
this.leaderCallbacks = leaderCallbacks;
77+
this.exitOnStopLeading = exitOnStopLeading;
7578
}
7679

7780
public Optional<String> getLeaseNamespace() {
@@ -101,4 +104,8 @@ public Optional<String> getIdentity() {
101104
public Optional<LeaderCallbacks> getLeaderCallbacks() {
102105
return Optional.ofNullable(leaderCallbacks);
103106
}
107+
108+
public boolean isExitOnStopLeading() {
109+
return exitOnStopLeading;
110+
}
104111
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public final class LeaderElectionConfigurationBuilder {
1616
private Duration renewDeadline = RENEW_DEADLINE_DEFAULT_VALUE;
1717
private Duration retryPeriod = RETRY_PERIOD_DEFAULT_VALUE;
1818
private LeaderCallbacks leaderCallbacks;
19+
private boolean exitOnStopLeading = true;
1920

2021
private LeaderElectionConfigurationBuilder(String leaseName) {
2122
this.leaseName = leaseName;
@@ -55,8 +56,13 @@ public LeaderElectionConfigurationBuilder withLeaderCallbacks(LeaderCallbacks le
5556
return this;
5657
}
5758

59+
public LeaderElectionConfigurationBuilder withExitOnStopLeading(boolean exitOnStopLeading) {
60+
this.exitOnStopLeading = exitOnStopLeading;
61+
return this;
62+
}
63+
5864
public LeaderElectionConfiguration build() {
5965
return new LeaderElectionConfiguration(leaseName, leaseNamespace, leaseDuration, renewDeadline,
60-
retryPeriod, identity, leaderCallbacks);
66+
retryPeriod, identity, leaderCallbacks, exitOnStopLeading);
6167
}
6268
}

0 commit comments

Comments
 (0)