Search in sources :

Example 16 with Lease

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());
}
Also used : Lease(io.fabric8.kubernetes.api.model.coordination.v1.Lease) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) LeaseBuilder(io.fabric8.kubernetes.api.model.coordination.v1.LeaseBuilder) Test(org.junit.jupiter.api.Test)

Example 17 with Lease

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);
    }
}
Also used : Lease(io.fabric8.kubernetes.api.model.coordination.v1.Lease)

Aggregations

Lease (com.palantir.lock.v2.Lease)12 Test (org.junit.Test)9 StartTransactionResponseV4 (com.palantir.lock.v2.StartTransactionResponseV4)4 ConjureStartTransactionsRequest (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsRequest)3 LockResponse (com.palantir.lock.v2.LockResponse)3 Lease (io.fabric8.kubernetes.api.model.coordination.v1.Lease)3 ConjureStartTransactionsResponse (com.palantir.atlasdb.timelock.api.ConjureStartTransactionsResponse)2 LeaderTime (com.palantir.lock.v2.LeaderTime)2 LockImmutableTimestampResponse (com.palantir.lock.v2.LockImmutableTimestampResponse)2 LockToken (com.palantir.lock.v2.LockToken)2 WaitForLocksRequest (com.palantir.lock.v2.WaitForLocksRequest)2 Optional (java.util.Optional)2 Set (java.util.Set)2 UUID (java.util.UUID)2 Test (org.junit.jupiter.api.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Sets (com.google.common.collect.Sets)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1