Search in sources :

Example 36 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.

the class PagerTest method testIteratorForEmptyList.

@Test
public void testIteratorForEmptyList() throws IOException {
    String namespaceListPage0Str = new String(Files.readAllBytes(Paths.get(LIST_PAGE0_FILE_PATH)));
    CoreV1Api api = new CoreV1Api(client);
    stubFor(get(urlPathEqualTo("/api/v1/namespaces")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(namespaceListPage0Str)));
    Pager<V1Namespace, V1NamespaceList> pager = new Pager<V1Namespace, V1NamespaceList>((Pager.PagerParams param) -> {
        try {
            return api.listNamespaceCall(null, null, null, null, null, null, null, null, null, null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, client, 1, V1NamespaceList.class);
    Iterator<V1Namespace> it = pager.iterator();
    assertFalse("Iterator should be empty.", it.hasNext());
}
Also used : V1NamespaceList(io.kubernetes.client.openapi.models.V1NamespaceList) V1Namespace(io.kubernetes.client.openapi.models.V1Namespace) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.

the class PagerTest method testPaginationForNamespaceListWithSuccessThreadSafely.

@Test
public void testPaginationForNamespaceListWithSuccessThreadSafely() throws IOException {
    String namespaceListPage1Str = new String(Files.readAllBytes(Paths.get(LIST_PAGE1_FILE_PATH)));
    String namespaceListPage2Str = new String(Files.readAllBytes(Paths.get(LIST_PAGE2_FILE_PATH)));
    CoreV1Api api = new CoreV1Api(client);
    stubFor(get(urlPathEqualTo("/api/v1/namespaces")).withQueryParam("limit", equalTo("1")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(namespaceListPage1Str)));
    stubFor(get(urlPathEqualTo("/api/v1/namespaces")).withQueryParam("limit", equalTo("1")).withQueryParam("continue", equalTo("c1")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(namespaceListPage2Str)));
    int threads = 10;
    CountDownLatch latch = new CountDownLatch(threads);
    ExecutorService service = Executors.newFixedThreadPool(threads);
    Pager<V1Namespace, V1NamespaceList> pager = new Pager<V1Namespace, V1NamespaceList>((Pager.PagerParams param) -> {
        try {
            return api.listNamespaceCall(null, null, param.getContinueToken(), null, null, param.getLimit(), null, null, null, null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, client, 1, V1NamespaceList.class);
    for (int i = 0; i < threads; i++) {
        service.submit(() -> {
            int size = 0;
            for (V1Namespace namespace : pager) {
                assertEquals("default", namespace.getMetadata().getName());
                size++;
            }
            assertEquals(2, size);
            latch.countDown();
        });
    }
    try {
        latch.await(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail("timed out waiting for pager finished");
    }
    verify(2 * threads, getRequestedFor(urlPathEqualTo("/api/v1/namespaces")).withQueryParam("limit", equalTo("1")));
}
Also used : V1NamespaceList(io.kubernetes.client.openapi.models.V1NamespaceList) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ExecutorService(java.util.concurrent.ExecutorService) V1Namespace(io.kubernetes.client.openapi.models.V1Namespace) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

Example 38 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.

the class FluentExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    CoreV1Api api = new CoreV1Api();
    V1Pod pod = new V1PodBuilder().withNewMetadata().withName("apod").endMetadata().withNewSpec().addNewContainer().withName("www").withImage("nginx").endContainer().endSpec().build();
    api.createNamespacedPod("default", pod, null, null, null, null);
    V1Pod pod2 = new V1Pod().metadata(new V1ObjectMeta().name("anotherpod")).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name("www").image("nginx"))));
    api.createNamespacedPod("default", pod2, null, null, null, null);
    V1PodList list = api.listNamespacedPod("default", null, null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
        System.out.println(item.getMetadata().getName());
    }
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1PodBuilder(io.kubernetes.client.openapi.models.V1PodBuilder) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec)

Example 39 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.

the class InClusterClientExample method main.

public static void main(String[] args) throws IOException, ApiException {
    // loading the in-cluster config, including:
    // 1. service-account CA
    // 2. service-account bearer-token
    // 3. service-account namespace
    // 4. master endpoints(ip, port) from pre-set environment variables
    ApiClient client = ClientBuilder.cluster().build();
    // if you prefer not to refresh service account token, please use:
    // ApiClient client = ClientBuilder.oldCluster().build();
    // set the global default api-client to the in-cluster one from above
    Configuration.setDefaultApiClient(client);
    // the CoreV1Api loads default api-client from global configuration.
    CoreV1Api api = new CoreV1Api();
    // invokes the CoreV1Api client
    V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
    for (V1Pod item : list.getItems()) {
        System.out.println(item.getMetadata().getName());
    }
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Example 40 with CoreV1Api

use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.

the class WatchExample method main.

public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    // infinite timeout
    OkHttpClient httpClient = client.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    client.setHttpClient(httpClient);
    Configuration.setDefaultApiClient(client);
    CoreV1Api api = new CoreV1Api();
    Watch<V1Namespace> watch = Watch.createWatch(client, api.listNamespaceCall(null, null, null, null, null, 5, null, null, null, Boolean.TRUE, null), new TypeToken<Watch.Response<V1Namespace>>() {
    }.getType());
    try {
        for (Watch.Response<V1Namespace> item : watch) {
            System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
        }
    } finally {
        watch.close();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) TypeToken(com.google.gson.reflect.TypeToken) V1Namespace(io.kubernetes.client.openapi.models.V1Namespace) Watch(io.kubernetes.client.util.Watch) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Aggregations

CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)48 V1Pod (io.kubernetes.client.openapi.models.V1Pod)18 ApiClient (io.kubernetes.client.openapi.ApiClient)16 Test (org.junit.Test)16 ApiException (io.kubernetes.client.openapi.ApiException)13 V1PodList (io.kubernetes.client.openapi.models.V1PodList)13 SneakyThrows (lombok.SneakyThrows)12 IOException (java.io.IOException)11 V1Namespace (io.kubernetes.client.openapi.models.V1Namespace)10 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)10 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)9 Watch (io.kubernetes.client.util.Watch)9 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)8 JSON (io.kubernetes.client.openapi.JSON)6 V1Status (io.kubernetes.client.openapi.models.V1Status)6 OkHttpClient (okhttp3.OkHttpClient)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5