use of com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces 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.api.core.v1.CoreV1Api.ListPodForAllNamespaces 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();
}
}
use of com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces in project yakc by manusa.
the class MetricsV1beta1ApiIT method readNamespacedPodMetrics.
@Test
@DisplayName("readNamespacedPodMetrics, cluster contains a Pod with some metrics")
void readNamespacedPodMetrics() throws IOException, InterruptedException {
// Given
final Pod pod = KC.create(CoreV1Api.class).listPodForAllNamespaces(new CoreV1Api.ListPodForAllNamespaces().labelSelector("k8s-app=metrics-server")).stream().findFirst().orElseThrow(() -> new AssertionError("No Pod found for metrics-server"));
// When
final PodMetrics result = getWithRetry(() -> KC.create(MetricsV1beta1Api.class).readNamespacedPodMetrics(pod.getMetadata().getName(), pod.getMetadata().getNamespace()).get());
// Then
assertThat(result).hasFieldOrProperty("apiVersion").hasFieldOrProperty("kind").hasFieldOrPropertyWithValue("metadata.name", pod.getMetadata().getName()).hasFieldOrPropertyWithValue("metadata.namespace", pod.getMetadata().getNamespace()).hasFieldOrProperty("window").hasFieldOrProperty("timestamp").extracting(PodMetrics::getContainers).asList().hasSizeGreaterThanOrEqualTo(1).allSatisfy(c -> assertThat(c).hasFieldOrProperty("usage.cpu").hasFieldOrProperty("usage.memory"));
}
Aggregations