use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.
the class KubernetesMultipleComponentLeaderElectionDriver method extractLeaderInformation.
private static Collection<LeaderInformationWithComponentId> extractLeaderInformation(KubernetesConfigMap configMap) {
final Map<String, String> data = configMap.getData();
final Collection<LeaderInformationWithComponentId> leaderInformationWithLeaderNames = new ArrayList<>();
for (Map.Entry<String, String> keyValuePair : data.entrySet()) {
final String key = keyValuePair.getKey();
if (KubernetesUtils.isSingleLeaderKey(key)) {
final String leaderName = KubernetesUtils.extractLeaderName(key);
final LeaderInformation leaderInformation = KubernetesUtils.parseLeaderInformationSafely(keyValuePair.getValue()).orElse(LeaderInformation.empty());
leaderInformationWithLeaderNames.add(LeaderInformationWithComponentId.create(leaderName, leaderInformation));
}
}
return leaderInformationWithLeaderNames;
}
use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.
the class KubernetesUtils method createConfigMapIfItDoesNotExist.
/**
* Creates a config map with the given name if it does not exist.
*
* @param flinkKubeClient to use for creating the config map
* @param configMapName name of the config map
* @param clusterId clusterId to which the map belongs
* @throws FlinkException if the config map could not be created
*/
public static void createConfigMapIfItDoesNotExist(FlinkKubeClient flinkKubeClient, String configMapName, String clusterId) throws FlinkException {
int attempt = 0;
CompletionException lastException = null;
final int maxAttempts = 10;
final KubernetesConfigMap configMap = new KubernetesConfigMap(new ConfigMapBuilder().withNewMetadata().withName(configMapName).withLabels(getConfigMapLabels(clusterId, LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY)).endMetadata().build());
while (!flinkKubeClient.getConfigMap(configMapName).isPresent() && attempt < maxAttempts) {
try {
flinkKubeClient.createConfigMap(configMap).join();
} catch (CompletionException e) {
// retrying
lastException = ExceptionUtils.firstOrSuppressed(e, lastException);
}
attempt++;
}
if (attempt >= maxAttempts && lastException != null) {
throw new FlinkException(String.format("Could not create the config map %s.", configMapName), lastException);
}
}
use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.
the class KubernetesTestFixture method createFlinkKubeClientBuilder.
TestingFlinkKubeClient.Builder createFlinkKubeClientBuilder() {
return TestingFlinkKubeClient.builder().setCreateConfigMapFunction(configMap -> {
configMapStore.put(configMap.getName(), configMap);
return CompletableFuture.completedFuture(null);
}).setGetConfigMapFunction(configMapName -> Optional.ofNullable(configMapStore.get(configMapName))).setCheckAndUpdateConfigMapFunction((configMapName, updateFunction) -> {
final KubernetesConfigMap configMap = configMapStore.get(configMapName);
if (configMap != null) {
try {
final boolean updated = updateFunction.apply(configMap).map(updateConfigMap -> {
configMapStore.put(configMap.getName(), updateConfigMap);
return true;
}).orElse(false);
return CompletableFuture.completedFuture(updated);
} catch (Throwable throwable) {
throw new CompletionException(throwable);
}
}
throw new CompletionException(new KubernetesException("ConfigMap " + configMapName + " does not exist."));
}).setDeleteConfigMapFunction(name -> {
configMapStore.remove(name);
return FutureUtils.completedVoidFuture();
}).setDeleteConfigMapByLabelFunction(labels -> {
if (deleteConfigMapByLabelsFuture.isDone()) {
return FutureUtils.completedExceptionally(new KubernetesException("ConfigMap with labels " + labels + " has already be deleted."));
} else {
deleteConfigMapByLabelsFuture.complete(labels);
return FutureUtils.completedVoidFuture();
}
}).setCloseConsumer(closeKubeClientFuture::complete).setCreateLeaderElectorFunction((leaderConfig, callbackHandler) -> {
leaderCallbackHandlerFuture.complete(callbackHandler);
return new TestingFlinkKubeClient.TestingKubernetesLeaderElector(leaderConfig, callbackHandler);
}).setCreateConfigMapSharedWatcherFunction((labels) -> {
final TestingFlinkKubeClient.TestingKubernetesConfigMapSharedWatcher watcher = new TestingFlinkKubeClient.TestingKubernetesConfigMapSharedWatcher(labels);
watcher.setWatchFunction((ignore, handler) -> {
final CompletableFuture<FlinkKubeClient.WatchCallbackHandler<KubernetesConfigMap>> future = CompletableFuture.completedFuture(handler);
configMapCallbackFutures.add(future);
final TestingFlinkKubeClient.MockKubernetesWatch watch = new TestingFlinkKubeClient.MockKubernetesWatch();
configMapWatches.add(watch);
return watch;
});
return watcher;
});
}
use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.
the class Fabric8FlinkKubeClientTest method testCheckAndUpdateConfigMapWhenReplaceConfigMapFailed.
@Test
public void testCheckAndUpdateConfigMapWhenReplaceConfigMapFailed() throws Exception {
final int configuredRetries = flinkConfig.getInteger(KubernetesConfigOptions.KUBERNETES_TRANSACTIONAL_OPERATION_MAX_RETRIES);
final KubernetesConfigMap configMap = buildTestingConfigMap();
this.flinkKubeClient.createConfigMap(configMap).get();
mockReplaceConfigMapFailed(configMap.getInternalResource());
final AtomicInteger retries = new AtomicInteger(0);
try {
this.flinkKubeClient.checkAndUpdateConfigMap(TESTING_CONFIG_MAP_NAME, c -> {
retries.incrementAndGet();
return Optional.of(configMap);
}).get();
fail("checkAndUpdateConfigMap should fail due to a PossibleInconsistentStateException when number of retries has been exhausted.");
} catch (Exception ex) {
assertThat(ex, FlinkMatchers.containsMessage("Could not complete the " + "operation. Number of retries has been exhausted."));
assertThat(retries.get(), is(configuredRetries + 1));
assertThat("An error while replacing the ConfigMap should cause an PossibleInconsistentStateException.", ExceptionUtils.findThrowable(ex, PossibleInconsistentStateException.class).isPresent(), is(true));
}
}
use of org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap in project flink by apache.
the class Fabric8FlinkKubeClientTest method testCreateConfigMap.
@Test
public void testCreateConfigMap() throws Exception {
final KubernetesConfigMap configMap = buildTestingConfigMap();
this.flinkKubeClient.createConfigMap(configMap).get();
final Optional<KubernetesConfigMap> currentOpt = this.flinkKubeClient.getConfigMap(TESTING_CONFIG_MAP_NAME);
assertThat(currentOpt.isPresent(), is(true));
assertThat(currentOpt.get().getData().get(TESTING_CONFIG_MAP_KEY), is(TESTING_CONFIG_MAP_VALUE));
}
Aggregations