ConfigMapLockRepository
This commit is contained in:
committed by
Ioannis Canellos
parent
561bc9d00c
commit
a67dbf37d1
@@ -16,8 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.kubernetes.lock;
|
package org.springframework.cloud.kubernetes.lock;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
|
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
|
||||||
@@ -53,22 +52,26 @@ public class ConfigMapLockRepository {
|
|||||||
this.namespace = namespace;
|
this.namespace = namespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigMap get(String name) {
|
public Optional<ConfigMap> get(String name) {
|
||||||
return kubernetesClient.configMaps()
|
String configMapName = getConfigMapName(name);
|
||||||
|
ConfigMap configMap = kubernetesClient.configMaps()
|
||||||
.inNamespace(namespace)
|
.inNamespace(namespace)
|
||||||
.withName(getConfigMapName(name))
|
.withName(configMapName)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
|
return Optional.ofNullable(configMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean create(String name, String holder, long expiration) {
|
public boolean create(String name, String holder, long expiration) {
|
||||||
|
String configMapName = getConfigMapName(name);
|
||||||
|
String expirationString = String.valueOf(expiration);
|
||||||
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata()
|
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata()
|
||||||
.withName(getConfigMapName(name))
|
.withName(configMapName)
|
||||||
.addToLabels(PROVIDER_LABEL, PROVIDER_LABEL_VALUE)
|
.addToLabels(PROVIDER_LABEL, PROVIDER_LABEL_VALUE)
|
||||||
.addToLabels(KIND_LABEL, KIND_LABEL_VALUE)
|
.addToLabels(KIND_LABEL, KIND_LABEL_VALUE)
|
||||||
.endMetadata()
|
.endMetadata()
|
||||||
.addToData(HOLDER_KEY, holder)
|
.addToData(HOLDER_KEY, holder)
|
||||||
.addToData(EXPIRATION_KEY, String.valueOf(expiration))
|
.addToData(EXPIRATION_KEY, expirationString)
|
||||||
// TODO add information about the creator
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -83,30 +86,23 @@ public class ConfigMapLockRepository {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteAll() {
|
public void delete(String name) {
|
||||||
|
// TODO make sure that only creator can delete the lock
|
||||||
kubernetesClient.configMaps()
|
kubernetesClient.configMaps()
|
||||||
.inNamespace(namespace)
|
.inNamespace(namespace)
|
||||||
.withLabel(PROVIDER_LABEL, PROVIDER_LABEL_VALUE)
|
.withName(getConfigMapName(name))
|
||||||
.withLabel(KIND_LABEL, KIND_LABEL_VALUE)
|
|
||||||
.delete();
|
.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteExpired() {
|
public void deleteIfExpired(String name) {
|
||||||
long now = System.currentTimeMillis();
|
get(name)
|
||||||
// TODO check that it was created by this process
|
.filter(this::isExpired)
|
||||||
List<ConfigMap> configMaps = kubernetesClient.configMaps()
|
.ifPresent(c -> delete(name));
|
||||||
.inNamespace(namespace)
|
}
|
||||||
.withLabel(PROVIDER_LABEL, PROVIDER_LABEL_VALUE)
|
|
||||||
.withLabel(KIND_LABEL, KIND_LABEL_VALUE)
|
|
||||||
.list()
|
|
||||||
.getItems()
|
|
||||||
.stream()
|
|
||||||
.filter(c -> Long.valueOf(c.getData().get("expiration")) < now)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
kubernetesClient.configMaps()
|
private boolean isExpired(ConfigMap configMap) {
|
||||||
.inNamespace(namespace)
|
String expirationString = configMap.getData().get(EXPIRATION_KEY);
|
||||||
.delete(configMaps);
|
return Long.valueOf(expirationString) < System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getConfigMapName(String name) {
|
private String getConfigMapName(String name) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.springframework.cloud.kubernetes.lock;
|
package org.springframework.cloud.kubernetes.lock;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||||
@@ -14,11 +15,17 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.cloud.kubernetes.lock.ConfigMapLockRepository.EXPIRATION_KEY;
|
||||||
|
import static org.springframework.cloud.kubernetes.lock.ConfigMapLockRepository.HOLDER_KEY;
|
||||||
|
|
||||||
@RunWith(Arquillian.class)
|
@RunWith(Arquillian.class)
|
||||||
@RequiresKubernetes
|
@RequiresKubernetes
|
||||||
public class ConfigMapLockRepositoryIT {
|
public class ConfigMapLockRepositoryIT {
|
||||||
|
|
||||||
|
private static final String NAME = "test-name";
|
||||||
|
|
||||||
|
private static final String HOLDER = "test-holder";
|
||||||
|
|
||||||
@ArquillianResource
|
@ArquillianResource
|
||||||
private KubernetesClient kubernetesClient;
|
private KubernetesClient kubernetesClient;
|
||||||
|
|
||||||
@@ -34,54 +41,46 @@ public class ConfigMapLockRepositoryIT {
|
|||||||
|
|
||||||
@After
|
@After
|
||||||
public void after() {
|
public void after() {
|
||||||
deleteConfigMap("test-name");
|
repository.delete(NAME);
|
||||||
deleteConfigMap("test-name-2");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateConfigMap() {
|
public void shouldCreate() {
|
||||||
long expiration = System.currentTimeMillis();
|
assertThat(repository.create(NAME, HOLDER, 1000)).isTrue();
|
||||||
boolean result = repository.create("test-name", "test-holder", expiration);
|
|
||||||
assertThat(result).isTrue();
|
|
||||||
|
|
||||||
ConfigMap configMap = repository.get("test-name");
|
Optional<ConfigMap> optionalConfigMap = repository.get(NAME);
|
||||||
Map<String, String> data = configMap.getData();
|
assertThat(optionalConfigMap.isPresent()).isTrue();
|
||||||
assertThat(data).containsEntry(ConfigMapLockRepository.HOLDER_KEY, "test-holder");
|
|
||||||
assertThat(data).containsEntry(ConfigMapLockRepository.EXPIRATION_KEY, String.valueOf(expiration));
|
Map<String, String> data = optionalConfigMap.get().getData();
|
||||||
|
assertThat(data).containsEntry(HOLDER_KEY, HOLDER);
|
||||||
|
assertThat(data).containsEntry(EXPIRATION_KEY, String.valueOf(1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldNotOverwriteConfigMap() {
|
public void shouldNotOverwrite() {
|
||||||
boolean firstResult = repository.create("test-name", "test-holder", 0);
|
assertThat(repository.create(NAME, HOLDER, 0)).isTrue();
|
||||||
assertThat(firstResult).isTrue();
|
assertThat(repository.create(NAME, HOLDER, 0)).isFalse();
|
||||||
|
|
||||||
boolean secondResult = repository.create("test-name", "test-holder", 0);
|
|
||||||
assertThat(secondResult).isFalse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldDeleteAllConfigMaps() {
|
public void shouldDelete() {
|
||||||
repository.create("test-name", "test-holder", 0);
|
repository.create(NAME, HOLDER, System.currentTimeMillis() + 10000);
|
||||||
repository.create("test-name-2", "test-holder-2", 0);
|
repository.delete(NAME);
|
||||||
repository.deleteAll();
|
assertThat(repository.get(NAME).isPresent()).isFalse();
|
||||||
assertThat(repository.get("test-name")).isNull();
|
|
||||||
assertThat(repository.get("test-name-2")).isNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldDeleteExpiredConfigMaps() {
|
public void shouldDeleteExpired() {
|
||||||
repository.create("test-name", "test-holder", System.currentTimeMillis() - 1);
|
repository.create(NAME, HOLDER, System.currentTimeMillis() - 1);
|
||||||
repository.create("test-name-2", "test-holder-2", System.currentTimeMillis() + 10000);
|
repository.deleteIfExpired(NAME);
|
||||||
repository.deleteExpired();
|
assertThat(repository.get(NAME).isPresent()).isFalse();
|
||||||
assertThat(repository.get("test-name")).isNull();
|
|
||||||
assertThat(repository.get("test-name-2")).isNotNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteConfigMap(String name) {
|
@Test
|
||||||
kubernetesClient.configMaps()
|
public void shouldKeepNotExpired() {
|
||||||
.inNamespace(session.getNamespace())
|
repository.create(NAME, HOLDER, System.currentTimeMillis() + 10000);
|
||||||
.withName(String.format("%s-%s", ConfigMapLockRepository.CONFIG_MAP_PREFIX, name))
|
repository.deleteIfExpired(NAME);
|
||||||
.delete();
|
assertThat(repository.get(NAME).isPresent()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user