Search in sources :

Example 1 with LeaderElectionRecord

use of io.kubernetes.client.extended.leaderelection.LeaderElectionRecord in project java by kubernetes-client.

the class ConfigMapLock method get.

@Override
public LeaderElectionRecord get() throws ApiException {
    V1ConfigMap configMap = coreV1Client.readNamespacedConfigMap(name, namespace, null);
    configMapRefer.set(configMap);
    Map<String, String> annotations = configMap.getMetadata().getAnnotations();
    if (annotations == null || annotations.isEmpty()) {
        configMap.getMetadata().setAnnotations(new HashMap<>());
    }
    String recordRawStringContent = configMap.getMetadata().getAnnotations().get(LeaderElectionRecordAnnotationKey);
    if (StringUtils.isEmpty(recordRawStringContent)) {
        return new LeaderElectionRecord();
    }
    LeaderElectionRecord record = coreV1Client.getApiClient().getJSON().deserialize(recordRawStringContent, LeaderElectionRecord.class);
    return record;
}
Also used : LeaderElectionRecord(io.kubernetes.client.extended.leaderelection.LeaderElectionRecord) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap)

Example 2 with LeaderElectionRecord

use of io.kubernetes.client.extended.leaderelection.LeaderElectionRecord in project java by kubernetes-client.

the class EndpointsLock method get.

@Override
public LeaderElectionRecord get() throws ApiException {
    V1Endpoints endpoints = coreV1Client.readNamespacedEndpoints(name, namespace, null);
    endpointsRefer.set(endpoints);
    Map<String, String> annotations = endpoints.getMetadata().getAnnotations();
    if (annotations == null || annotations.isEmpty()) {
        endpoints.getMetadata().setAnnotations(new HashMap<>());
    }
    String recordRawStringContent = endpoints.getMetadata().getAnnotations().get(LeaderElectionRecordAnnotationKey);
    if (StringUtils.isEmpty(recordRawStringContent)) {
        return new LeaderElectionRecord();
    }
    LeaderElectionRecord record = coreV1Client.getApiClient().getJSON().deserialize(recordRawStringContent, LeaderElectionRecord.class);
    return record;
}
Also used : V1Endpoints(io.kubernetes.client.openapi.models.V1Endpoints) LeaderElectionRecord(io.kubernetes.client.extended.leaderelection.LeaderElectionRecord)

Example 3 with LeaderElectionRecord

use of io.kubernetes.client.extended.leaderelection.LeaderElectionRecord in project java by kubernetes-client.

the class LeaderElectingControllerTest method testLeaderElectingController.

@Test(timeout = 90000)
public void testLeaderElectingController() throws ApiException, InterruptedException {
    AtomicReference<LeaderElectionRecord> record = new AtomicReference<>();
    record.set(new LeaderElectionRecord());
    Semaphore latch = new Semaphore(2);
    Semaphore controllerLatch = new Semaphore(2);
    when(mockLock.identity()).thenReturn("foo");
    when(mockLock.get()).thenThrow(new ApiException("Record Not Found", HttpURLConnection.HTTP_NOT_FOUND, null, null)).thenReturn(record.get());
    doAnswer(invocationOnMock -> {
        record.set(invocationOnMock.getArgument(0));
        latch.release();
        return true;
    }).when(mockLock).create(any());
    doAnswer(invocationOnMock -> {
        latch.release();
        return false;
    }).when(mockLock).update(any());
    doAnswer(invocationOnMock -> {
        controllerLatch.release();
        return null;
    }).when(mockController).run();
    doAnswer(invocationOnMock -> {
        controllerLatch.release();
        return null;
    }).when(mockController).shutdown();
    LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(mockLock, Duration.ofMillis(300), Duration.ofMillis(200), Duration.ofMillis(100))), mockController);
    latch.acquire(2);
    controllerLatch.acquire(2);
    Thread controllerThread = new Thread(leaderElectingController::run);
    controllerThread.start();
    latch.acquire(2);
    controllerThread.interrupt();
    verify(mockLock, times(1)).create(any());
    verify(mockLock, atLeastOnce()).update(any());
    controllerLatch.acquire(2);
    verify(mockController, times(1)).run();
    verify(mockController, times(1)).shutdown();
}
Also used : LeaderElectionRecord(io.kubernetes.client.extended.leaderelection.LeaderElectionRecord) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Example 4 with LeaderElectionRecord

use of io.kubernetes.client.extended.leaderelection.LeaderElectionRecord in project java by kubernetes-client.

the class LeaseLock method getRecordFromLease.

private LeaderElectionRecord getRecordFromLease(V1LeaseSpec lease) {
    LeaderElectionRecord record = new LeaderElectionRecord();
    if (lease.getAcquireTime() != null) {
        record.setAcquireTime(new Date(lease.getAcquireTime().toInstant().toEpochMilli()));
    }
    if (lease.getRenewTime() != null) {
        record.setRenewTime(new Date(lease.getRenewTime().toInstant().toEpochMilli()));
    }
    record.setHolderIdentity(lease.getHolderIdentity());
    record.setLeaderTransitions(lease.getLeaseTransitions());
    record.setLeaseDurationSeconds(lease.getLeaseDurationSeconds());
    return record;
}
Also used : LeaderElectionRecord(io.kubernetes.client.extended.leaderelection.LeaderElectionRecord) Date(java.util.Date)

Aggregations

LeaderElectionRecord (io.kubernetes.client.extended.leaderelection.LeaderElectionRecord)4 LeaderElectionConfig (io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)1 LeaderElector (io.kubernetes.client.extended.leaderelection.LeaderElector)1 ApiException (io.kubernetes.client.openapi.ApiException)1 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)1 V1Endpoints (io.kubernetes.client.openapi.models.V1Endpoints)1 Date (java.util.Date)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1