use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.
the class PodIT method deleteNamespacedPod.
@Test
@DisplayName("deleteNamespacedPod, should delete existing Pod")
void deleteNamespacedPod() throws IOException {
// When
final Pod result = KC.create(CoreV1Api.class).deleteNamespacedPod(podName, NAMESPACE).get();
// Then
assertThat(result).isNotNull().extracting(Pod::getMetadata).hasFieldOrPropertyWithValue("name", podName).extracting(ObjectMeta::getDeletionTimestamp).isNotNull();
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.
the class PodIT method awaitCreateWatch.
@Test
@DisplayName("listNamespacedPod.watch, should await for notification of newly created Pod")
void awaitCreateWatch() throws IOException {
// Given
final AtomicBoolean hasError = new AtomicBoolean(false);
final AtomicBoolean hasCompleted = new AtomicBoolean(false);
// When
final Disposable d = KC.create(CoreV1Api.class).listNamespacedPod(NAMESPACE).watch().filter(we -> we.getObject().getMetadata().getName().equals(podName)).takeUntil(we -> we.getType() == Type.ADDED).timeout(20, TimeUnit.SECONDS).subscribe(we -> hasCompleted.set(true), we -> hasError.set(true));
// Then
assertThat(d).isNotNull();
assertThat(hasError.get()).as("Watch subscribe ended with an error").isFalse();
assertThat(hasCompleted.get()).as("Watch subscribe did not complete").isTrue();
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.
the class WipIT method deployment.
@Test
void deployment() throws IOException {
final AppsV1Api api = KC.create(AppsV1Api.class);
final String deploymentName = "java-test";
try {
api.deleteNamespacedDeployment(deploymentName, "default").get();
System.out.println("Deployment created");
} catch (NotFoundException ex) {
System.out.println("Deployment not found, deletion not needed");
}
final Deployment deployment = api.createNamespacedDeployment("default", Deployment.builder().metadata(ObjectMeta.builder().name(deploymentName).build()).spec(DeploymentSpec.builder().replicas(1).selector(LabelSelector.builder().putInMatchLabels("k8s-app", "test-java").build()).template(PodTemplateSpec.builder().metadata(ObjectMeta.builder().name("java-test-pod").putInLabels("k8s-app", "test-java").build()).spec(PodSpec.builder().addToContainers(Container.builder().image("containous/whoami").name("java-test-pod").build()).build()).build()).build()).build()).get();
System.out.println(deployment);
api.patchNamespacedDeployment(deploymentName, "default", Deployment.builder().spec(deployment.getSpec().toBuilder().replicas(2).build()).build()).get();
final PodList selectedPodList = KC.create(CoreV1Api.class).listPodForAllNamespaces(new ListPodForAllNamespaces().labelSelector("k8s-app=test-pod")).get();
System.out.println(selectedPodList);
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project java by kubernetes-client.
the class ProtoExample method main.
public static void main(String[] args) throws IOException, ApiException, InterruptedException {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
ProtoClient pc = new ProtoClient(client);
ObjectOrStatus<PodList> list = pc.list(PodList.newBuilder(), "/api/v1/namespaces/default/pods");
if (list.object.getItemsCount() > 0) {
Pod p = list.object.getItems(0);
System.out.println(p);
}
Namespace namespace = Namespace.newBuilder().setMetadata(ObjectMeta.newBuilder().setName("test").build()).build();
ObjectOrStatus<Namespace> ns = pc.create(namespace, "/api/v1/namespaces", "v1", "Namespace");
System.out.println(ns);
if (ns.object != null) {
namespace = ns.object.toBuilder().setSpec(NamespaceSpec.newBuilder().addFinalizers("test").build()).build();
// This is how you would update an object, but you can't actually
// update namespaces, so this returns a 405
ns = pc.update(namespace, "/api/v1/namespaces/test", "v1", "Namespace");
System.out.println(ns.status);
}
ns = pc.delete(Namespace.newBuilder(), "/api/v1/namespaces/test");
System.out.println(ns);
}
use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.
the class TopNodes method main.
public static void main(String[] args) {
try (KubernetesClient kc = new KubernetesClient()) {
final Node node = kc.create(CoreV1Api.class).listNode().stream().findFirst().orElseThrow(() -> new IllegalStateException("No nodes found in cluster"));
final Map<String, String> nodeAllocatable = node.getStatus().getAllocatable();
final double allocatableCpu = Resource.CPU.get(nodeAllocatable);
final double allocatableMemory = Resource.MEMORY.get(nodeAllocatable);
final double allocatablePods = Resource.PODS.get(nodeAllocatable);
System.out.printf("Node %s allocatable resources, cpu: %s, memory: %s, pods %s%n", node.getMetadata().getName(), allocatableCpu, bytesToHumanReadable(allocatableMemory), (int) allocatablePods);
System.out.printf(LOG_FORMAT, "POD (CONTAINER)", "CPU / LIMIT", "MEM / LIMIT");
logSeparator();
double totalCpu = 0D;
double totalCpuLimit = 0D;
double totalMemory = 0D;
double totalMemoryLimit = 0D;
final List<PodContainer> containers = kc.create(CoreV1Api.class).listPodForAllNamespaces(new ListPodForAllNamespaces().fieldSelector("spec.nodeName=" + node.getMetadata().getName())).stream().flatMap(p -> p.getSpec().getContainers().stream().map(c -> new PodContainer(p, c))).collect(Collectors.toList());
for (PodContainer c : containers) {
final Map<String, String> containerRequests = c.container.getResources().getRequests();
final double cpu = Resource.CPU.get(containerRequests);
final double memory = Resource.MEMORY.get(containerRequests);
final Map<String, String> containerLimits = c.container.getResources().getLimits();
final double cpuLimit = Resource.CPU.get(containerLimits);
final double memoryLimit = Resource.MEMORY.get(containerLimits);
System.out.printf(LOG_FORMAT, String.format("%s (%s)", c.pod.getMetadata().getName(), c.container.getName()), cpu + " / " + cpuLimit, bytesToHumanReadable(memory) + " / " + bytesToHumanReadable(memoryLimit));
totalCpu += cpu;
totalMemory += memory;
totalCpuLimit += cpuLimit;
totalMemoryLimit += memoryLimit;
}
logSeparator();
System.out.printf(LOG_FORMAT, "TOTAL", totalCpu + " / " + totalCpuLimit, bytesToHumanReadable(totalMemory) + " / " + bytesToHumanReadable(totalMemoryLimit));
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations