use of io.kubernetes.client.openapi.apis.CoreV1Api 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.apis.CoreV1Api in project twister2 by DSC-SPIDAL.
the class PodWatchUtils method createApiInstances.
private static void createApiInstances() {
try {
apiClient = io.kubernetes.client.util.Config.defaultClient();
} catch (IOException e) {
LOG.log(Level.SEVERE, "Exception when creating ApiClient: ", e);
throw new RuntimeException(e);
}
OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
apiClient.setHttpClient(httpClient);
Configuration.setDefaultApiClient(apiClient);
coreApi = new CoreV1Api(apiClient);
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project twister2 by DSC-SPIDAL.
the class JobKillWatcher method run.
/**
* start the watcher
*/
@Override
public void run() {
String killParam = "KILL_JOB";
String cmName = jobID;
String labelSelector = KubernetesUtils.jobLabelSelector(jobID);
Integer timeoutSeconds = Integer.MAX_VALUE;
CoreV1Api v1Api = controller.createCoreV1Api();
try {
watcher = Watch.createWatch(controller.getApiClient(), v1Api.listNamespacedConfigMapCall(namespace, null, null, null, null, labelSelector, null, null, timeoutSeconds, Boolean.TRUE, null), new TypeToken<Watch.Response<V1ConfigMap>>() {
}.getType());
} catch (ApiException e) {
String logMessage = "Exception when watching the ConfigMap: \n" + "exCode: " + e.getCode() + "\n" + "responseBody: " + e.getResponseBody();
LOG.log(Level.SEVERE, logMessage, e);
throw new RuntimeException(e);
}
try {
for (Watch.Response<V1ConfigMap> item : watcher) {
// it means that the pod is in the process of being deleted
if (item.object != null && item.object.getData() != null && item.object.getMetadata().getName().equals(cmName) && item.object.getData().get(killParam) != null) {
LOG.info("Job Kill parameter received. Killing the job");
jobMaster.endJob(JobAPI.JobState.KILLED);
return;
}
}
} catch (RuntimeException e) {
if (stopWatcher) {
LOG.fine("Watcher is stopped.");
return;
} else {
throw e;
}
} finally {
try {
watcher.close();
} catch (IOException e) {
LOG.warning("IOException when closing ConfigMapWatcher");
}
}
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.
the class K8sClient method createConfigMap.
/**
* Create ConfigMap.
* @param namespace The namespace where the ConfigMap should be created.
* @param binding The cluster ConfigMap.
* @return A future indicating the status of the ConfigMap operation.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1ConfigMap> createConfigMap(String namespace, V1ConfigMap binding) {
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1ConfigMap> callback = new K8AsyncCallback<>("createConfigMap-" + binding.getMetadata().getName());
api.createNamespacedConfigMapAsync(namespace, binding, PRETTY_PRINT, DRY_RUN, FIELD_MANAGER, callback);
return exceptionallyExpecting(callback.getFuture(), isConflict, null);
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.
the class K8sClient method getPodsWithLabels.
/**
* Method to fetch all pods which match a set of labels.
* @param namespace Namespace on which the pod(s) reside.
* @param labels Name of the label.
* @return Future representing the list of pod status.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1PodList> getPodsWithLabels(String namespace, Map<String, String> labels) {
CoreV1Api api = new CoreV1Api();
log.debug("Current number of http interceptors {}", api.getApiClient().getHttpClient().networkInterceptors().size());
String labelSelector = labels.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining());
K8AsyncCallback<V1PodList> callback = new K8AsyncCallback<>("listPods-" + labels);
api.listNamespacedPodAsync(namespace, PRETTY_PRINT, ALLOW_WATCH_BOOKMARKS, null, null, labelSelector, null, null, null, false, callback);
return callback.getFuture();
}
Aggregations