Search in sources :

Example 6 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod 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"));
}
Also used : Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) PodMetrics(com.marcnuri.yakc.model.io.k8s.metrics.pkg.apis.metrics.v1beta1.PodMetrics) MetricsV1beta1Api(com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 7 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod 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();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) KubernetesClient(com.marcnuri.yakc.KubernetesClient) Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) IOException(java.io.IOException) NotFoundException(com.marcnuri.yakc.api.NotFoundException)

Example 8 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.

the class PodIT method patchNamespacedPod.

@Test
@DisplayName("patchNamespacedPod, should patch labels of Pod")
void patchNamespacedPod() throws IOException {
    // When
    final Pod result = KC.create(CoreV1Api.class).patchNamespacedPod(podName, NAMESPACE, Pod.builder().metadata(ObjectMeta.builder().putInLabels("patched", "label").build()).build()).get();
    // Then
    assertThat(result).isNotNull().hasFieldOrPropertyWithValue("metadata.name", podName).extracting(Pod::getMetadata).hasFieldOrPropertyWithValue("name", podName).extracting(ObjectMeta::getLabels).asInstanceOf(InstanceOfAssertFactories.MAP).hasSize(2).containsEntry("patched", "label");
}
Also used : Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 9 with Pod

use of com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod in project yakc by manusa.

the class PodIT method replaceNamespacedPod.

@Test
@DisplayName("replaceNamespacedPod, should replace existing Pod's image")
void replaceNamespacedPod() throws IOException {
    // Given
    awaitPodReady();
    final Pod existingPod = KC.create(CoreV1Api.class).readNamespacedPod(podName, NAMESPACE).get();
    existingPod.getMetadata().setResourceVersion(null);
    existingPod.getSpec().getContainers().get(0).setImage("nginxdemos/hello");
    // When
    final Pod result = KC.create(CoreV1Api.class).replaceNamespacedPod(podName, NAMESPACE, existingPod).get();
    // Then
    assertThat(result).isNotNull().hasFieldOrPropertyWithValue("metadata.name", podName).extracting(Pod::getSpec).extracting(PodSpec::getContainers).asList().hasSize(1).element(0).hasFieldOrPropertyWithValue("image", "nginxdemos/hello");
}
Also used : Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) PodSpec(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Pod (com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod)7 Test (org.junit.jupiter.api.Test)6 DisplayName (org.junit.jupiter.api.DisplayName)5 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)4 NotFoundException (com.marcnuri.yakc.api.NotFoundException)3 KubernetesClient (com.marcnuri.yakc.KubernetesClient)2 ListPodForAllNamespaces (com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)2 Container (com.marcnuri.yakc.model.io.k8s.api.core.v1.Container)2 PodSpec (com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec)2 Disposable (io.reactivex.disposables.Disposable)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 KC (com.marcnuri.yakc.KubernetesClientExtension.KC)1 ExecMessage (com.marcnuri.yakc.api.ExecMessage)1 StandardStream (com.marcnuri.yakc.api.ExecMessage.StandardStream)1 KubernetesException (com.marcnuri.yakc.api.KubernetesException)1 Type (com.marcnuri.yakc.api.WatchEvent.Type)1 AppsV1Api (com.marcnuri.yakc.api.apps.v1.AppsV1Api)1 ReadNamespacedPodLog (com.marcnuri.yakc.api.core.v1.CoreV1Api.ReadNamespacedPodLog)1 MetricsV1beta1Api (com.marcnuri.yakc.api.metrics.v1beta1.MetricsV1beta1Api)1