Skip to content

Commit f2448c6

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent d423c55 commit f2448c6

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

docs/content/en/docs/documentation/features.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ public class DeploymentReconciler
2323
}
2424
```
2525

26-
## Leader Election
27-
28-
Operators are typically deployed with a single active instance. However, you can deploy multiple instances where only one (the "leader") processes events. This is achieved through "leader election."
29-
30-
While all instances run and start their event sources to populate caches, only the leader processes events. If the leader crashes, other instances are already warmed up and ready to take over when a new leader is elected.
31-
32-
See sample configuration in the [E2E test](https://github.com/java-operator-sdk/java-operator-sdk/blob/8865302ac0346ee31f2d7b348997ec2913d5922b/sample-operators/leader-election/src/main/java/io/javaoperatorsdk/operator/sample/LeaderElectionTestOperator.java#L21-L23).
33-
3426
## Automatic CRD Generation
3527

3628
**Note:** This feature is provided by the [Fabric8 Kubernetes Client](https://github.com/fabric8io/kubernetes-client), not JOSDK itself.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: Leader Election
3+
weight: 89
4+
---
5+
6+
When running multiple replicas of an operator for high availability, leader election ensures that
7+
only one instance actively reconciles resources at a time. JOSDK uses Kubernetes
8+
[Lease](https://kubernetes.io/docs/concepts/architecture/leases/) objects for leader election.
9+
10+
## Enabling Leader Election
11+
12+
### Programmatic Configuration
13+
14+
```java
15+
var operator = new Operator(o -> o.withLeaderElectionConfiguration(
16+
new LeaderElectionConfiguration("my-operator-lease", "operator-namespace")));
17+
```
18+
19+
Or using the builder for full control:
20+
21+
```java
22+
import static io.javaoperatorsdk.operator.api.config.LeaderElectionConfigurationBuilder.aLeaderElectionConfiguration;
23+
24+
var config = aLeaderElectionConfiguration("my-operator-lease")
25+
.withLeaseNamespace("operator-namespace")
26+
.withIdentity(System.getenv("POD_NAME"))
27+
.withLeaseDuration(Duration.ofSeconds(15))
28+
.withRenewDeadline(Duration.ofSeconds(10))
29+
.withRetryPeriod(Duration.ofSeconds(2))
30+
.build();
31+
32+
var operator = new Operator(o -> o.withLeaderElectionConfiguration(config));
33+
```
34+
35+
### External Configuration
36+
37+
Leader election can also be configured via properties (e.g. environment variables or a config file).
38+
39+
See details under [configurations](configuration.md) page.
40+
41+
## How It Works
42+
43+
1. When leader election is enabled, the operator starts but **does not process events** until it acquires
44+
the lease.
45+
2. Once leadership is acquired, event processing begins normally.
46+
3. If leadership is lost (e.g. the leader pod becomes unresponsive), another instance acquires the lease
47+
and takes over reconciliation. The instance that lost the lead is terminated (`System.exit()`)
48+
49+
### Identity and Namespace Inference
50+
51+
If not explicitly set:
52+
- **Identity** is resolved from the `HOSTNAME` environment variable, then the pod name, falling back to a
53+
random UUID.
54+
- **Lease namespace** defaults to the namespace the operator pod is running in.
55+
56+
## RBAC Requirements
57+
58+
The operator's service account needs permissions to manage Lease objects:
59+
60+
```yaml
61+
- apiGroups: ["coordination.k8s.io"]
62+
resources: ["leases"]
63+
verbs: ["create", "update", "get"]
64+
```
65+
66+
JOSDK checks for these permissions at startup and throws a clear error if they are missing.
67+
68+
## Sample E2E Test
69+
70+
A complete working example is available in the
71+
[`leader-election` sample operator](https://github.com/java-operator-sdk/java-operator-sdk/tree/main/sample-operators/leader-election),
72+
including multi-replica deployment manifests and an E2E test that verifies failover behavior.

0 commit comments

Comments
 (0)