Skip to content

Commit d02a3fc

Browse files
authored
feat: add flag to support listening to resource changes when not leader. (#442)
1 parent d221b6c commit d02a3fc

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/KubeOps/Operator/Controller/ResourceControllerManager.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ internal class ResourceControllerManager : IHostedService
1111
{
1212
private readonly IControllerInstanceBuilder _controllerInstanceBuilder;
1313
private readonly ILeaderElection _leaderElection;
14+
private readonly OperatorSettings _operatorSettings;
1415
private readonly List<IManagedResourceController> _controllerList;
1516

1617
private IDisposable? _leadershipSubscription;
1718

1819
public ResourceControllerManager(
1920
IControllerInstanceBuilder controllerInstanceBuilder,
20-
ILeaderElection leaderElection)
21+
ILeaderElection leaderElection,
22+
OperatorSettings operatorSettings)
2123
{
2224
_controllerInstanceBuilder = controllerInstanceBuilder;
2325
_leaderElection = leaderElection;
26+
_operatorSettings = operatorSettings;
2427
_controllerList = new List<IManagedResourceController>();
2528
}
2629

@@ -54,7 +57,8 @@ private void LeadershipChanged(LeaderState state)
5457

5558
foreach (var controller in _controllerList)
5659
{
57-
if (state == LeaderState.Leader)
60+
if (state == LeaderState.Leader
61+
|| !_operatorSettings.OnlyWatchEventsWhenLeader)
5862
{
5963
controller.StartAsync();
6064
}

src/KubeOps/Operator/OperatorSettings.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ public sealed class OperatorSettings
9494
/// </summary>
9595
public bool EnableLeaderElection { get; set; } = true;
9696

97+
/// <summary>
98+
/// <para>
99+
/// If set to true, controllers will only watch for new events when in a leader state,
100+
/// or if leadership is disabled. When false, this check is disabled,
101+
/// controllers will always watch for resource changes regardless of leadership state.
102+
/// </para>
103+
/// <para>
104+
/// If this is disabled, you should consider checking leadership state manually,
105+
/// to prevent a "split brain" problem.
106+
/// </para>
107+
/// <para>
108+
/// Defaults to true.
109+
/// </para>
110+
/// </summary>
111+
public bool OnlyWatchEventsWhenLeader { get; set; } = true;
112+
97113
/// <summary>
98114
/// The interval in seconds in which this particular instance of the operator
99115
/// will check for leader election.

0 commit comments

Comments
 (0)