Search in sources :

Example 1 with NamespaceList

use of io.fabric8.kubernetes.api.model.NamespaceList in project camel by apache.

the class KubernetesNamespacesProducer method doListNamespaceByLabel.

protected void doListNamespaceByLabel(Exchange exchange, String operation) {
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_LABELS, Map.class);
    if (ObjectHelper.isEmpty(labels)) {
        LOG.error("Get a specific namespace by labels require specify a labels set");
        throw new IllegalArgumentException("Get a specific namespace by labels require specify a labels set");
    }
    NonNamespaceOperation<Namespace, NamespaceList, DoneableNamespace, Resource<Namespace, DoneableNamespace>> namespaces = getEndpoint().getKubernetesClient().namespaces();
    for (Map.Entry<String, String> entry : labels.entrySet()) {
        namespaces.withLabel(entry.getKey(), entry.getValue());
    }
    NamespaceList namespace = namespaces.list();
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(namespace.getItems());
}
Also used : DoneableNamespace(io.fabric8.kubernetes.api.model.DoneableNamespace) Resource(io.fabric8.kubernetes.client.dsl.Resource) Map(java.util.Map) DoneableNamespace(io.fabric8.kubernetes.api.model.DoneableNamespace) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceList(io.fabric8.kubernetes.api.model.NamespaceList)

Example 2 with NamespaceList

use of io.fabric8.kubernetes.api.model.NamespaceList in project strimzi by strimzi.

the class ClusterControllerTest method startStop.

/**
 * Does the CC start and then stop a verticle per namespace?
 * @param context
 * @param namespaces
 */
private void startStop(TestContext context, String namespaces) {
    AtomicInteger numWatchers = new AtomicInteger(0);
    KubernetesClient client = mock(KubernetesClient.class);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(client.configMaps()).thenReturn(mockCms);
    List<String> namespaceList = asList(namespaces.split(" *,+ *"));
    for (String namespace : namespaceList) {
        MixedOperation mockNamespacedCms = mock(MixedOperation.class);
        when(mockNamespacedCms.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(mockNamespacedCms.withLabels(any())).thenReturn(mockNamespacedCms);
        when(mockCms.inNamespace(namespace)).thenReturn(mockNamespacedCms);
    }
    Async async = context.async();
    Map<String, String> env = new HashMap<>();
    env.put(ClusterControllerConfig.STRIMZI_NAMESPACE, namespaces);
    env.put(ClusterControllerConfig.STRIMZI_CONFIGMAP_LABELS, STRIMZI_IO_KIND_CLUSTER);
    env.put(ClusterControllerConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS, "120000");
    Main.run(vertx, client, true, env).setHandler(ar -> {
        context.assertNull(ar.cause(), "Expected all verticles to start OK");
        async.complete();
    });
    async.await();
    context.assertEquals(namespaceList.size(), vertx.deploymentIDs().size(), "A verticle per namespace");
    List<Async> asyncs = new ArrayList<>();
    for (String deploymentId : vertx.deploymentIDs()) {
        Async async2 = context.async();
        asyncs.add(async2);
        vertx.undeploy(deploymentId, ar -> {
            context.assertNull(ar.cause(), "Didn't expect error when undeploying verticle " + deploymentId);
            async2.complete();
        });
    }
    for (Async async2 : asyncs) {
        async2.await();
    }
    if (numWatchers.get() > namespaceList.size()) {
        context.fail("Looks like there were more watchers than namespaces");
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) Watch(io.fabric8.kubernetes.client.Watch) ArrayList(java.util.ArrayList) Watcher(io.fabric8.kubernetes.client.Watcher) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation)

Example 3 with NamespaceList

use of io.fabric8.kubernetes.api.model.NamespaceList in project camel by apache.

the class KubernetesNamespacesProducer method doList.

protected void doList(Exchange exchange, String operation) throws Exception {
    NamespaceList namespacesList = getEndpoint().getKubernetesClient().namespaces().list();
    exchange.getOut().setBody(namespacesList.getItems());
}
Also used : NamespaceList(io.fabric8.kubernetes.api.model.NamespaceList)

Aggregations

NamespaceList (io.fabric8.kubernetes.api.model.NamespaceList)2 DoneableNamespace (io.fabric8.kubernetes.api.model.DoneableNamespace)1 Namespace (io.fabric8.kubernetes.api.model.Namespace)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 Watch (io.fabric8.kubernetes.client.Watch)1 Watcher (io.fabric8.kubernetes.client.Watcher)1 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)1 Resource (io.fabric8.kubernetes.client.dsl.Resource)1 Async (io.vertx.ext.unit.Async)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1