use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.
the class CrdQuickstart method main.
public static void main(String[] args) {
System.out.println("Complete Custom Resource Definition (CRD) example:\n" + " - Creates a namespace\n" + " - Deletes CRD if exists\n" + " - Creates CRD\n" + " - Creates custom resource - TV Show\n" + " - List entries for custom resource - TV Show\n" + " - Patch entry for custom resource - TV Show\n" + " - List patched entries for custom resource - TV Show\n" + "Starting...\n");
try (KubernetesClient kc = new KubernetesClient()) {
final CoreV1Api core = kc.create(CoreV1Api.class);
final ApiextensionsV1Api extensions = kc.create(ApiextensionsV1Api.class);
final ShowsV1Api shows = kc.create(ShowsV1Api.class);
createNamespace(core);
deleteCrdIfExists(extensions);
createCrd(extensions);
createShows(shows);
listShows(shows);
patchShow(shows);
listShows(shows);
deleteCrd(extensions);
} catch (IOException ex) {
ex.printStackTrace();
}
}
use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.
the class PodExec method main.
public static void main(String[] args) {
try (KubernetesClient kc = new KubernetesClient()) {
final ExtendedCoreV1Api api = kc.create(ExtendedCoreV1Api.class);
createPod(api);
api.execInNamespacedPod(POD_NAME, NAMESPACE, Collections.singletonList("pwd")).exec().skip(1).subscribe(em -> System.out.printf("Current container Directory: %s", em.toString()));
System.out.println("Running memory monitor for 5s in the background");
api.execInNamespacedPod(POD_NAME, NAMESPACE, Arrays.asList("/bin/sh", "-c", "for i in $(seq 1 5); do cat /proc/meminfo | grep MemFree & sleep 1; done")).exec().subscribeOn(Schedulers.io()).skip(1).subscribe(t -> System.out.printf(" ++ %s", t.toString()), Throwable::printStackTrace);
api.execInNamespacedPod(POD_NAME, NAMESPACE, Arrays.asList("/bin/sh", "-c", "echo 'Waiting 5S' && sleep 5" + "&& echo 'Hello World in error standard stream' >> /dev/stderr" + "&& echo 'Container execution completed, bye!'")).exec().skip(1).subscribe(t -> System.out.printf("%s: %s", t.getStandardStream(), t.toString()), Throwable::printStackTrace);
System.out.println("Subscriptions completed, cleaning up.");
deletePod(api);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of com.marcnuri.yakc.KubernetesClient in project yakc by manusa.
the class PodLogs method main.
public static void main(String[] args) {
final boolean follow = Stream.of(args).anyMatch(s -> s.matches("-*?follow"));
try (KubernetesClient kc = new KubernetesClient()) {
final CoreV1Api api = kc.create(CoreV1Api.class);
createPod(api);
final KubernetesCall<String> podLogCall = api.readNamespacedPodLog(POD_NAME, NAMESPACE, new ReadNamespacedPodLog().follow(follow).pretty("true").timestamps(true));
if (follow) {
final ExecutorService readLogService = Executors.newSingleThreadExecutor();
final Future<Void> logReader = readLogService.submit(new LogReader(podLogCall));
System.out.println("Log reading started in a parallel thread, following logs for a couple of seconds");
Thread.sleep(10500L);
logReader.cancel(true);
readLogService.shutdownNow();
System.out.println("Log thread finished");
} else {
System.out.println(podLogCall.get());
}
System.out.println("Cleaning up");
api.deleteCollectionNamespacedPod(NAMESPACE, new DeleteCollectionNamespacedPod().gracePeriodSeconds(0).labelSelector(String.format("app=%s", POD_NAME))).get();
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of com.marcnuri.yakc.KubernetesClient 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.KubernetesClient in project yakc by manusa.
the class PodQuickstart method main.
public static void main(String[] args) {
System.out.println("Complete POD example:\n" + " - Creates a namespace\n" + " - Deletes POD if exists\n" + " - Creates POD\n" + " - Patches POD's labels\n" + " - Updates POD removing annotations and adding another label\n" + "Starting...\n");
try (KubernetesClient kc = new KubernetesClient()) {
final CoreV1Api api = kc.create(CoreV1Api.class);
final Disposable podMonitor = monitorPods(api);
createNamespace(api);
deletePodIfExists(api);
createPod(api);
final Pod patchedPod = patchPodLabels(api);
System.out.printf("POD labels patched [%s]%n", patchedPod.getMetadata().getLabels());
final Pod updatedPod = replacePod(patchedPod, api);
System.out.printf("POD replaced: annotations removed, labels modified [%s]%n", updatedPod.getMetadata().getLabels());
podMonitor.dispose();
System.out.println("Pod demo concluded successfully!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations