-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add leader election via KubernetesClient (#627)
- Loading branch information
Showing
11 changed files
with
285 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using KubeOps.Abstractions.Controller; | ||
using KubeOps.Abstractions.Events; | ||
using KubeOps.Abstractions.Finalizer; | ||
using KubeOps.Abstractions.Queue; | ||
using KubeOps.Abstractions.Rbac; | ||
using KubeOps.KubernetesClient; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
using Operator.Entities; | ||
using Operator.Finalizer; | ||
|
||
namespace Operator.Controller; | ||
|
||
[EntityRbac(typeof(V1SecondEntity), Verbs = RbacVerb.All)] | ||
public class V1SecondEntityController : IEntityController<V1SecondEntity> | ||
{ | ||
private readonly ILogger<V1SecondEntityController> _logger; | ||
|
||
public V1SecondEntityController( | ||
ILogger<V1SecondEntityController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public async Task ReconcileAsync(V1SecondEntity entity) | ||
{ | ||
_logger.LogInformation("Reconciling entity {Entity}.", entity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using k8s.Models; | ||
|
||
using KubeOps.Abstractions.Entities; | ||
|
||
namespace Operator.Entities; | ||
|
||
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "SecondEntity")] | ||
public partial class V1SecondEntity : CustomKubernetesEntity | ||
{ | ||
public override string ToString() => $"Second Entity ({Metadata.Name})"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
todo: | ||
- leadership election | ||
- build targets | ||
- other CLI commands | ||
- error handling | ||
- namespaced operator | ||
- web: webhooks | ||
- docs | ||
- try .net 8 AOT? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/KubeOps.Operator/Watcher/LeaderAwareResourceWatcher{TEntity}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using k8s; | ||
using k8s.LeaderElection; | ||
using k8s.Models; | ||
|
||
using KubeOps.KubernetesClient; | ||
using KubeOps.Operator.Queue; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace KubeOps.Operator.Watcher; | ||
|
||
internal class LeaderAwareResourceWatcher<TEntity> : ResourceWatcher<TEntity> | ||
where TEntity : IKubernetesObject<V1ObjectMeta> | ||
{ | ||
private readonly ILogger<LeaderAwareResourceWatcher<TEntity>> _logger; | ||
private readonly LeaderElector _elector; | ||
|
||
public LeaderAwareResourceWatcher( | ||
ILogger<LeaderAwareResourceWatcher<TEntity>> logger, | ||
IServiceProvider provider, | ||
IKubernetesClient<TEntity> client, | ||
TimedEntityQueue<TEntity> queue, | ||
LeaderElector elector) | ||
: base(logger, provider, client, queue) | ||
{ | ||
_logger = logger; | ||
_elector = elector; | ||
} | ||
|
||
public override Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogDebug("Subscribe for leadership updates."); | ||
_elector.OnStartedLeading += StartedLeading; | ||
_elector.OnStoppedLeading += StoppedLeading; | ||
if (_elector.IsLeader()) | ||
{ | ||
StartedLeading(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogDebug("Unsubscribe from leadership updates."); | ||
_elector.OnStartedLeading -= StartedLeading; | ||
_elector.OnStoppedLeading -= StoppedLeading; | ||
return Task.CompletedTask; | ||
} | ||
|
||
private void StartedLeading() | ||
{ | ||
_logger.LogInformation("This instance started leading, starting watcher."); | ||
base.StartAsync(default); | ||
} | ||
|
||
private void StoppedLeading() | ||
{ | ||
_logger.LogInformation("This instance stopped leading, stopping watcher."); | ||
base.StopAsync(default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.