Search in sources :

Example 1 with SerializedEntandoResource

use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdManagementTest method testCrdRegistration.

@Test
@Description("All information related to my custom resource, the capabilities it supports and its controller images should be extracted" + " from my CustomResourceDefinition's annotations")
void testCrdRegistration() throws IOException {
    step("Given I have started the Entando Operator", () -> entandoControllerCoordinator.onStartup(new StartupEvent()));
    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
    final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class);
    final MetadataNested<CustomResourceDefinitionBuilder> builder = new CustomResourceDefinitionBuilder(value).editMetadata();
    step("And I have a CustomResourceDefinition with", () -> {
        step(format("the %s label ", LabelNames.CRD_OF_INTEREST.getName()), () -> {
            builder.addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD");
        });
        step(format("and the %s annotation ", AnnotationNames.CONTROLLER_IMAGE), () -> {
            builder.addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller");
        });
        attachment("CustomResourceDefinition", objectMapper.writeValueAsString(builder.endMetadata().build()));
    });
    step("When I register my CustomResourceDefinition", () -> {
        final CustomResourceDefinition crd = client.getCluster().putCustomResourceDefinition(builder.endMetadata().build());
        attachment("CustomResourceDefinition", objectMapper.writeValueAsString(crd));
    });
    step("Then my custom resource definition is mapped in the " + CoordinatorUtils.ENTANDO_CRD_NAMES_CONFIGMAP_NAME + " ConfigMap", () -> {
        assertThat(client.findOrCreateControllerConfigMap(CoordinatorUtils.ENTANDO_CRD_NAMES_CONFIGMAP_NAME).getData()).containsEntry("MyCRD.test.org", "mycrds.test.org");
    });
    step("Then my controller image is registered in the EntandoControllerCoordinator", () -> {
        final SerializedEntandoResource resource = new SerializedEntandoResource();
        resource.setDefinition(CustomResourceDefinitionContext.fromCrd(builder.endMetadata().build()));
        assertThat(entandoControllerCoordinator.getControllerImageFor(resource)).isEqualTo("test/my-controller");
    });
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) StartupEvent(io.quarkus.runtime.StartupEvent) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 2 with SerializedEntandoResource

use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdManagementTest method testCustomResourceEventWithControllerImageOverride.

@Test
@Description("New instances of my CustomResourceDefinitions should result in the image specified in the " + CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP + " ConfigMap to be executed against the resource rather than the one on my CRD's annotation")
void testCustomResourceEventWithControllerImageOverride() throws IOException {
    step("Given I have prepared a cluster scoped deployment of the EntandoOperator", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), "*"));
    step("And I have started the Entando Operator", () -> entandoControllerCoordinator.onStartup(new StartupEvent()));
    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
    final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class);
    final MetadataNested<CustomResourceDefinitionBuilder> builder = new CustomResourceDefinitionBuilder(value).editMetadata();
    step("And I have a CustomResourceDefinition with", () -> {
        step(format("the %s label ", LabelNames.CRD_OF_INTEREST.getName()), () -> {
            builder.addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD");
        });
        step(format("and the %s annotation ", AnnotationNames.CONTROLLER_IMAGE.getName()), () -> {
            builder.addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller");
        });
    });
    step("And I have registered my custom resource definition", () -> {
        client.getCluster().putCustomResourceDefinition(builder.endMetadata().build());
    });
    step(format("But I have overridden the image to 'test/image-override' in the %s ConfigMap", CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP), () -> {
        client.patchControllerConfigMap(new ConfigMapBuilder(client.findOrCreateControllerConfigMap(CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP)).addToData("MyCRD.test.org", "test/image-override").build());
    });
    final SerializedEntandoResource resource = new SerializedEntandoResource();
    resource.setMetadata(new ObjectMetaBuilder().withName("my-resource").withNamespace(MY_NAMESPACE).build());
    resource.setDefinition(CustomResourceDefinitionContext.fromCrd(builder.endMetadata().build()));
    step("When I create a new custom resource based on my CustomResourceDefinition the overriding controller image is used to " + "execute" + " a " + "Controller pod that runs to completion", () -> client.createOrPatchEntandoResource(resource));
    step(format("Then a controller pod has been created with the image that I specified in the %s ConfigMap", CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP), () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(resource)) != null);
        assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(resource)).getSpec().getContainers().get(0).getImage()).contains("test/image-override");
    });
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) StartupEvent(io.quarkus.runtime.StartupEvent) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 3 with SerializedEntandoResource

use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleEntandoOperationsTest method shouldWatchInAnyNamespace.

@Test
@Description("Should watch in any namespace")
void shouldWatchInAnyNamespace() {
    final Map<String, SerializedEntandoResource> resourcesObserved = new ConcurrentHashMap<>();
    step("Given I have registered a Watcher for TestResource in any namespace", () -> {
        getMyOperations().inAnyNamespace().watch((action, serializedEntandoResource) -> resourcesObserved.put(serializedEntandoResource.getMetadata().getName(), serializedEntandoResource));
    });
    step(format("When  I create two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
        final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
        attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
        final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
        attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
    });
    step("Then both resources were observed", () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> resourcesObserved.containsKey("my-test-resource2"));
        assertThat(resourcesObserved).containsKeys("my-test-resource1", "my-test-resource2");
        attachment("TestResources", objectMapper.writeValueAsString(resourcesObserved));
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) TestResource(org.entando.kubernetes.fluentspi.TestResource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 4 with SerializedEntandoResource

use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleEntandoOperationsTest method shouldListInAnyNamespace.

@Test
@Description("Should list in any namespace")
void shouldListInAnyNamespace() {
    step(format("Given I have created two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
        final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
        attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
        final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
        attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
    });
    List<SerializedEntandoResource> actual = new ArrayList<>();
    step("When I list TestResources in any namespace", () -> {
        actual.addAll(getMyOperations().inAnyNamespace().list());
    });
    step("Then both resources are found", () -> {
        assertThat(actual).anyMatch(r -> r.getMetadata().getName().equals("my-test-resource1"));
        assertThat(actual).anyMatch(r -> r.getMetadata().getName().equals("my-test-resource2"));
        attachment("TestResources", objectMapper.writeValueAsString(actual));
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) ArrayList(java.util.ArrayList) TestResource(org.entando.kubernetes.fluentspi.TestResource) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 5 with SerializedEntandoResource

use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleEntandoOperationsTest method shouldListInSingleNamespace.

@Test
@Description("Should list in a single namespace")
void shouldListInSingleNamespace() {
    step(format("Given I have created two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
        final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
        attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
        final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
        attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
    });
    List<SerializedEntandoResource> actual = new ArrayList<>();
    step("When I list TestResources in any namespace", () -> {
        actual.addAll(getMyOperations().inNamespace(MY_APP_NAMESPACE_1).list());
    });
    step("Then both resources are found", () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> actual.size() == 1);
        assertThat(actual).size().isEqualTo(1);
        assertThat(actual).anyMatch(r -> r.getMetadata().getName().equals("my-test-resource1"));
        attachment("TestResource 1", objectMapper.writeValueAsString(actual));
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) ArrayList(java.util.ArrayList) TestResource(org.entando.kubernetes.fluentspi.TestResource) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Aggregations

SerializedEntandoResource (org.entando.kubernetes.controller.spi.client.SerializedEntandoResource)25 Description (io.qameta.allure.Description)19 Test (org.junit.jupiter.api.Test)19 TestResource (org.entando.kubernetes.fluentspi.TestResource)15 StartupEvent (io.quarkus.runtime.StartupEvent)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 ValueHolder (org.entando.kubernetes.test.common.ValueHolder)12 BasicDeploymentSpecBuilder (org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder)10 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)9 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)9 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)9 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)8 IOException (java.io.IOException)7 Pod (io.fabric8.kubernetes.api.model.Pod)6 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)6 Allure.attachment (io.qameta.allure.Allure.attachment)6 Allure.step (io.qameta.allure.Allure.step)6 Feature (io.qameta.allure.Feature)6 Issue (io.qameta.allure.Issue)6 TimeUnit (java.util.concurrent.TimeUnit)6