use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.
the class CrdManagementTest method testCustomResourceEvent.
@Test
@Description("New instances of my CustomResources should result in my controller image to be executed against the resource")
void testCustomResourceEvent() 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());
});
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 my 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 annotation", AnnotationNames.CONTROLLER_IMAGE.getName()), () -> {
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/my-controller");
});
}
use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.
the class DefaultSimpleEntandoOperationsTest method shouldAndAndRemoveAnnotations.
@Test
@Description("Should add and remove annotations")
void shouldAndAndRemoveAnnotations() {
ValueHolder<SerializedEntandoResource> resource = new ValueHolder<>();
step("Given I have a TestResource", () -> {
final TestResource testResource = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource"));
resource.set(CoordinatorTestUtils.toSerializedResource(testResource));
attachment("TestResource", objectMapper.writeValueAsString(resource.get()));
});
step("And I have added the annotation 'entando.org/processed-by-version=6.3.0' to it", () -> {
resource.set(getMyOperations().putAnnotation(resource.get(), AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName(), "6.3.0"));
assertThat(CoordinatorUtils.resolveAnnotation(resource.get(), AnnotationNames.PROCESSED_BY_OPERATOR_VERSION)).contains("6.3.0");
attachment("TestResource", objectMapper.writeValueAsString(resource.get()));
});
step("When I remove the annotation 'entando.org/processed-by-version=6.3.0' from it it", () -> {
resource.set(getMyOperations().removeAnnotation(resource.get(), AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName()));
attachment("TestResource", objectMapper.writeValueAsString(resource.get()));
});
step("The annotation is no longer present on the resource", () -> {
assertThat(resource.get().getMetadata().getAnnotations()).doesNotContainKey(AnnotationNames.PROCESSED_BY_OPERATOR_VERSION.getName());
});
}
use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.
the class DefaultSimpleEntandoOperationsTest method shouldWatchInSingleNamespace.
@Test
@Description("Should watch in a single namespace")
void shouldWatchInSingleNamespace() {
final Map<String, SerializedEntandoResource> resourcesObserved = new ConcurrentHashMap<>();
step(format("Given I have registered a Watcher for TestResource in namespace %s", MY_APP_NAMESPACE_2), () -> {
getMyOperations().inNamespace(MY_APP_NAMESPACE_2).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).containsKey("my-test-resource2");
assertThat(resourcesObserved).doesNotContainKey("my-test-resource1");
attachment("TestResources", objectMapper.writeValueAsString(resourcesObserved));
});
}
use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource 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();
});
}
use of org.entando.kubernetes.controller.spi.client.SerializedEntandoResource in project entando-k8s-controller-coordinator by entando-k8s.
the class PodManagementTests method shouldGarbageCollectSuccessulPods.
@Test
@Description("Should automatically remove successful controller pods once the resource has progress to the 'successful' phase")
void shouldGarbageCollectSuccessulPods() {
step("Given I have activated controller Pod garbage collection with a removal delay of 1 second", () -> {
System.setProperty(ControllerCoordinatorProperty.ENTANDO_K8S_CONTROLLER_REMOVAL_DELAY.getJvmSystemProperty(), "1");
System.setProperty(EntandoOperatorConfigProperty.ENTANDO_K8S_OPERATOR_GC_CONTROLLER_PODS.getJvmSystemProperty(), "true");
});
step("And 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", () -> {
TestResource f = new TestResource().withNames(clientDouble.getNamespace(), "test-keycloak").withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.EMBEDDED).build());
f.getMetadata().setGeneration(1L);
testResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(f)));
attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
});
step("And I have waited for the controller pod to be created", () -> {
await().ignoreExceptions().atMost(3, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())) != null);
attachment("Controller Pod", objectMapper.writeValueAsString(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get()))));
});
step("When I complete the deployment of the TestResource successfully", () -> {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
clientDouble.updatePodStatus(podWithSucceededStatus(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get()))));
});
attachment("Suffessful Resource", objectMapper.writeValueAsString(clientDouble.updatePhase(testResource.get(), EntandoDeploymentPhase.SUCCESSFUL)));
});
step("Then the previously created, but successfully completed controller pod will be removed", () -> await().ignoreExceptions().atMost(3, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())) == null));
}
Aggregations