Skip to content
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
8 changes: 6 additions & 2 deletions src/KubeOps/Operator/Controller/ResourceControllerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ internal class ResourceControllerManager : IHostedService
{
private readonly IControllerInstanceBuilder _controllerInstanceBuilder;
private readonly ILeaderElection _leaderElection;
private readonly OperatorSettings _operatorSettings;
private readonly List<IManagedResourceController> _controllerList;

private IDisposable? _leadershipSubscription;

public ResourceControllerManager(
IControllerInstanceBuilder controllerInstanceBuilder,
ILeaderElection leaderElection)
ILeaderElection leaderElection,
OperatorSettings operatorSettings)
{
_controllerInstanceBuilder = controllerInstanceBuilder;
_leaderElection = leaderElection;
_operatorSettings = operatorSettings;
_controllerList = new List<IManagedResourceController>();
}

Expand Down Expand Up @@ -54,7 +57,8 @@ private void LeadershipChanged(LeaderState state)

foreach (var controller in _controllerList)
{
if (state == LeaderState.Leader)
if (state == LeaderState.Leader
|| !_operatorSettings.OnlyWatchEventsWhenLeader)
{
controller.StartAsync();
}
Expand Down
16 changes: 16 additions & 0 deletions src/KubeOps/Operator/OperatorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ public sealed class OperatorSettings
/// </summary>
public bool EnableLeaderElection { get; set; } = true;

/// <summary>
/// <para>
/// If set to true, controllers will only watch for new events when in a leader state,
/// or if leadership is disabled. When false, this check is disabled,
/// controllers will always watch for resource changes regardless of leadership state.
/// </para>
/// <para>
/// If this is disabled, you should consider checking leadership state manually,
/// to prevent a "split brain" problem.
/// </para>
/// <para>
/// Defaults to true.
/// </para>
/// </summary>
public bool OnlyWatchEventsWhenLeader { get; set; } = true;

/// <summary>
/// The interval in seconds in which this particular instance of the operator
/// will check for leader election.
Expand Down