Search in sources :

Example 56 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8 by fabric8io.

the class KubernetesConfigAdminBridge method watchConfigMapList.

private void watchConfigMapList() {
    if (configWatch) {
        KubernetesClient client = kubernetesClient.get();
        if (client != null) {
            FilterWatchListDeletable<ConfigMap, ConfigMapList, Boolean, Watch, Watcher<ConfigMap>> configMapsSelector = client.configMaps().withLabel(pidLabel);
            for (String key : filters.keySet()) {
                configMapsSelector.withLabelIn(key, filters.get(key).toArray(new String[filters.get(key).size()]));
            }
            watch = configMapsSelector.watch(this);
        } else {
            throw new RuntimeException("KubernetesClient not set");
        }
    }
}
Also used : ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher)

Example 57 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project vertx-openshift-it by cescoffier.

the class Deployment method findImageStream.

public static ImageStream findImageStream(KubernetesClient client, String name) {
    List<ImageStream> items = oc(client).imageStreams().list().getItems();
    for (ImageStream item : items) {
        if (item.getMetadata().getName().equalsIgnoreCase(name)) {
            return item;
        }
    }
    // Try in the "default" namespace
    items = oc(client).imageStreams().inNamespace("default").list().getItems();
    for (ImageStream item : items) {
        if (item.getMetadata().getName().equalsIgnoreCase(name)) {
            return item;
        }
    }
    return null;
}
Also used : ImageStream(io.fabric8.openshift.api.model.ImageStream)

Example 58 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project vertx-openshift-it by cescoffier.

the class Deployment method deployIfNeeded.

public static String deployIfNeeded(KubernetesClient client, File input) {
    OpenShiftClient oc = oc(client);
    assertThat(input).isFile();
    try {
        byte[] bytes = Files.readBytes(input);
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
            DeploymentConfig deploymentConfig = oc.deploymentConfigs().load(bis).get();
            assertThat(deploymentConfig).isNotNull();
            if (oc.deploymentConfigs().withName(deploymentConfig.getMetadata().getName()).get() != null) {
                System.out.println("Skipping the creation of dc/" + deploymentConfig.getMetadata().getName());
                return deploymentConfig.getMetadata().getName();
            }
            oc.deploymentConfigs().create(deploymentConfig);
            OC.execute("deploy", deploymentConfig.getMetadata().getName(), "--latest", "-n", oc.getNamespace());
            return deploymentConfig.getMetadata().getName();
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to deploy deployment config " + input.getAbsolutePath(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 59 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project vertx-openshift-it by cescoffier.

the class Kube method setReplicasAndWait.

public static DeploymentConfig setReplicasAndWait(KubernetesClient client, String name, int number) {
    OpenShiftClient oc = oc(client);
    DeploymentConfig config = oc.deploymentConfigs().withName(name).get();
    if (config == null) {
        fail("Unable to find the deployment config " + name);
        return null;
    }
    if (config.getSpec().getReplicas() == number) {
        return config;
    }
    config = oc.deploymentConfigs().withName(name).edit().editSpec().withReplicas(number).endSpec().done();
    if (number == 0) {
        // Wait until no pods
        await().atMost(duration()).until(() -> getPodsForDeploymentConfig(client, name).size() == 0);
    } else {
        // Wait until the right number of pods
        await().atMost(duration()).until(() -> getPodsForDeploymentConfig(client, name).size() == number);
        // Wait for readiness
        await().atMost(duration()).until(() -> getPodsForDeploymentConfig(client, name).stream().filter(KubernetesHelper::isPodReady).count() == number);
    }
    return config;
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 60 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project strimzi by strimzi.

the class PodOperatorTest method testCreateReadUpdate.

@Test
public void testCreateReadUpdate(TestContext context) {
    vertx.createSharedWorkerExecutor("kubernetes-ops-pool", 10);
    KubernetesClient client = server.getKubernetesClient();
    PodOperator pr = new PodOperator(vertx, client);
    context.assertEquals(emptyList(), pr.list(NAMESPACE, Labels.EMPTY));
    Async async = context.async(1);
    pr.createOrUpdate(resource()).setHandler(createResult -> {
        context.assertTrue(createResult.succeeded());
        context.assertEquals(singletonList(RESOURCE_NAME), pr.list(NAMESPACE, Labels.EMPTY).stream().map(p -> p.getMetadata().getName()).collect(Collectors.toList()));
        // Pod got = pr.get(NAMESPACE, RESOURCE_NAME);
        // context.assertNotNull(got);
        // context.assertNotNull(got.getMetadata());
        // context.assertEquals(RESOURCE_NAME, got.getMetadata().getName());
        context.assertFalse(pr.isReady(NAMESPACE, RESOURCE_NAME));
        /*pr.watch(NAMESPACE, RESOURCE_NAME, new Watcher<Pod>() {
                @Override
                public void eventReceived(Action action, Pod resource) {
                    if (action == Action.DELETED) {
                        context.assertEquals(RESOURCE_NAME, resource.getMetadata().getName());
                    } else {
                        context.fail();
                    }
                    async.countDown();
                }

                @Override
                public void onClose(KubernetesClientException cause) {

                }
            });*/
        /*Pod modified = resource();
            modified.getSpec().setHostname("bar");
            Async patchAsync = context.async();
            pr.patch(NAMESPACE, RESOURCE_NAME, modified, patchResult -> {
                context.assertTrue(patchResult.succeeded());
                patchAsync.complete();
            });
            patchAsync.await();*/
        pr.reconcile(NAMESPACE, RESOURCE_NAME, null).setHandler(deleteResult -> {
            context.assertTrue(deleteResult.succeeded());
            async.countDown();
        });
    });
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Aggregations

KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)62 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)40 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)21 HashMap (java.util.HashMap)19 Pod (io.fabric8.kubernetes.api.model.Pod)17 Service (io.fabric8.kubernetes.api.model.Service)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 IOException (java.io.IOException)12 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 File (java.io.File)10 ArrayList (java.util.ArrayList)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Test (org.junit.Test)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)7 Map (java.util.Map)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 Controller (io.fabric8.kubernetes.api.Controller)6