Skip to content

Commit e65b329

Browse files
authored
fix: respect the duration in the current record
closes: #7160 Signed-off-by: Steve Hawkins <[email protected]>
1 parent 82aa03b commit e65b329

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#### Bugs
66
* Fix #7356: ensure request config is preserved when adapting to OpenShiftClient
77
* Fix #7343: Leader election callbacks to be called only once (instead of 2)
8+
* Fix #7160: Use the duration from current leader record, not from the config
89
* Fix #7347: Ensure vert.x websockets handle multiple frames
910

1011
#### Improvements

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/LeaderElector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ protected final boolean isLeader(LeaderElectionRecord leaderElectionRecord) {
275275

276276
protected final boolean canBecomeLeader(LeaderElectionRecord leaderElectionRecord) {
277277
return Utils.isNullOrEmpty(leaderElectionRecord.getHolderIdentity())
278-
|| now().isAfter(leaderElectionRecord.getRenewTime().plus(leaderElectionConfig.getLeaseDuration()));
278+
|| now().isAfter(leaderElectionRecord.getRenewTime().plus(leaderElectionRecord.getLeaseDuration()));
279279
}
280280

281281
/**

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/extended/leaderelection/LeaderElectorTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ void canBecomeLeaderAndDifferentLeaderWithExpiredLockShouldReturnTrue() {
279279
void canBecomeLeaderAndDifferentLeaderWithActiveLockShouldReturnFalse() {
280280
// Given
281281
final LeaderElectionConfig lec = mock(LeaderElectionConfig.class);
282-
when(lec.getLeaseDuration()).thenReturn(Duration.ofHours(1L));
283282
final LeaderElectionRecord ler = mock(LeaderElectionRecord.class);
283+
when(ler.getLeaseDuration()).thenReturn(Duration.ofHours(1L));
284284
when(ler.getHolderIdentity()).thenReturn("someone");
285285
when(ler.getRenewTime()).thenReturn(ZonedDateTime.now(ZoneOffset.UTC));
286286
// When
@@ -289,6 +289,21 @@ void canBecomeLeaderAndDifferentLeaderWithActiveLockShouldReturnFalse() {
289289
assertFalse(result);
290290
}
291291

292+
@Test
293+
void canBecomeLeaderRecordDurationDiffersFromConfig() {
294+
// Given
295+
final LeaderElectionConfig lec = mock(LeaderElectionConfig.class);
296+
when(lec.getLeaseDuration()).thenReturn(Duration.ofHours(1L));
297+
final LeaderElectionRecord ler = mock(LeaderElectionRecord.class);
298+
when(ler.getLeaseDuration()).thenReturn(Duration.ofMinutes(15L));
299+
when(ler.getHolderIdentity()).thenReturn("someone");
300+
when(ler.getRenewTime()).thenReturn(ZonedDateTime.now(ZoneOffset.UTC).minus(Duration.ofMinutes(20L)));
301+
// When
302+
final boolean result = new LeaderElector(mock(NamespacedKubernetesClient.class), lec, Runnable::run).canBecomeLeader(ler);
303+
// Then - because the duration from the record is used
304+
assertTrue(result);
305+
}
306+
292307
@Test
293308
void loopCompletesOk() throws Exception {
294309
// Given

0 commit comments

Comments
 (0)