Search in sources :

Example 1 with BasicDeploymentSpecBuilder

use of org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class PodManagementTests method shouldKillAllPreviousPods.

@Test
@Description("Should always kill existing pods running against the same resource to avoid racing conditions")
void shouldKillAllPreviousPods() {
    step("Given the Coordinator observes its own namespace", () -> {
        System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), clientDouble.getNamespace());
        coordinator.onStartup(new StartupEvent());
    });
    step("And automatic controller pod garbage collections has been switched off", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_K8S_OPERATOR_GC_CONTROLLER_PODS.getJvmSystemProperty(), "false"));
    ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
    step("And I have created a new TestResource resource", () -> {
        final TestResource entandoCustomResource = new TestResource().withNames(clientDouble.getNamespace(), "test-keycloak").withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.EMBEDDED).build());
        testResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(entandoCustomResource)));
        attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
    });
    ValueHolder<Pod> firstPod = new ValueHolder<>();
    step("And I have waited to see at least one controller pod", () -> {
        await().ignoreExceptions().atMost(5, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())) != null);
        firstPod.set(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())));
        attachment("FirstPod", objectMapper.writeValueAsString(firstPod.get()));
    });
    step("When I force a second attempt at processing the resource", () -> {
        final SerializedEntandoResource reloaded = clientDouble.reload(testResource.get());
        reloaded.getMetadata().setAnnotations(Map.of(AnnotationNames.PROCESSING_INSTRUCTION.getName(), "force"));
        clientDouble.createOrPatchEntandoResource(reloaded);
    });
    step("Then a new pod was created", () -> {
        await().atMost(4, TimeUnit.SECONDS).ignoreExceptions().until(() -> {
            final List<Pod> pods = clientDouble.loadPods(clientDouble.getNamespace(), labelsFromResource(testResource.get()));
            return !pods.get(pods.size() - 1).getMetadata().getUid().equals(firstPod.get().getMetadata().getUid());
        });
        final List<Pod> pods = clientDouble.loadPods(clientDouble.getNamespace(), labelsFromResource(testResource.get()));
        attachment("Second Pod", objectMapper.writeValueAsString(pods.get(pods.size() - 1)));
    });
    step("And the old pod was removed", () -> {
        final List<Pod> pods = clientDouble.loadPods(clientDouble.getNamespace(), labelsFromResource(testResource.get()));
        assertThat(pods.size()).isOne();
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Pod(io.fabric8.kubernetes.api.model.Pod) StartupEvent(io.quarkus.runtime.StartupEvent) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 2 with BasicDeploymentSpecBuilder

use of org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleKubernetesClientTest method shouldUpdateStatusOfOpaqueCustomResource.

@Test
@Description("Should track phase updates on the status of opaque custom resources and in Kubernetes events")
void shouldUpdateStatusOfOpaqueCustomResource() throws IOException {
    ValueHolder<TestResource> testResource = new ValueHolder<>();
    step("Given I have created an instance of the CustomResourceDefinition TestResource", () -> {
        testResource.set(createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, MY_APP).withSpec(new BasicDeploymentSpecBuilder().withReplicas(1).build())));
        attachResource("TestResource", testResource.get());
    });
    SerializedEntandoResource serializedEntandoResource = objectMapper.readValue(objectMapper.writeValueAsBytes(testResource.get()), SerializedEntandoResource.class);
    step("And it is represented in an opaque format using the SerializedEntandoResource class", () -> {
        serializedEntandoResource.setDefinition(CustomResourceDefinitionContext.fromCustomResourceType(TestResource.class));
        attachResource("Opaque Resource", serializedEntandoResource);
    });
    step("When I update its phase to 'successful'", () -> getMyClient().updatePhase(serializedEntandoResource, EntandoDeploymentPhase.SUCCESSFUL));
    step("Then the updated status reflects on the TestResource", () -> {
        final TestResource actual = getFabric8Client().customResources(TestResource.class).inNamespace(MY_APP_NAMESPACE_1).withName(MY_APP).get();
        assertThat(actual.getStatus().getPhase()).isEqualTo(EntandoDeploymentPhase.SUCCESSFUL);
        attachResource("TestResource", actual);
    });
    step("And a PHASE_CHANGE event has been issued to Kubernetes", () -> {
        final List<Event> events = getMyClient().listEventsFor(testResource.get());
        attachResources("Events", events);
        assertThat(events).allMatch(event -> event.getInvolvedObject().getName().equals(testResource.get().getMetadata().getName()));
        assertThat(events).anyMatch(event -> event.getAction().equals("PHASE_CHANGE"));
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Event(io.fabric8.kubernetes.api.model.Event) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 3 with BasicDeploymentSpecBuilder

use of org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class LivenessTests method shouldCreateControllerPodPointingToResource.

@Test
@Description("Should create a  controller pod that points to the newly created resource")
void shouldCreateControllerPodPointingToResource() {
    System.setProperty(EntandoOperatorSpiConfigProperty.ENTANDO_CONTROLLER_POD_NAME.getJvmSystemProperty(), "my-pod");
    step("Given the Coordinator observes its own namespace", () -> {
        System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), clientDouble.getNamespace());
        coordinator.onStartup(new StartupEvent());
    });
    ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
    step("And I have created a new TestResource resource", () -> {
        final TestResource r = new TestResource().withNames(clientDouble.getNamespace(), "test-keycloak").withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.EMBEDDED).build());
        r.getMetadata().setGeneration(1L);
        testResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(r)));
        attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
    });
    final File file = Paths.get("/tmp/EntandoControllerCoordinator.ready").toFile();
    clientDouble.getCluster().getResourceProcessor().getAllWatchers().forEach(watcher -> {
        Liveness.alive();
        step(format("When the watcher %s is closed", watcher.getClass().getSimpleName()), () -> {
            watcher.onClose(new WatcherException("Closed"));
        });
        step("Then the liveness probe will fail", () -> {
            assertThat(file).doesNotExist();
        });
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) StartupEvent(io.quarkus.runtime.StartupEvent) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) File(java.io.File) WatcherException(io.fabric8.kubernetes.client.WatcherException) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 4 with BasicDeploymentSpecBuilder

use of org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class ControllerCoordinatorProcessingCriteriaTest method createTestResource.

protected SerializedEntandoResource createTestResource(Long generation, Map<String, String> annotations) throws JsonProcessingException {
    TestResource keycloakServer = new TestResource().withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.MYSQL).build());
    keycloakServer.setMetadata(new ObjectMetaBuilder().withGeneration(generation).withUid(RandomStringUtils.randomAlphanumeric(12)).withName("test-keycloak").withNamespace(OBSERVED_NAMESPACE).withAnnotations(annotations).build());
    final SerializedEntandoResource serializedResource = CoordinatorTestUtils.toSerializedResource(keycloakServer);
    serializedResource.setDefinition(CustomResourceDefinitionContext.fromCustomResourceType(TestResource.class));
    final SerializedEntandoResource createdResource = clientDouble.createOrPatchEntandoResource(serializedResource);
    attachment("TestResource", objectMapper.writeValueAsString(createdResource));
    return createdResource;
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) TestResource(org.entando.kubernetes.fluentspi.TestResource) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder)

Example 5 with BasicDeploymentSpecBuilder

use of org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class PodManagementTests method shouldCreateControllerPodPointingToResource.

@Test
@Description("Should create a  controller pod that points to the newly created resource")
void shouldCreateControllerPodPointingToResource() {
    step("Given the Coordinator observes its own namespace", () -> {
        System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), clientDouble.getNamespace());
        coordinator.onStartup(new StartupEvent());
    });
    ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
    step("And I have created a new TestResource resource", () -> {
        final TestResource r = new TestResource().withNames(clientDouble.getNamespace(), "test-keycloak").withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.EMBEDDED).build());
        r.getMetadata().setGeneration(1L);
        testResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(r)));
        attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
    });
    ValueHolder<Pod> firstPod = new ValueHolder<>();
    step("Then one controller pod gets created", () -> {
        await().ignoreExceptions().atMost(5, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())) != null);
        firstPod.set(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())));
        attachment("Controller Pod", objectMapper.writeValueAsString(firstPod.get()));
    });
    step("And the pod is given the necessary information to resolve the resource being processed", () -> {
        assertThat(theVariableNamed(EntandoOperatorSpiConfigProperty.ENTANDO_RESOURCE_NAME.name()).on(thePrimaryContainerOn(firstPod.get()))).isEqualTo(testResource.get().getMetadata().getName());
        assertThat(theVariableNamed(EntandoOperatorSpiConfigProperty.ENTANDO_RESOURCE_NAMESPACE.name()).on(thePrimaryContainerOn(firstPod.get()))).isEqualTo(testResource.get().getMetadata().getNamespace());
        assertThat(theVariableNamed(EntandoOperatorSpiConfigProperty.ENTANDO_RESOURCE_KIND.name()).on(thePrimaryContainerOn(firstPod.get()))).isEqualTo(testResource.get().getKind());
    });
    step("And this pod remains present even if I complete the deployment of the TestResource successfully", () -> {
        clientDouble.updatePodStatus(podWithSucceededStatus(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get()))));
        attachment("Successful Resource", objectMapper.writeValueAsString(clientDouble.updatePhase(testResource.get(), EntandoDeploymentPhase.SUCCESSFUL)));
        await().ignoreExceptions().atMost(2, TimeUnit.SECONDS).ignoreExceptions().until(() -> LogInterceptor.getLogEntries().stream().anyMatch(s -> s.contains("was processed successfully")));
        assertThat(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get()))).isNotNull();
    });
}
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) EntandoOperatorSpiConfigProperty(org.entando.kubernetes.controller.spi.common.EntandoOperatorSpiConfigProperty) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) 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) Tag(org.junit.jupiter.api.Tag) Issue(io.qameta.allure.Issue) CoordinatorTestUtils(org.entando.kubernetes.controller.coordinator.common.CoordinatorTestUtils) ExecutorService(java.util.concurrent.ExecutorService) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) SimpleKubernetesClientDouble(org.entando.kubernetes.controller.coordinator.common.SimpleKubernetesClientDouble) FluentTraversals(org.entando.kubernetes.test.common.FluentTraversals) 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) IOException(java.io.IOException) CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) Executors(java.util.concurrent.Executors) TestResource(org.entando.kubernetes.fluentspi.TestResource) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) PodBehavior(org.entando.kubernetes.test.common.PodBehavior) AfterEach(org.junit.jupiter.api.AfterEach) Allure.step(io.qameta.allure.Allure.step) VariableReferenceAssertions(org.entando.kubernetes.test.common.VariableReferenceAssertions) Tags(org.junit.jupiter.api.Tags) StartupEvent(io.quarkus.runtime.StartupEvent) Description(io.qameta.allure.Description) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) EntandoDeploymentPhase(org.entando.kubernetes.model.common.EntandoDeploymentPhase) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Pod(io.fabric8.kubernetes.api.model.Pod) StartupEvent(io.quarkus.runtime.StartupEvent) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Aggregations

SerializedEntandoResource (org.entando.kubernetes.controller.spi.client.SerializedEntandoResource)6 BasicDeploymentSpecBuilder (org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder)6 TestResource (org.entando.kubernetes.fluentspi.TestResource)6 Description (io.qameta.allure.Description)5 ValueHolder (org.entando.kubernetes.test.common.ValueHolder)5 Test (org.junit.jupiter.api.Test)5 StartupEvent (io.quarkus.runtime.StartupEvent)4 Pod (io.fabric8.kubernetes.api.model.Pod)2 ExecutorService (java.util.concurrent.ExecutorService)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Event (io.fabric8.kubernetes.api.model.Event)1 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)1 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)1 WatcherException (io.fabric8.kubernetes.client.WatcherException)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