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;
}
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;
}
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();
}
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;
}
Aggregations