Search in sources :

Example 1 with ProvidedCapabilityBuilder

use of org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdManagementTest method testCapabilityEvent.

@Test
@Description("Capabilities that my controller image supports should also result in my controller image to be executed against the " + "ProvidedCapability")
void testCapabilityEvent() throws IOException {
    step("Given I prepared a namespace scoped deployment of the EntandoOperator", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), MY_NAMESPACE));
    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.getName()), () -> {
            builder.addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller");
        });
        step(format("and the %s annotation ", AnnotationNames.SUPPORTED_CAPABILITIES), () -> {
            builder.addToAnnotations(AnnotationNames.SUPPORTED_CAPABILITIES.getName(), "mysql.dbms");
        });
    });
    step("And I have registered my custom resource definition", () -> {
        client.getCluster().putCustomResourceDefinition(builder.endMetadata().build());
    });
    final ProvidedCapability providedCapability = new ProvidedCapabilityBuilder().withNewMetadata().withName("my-capability").withNamespace(MY_NAMESPACE).endMetadata().withNewSpec().withCapability(StandardCapability.DBMS).withImplementation(StandardCapabilityImplementation.MYSQL).endSpec().build();
    step("When I create a new ProvidedCapability requiring the capability associated with my CustomResourceDefinition ", () -> client.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(providedCapability)));
    step(format("Then a controller pod has been created with the image that I specified in the %s annotation", AnnotationNames.CONTROLLER_IMAGE.getName()), () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(providedCapability)) != null);
        assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(providedCapability)).getSpec().getContainers().get(0).getImage()).contains("test/my-controller");
    });
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) StartupEvent(io.quarkus.runtime.StartupEvent) ProvidedCapability(org.entando.kubernetes.model.capability.ProvidedCapability) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) 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 ProvidedCapabilityBuilder

use of org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdManagementTest method testCapabilityEventWithControllerImageOverride.

@Test
@Description("Capabilities that my controller image supports should also result in the image specified in the " + CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP + " ConfigMap to be executed against the ProvidedCapability")
void testCapabilityEventWithControllerImageOverride() throws IOException {
    step("Given I prepared a namespace scoped deployment of the EntandoOperator", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), MY_NAMESPACE));
    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.getName()), () -> {
            builder.addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller");
        });
        step(format("and the %s annotation ", AnnotationNames.SUPPORTED_CAPABILITIES), () -> {
            builder.addToAnnotations(AnnotationNames.SUPPORTED_CAPABILITIES.getName(), "mysql.dbms");
        });
    });
    step("And I have registered my custom resource definition", () -> {
        client.getCluster().putCustomResourceDefinition(builder.endMetadata().build());
    });
    final ProvidedCapability providedCapability = new ProvidedCapabilityBuilder().withNewMetadata().withName("my-capability").withNamespace(MY_NAMESPACE).endMetadata().withNewSpec().withCapability(StandardCapability.DBMS).withImplementation(StandardCapabilityImplementation.MYSQL).endSpec().build();
    step(format("But I have overridden the DBMS capability's 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("dbms.capability.org", "test/image-override").build());
    });
    step("When I create a new ProvidedCapability requiring the capability associated with my CustomResourceDefinition ", () -> client.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(providedCapability)));
    step(format("Then a controller pod has been created with the overriding 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(providedCapability)) != null);
        assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(providedCapability)).getSpec().getContainers().get(0).getImage()).contains("test/image-override");
    });
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) StartupEvent(io.quarkus.runtime.StartupEvent) ProvidedCapability(org.entando.kubernetes.model.capability.ProvidedCapability) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) 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 3 with ProvidedCapabilityBuilder

use of org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class ControllerCoordinatorProcessingCriteriaTest method testIgnoredWhenOwnedByResourceOfInterest.

@Test
@Description("Resources should be ignored when they are owned by other resources of interest")
void testIgnoredWhenOwnedByResourceOfInterest() {
    step("Given  the Coordinator observes this namespace", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), OBSERVED_NAMESPACE));
    ValueHolder<SerializedEntandoResource> owningResource = new ValueHolder<>();
    step("And I have created another resource of interest, a ProvidedCapability", () -> {
        owningResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(new ProvidedCapabilityBuilder().withNewMetadata().withName("my-capability").withNamespace(OBSERVED_NAMESPACE).endMetadata().withNewSpec().withCapability(StandardCapability.DBMS).endSpec().build())));
        attachment("CapabilityResource", objectMapper.writeValueAsString(owningResource.get()));
    });
    ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
    step("And I have created an TestResource resource that is owned by another resource of interest", () -> {
        testResource.set(createTestResource(1L, Collections.emptyMap()));
        testResource.get().getMetadata().getOwnerReferences().add(ResourceUtils.buildOwnerReference(owningResource.get()));
        clientDouble.createOrPatchEntandoResource(testResource.get());
    });
    step("when I start the ControllerCoordinator", () -> coordinator.onStartup(new StartupEvent()));
    step("Then a new pod gets created for the owning resource", () -> {
        await().ignoreExceptions().atMost(10, SECONDS).until(() -> clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(owningResource.get())) != null);
        attachment("Controller Pod", objectMapper.writeValueAsString(clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(owningResource.get()))));
    });
    step("Then no new pod gets created for the owned resource", () -> {
        assertThat(clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(testResource.get()))).isNull();
    });
    step("And the ignored event was logged", () -> {
        final Optional<String> logEntry = LogInterceptor.getLogEntries().stream().filter(s -> s.contains("is ignored because it is not a top level resource")).findFirst();
        assertThat(logEntry).isPresent();
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Allure.attachment(io.qameta.allure.Allure.attachment) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) DbmsVendor(org.entando.kubernetes.model.common.DbmsVendor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) Action(io.fabric8.kubernetes.client.Watcher.Action) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) EntandoOperatorConfigProperty(org.entando.kubernetes.controller.support.common.EntandoOperatorConfigProperty) Feature(io.qameta.allure.Feature) LabelNames(org.entando.kubernetes.controller.spi.common.LabelNames) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) LogInterceptor(org.entando.kubernetes.test.common.LogInterceptor) Map(java.util.Map) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ResourceUtils(org.entando.kubernetes.controller.spi.common.ResourceUtils) Tag(org.junit.jupiter.api.Tag) Issue(io.qameta.allure.Issue) CoordinatorTestUtils(org.entando.kubernetes.controller.coordinator.common.CoordinatorTestUtils) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) SimpleKubernetesClientDouble(org.entando.kubernetes.controller.coordinator.common.SimpleKubernetesClientDouble) CommonLabels(org.entando.kubernetes.test.common.CommonLabels) FluentIntegrationTesting(org.entando.kubernetes.controller.support.client.impl.integrationtesthelpers.FluentIntegrationTesting) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pod(io.fabric8.kubernetes.api.model.Pod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) StandardCapability(org.entando.kubernetes.model.capability.StandardCapability) TestResource(org.entando.kubernetes.fluentspi.TestResource) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) EntandoResourceClientDouble(org.entando.kubernetes.controller.support.client.doubles.EntandoResourceClientDouble) AfterEach(org.junit.jupiter.api.AfterEach) Allure.step(io.qameta.allure.Allure.step) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) Optional(java.util.Optional) Tags(org.junit.jupiter.api.Tags) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) StartupEvent(io.quarkus.runtime.StartupEvent) Description(io.qameta.allure.Description) Collections(java.util.Collections) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) EntandoDeploymentPhase(org.entando.kubernetes.model.common.EntandoDeploymentPhase) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) StartupEvent(io.quarkus.runtime.StartupEvent) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)3 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)3 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)3 Description (io.qameta.allure.Description)3 StartupEvent (io.quarkus.runtime.StartupEvent)3 ProvidedCapabilityBuilder (org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder)3 ProvidedCapability (org.entando.kubernetes.model.capability.ProvidedCapability)2 Test (org.junit.jupiter.api.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)1 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 Action (io.fabric8.kubernetes.client.Watcher.Action)1 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)1 Allure.attachment (io.qameta.allure.Allure.attachment)1 Allure.step (io.qameta.allure.Allure.step)1 Feature (io.qameta.allure.Feature)1 Issue (io.qameta.allure.Issue)1 IOException (java.io.IOException)1