Search in sources :

Example 1 with ListOptionsBuilder

use of io.fabric8.kubernetes.api.model.ListOptionsBuilder in project kubernetes-client by fabric8io.

the class CustomResourceTest method testWatchWithListOptions.

@Test
@DisplayName("Should be able to test watch with ListOptions provided")
void testWatchWithListOptions() throws IOException, InterruptedException {
    // Given
    server.expect().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos?resourceVersion=1003&timeoutSeconds=30&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(WATCH_EVENT_PERIOD).andEmit("{\"type\":\"ADDED\", \"object\":{\"kind\": \"Hello\", \"metadata\": {\"resourceVersion\": 1003}}}").done().always();
    CountDownLatch anyEventReceived = new CountDownLatch(1);
    // When
    Watch watch = client.genericKubernetesResources(customResourceDefinitionContext).inNamespace("ns1").watch(new ListOptionsBuilder().withTimeoutSeconds(30L).withResourceVersion("1003").withAllowWatchBookmarks(true).build(), new Watcher<GenericKubernetesResource>() {

        @Override
        public void eventReceived(Action action, GenericKubernetesResource resource) {
            anyEventReceived.countDown();
        }

        @Override
        public void onClose(WatcherException cause) {
        }
    });
    // Then
    assertTrue(anyEventReceived.await(1, TimeUnit.SECONDS));
    watch.close();
}
Also used : Watch(io.fabric8.kubernetes.client.Watch) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with ListOptionsBuilder

use of io.fabric8.kubernetes.api.model.ListOptionsBuilder in project kubernetes-client by fabric8io.

the class WatchTest method testTryWithResourcesConnectsThenReceivesEventBookmark.

@Test
@DisplayName("TryWithResources, connects and receives event then receives GONE, should receive first event and then close")
void testTryWithResourcesConnectsThenReceivesEventBookmark() throws InterruptedException {
    // Given
    server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "BOOKMARK")).waitFor(EVENT_WAIT_PERIOD_MS).andEmit(outdatedEvent()).done().once();
    final CountDownLatch bookmarkLatch = new CountDownLatch(1);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final Watcher<Pod> watcher = new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            if (action != BOOKMARK) {
                fail();
            }
            bookmarkLatch.countDown();
        }

        @Override
        public void onClose(WatcherException cause) {
            assertTrue(cause.isHttpGone());
            closeLatch.countDown();
        }
    };
    // When
    try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new ListOptionsBuilder().withAllowWatchBookmarks(true).build(), watcher)) {
        // Then
        assertNotNull(watch);
        assertTrue(bookmarkLatch.await(10, TimeUnit.SECONDS));
        assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with ListOptionsBuilder

use of io.fabric8.kubernetes.api.model.ListOptionsBuilder in project dubbo-spi-extensions by apache.

the class KubernetesMeshEnvListener method subscribeVs.

private void subscribeVs(String appName) {
    if (vsAppWatch.containsKey(appName)) {
        return;
    }
    try {
        Watch watch = kubernetesClient.customResource(MeshConstant.getVsDefinition()).watch(namespace, appName, null, new ListOptionsBuilder().build(), new Watcher<String>() {

            @Override
            public void eventReceived(Action action, String resource) {
                logger.info("Received VS Rule notification. AppName: " + appName + " Action:" + action + " Resource:" + resource);
                if (action == Action.ADDED || action == Action.MODIFIED) {
                    Map drRuleMap = new Gson().fromJson(resource, Map.class);
                    String vsRule = new Yaml(new SafeConstructor()).dump(drRuleMap);
                    vsAppCache.put(appName, vsRule);
                    if (drAppCache.containsKey(appName)) {
                        notifyListener(vsRule, appName, drAppCache.get(appName));
                    }
                } else {
                    appRuleListenerMap.get(appName).receiveConfigInfo("");
                }
            }

            @Override
            public void onClose(WatcherException cause) {
            // ignore
            }
        });
        vsAppWatch.put(appName, watch);
        try {
            Map<String, Object> vsRule = kubernetesClient.customResource(MeshConstant.getVsDefinition()).get(namespace, appName);
            vsAppCache.put(appName, new Yaml(new SafeConstructor()).dump(vsRule));
        } catch (Throwable ignore) {
        }
    } catch (IOException e) {
        logger.error("Error occurred when listen kubernetes crd.", e);
    }
}
Also used : Gson(com.google.gson.Gson) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) WatcherException(io.fabric8.kubernetes.client.WatcherException) Watch(io.fabric8.kubernetes.client.Watch) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 4 with ListOptionsBuilder

use of io.fabric8.kubernetes.api.model.ListOptionsBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ManagedKafkaResourceType method readiness.

@Override
public Predicate<ManagedKafka> readiness(KubeClient client) {
    AtomicInteger count = new AtomicInteger();
    Set<String> messages = Collections.synchronizedSet(new LinkedHashSet<>());
    return mk -> {
        if (mk == null) {
            throw new IllegalStateException("ManagedKafka is null");
        }
        ManagedKafkaCondition mkc = getCondition(mk.getStatus(), ManagedKafkaCondition.Type.Ready).orElse(null);
        if (mkc == null) {
            return false;
        }
        if (ManagedKafkaCondition.Status.True.name().equals(mkc.getStatus())) {
            return true;
        }
        if (ManagedKafkaCondition.Reason.Error.name().equals(mkc.getReason())) {
            if (messages.add(mkc.getMessage())) {
                LOGGER.warn("ManagedKafka {} in error state {}", mk.getMetadata().getName(), mkc.getMessage());
            }
        // throw new IllegalStateException(String.format("ManagedKafka %s in error state %s", mk.getMetadata().getName(), mkc.getMessage()));
        }
        if (count.getAndIncrement() % 15 == 0) {
            ListOptions opts = new ListOptionsBuilder().withFieldSelector("status.phase=Pending").build();
            client.client().pods().inNamespace(mk.getMetadata().getNamespace()).withLabel("strimzi.io/cluster").list(opts).getItems().forEach(ManagedKafkaResourceType::checkUnschedulablePod);
        }
        return false;
    };
}
Also used : KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ListOptions(io.fabric8.kubernetes.api.model.ListOptions) Resource(io.fabric8.kubernetes.client.dsl.Resource) KubeClient(org.bf2.test.k8s.KubeClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) LinkedHashSet(java.util.LinkedHashSet) KeycloakInstance(org.bf2.systemtest.framework.KeycloakInstance) SecurityUtils(org.bf2.systemtest.framework.SecurityUtils) Predicate(java.util.function.Predicate) TestUtils(org.bf2.test.TestUtils) Pod(io.fabric8.kubernetes.api.model.Pod) SystemTestEnvironment(org.bf2.systemtest.framework.SystemTestEnvironment) Set(java.util.Set) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) Optional(java.util.Optional) Kafka(io.strimzi.api.kafka.model.Kafka) LogManager(org.apache.logging.log4j.LogManager) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListOptions(io.fabric8.kubernetes.api.model.ListOptions) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition)

Example 5 with ListOptionsBuilder

use of io.fabric8.kubernetes.api.model.ListOptionsBuilder in project kubernetes-client by fabric8io.

the class CustomResourceTest method testWatchWithNamespaceAndListOptions.

@Test
@DisplayName("Should be able to test watch with Namespace and ListOptions provided")
void testWatchWithNamespaceAndListOptions() throws IOException, InterruptedException {
    // Given
    server.expect().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos?resourceVersion=1003&timeoutSeconds=30&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(WATCH_EVENT_PERIOD).andEmit("{\"type\":\"ADDED\", \"object\":{\"kind\": \"Hello\", \"metadata\": {\"resourceVersion\": 1003}}}").done().always();
    CountDownLatch anyEventReceived = new CountDownLatch(1);
    // When
    Watch watch = client.genericKubernetesResources(customResourceDefinitionContext).inNamespace("ns1").watch(new ListOptionsBuilder().withTimeoutSeconds(30L).withResourceVersion("1003").withAllowWatchBookmarks(true).build(), new Watcher<GenericKubernetesResource>() {

        @Override
        public void eventReceived(Action action, GenericKubernetesResource resource) {
            anyEventReceived.countDown();
        }

        @Override
        public void onClose(WatcherException cause) {
        }
    });
    // Then
    assertTrue(anyEventReceived.await(1, TimeUnit.SECONDS));
    watch.close();
}
Also used : Watch(io.fabric8.kubernetes.client.Watch) ListOptionsBuilder(io.fabric8.kubernetes.api.model.ListOptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) GenericKubernetesResource(io.fabric8.kubernetes.api.model.GenericKubernetesResource) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ListOptionsBuilder (io.fabric8.kubernetes.api.model.ListOptionsBuilder)11 Watch (io.fabric8.kubernetes.client.Watch)6 WatcherException (io.fabric8.kubernetes.client.WatcherException)6 Test (org.junit.jupiter.api.Test)5 Pod (io.fabric8.kubernetes.api.model.Pod)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 DisplayName (org.junit.jupiter.api.DisplayName)3 Gson (com.google.gson.Gson)2 GenericKubernetesResource (io.fabric8.kubernetes.api.model.GenericKubernetesResource)2 PodList (io.fabric8.kubernetes.api.model.PodList)2 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Yaml (org.yaml.snakeyaml.Yaml)2 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)2 KubernetesResourceList (io.fabric8.kubernetes.api.model.KubernetesResourceList)1 ListOptions (io.fabric8.kubernetes.api.model.ListOptions)1