Search in sources :

Example 46 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class SharedInformerFactoryTest method testClusterScopedNewInformerUsingGenericApi.

@Test
public void testClusterScopedNewInformerUsingGenericApi() {
    SharedInformerFactory factory = new SharedInformerFactory();
    SharedInformer<V1Pod> podInformer = factory.sharedIndexInformerFor(genericKubernetesApi, V1Pod.class, 0);
    assertThat(podInformer).isNotNull();
    when(genericKubernetesApi.list(any(ListOptions.class))).thenReturn(new KubernetesApiResponse<V1PodList>(new V1PodList().metadata(new V1ListMeta().resourceVersion("0"))));
    factory.startAllRegisteredInformers();
    await().timeout(Duration.ofSeconds(2)).until(podInformer::hasSynced);
    verify(genericKubernetesApi, atLeastOnce()).list(any(ListOptions.class));
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ListOptions(io.kubernetes.client.util.generic.options.ListOptions) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Example 47 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class SharedInformerFactoryTest method testNamespaceScopedNewInformerUsingGenericApi.

@Test
public void testNamespaceScopedNewInformerUsingGenericApi() {
    SharedInformerFactory factory = new SharedInformerFactory();
    SharedInformer<V1Pod> podInformer = factory.sharedIndexInformerFor(genericKubernetesApi, V1Pod.class, 0, "default");
    assertThat(podInformer).isNotNull();
    when(genericKubernetesApi.list(eq("default"), any(ListOptions.class))).thenReturn(new KubernetesApiResponse<V1PodList>(new V1PodList().metadata(new V1ListMeta().resourceVersion("0"))));
    factory.startAllRegisteredInformers();
    await().timeout(Duration.ofSeconds(2)).until(podInformer::hasSynced);
    verify(genericKubernetesApi, atLeastOnce()).list(eq("default"), any(ListOptions.class));
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) V1Pod(io.kubernetes.client.openapi.models.V1Pod) ListOptions(io.kubernetes.client.util.generic.options.ListOptions) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test)

Example 48 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class CachesTest method testDefaultNamespaceNameKey.

@Test
public void testDefaultNamespaceNameKey() {
    String testName = "test-name";
    String testNamespace = "test-namespace";
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(testName).namespace(testNamespace));
    assertEquals(testNamespace + "/" + testName, Caches.metaNamespaceKeyFunc(pod));
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 49 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod in project java by kubernetes-client.

the class DeltaFIFOTest method testDeltaFIFOBasic.

@Test
public void testDeltaFIFOBasic() throws InterruptedException {
    Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> receivingDeltas = new LinkedList<>();
    V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
    Cache cache = new Cache();
    DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);
    MutablePair<DeltaFIFO.DeltaType, KubernetesObject> receivingDelta;
    // basic add operation
    deltaFIFO.add(foo1);
    cache.add(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Added, receivingDelta.getLeft());
    // basic update operation
    deltaFIFO.update(foo1);
    cache.update(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Updated, receivingDelta.getLeft());
    // basic delete operation
    deltaFIFO.delete(foo1);
    cache.delete(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Deleted, receivingDelta.getLeft());
    // basic sync operation
    deltaFIFO.replace(Arrays.asList(foo1), "0");
    cache.replace(Arrays.asList(foo1), "0");
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Sync, receivingDelta.getLeft());
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 50 with V1Pod

use of io.kubernetes.client.openapi.models.V1Pod 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)

Aggregations

V1Pod (io.kubernetes.client.openapi.models.V1Pod)96 Test (org.junit.Test)55 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)38 V1PodList (io.kubernetes.client.openapi.models.V1PodList)31 Type (java.lang.reflect.Type)22 ApiException (io.kubernetes.client.openapi.ApiException)17 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)17 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)15 ApiClient (io.kubernetes.client.openapi.ApiClient)12 V1Status (io.kubernetes.client.openapi.models.V1Status)9 Watch (io.kubernetes.client.util.Watch)9 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)8 JSON (io.kubernetes.client.openapi.JSON)7 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)7 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)4 V1Patch (io.kubernetes.client.custom.V1Patch)4 V1PodBuilder (io.kubernetes.client.openapi.models.V1PodBuilder)4