use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.
the class K8sClient method createAWatchAndReturnOnTermination.
/**
* Create a Watch for a pod and return once the pod has terminated.
* @param namespace Namespace.
* @param podName Name of the pod.
* @return V1ContainerStateTerminated.
*/
@SneakyThrows({ ApiException.class, IOException.class })
private Optional<V1ContainerStateTerminated> createAWatchAndReturnOnTermination(String namespace, String podName) {
log.debug("Creating a watch for pod {}/{}", namespace, podName);
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1ServiceAccount> callback = new K8AsyncCallback<>("createAWatchAndReturnOnTermination-" + podName);
@Cleanup Watch<V1Pod> watch = Watch.createWatch(client, api.listNamespacedPodCall(namespace, PRETTY_PRINT, ALLOW_WATCH_BOOKMARKS, null, null, "POD_NAME=" + podName, null, null, null, Boolean.TRUE, callback), new TypeToken<Watch.Response<V1Pod>>() {
}.getType());
for (Watch.Response<V1Pod> v1PodResponse : watch) {
List<V1ContainerStatus> containerStatuses = v1PodResponse.object.getStatus().getContainerStatuses();
log.debug("Container status for the pod {} is {}", podName, containerStatuses);
if (containerStatuses == null || containerStatuses.size() == 0) {
log.debug("Container status is not part of the pod {}/{}, wait for the next update from KUBERNETES Cluster", namespace, podName);
continue;
}
// We check only the first container as there is only one container in the pod.
V1ContainerState containerStatus = containerStatuses.get(0).getState();
log.debug("Current container status is {}", containerStatus);
if (containerStatus.getTerminated() != null) {
log.info("Pod {}/{} has terminated", namespace, podName);
return Optional.of(containerStatus.getTerminated());
}
}
return Optional.empty();
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.
the class K8sClient method getSecret.
/**
* Method to get V1Secret.
* @param name Name of the Secret.
* @param namespace Namespace on which the pod(s) reside.
* @return Future representing the V1Secret.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1Secret> getSecret(String name, String namespace) {
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1Secret> callback = new K8AsyncCallback<>("readNamespacedSecret-" + name);
api.readNamespacedSecretAsync(name, namespace, PRETTY_PRINT, false, false, callback);
return callback.getFuture();
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.
the class K8sClient method createSecret.
/**
* Method to create V1Secret.
* @param namespace Namespace in which the secret should be created. Secrets cannot be shared outside namespace.
* @param secret V1Secret to create
* @return Future representing the V1Secret.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1Secret> createSecret(String namespace, V1Secret secret) {
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1Secret> callback = new K8AsyncCallback<>("createNamespacedSecret-" + secret.getMetadata().getName());
api.createNamespacedSecretAsync(namespace, secret, PRETTY_PRINT, null, null, callback);
return exceptionallyExpecting(callback.getFuture(), isConflict, null);
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project pravega by pravega.
the class K8sClient method getConfigMap.
/**
* Method to get V1ConfigMap.
* @param name Name of the ConfigMap.
* @param namespace Namespace on which the pod(s) reside.
* @return Future representing the V1ConfigMap.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1ConfigMap> getConfigMap(String name, String namespace) {
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1ConfigMap> callback = new K8AsyncCallback<>("readNamespacedConfigMap-" + name);
api.readNamespacedConfigMapAsync(name, namespace, PRETTY_PRINT, false, false, callback);
return callback.getFuture();
}
use of io.kubernetes.client.openapi.apis.CoreV1Api in project java by kubernetes-client.
the class DefaultControllerBuilderTest method testBuildWatchEventNotificationShouldWork.
@Test
public void testBuildWatchEventNotificationShouldWork() throws InterruptedException {
V1PodList podList = new V1PodList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().name("test-pod1")).spec(new V1PodSpec().hostname("hostname1"))));
stubFor(get(urlPathEqualTo("/api/v1/pods")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(new JSON().serialize(podList))));
CoreV1Api api = new CoreV1Api(client);
SharedIndexInformer<V1Pod> podInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
return api.listPodForAllNamespacesCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
}, V1Pod.class, V1PodList.class);
List<Request> keyFuncReceivingRequests = new ArrayList<>();
Function<V1Pod, Request> podKeyFunc = (V1Pod pod) -> {
// twisting pod name key
Request request = new Request(pod.getSpec().getHostname() + "/" + pod.getMetadata().getName());
keyFuncReceivingRequests.add(request);
return request;
};
List<Request> controllerReceivingRequests = new ArrayList<>();
final Semaphore latch = new Semaphore(1);
latch.acquire();
final Controller testController = ControllerBuilder.defaultBuilder(informerFactory).withReconciler(new Reconciler() {
@Override
public Result reconcile(Request request) {
controllerReceivingRequests.add(request);
latch.release();
return new Result(false);
}
}).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Pod.class, workQueue).withWorkQueueKeyFunc(podKeyFunc).build()).build();
controllerThead.submit(testController::run);
informerFactory.startAllRegisteredInformers();
// Wait for the request to be processed.
latch.acquire(1);
Request expectedRequest = new Request("hostname1/test-pod1");
assertEquals(1, keyFuncReceivingRequests.size());
assertEquals(expectedRequest, keyFuncReceivingRequests.get(0));
assertEquals(1, controllerReceivingRequests.size());
assertEquals(expectedRequest, controllerReceivingRequests.get(0));
}
Aggregations