io.fabric8.kubernetes.client.dsl.FilterWatchListMultiDeletable Java Examples

The following examples show how to use io.fabric8.kubernetes.client.dsl.FilterWatchListMultiDeletable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AddressUtils.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private static void waitForAddressesMatched(TimeoutBudget timeoutBudget, int totalDestinations, FilterWatchListMultiDeletable<Address, AddressList, Boolean, Watch, Watcher<Address>> addressClient, AddressListMatcher addressListMatcher) {
    TestUtils.waitUntilCondition(totalDestinations + " match", phase -> {
        try {
            List<Address> addressList = addressClient.list().getItems();
            Map<String, Address> notMatched = addressListMatcher.matchAddresses(addressList);
            if (verboseLogs) {
                notMatched.values().forEach(address ->
                    log.info("Waiting until address {} ready, message {}", address.getMetadata().getName(), address.getStatus().getMessages()));
            }
            if (!notMatched.isEmpty() && phase == WaitPhase.LAST_TRY) {
                log.info(notMatched.size() + " out of " + totalDestinations + " addresses are not matched: " + notMatched.values());
            }
            return notMatched.isEmpty();
        } catch (KubernetesClientException e) {
            if (phase == WaitPhase.LAST_TRY) {
                log.error("Client can't read address resources", e);
            } else {
                log.warn("Client can't read address resources");
            }
            return false;
        }
    }, timeoutBudget);
}
 
Example #2
Source File: SparkClusterOperator.java    From spark-operator with Apache License 2.0 5 votes vote down vote up
private Map<String, Integer> getActual() {
    MixedOperation<ReplicationController, ReplicationControllerList, DoneableReplicationController, RollableScalableResource<ReplicationController, DoneableReplicationController>> aux1 =
            client.replicationControllers();
    FilterWatchListMultiDeletable<ReplicationController, ReplicationControllerList, Boolean, Watch, Watcher<ReplicationController>> aux2 =
            "*".equals(namespace) ? aux1.inAnyNamespace() : aux1.inNamespace(namespace);
    Map<String, String> labels =new HashMap<>(2);
    labels.put(prefix + OPERATOR_KIND_LABEL, entityName);
    labels.put(prefix + OPERATOR_RC_TYPE_LABEL, "worker");
    List<ReplicationController> workerRcs = aux2.withLabels(labels).list().getItems();
    Map<String, Integer> retMap = workerRcs
            .stream()
            .collect(Collectors.toMap(rc -> rc.getMetadata().getLabels().get(prefix + entityName),
                    rc -> rc.getSpec().getReplicas()));
    return retMap;
}
 
Example #3
Source File: AbstractResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked") // due to L extends KubernetesResourceList/*<T>*/
protected List<T> listInAnyNamespace(Labels selector) {
    FilterWatchListMultiDeletable<T, L, Boolean, Watch, Watcher<T>> operation = operation().inAnyNamespace();

    if (selector != null) {
        Map<String, String> labels = selector.toMap();
        return operation.withLabels(labels)
                .list()
                .getItems();
    } else {
        return operation
                .list()
                .getItems();
    }
}
 
Example #4
Source File: AbstractNonNamespacedResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked") // due to L extends KubernetesResourceList/*<T>*/
protected List<T> listInAnyNamespace(Labels selector) {
    FilterWatchListMultiDeletable<T, L, Boolean, Watch, Watcher<T>> operation = operation();

    if (selector != null) {
        Map<String, String> labels = selector.toMap();
        return operation.withLabels(labels)
                .list()
                .getItems();
    } else {
        return operation
                .list()
                .getItems();
    }
}
 
Example #5
Source File: AddressUtils.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private static FilterWatchListMultiDeletable<Address, AddressList, Boolean, Watch, Watcher<Address>> getAddressClient(Address... destinations) {
    List<String> namespaces = Stream.of(destinations)
            .map(address -> address.getMetadata().getNamespace())
            .distinct()
            .collect(Collectors.toList());
    if (namespaces.size() != 1) {
        return Kubernetes.getInstance().getAddressClient().inAnyNamespace();
    } else {
        return Kubernetes.getInstance().getAddressClient(namespaces.get(0));
    }
}
 
Example #6
Source File: ClusterOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that Cluster Operator starts and then stops a verticle in every namespace using the namespace wildcard (*)
 * @param context test context passed in for assertions
 * @param namespaces namespaces the operator should be watching and operating on
 */
private void startStopAllNamespaces(VertxTestContext context, String namespaces, boolean openShift) throws InterruptedException {
    AtomicInteger numWatchers = new AtomicInteger(0);
    KubernetesClient client;
    if (openShift) {
        client = mock(OpenShiftClient.class);
        when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(true);
        when(client.adapt(eq(OpenShiftClient.class))).thenReturn((OpenShiftClient) client);
    } else {
        client = mock(KubernetesClient.class);
        when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(false);
    }
    when(client.isAdaptable(eq(OkHttpClient.class))).thenReturn(true);

    try {
        when(client.getMasterUrl()).thenReturn(new URL("http://localhost"));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    MixedOperation mockCms = mock(MixedOperation.class);
    NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition,
            Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> mockCrds = mock(NonNamespaceOperation.class);
    Resource<CustomResourceDefinition, DoneableCustomResourceDefinition> mockResource = mock(Resource.class);
    if (openShift) {
        when(mockResource.get()).thenReturn(Crds.kafkaConnectS2I());
    } else {
        when(mockResource.get()).thenReturn(null);
    }
    when(mockCrds.withName(KafkaConnectS2I.CRD_NAME)).thenReturn(mockResource);
    when(client.customResourceDefinitions()).thenReturn(mockCrds);
    when(client.customResources(any(), any(), any(), any())).thenReturn(mockCms);

    FilterWatchListMultiDeletable mockFilteredCms = mock(FilterWatchListMultiDeletable.class);
    when(mockFilteredCms.withLabels(any())).thenReturn(mockFilteredCms);
    when(mockFilteredCms.watch(any())).thenAnswer(invo -> {
        numWatchers.incrementAndGet();
        Watch mockWatch = mock(Watch.class);
        doAnswer(invo2 -> {
            ((Watcher) invo.getArgument(0)).onClose(null);
            return null;
        }).when(mockWatch).close();
        return mockWatch;
    });
    when(mockCms.inAnyNamespace()).thenReturn(mockFilteredCms);

    Map<String, String> env = buildEnv(namespaces);

    CountDownLatch latch = new CountDownLatch(2);
    Main.run(vertx, client, new PlatformFeaturesAvailability(openShift, KubernetesVersion.V1_9),
            ClusterOperatorConfig.fromMap(env, KafkaVersionTestUtils.getKafkaVersionLookup()))
        .onComplete(context.succeeding(v -> context.verify(() -> {
            assertThat("A verticle per namespace", vertx.deploymentIDs(), hasSize(1));
            for (String deploymentId: vertx.deploymentIDs()) {
                vertx.undeploy(deploymentId, asyncResult -> {
                    if (asyncResult.failed()) {
                        log.error("Failed to undeploy {}", deploymentId);
                        context.failNow(asyncResult.cause());
                    }
                    latch.countDown();
                });
            }

            int maximumExpectedNumberOfWatchers = openShift ? 9 : 7; // we do not have connectS2I on k8s
            assertThat("Looks like there were more watchers than namespaces", numWatchers.get(), lessThanOrEqualTo(maximumExpectedNumberOfWatchers));
            latch.countDown();
        })));
    latch.await(10, TimeUnit.SECONDS);
    context.completeNow();
}