use of io.kubernetes.client.openapi.models.V1ListMeta in project java by kubernetes-client.
the class GenericKubernetesApiTest method listNamespacedJobReturningObject.
@Test
public void listNamespacedJobReturningObject() {
V1JobList jobList = new V1JobList().kind("JobList").metadata(new V1ListMeta());
stubFor(get(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")).willReturn(aResponse().withStatus(200).withBody(json.serialize(jobList))));
KubernetesApiResponse<V1JobList> jobListResp = jobClient.list("default");
assertTrue(jobListResp.isSuccess());
assertEquals(jobList, jobListResp.getObject());
assertNull(jobListResp.getStatus());
verify(1, getRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")));
}
use of io.kubernetes.client.openapi.models.V1ListMeta in project java by kubernetes-client.
the class GenericKubernetesApiTest method watchNamespacedJobReturningObject.
@Test
public void watchNamespacedJobReturningObject() throws ApiException {
V1JobList jobList = new V1JobList().kind("JobList").metadata(new V1ListMeta());
stubFor(get(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")).willReturn(aResponse().withStatus(200).withBody(json.serialize(jobList))));
Watchable<V1Job> jobListWatch = jobClient.watch("default", new ListOptions());
verify(1, getRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs")).withQueryParam("watch", equalTo("true")));
}
use of io.kubernetes.client.openapi.models.V1ListMeta in project java by kubernetes-client.
the class DefaultSharedIndexInformerWireMockTest method testAllNamespacedPodInformerNormalBehavior.
@Test
public void testAllNamespacedPodInformerNormalBehavior() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
String startRV = "1000";
String endRV = "1001";
V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
stubFor(get(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
Watch.Response<V1Pod> watchResponse = new Watch.Response<>(EventType.ADDED.name(), new V1Pod().metadata(new V1ObjectMeta().namespace(namespace).name(podName).resourceVersion(endRV).labels(Collections.singletonMap("foo", "bar")).annotations(Collections.singletonMap("foo", "bar"))));
stubFor(get(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(watchResponse))));
SharedInformerFactory factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
try {
return coreV1Api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
}, V1Pod.class, V1PodList.class);
podInformer.setTransform((obj) -> {
// deepcopy
String json = new JSON().serialize(obj);
V1Pod pod = new JSON().deserialize(json, V1Pod.class);
// remove pod annotations
pod.getMetadata().setAnnotations(null);
return pod;
});
AtomicBoolean foundExistingPod = new AtomicBoolean(false);
AtomicBoolean transformed = new AtomicBoolean(false);
AtomicBoolean setTransformAfterStarted = new AtomicBoolean(false);
podInformer.addEventHandler(new ResourceEventHandler<V1Pod>() {
@Override
public void onAdd(V1Pod obj) {
if (podName.equals(obj.getMetadata().getName()) && namespace.equals(obj.getMetadata().getNamespace())) {
foundExistingPod.set(true);
}
V1ObjectMeta metadata = obj.getMetadata();
// check if the object was transformed
if (metadata.getLabels().get("foo").equals("bar") && metadata.getAnnotations() == null) {
transformed.set(true);
}
}
@Override
public void onUpdate(V1Pod oldObj, V1Pod newObj) {
}
@Override
public void onDelete(V1Pod obj, boolean deletedFinalStateUnknown) {
}
});
factory.startAllRegisteredInformers();
Thread.sleep(1000);
// can not set transform func if the informer has started
try {
podInformer.setTransform((obj) -> new V1Pod());
setTransformAfterStarted.set(true);
} catch (IllegalStateException e) {
}
assertTrue(foundExistingPod.get());
assertTrue(transformed.get());
assertFalse(setTransformAfterStarted.get());
assertEquals(endRV, podInformer.lastSyncResourceVersion());
verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")));
factory.stopAllRegisteredInformers();
}
use of io.kubernetes.client.openapi.models.V1ListMeta in project java by kubernetes-client.
the class DefaultSharedIndexInformerWireMockTest method testInformerReListWatchOnWatchConflict.
@Test
public void testInformerReListWatchOnWatchConflict() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
String startRV = "1000";
V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
Watch.Response<V1Pod> watchResponse = new Watch.Response<>(EventType.ERROR.name(), new V1Status().apiVersion("v1").kind("Status").code(409));
stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")).withQueryParam("resourceVersion", equalTo(startRV)).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(watchResponse))));
SharedInformerFactory factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> podInformer = factory.sharedIndexInformerFor((CallGeneratorParams params) -> {
try {
return coreV1Api.listNamespacedPodCall(namespace, null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
}, V1Pod.class, V1PodList.class);
factory.startAllRegisteredInformers();
// Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
Thread.sleep(3000);
verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("false")));
verify(moreThan(1), getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods")).withQueryParam("watch", equalTo("true")));
factory.stopAllRegisteredInformers();
}
use of io.kubernetes.client.openapi.models.V1ListMeta in project java by kubernetes-client.
the class ControllerTest method testControllerProcessDeltas.
@Test
public void testControllerProcessDeltas() {
AtomicInteger receivingDeltasCount = new AtomicInteger(0);
V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
V1Pod foo2 = new V1Pod().metadata(new V1ObjectMeta().name("foo2").namespace("default"));
V1Pod foo3 = new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default").resourceVersion("rva"));
V1Pod foo3Updated = new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default").resourceVersion("rvb"));
V1PodList podList = new V1PodList().metadata(new V1ListMeta()).items(Arrays.asList(foo1, foo2, foo3));
DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, new Cache());
ListerWatcher<V1Pod, V1PodList> listerWatcher = new MockRunOnceListerWatcher<V1Pod, V1PodList>(podList, new Watch.Response<V1Pod>(EventType.MODIFIED.name(), foo3Updated));
Controller<V1Pod, V1PodList> controller = new Controller<>(V1Pod.class, deltaFIFO, listerWatcher, (deltas) -> {
receivingDeltasCount.addAndGet(deltas.size());
});
Thread controllerThread = new Thread(controller::run);
controllerThread.setDaemon(true);
controllerThread.start();
try {
Awaitility.await().pollInterval(Duration.ofSeconds(1)).timeout(Duration.ofSeconds(5)).untilAtomic(receivingDeltasCount, IsEqual.equalTo(4));
assertEquals(4, receivingDeltasCount.get());
} catch (Throwable t) {
throw new RuntimeException(t);
} finally {
controller.stop();
}
}
Aggregations