Search in sources :

Example 1 with ConfigMap

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

the class WipIT method configMaps.

@Test
void configMaps() throws Exception {
    final String configMapName = "test";
    final CountDownLatch cdl = new CountDownLatch(1);
    KC.create(CoreV1Api.class).listConfigMapForAllNamespaces().watch().subscribeOn(Schedulers.newThread()).filter(we -> Stream.of(Type.ADDED, Type.DELETED).anyMatch(t -> we.getType() == t)).filter(we -> we.getObject().getMetadata().getName().equals(configMapName)).subscribe(we -> {
        cdl.countDown();
        System.out.printf("++ ConfigMap Watch %-15s - %s/%s%n", we.getType(), we.getObject().getMetadata().getNamespace(), we.getObject().getMetadata().getName());
    });
    try {
        KC.create(CoreV1Api.class).deleteNamespacedConfigMap(configMapName, "default").get();
        System.out.println("Existing ConfigMap was deleted");
    } catch (NotFoundException ex) {
        System.out.println("ConfigMap not found, deletion not needed");
    }
    KC.create(CoreV1Api.class).createNamespacedConfigMap("default", ConfigMap.builder().metadata(ObjectMeta.builder().name(configMapName).putInAnnotations("one-annotation-key", "annotation-value").build()).data(Collections.singletonMap("some", "to-keep")).putInData("test-key", "test-value").build()).get();
    KC.create(CoreV1Api.class).patchNamespacedConfigMap(configMapName, "default", ConfigMap.builder().metadata(ObjectMeta.builder().putInLabels("other", "label").build()).build()).get();
    cdl.await(5, TimeUnit.SECONDS);
}
Also used : APIResourceList(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.APIResourceList) LabelSelector(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector) ListPodForAllNamespaces(com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Container(com.marcnuri.yakc.model.io.k8s.api.core.v1.Container) PodSpec(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec) ObjectMeta(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta) DeleteOptions(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions) NotFoundException(com.marcnuri.yakc.api.NotFoundException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigMap(com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap) Schedulers(io.reactivex.schedulers.Schedulers) Type(com.marcnuri.yakc.api.WatchEvent.Type) KC(com.marcnuri.yakc.KubernetesClientExtension.KC) IOException(java.io.IOException) Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) CountDownLatch(java.util.concurrent.CountDownLatch) Disposable(io.reactivex.disposables.Disposable) AppsV1Api(com.marcnuri.yakc.api.apps.v1.AppsV1Api) Deployment(com.marcnuri.yakc.model.io.k8s.api.apps.v1.Deployment) Stream(java.util.stream.Stream) PodList(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodList) PodTemplateSpec(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodTemplateSpec) KubernetesException(com.marcnuri.yakc.api.KubernetesException) Collections(java.util.Collections) DeploymentSpec(com.marcnuri.yakc.model.io.k8s.api.apps.v1.DeploymentSpec) NotFoundException(com.marcnuri.yakc.api.NotFoundException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigMap

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

the class ConfigMapIT method readNamespacedConfigMap.

@Test
@DisplayName("readNamespacedConfigMap, should retrieve newly created ConfigMap")
void readNamespacedConfigMap() throws IOException {
    // When
    final ConfigMap result = KC.create(CoreV1Api.class).readNamespacedConfigMap(configMapName, NAMESPACE).get();
    // Then
    assertAll("Reads expected configMap", () -> assertThat(result).isNotNull(), () -> assertThat(result.getKind()).isEqualTo("ConfigMap"), () -> assertThat(result.getMetadata().getCreationTimestamp()).isNotNull(), () -> assertThat(result.getMetadata().getResourceVersion()).isNotNull(), () -> assertThat(result.getData()).containsEntry("VAR_1", "VAR_1_VALUE"));
}
Also used : ConfigMap(com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with ConfigMap

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

the class ConfigMapIT method awaitCreateWatch.

@Test
@DisplayName("listNamespacedConfigMap.watch, should await for notification of newly created ConfigMap")
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).listNamespacedConfigMap(NAMESPACE).watch().filter(we -> we.getObject().getMetadata().getName().equals(configMapName)).takeUntil(we -> we.getType() == Type.ADDED).timeout(20, TimeUnit.SECONDS).subscribe(we -> hasCompleted.set(true), we -> hasError.set(true));
    // Then
    assertAll("Watch completed", () -> assertThat(d).isNotNull(), () -> assertThat(hasError.get()).as("Watch subscribe ended with an error").isFalse(), () -> assertThat(hasCompleted.get()).as("Watch subscribe did not complete").isTrue());
}
Also used : Disposable(io.reactivex.disposables.Disposable) BeforeEach(org.junit.jupiter.api.BeforeEach) KC(com.marcnuri.yakc.KubernetesClientExtension.KC) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) UUID(java.util.UUID) Status(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.Status) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) ObjectMeta(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta) Disposable(io.reactivex.disposables.Disposable) AfterEach(org.junit.jupiter.api.AfterEach) NotFoundException(com.marcnuri.yakc.api.NotFoundException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ConfigMap(com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) Collections(java.util.Collections) Type(com.marcnuri.yakc.api.WatchEvent.Type) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with ConfigMap

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

the class ConfigMapIT method replaceNamespacedConfigMap.

@Test
@DisplayName("replaceNamespacedConfigMap, should replace existing ConfigMap data")
void replaceNamespacedConfigMap() throws IOException {
    // Given
    final ConfigMap existingConfigMap = KC.create(CoreV1Api.class).readNamespacedConfigMap(configMapName, NAMESPACE).get();
    final String priorResourceVersion = existingConfigMap.getMetadata().getResourceVersion();
    existingConfigMap.getMetadata().setResourceVersion(null);
    existingConfigMap.setData(Collections.singletonMap("VAR_Z", "VAR_Z_VALUE"));
    // When
    final ConfigMap result = KC.create(CoreV1Api.class).replaceNamespacedConfigMap(configMapName, NAMESPACE, existingConfigMap).get();
    // Then
    assertAll("Replaced ConfigMap is as expected", () -> assertThat(result).isNotNull(), () -> assertThat(result.getMetadata().getResourceVersion()).isNotNull(), () -> assertThat(result.getMetadata().getResourceVersion()).isNotEqualTo(priorResourceVersion), () -> assertThat(result.getData()).hasSize(1), () -> assertThat(result.getData()).containsEntry("VAR_Z", "VAR_Z_VALUE"));
}
Also used : ConfigMap(com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with ConfigMap

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

the class ConfigMapIT method patchNamespacedConfigMap.

@Test
@DisplayName("patchNamespacedConfigMap, should patch labels of ConfigMap")
void patchNamespacedConfigMap() throws IOException {
    // When
    final ConfigMap result = KC.create(CoreV1Api.class).patchNamespacedConfigMap(configMapName, NAMESPACE, ConfigMap.builder().metadata(ObjectMeta.builder().putInLabels("patched", "label").build()).putInData("VAR_2", "VAR_2_VALUE_CHANGED").putInData("VAR_3", "VAR_3_VALUE").build()).get();
    // Then
    assertAll("Patched ConfigMap is as expected", () -> assertThat(result).isNotNull(), () -> assertThat(result.getMetadata().getLabels()).containsEntry("patched", "label"), () -> assertThat(result.getData()).containsEntry("VAR_1", "VAR_1_VALUE"), () -> assertThat(result.getData()).containsEntry("VAR_2", "VAR_2_VALUE_CHANGED"), () -> assertThat(result.getData()).containsEntry("VAR_3", "VAR_3_VALUE"));
}
Also used : ConfigMap(com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ConfigMap (com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap)5 Test (org.junit.jupiter.api.Test)5 DisplayName (org.junit.jupiter.api.DisplayName)4 KC (com.marcnuri.yakc.KubernetesClientExtension.KC)2 NotFoundException (com.marcnuri.yakc.api.NotFoundException)2 Type (com.marcnuri.yakc.api.WatchEvent.Type)2 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)2 ObjectMeta (com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta)2 Disposable (io.reactivex.disposables.Disposable)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 TimeUnit (java.util.concurrent.TimeUnit)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 KubernetesException (com.marcnuri.yakc.api.KubernetesException)1 AppsV1Api (com.marcnuri.yakc.api.apps.v1.AppsV1Api)1 ListPodForAllNamespaces (com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)1 Deployment (com.marcnuri.yakc.model.io.k8s.api.apps.v1.Deployment)1 DeploymentSpec (com.marcnuri.yakc.model.io.k8s.api.apps.v1.DeploymentSpec)1 Container (com.marcnuri.yakc.model.io.k8s.api.core.v1.Container)1