Search in sources :

Example 86 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) {
    System.out.println("\n\nfabric8 Kubernetes-api example");
    KubernetesClient kube = new DefaultKubernetesClient();
    System.out.println("=========================================================================");
    try {
        listPods(kube);
        listReplicationControllers(kube);
        listServices(kube);
        listServiceAccounts(kube);
        listEndpoints(kube);
    } catch (Exception e) {
        System.out.println("FAILED: " + e);
        e.printStackTrace();
    } finally {
        kube.close();
    }
    System.out.println("=========================================================================");
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 87 with DefaultKubernetesClient

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

the class ViewPipeline method main.

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("Usage: ViewPipeline jobName [branchName] [gitUrl]");
        return;
    }
    String jobName = args[0];
    String branchName = "master";
    String gitUrl = null;
    if (args.length > 1) {
        branchName = args[1];
    }
    if (args.length > 2) {
        gitUrl = args[2];
    }
    try {
        JobEnvironment environment = new JobEnvironment();
        environment.setJobName(jobName);
        environment.setBranchName(branchName);
        environment.setGitUrl(gitUrl);
        KubernetesClient kubernetesClient = new DefaultKubernetesClient();
        String namespace = kubernetesClient.getNamespace();
        if (Strings.isNullOrBlank(namespace)) {
            namespace = KubernetesHelper.defaultNamespace();
        }
        Pipeline pipeline = Pipelines.getPipeline(kubernetesClient, namespace, environment);
        System.out.println("Found pipeline for job: " + pipeline.getJobName() + " of kind: " + pipeline.getKind());
    } catch (IntrospectionException e) {
        System.out.println("Failed with: " + e);
        e.printStackTrace();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) IntrospectionException(java.beans.IntrospectionException) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 88 with DefaultKubernetesClient

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

the class WatchServicesExample method main.

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

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

        @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) Service(io.fabric8.kubernetes.api.model.Service) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 89 with DefaultKubernetesClient

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

the class ListEnvironments method main.

public static void main(String[] args) {
    KubernetesClient kubernetesClient = new DefaultKubernetesClient();
    Environments environments;
    if (args.length > 0) {
        String namespace = args[0];
        System.out.println("Listing environments for namespace: " + namespace);
        environments = Environments.load(namespace);
    } else {
        environments = Environments.load();
    }
    String environmentKey = "testing";
    if (args.length > 1) {
        environmentKey = args[1];
    }
    System.out.println("Space namespace: " + environments.getNamespace());
    SortedSet<Environment> set = environments.getEnvironmentSet();
    for (Environment environment : set) {
        String onCluster = "";
        String clusterAPiServer = environment.getClusterAPiServer();
        if (Strings.isNotBlank(clusterAPiServer)) {
            onCluster += " on API server: " + clusterAPiServer;
        }
        System.out.println("Environment " + environment.getName() + " maps to namespace: " + environment.getNamespace() + onCluster);
    }
    System.out.println("Namespace for environment key: " + environmentKey + " is " + Environments.namespaceForEnvironment(environmentKey));
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 90 with DefaultKubernetesClient

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

the class OpenShiftBuildConfigTest method mainFunctionTest.

/**
 * Test case openshift build config annotation with a main function.
 */
@Test(groups = { "openshift" })
public void mainFunctionTest() throws IOException, InterruptedException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "main_function.bal"), 0);
    File yamlFile = new File(KUBERNETES_TARGET_PATH + File.separator + "main_function.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(), "main-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"), "main-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(), "main_function: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(), "main_function", "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"), "main-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