ConfigMapLock implements Lock { @Override public <C extends Namespaceable<C> & KubernetesClient> LeaderElectionRecord get(C client) { final ConfigMap configMap = client .inNamespace(configMapNamespace).configMaps().withName(configMapName).get(); return Optional.ofNullable(configMap) .map(ConfigMap::getMetadata) .map(ObjectMeta::getAnnotations) .map(annotations -> annotations.get(LEADER_ELECTION_RECORD_ANNOTATION_KEY)) .map(annotation -> { try { return objectMapper.readValue(annotation, new TypeReference<LeaderElectionRecord>() {}); } catch (JsonProcessingException ex) { LOGGER.error("Error deserializing LeaderElectionRecord from ConfigMap", ex); return null; } }) .map(record -> { record.setVersion(configMap.getMetadata().getResourceVersion()); return record; }) .orElse(null); } ConfigMapLock(String configMapNamespace, String configMapName, String identity); @Override LeaderElectionRecord get(C client); @Override void create(
    C client, LeaderElectionRecord leaderElectionRecord); @Override void update(
    C client, LeaderElectionRecord leaderElectionRecord); @Override String identity(); @Override String describe();  }
@Test void getWithExistingConfigMapShouldReturnLeaderElectionRecord() { final ConfigMap cm = new ConfigMap(); when(configMaps.withName(eq("name")).get()).thenReturn(cm); cm.setMetadata(new ObjectMetaBuilder() .withAnnotations( Collections.singletonMap("control-plane.alpha.kubernetes.io/leader", "{\"holderIdentity\":\"1337\",\"leaseDuration\":15,\"acquireTime\":1445401740,\"renewTime\":1445412480}") ) .withResourceVersion("313373").build()); final ConfigMapLock lock = new ConfigMapLock("namespace", "name", "1337"); final LeaderElectionRecord result = lock.get(kc); 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()); }