use of com.palantir.lock.v2.Lease in project kubernetes-client by fabric8io.
the class LeaseLockTest method getWithExistingLeaseShouldReturnLeaderElectionRecord.
@Test
void getWithExistingLeaseShouldReturnLeaderElectionRecord() {
// Given
final Lease lease = new LeaseBuilder().withNewSpec().withHolderIdentity("1337").withLeaseDurationSeconds(15).withAcquireTime(ZonedDateTime.of(2015, 10, 21, 4, 29, 0, 0, ZoneId.of("UTC"))).withRenewTime(ZonedDateTime.of(2015, 10, 21, 7, 28, 0, 0, ZoneId.of("UTC"))).withLeaseTransitions(0).endSpec().build();
when(leases.withName(eq("name")).get()).thenReturn(lease);
lease.setMetadata(new ObjectMetaBuilder().withResourceVersion("313373").build());
final LeaseLock lock = new LeaseLock("namespace", "name", "1337");
// When
final LeaderElectionRecord result = lock.get(kc);
// Then
assertNotNull(result);
assertEquals("313373", result.getVersion());
assertEquals("1337", result.getHolderIdentity());
assertEquals(15, result.getLeaseDuration().getSeconds());
assertEquals(ZonedDateTime.of(2015, 10, 21, 4, 29, 0, 0, ZoneId.of("UTC")), result.getAcquireTime());
}
use of com.palantir.lock.v2.Lease in project kubernetes-client by fabric8io.
the class LeaseLock method update.
/**
* {@inheritDoc}
*/
@Override
public <C extends NamespacedKubernetesClient> void update(C client, LeaderElectionRecord leaderElectionRecord) throws LockException {
try {
final Lease toReplace = client.inNamespace(leaseNamespace).leases().withName(leaseName).get();
toReplace.getSpec().setHolderIdentity(leaderElectionRecord.getHolderIdentity());
toReplace.getSpec().setLeaseDurationSeconds((int) leaderElectionRecord.getLeaseDuration().get(ChronoUnit.SECONDS));
toReplace.getSpec().setAcquireTime(leaderElectionRecord.getAcquireTime());
toReplace.getSpec().setRenewTime(leaderElectionRecord.getRenewTime());
toReplace.getSpec().setLeaseTransitions(leaderElectionRecord.getLeaderTransitions());
// Use replace instead of edit to avoid concurrent modifications, resourceVersion is locked to original record version
client.inNamespace(leaseNamespace).leases().withName(leaseName).lockResourceVersion((String) Objects.requireNonNull(leaderElectionRecord.getVersion())).replace(toReplace);
} catch (Exception e) {
throw new LockException("Unable to update LeaseLock", e);
}
}
Aggregations