Search in sources :

Example 16 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project fabric8 by jboss-fuse.

the class ListSpaces method main.

public static void main(String[] args) {
    String namespace = null;
    if (args.length > 0) {
        namespace = args[0];
    }
    KubernetesClient kubernetesClient = new DefaultKubernetesClient();
    Spaces spaces = Spaces.load(kubernetesClient, namespace);
    SortedSet<Space> set = spaces.getSpaceSet();
    for (Space space : set) {
        System.out.println("Space " + space.getName() + " = " + space);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 17 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project fabric8 by jboss-fuse.

the class WatchPodsExample method main.

public static void main(String... args) throws Exception {
    KubernetesClient client = new DefaultKubernetesClient();
    client.pods().watch(new io.fabric8.kubernetes.client.Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod pod) {
            System.out.println(action + ": " + pod);
        }

        @Override
        public void onClose(KubernetesClientException e) {
            System.out.println("Closed: " + e);
        }
    });
    client.close();
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 18 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project fabric8 by jboss-fuse.

the class Example method main.

public static void main(String[] args) {
    try {
        final KubernetesClient client = new DefaultKubernetesClient();
        assertThat(client).pods().runningStatus().hasSize(6);
        assertThat(client).pods().runningStatus().filterLabel("provider", "fabric8").assertSize().isGreaterThan(0);
        assertThat(client.services().inNamespace("default").withName("fabric8").get().getMetadata()).name().isEqualTo("fabric8");
        Map<String, String> consoleLabels = new HashMap<>();
        consoleLabels.put("component", "console");
        consoleLabels.put("provider", "fabric8");
        assertThat(client).podsForService("fabric8").runningStatus().extracting("metadata").extracting("labels").contains(consoleLabels);
        assertThat(client).podsForService("fabric8").runningStatus().hasSize(1).extracting("metadata").extracting("labels").contains(consoleLabels);
        assertThat(client).podsForService("fabric8").logs().doesNotContainText("Exception", "Error");
        assertThat(client).pods().logs().doesNotContainText("Exception", "Error");
        assertAssertionError(new Block() {

            @Override
            public void invoke() throws Exception {
                try {
                    assertThat(client.services().inNamespace("default").withName("doesNotExist").get().getMetadata()).name().isEqualTo("fabric8-console-controller");
                } catch (KubernetesClientException e) {
                    if (e.getCode() != 404) {
                        throw e;
                    } else {
                        throw new AssertionError(e);
                    }
                }
            }
        });
        assertAssertionError(new Block() {

            @Override
            public void invoke() throws Exception {
                try {
                    assertThat(client).pods().runningStatus().filterLabel("component", "doesNotExist").hasSize(1);
                } catch (KubernetesClientException e) {
                    if (e.getCode() != 404) {
                        throw e;
                    } else {
                        throw new AssertionError(e);
                    }
                }
            }
        });
        System.out.println("Done!");
    } catch (Throwable e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HashMap(java.util.HashMap) Asserts.assertAssertionError(io.fabric8.utils.Asserts.assertAssertionError) Block(io.fabric8.utils.Block) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 19 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project fabric8 by jboss-fuse.

the class Apply method main.

public static void main(String... args) {
    if (args.length < 1) {
        System.out.println("Usage jsonFileToApply");
        return;
    }
    try {
        KubernetesClient kube = new DefaultKubernetesClient();
        File file = new File(args[0]);
        System.out.println("Applying file: " + file);
        if (!file.exists() || !file.isFile()) {
            System.out.println("File does not exist! " + file.getAbsolutePath());
            return;
        }
        Controller controller = new Controller(kube);
        String answer = controller.apply(file);
        System.out.println("Applied!: " + answer);
    } catch (Exception e) {
        System.out.println("FAILED: " + e);
        e.printStackTrace();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File)

Example 20 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project kubernetes by ballerinax.

the class OpenShiftBuildConfigTest method simpleBuildConfigTest.

/**
 * Test case openshift build config annotation with default values.
 */
@Test(groups = { "openshift" })
public void simpleBuildConfigTest() throws IOException, InterruptedException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "simple_bc.bal"), 0);
    File yamlFile = new File(KUBERNETES_TARGET_PATH + File.separator + "simple_bc.yaml");
    Assert.assertTrue(yamlFile.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
    for (HasMetadata data : k8sItems) {
        switch(data.getKind()) {
            case "Service":
            case "Deployment":
                break;
            case "BuildConfig":
                BuildConfig bc = (BuildConfig) data;
                // metadata
                Assert.assertNotNull(bc.getMetadata());
                Assert.assertEquals(bc.getMetadata().getName(), "helloep-openshift-bc", "Invalid name found.");
                Assert.assertNotNull(bc.getMetadata().getLabels(), "Labels are missing");
                Assert.assertEquals(bc.getMetadata().getLabels().size(), 1, "Labels are missing");
                Assert.assertNotNull(bc.getMetadata().getLabels().get("build"), "'build' label is missing");
                Assert.assertEquals(bc.getMetadata().getLabels().get("build"), "helloep-openshift-bc", "Invalid label 'build' label value.");
                // spec
                Assert.assertNotNull(bc.getSpec());
                Assert.assertNotNull(bc.getSpec().getOutput());
                Assert.assertNotNull(bc.getSpec().getOutput().getTo());
                Assert.assertEquals(bc.getSpec().getOutput().getTo().getKind(), "ImageStreamTag", "Invalid output kind.");
                Assert.assertEquals(bc.getSpec().getOutput().getTo().getName(), "simple_bc:latest", "Invalid image stream name.");
                Assert.assertNotNull(bc.getSpec().getSource());
                Assert.assertNotNull(bc.getSpec().getSource().getBinary(), "Binary source is missing");
                Assert.assertNotNull(bc.getSpec().getStrategy());
                Assert.assertNotNull(bc.getSpec().getStrategy().getDockerStrategy(), "Docker strategy is missing.");
                Assert.assertEquals(bc.getSpec().getStrategy().getDockerStrategy().getBuildArgs().size(), 0, "Invalid number of build args.");
                Assert.assertEquals(bc.getSpec().getStrategy().getDockerStrategy().getDockerfilePath(), "kubernetes/docker/Dockerfile", "Invalid docker path.");
                Assert.assertFalse(bc.getSpec().getStrategy().getDockerStrategy().getForcePull(), "Force pull image set to false");
                Assert.assertFalse(bc.getSpec().getStrategy().getDockerStrategy().getNoCache(), "No cache for image build set to false");
                break;
            case "ImageStream":
                ImageStream is = (ImageStream) data;
                Assert.assertNotNull(is.getMetadata());
                Assert.assertEquals(is.getMetadata().getName(), "simple_bc", "Invalid name found.");
                Assert.assertEquals(is.getMetadata().getLabels().size(), 1, "Labels are missing");
                Assert.assertNotNull(is.getMetadata().getLabels().get("build"), "'build' label is missing");
                Assert.assertEquals(is.getMetadata().getLabels().get("build"), "helloep-openshift-bc", "Invalid label 'build' label value.");
                Assert.assertNull(is.getSpec());
                break;
            default:
                Assert.fail("Unexpected k8s resource found: " + data.getKind());
                break;
        }
    }
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ImageStream(io.fabric8.openshift.api.model.ImageStream) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Aggregations

DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)101 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)77 File (java.io.File)27 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)22 FileInputStream (java.io.FileInputStream)22 HashMap (java.util.HashMap)13 Test (org.testng.annotations.Test)12 Config (io.fabric8.kubernetes.client.Config)11 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)11 BeforeClass (org.testng.annotations.BeforeClass)10 Service (io.fabric8.kubernetes.api.model.Service)8 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)6 TaskAction (org.gradle.api.tasks.TaskAction)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 Map (java.util.Map)5 Test (org.junit.Test)5 KubernetesModelGenerator (com.github.isdream.chameleon.container.kubernetes.KubernetesModelGenerator)4 ImmutableMap (com.google.common.collect.ImmutableMap)4