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());
}
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")));
}
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());
}
}
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());
}
}
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();
}
}
Aggregations