Search in sources :

Example 21 with CoreV1Api

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();
}
Also used : V1ContainerStatus(io.kubernetes.client.openapi.models.V1ContainerStatus) V1ServiceAccount(io.kubernetes.client.openapi.models.V1ServiceAccount) Cleanup(lombok.Cleanup) TypeToken(com.google.gson.reflect.TypeToken) V1ContainerState(io.kubernetes.client.openapi.models.V1ContainerState) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) SneakyThrows(lombok.SneakyThrows)

Example 22 with CoreV1Api

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();
}
Also used : V1Secret(io.kubernetes.client.openapi.models.V1Secret) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) SneakyThrows(lombok.SneakyThrows)

Example 23 with CoreV1Api

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);
}
Also used : V1Secret(io.kubernetes.client.openapi.models.V1Secret) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) SneakyThrows(lombok.SneakyThrows)

Example 24 with CoreV1Api

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();
}
Also used : CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) SneakyThrows(lombok.SneakyThrows)

Example 25 with CoreV1Api

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));
}
Also used : V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) JSON(io.kubernetes.client.openapi.JSON) Arrays(java.util.Arrays) Result(io.kubernetes.client.extended.controller.reconciler.Result) V1PodList(io.kubernetes.client.openapi.models.V1PodList) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Function(java.util.function.Function) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) WireMock(com.github.tomakehurst.wiremock.client.WireMock) ApiClient(io.kubernetes.client.openapi.ApiClient) ArrayList(java.util.ArrayList) Request(io.kubernetes.client.extended.controller.reconciler.Request) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) After(org.junit.After) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Controller(io.kubernetes.client.extended.controller.Controller) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Executors(java.util.concurrent.Executors) ClientBuilder(io.kubernetes.client.util.ClientBuilder) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) List(java.util.List) Rule(org.junit.Rule) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) Assert(org.junit.Assert) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Request(io.kubernetes.client.extended.controller.reconciler.Request) ArrayList(java.util.ArrayList) JSON(io.kubernetes.client.openapi.JSON) Semaphore(java.util.concurrent.Semaphore) Controller(io.kubernetes.client.extended.controller.Controller) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Result(io.kubernetes.client.extended.controller.reconciler.Result) V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.Test)

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