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());
}
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();
}
}
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());
}
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;
}
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")));
}
Aggregations