Search in sources :

Example 6 with KubernetesConfigMap

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;
}
Also used : LeaderInformationWithComponentId(org.apache.flink.runtime.leaderelection.LeaderInformationWithComponentId) ArrayList(java.util.ArrayList) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Map(java.util.Map) LeaderInformation(org.apache.flink.runtime.leaderelection.LeaderInformation)

Example 7 with KubernetesConfigMap

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);
    }
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) CompletionException(java.util.concurrent.CompletionException) CompletedCheckpoint(org.apache.flink.runtime.checkpoint.CompletedCheckpoint) FlinkException(org.apache.flink.util.FlinkException)

Example 8 with KubernetesConfigMap

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;
    });
}
Also used : KubernetesUtils(org.apache.flink.kubernetes.utils.KubernetesUtils) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY(org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY) LEADER_ANNOTATION_KEY(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector.LEADER_ANNOTATION_KEY) KubernetesConfigMapSharedWatcher(org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher) Configuration(org.apache.flink.configuration.Configuration) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException) Map(java.util.Map) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesLeaderElector(org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector) TestingFlinkKubeClient(org.apache.flink.kubernetes.kubeclient.TestingFlinkKubeClient) FlinkKubeClient(org.apache.flink.kubernetes.kubeclient.FlinkKubeClient) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) TestingFlinkKubeClient(org.apache.flink.kubernetes.kubeclient.TestingFlinkKubeClient) CompletionException(java.util.concurrent.CompletionException) KubernetesException(org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)

Example 9 with KubernetesConfigMap

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));
    }
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Arrays(java.util.Arrays) KubernetesTestUtils(org.apache.flink.kubernetes.KubernetesTestUtils) ExceptionUtils(org.apache.flink.util.ExceptionUtils) KubernetesService(org.apache.flink.kubernetes.kubeclient.resources.KubernetesService) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) KubernetesPod(org.apache.flink.kubernetes.kubeclient.resources.KubernetesPod) CONFIG_FILE_LOG4J_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOG4J_NAME) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ExternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator) BlobServerOptions(org.apache.flink.configuration.BlobServerOptions) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) KubernetesClientTestBase(org.apache.flink.kubernetes.KubernetesClientTestBase) JobManagerOptions(org.apache.flink.configuration.JobManagerOptions) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) Matchers.is(org.hamcrest.Matchers.is) Constants(org.apache.flink.kubernetes.utils.Constants) ClusterSpecification(org.apache.flink.client.deployment.ClusterSpecification) FlinkMatchers(org.apache.flink.core.testutils.FlinkMatchers) KubernetesConfigOptionsInternal(org.apache.flink.kubernetes.configuration.KubernetesConfigOptionsInternal) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) ArrayList(java.util.ArrayList) DeploymentOptions(org.apache.flink.configuration.DeploymentOptions) RestOptions(org.apache.flink.configuration.RestOptions) Service(io.fabric8.kubernetes.api.model.Service) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesJobManagerParameters(org.apache.flink.kubernetes.kubeclient.parameters.KubernetesJobManagerParameters) Matchers.isIn(org.hamcrest.Matchers.isIn) KubernetesDeploymentTarget(org.apache.flink.kubernetes.configuration.KubernetesDeploymentTarget) ExecutorService(java.util.concurrent.ExecutorService) Assert.assertNotNull(org.junit.Assert.assertNotNull) Pod(io.fabric8.kubernetes.api.model.Pod) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ExecutionException(java.util.concurrent.ExecutionException) Assert.assertNull(org.junit.Assert.assertNull) CONFIG_FILE_LOGBACK_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOGBACK_NAME) KubernetesJobManagerFactory(org.apache.flink.kubernetes.kubeclient.factory.KubernetesJobManagerFactory) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) PossibleInconsistentStateException(org.apache.flink.runtime.persistence.PossibleInconsistentStateException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 10 with KubernetesConfigMap

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));
}
Also used : KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Test(org.junit.Test)

Aggregations

KubernetesConfigMap (org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap)13 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 PossibleInconsistentStateException (org.apache.flink.runtime.persistence.PossibleInconsistentStateException)4 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CompletionException (java.util.concurrent.CompletionException)3 ExecutionException (java.util.concurrent.ExecutionException)3 KubernetesConfigOptions (org.apache.flink.kubernetes.configuration.KubernetesConfigOptions)3 KubernetesException (org.apache.flink.kubernetes.kubeclient.resources.KubernetesException)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 OwnerReference (io.fabric8.kubernetes.api.model.OwnerReference)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Service (io.fabric8.kubernetes.api.model.Service)2