Search in sources :

Example 26 with V1ObjectMeta

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

the class CacheTest method testAddIndexers.

@Test
public void testAddIndexers() {
    Cache<V1Pod> podCache = new Cache<>();
    String nodeIndex = "node-index";
    String clusterIndex = "cluster-index";
    Map<String, Function<V1Pod, List<String>>> indexers = new HashMap<>();
    indexers.put(nodeIndex, (V1Pod pod) -> {
        return Arrays.asList(pod.getSpec().getNodeName());
    });
    indexers.put(clusterIndex, (V1Pod pod) -> {
        return Arrays.asList(pod.getMetadata().getClusterName());
    });
    podCache.addIndexers(indexers);
    V1Pod testPod = new V1Pod().metadata(new V1ObjectMeta().namespace("ns").name("n").clusterName("cluster1")).spec(new V1PodSpec().nodeName("node1"));
    podCache.add(testPod);
    List<V1Pod> namespaceIndexedPods = podCache.byIndex(Caches.NAMESPACE_INDEX, "ns");
    assertEquals(1, namespaceIndexedPods.size());
    List<V1Pod> nodeNameIndexedPods = podCache.byIndex(nodeIndex, "node1");
    assertEquals(1, nodeNameIndexedPods.size());
    List<V1Pod> clusterNameIndexedPods = podCache.byIndex(clusterIndex, "cluster1");
    assertEquals(1, clusterNameIndexedPods.size());
}
Also used : Function(java.util.function.Function) HashMap(java.util.HashMap) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) Test(org.junit.Test)

Example 27 with V1ObjectMeta

use of io.kubernetes.client.openapi.models.V1ObjectMeta 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();
    }
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) V1PodList(io.kubernetes.client.openapi.models.V1PodList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Watch(io.kubernetes.client.util.Watch) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 28 with V1ObjectMeta

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

the class ListerTest method testListerBasic.

@Test
public void testListerBasic() {
    Cache<V1Pod> podCache = new Cache<>();
    Lister<V1Pod> namespacedPodLister = new Lister<>(podCache, "default");
    List<V1Pod> emptyPodList = namespacedPodLister.list();
    assertEquals(0, emptyPodList.size());
    podCache.replace(Arrays.asList(new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default")), new V1Pod().metadata(new V1ObjectMeta().name("foo2").namespace("default")), new V1Pod().metadata(new V1ObjectMeta().name("foo3").namespace("default"))), "0");
    List<V1Pod> namespacedPodList = namespacedPodLister.list();
    assertEquals(3, namespacedPodList.size());
    Lister<V1Pod> allNamespacedPodLister = new Lister<>(podCache);
    List<V1Pod> allPodList = allNamespacedPodLister.list();
    assertEquals(3, allPodList.size());
    namespacedPodList = allNamespacedPodLister.namespace("default").list();
    assertEquals(3, namespacedPodList.size());
}
Also used : V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) Test(org.junit.Test)

Example 29 with V1ObjectMeta

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

the class Jobs method cronJobToJob.

/**
 * Convert V1beta1CronJob object into V1Job object, based on kubectl code
 * https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/create/create_job.go
 *
 * @param cronJob cronJob object (required)
 * @param jobName cronJob name
 * @return V1Job object
 */
public static V1Job cronJobToJob(V1beta1CronJob cronJob, String jobName) {
    Map<String, String> annotations = new HashMap<>();
    Map<String, String> labels = new HashMap<>();
    V1JobSpec jobSpec = null;
    V1beta1CronJobSpec cronJobSpec = cronJob.getSpec();
    if (cronJobSpec != null && cronJobSpec.getJobTemplate() != null) {
        V1ObjectMeta metadata = cronJobSpec.getJobTemplate().getMetadata();
        if (metadata != null) {
            if (metadata.getAnnotations() != null) {
                annotations.putAll(metadata.getAnnotations());
            }
            if (metadata.getLabels() != null) {
                labels.putAll(metadata.getLabels());
            }
        }
        jobSpec = cronJobSpec.getJobTemplate().getSpec();
    }
    annotations.put("cronjob.kubernetes.io/instantiate", "manual");
    V1OwnerReference v1OwnerReference = new V1OwnerReference();
    v1OwnerReference.setKind("CronJob");
    v1OwnerReference.setName(cronJob.getMetadata().getName());
    v1OwnerReference.setBlockOwnerDeletion(true);
    v1OwnerReference.setController(true);
    v1OwnerReference.setUid(cronJob.getMetadata().getUid());
    v1OwnerReference.setApiVersion("batch/v1beta1");
    V1ObjectMeta jobMetadata = new V1ObjectMeta();
    jobMetadata.setName(jobName != null ? jobName : cronJob.getMetadata().getName() + "-manual");
    jobMetadata.setAnnotations(annotations);
    jobMetadata.setLabels(labels);
    jobMetadata.setOwnerReferences(Arrays.asList(v1OwnerReference));
    V1Job job = new V1Job();
    job.setKind("Job");
    job.setApiVersion("batch/v1");
    job.setMetadata(jobMetadata);
    job.setSpec(jobSpec);
    return job;
}
Also used : V1Job(io.kubernetes.client.openapi.models.V1Job) HashMap(java.util.HashMap) V1beta1CronJobSpec(io.kubernetes.client.openapi.models.V1beta1CronJobSpec) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1JobSpec(io.kubernetes.client.openapi.models.V1JobSpec) V1OwnerReference(io.kubernetes.client.openapi.models.V1OwnerReference)

Example 30 with V1ObjectMeta

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

the class PodLogsTest method testNotFound.

@Test
public void testNotFound() throws ApiException, IOException {
    V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace)).spec(new V1PodSpec().containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
    wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).willReturn(aResponse().withStatus(404).withHeader("Content-Type", "text/plain").withBody("Not Found")));
    PodLogs logs = new PodLogs(client);
    boolean thrown = false;
    try {
        logs.streamNamespacedPodLog(pod);
    } catch (ApiException ex) {
        assertEquals(404, ex.getCode());
        thrown = true;
    }
    assertEquals(thrown, true);
    wireMockRule.verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/log")).withQueryParam("container", equalTo(container)).withQueryParam("follow", equalTo("true")).withQueryParam("pretty", equalTo("false")).withQueryParam("previous", equalTo("false")).withQueryParam("timestamps", equalTo("false")));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) V1PodSpec(io.kubernetes.client.openapi.models.V1PodSpec) ApiException(io.kubernetes.client.openapi.ApiException) Test(org.junit.Test)

Aggregations

V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)78 Test (org.junit.Test)49 V1Pod (io.kubernetes.client.openapi.models.V1Pod)37 HashMap (java.util.HashMap)14 ApiException (io.kubernetes.client.openapi.ApiException)11 V1PodList (io.kubernetes.client.openapi.models.V1PodList)11 V1PodSpec (io.kubernetes.client.openapi.models.V1PodSpec)10 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)8 V1Container (io.kubernetes.client.openapi.models.V1Container)7 ApiClient (io.kubernetes.client.openapi.ApiClient)6 JSON (io.kubernetes.client.openapi.JSON)6 V1Job (io.kubernetes.client.openapi.models.V1Job)6 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)6 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)6 V1Patch (io.kubernetes.client.custom.V1Patch)5 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 V1Service (io.kubernetes.client.openapi.models.V1Service)5 V1ServiceSpec (io.kubernetes.client.openapi.models.V1ServiceSpec)5 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)5