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");
});
}
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");
});
}
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));
});
}
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));
});
}
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));
});
}
Aggregations