Search in sources :

Example 11 with CustomResourceDefinitionBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdManagementTest method testCustomResourceEventWithNoControllerImage.

@Test
@Description("The operator should automatically mark new instances of my CustomResourceDefinitions to 'successful' when I don't have " + "a controller image for it (yet)")
void testCustomResourceEventWithNoControllerImage() 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("but no %s annotation ", AnnotationNames.CONTROLLER_IMAGE.getName()), () -> {
        });
    });
    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("The phase on the resource status was updated to 'successful", () -> {
        await().atMost(3, TimeUnit.SECONDS).ignoreExceptions().until(() -> client.reload(resource).getStatus().getPhase().equals(EntandoDeploymentPhase.SUCCESSFUL));
    });
    step("But no controller pod has been created", () -> assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(resource))).isNull());
    step("And the fact that no image was found was logged", () -> assertThat(LogInterceptor.getLogEntries().stream().anyMatch(s -> s.contains("has neither the entando.org/controller-image annotation, nor is there an entry in the configmap " + "entando-controller-image-overrides")))).isTrue();
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Allure.attachment(io.qameta.allure.Allure.attachment) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) EntandoOperatorConfigProperty(org.entando.kubernetes.controller.support.common.EntandoOperatorConfigProperty) MetadataNested(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested) 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) 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) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) SimpleKubernetesClientDouble(org.entando.kubernetes.controller.coordinator.common.SimpleKubernetesClientDouble) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ProvidedCapability(org.entando.kubernetes.model.capability.ProvidedCapability) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) StandardCapability(org.entando.kubernetes.model.capability.StandardCapability) String.format(java.lang.String.format) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) StandardCapabilityImplementation(org.entando.kubernetes.model.capability.StandardCapabilityImplementation) AfterEach(org.junit.jupiter.api.AfterEach) Allure.step(io.qameta.allure.Allure.step) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) Tags(org.junit.jupiter.api.Tags) StartupEvent(io.quarkus.runtime.StartupEvent) Description(io.qameta.allure.Description) AbstractK8SClientDouble(org.entando.kubernetes.controller.support.client.doubles.AbstractK8SClientDouble) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) EntandoDeploymentPhase(org.entando.kubernetes.model.common.EntandoDeploymentPhase) 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) 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 12 with CustomResourceDefinitionBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class CrdManagementTest method crdUpdatesShouldRestartObservers.

@Test
@Description("Updates to my CustomResourceDefinitions should result in their observers being restarted")
void crdUpdatesShouldRestartObservers() throws IOException {
    step("Given I have started the Entando Operator", () -> entandoControllerCoordinator.onStartup(new StartupEvent()));
    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
    final ValueHolder<EntandoResourceObserver> observer = new ValueHolder<>();
    final ValueHolder<CustomResourceDefinition> crd = new ValueHolder<>();
    crd.set(objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class));
    final MetadataNested<CustomResourceDefinitionBuilder> builder = new CustomResourceDefinitionBuilder(crd.get()).editMetadata();
    step("And I have registered 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");
        });
        step("and I have registered my CustomResourceDefinition", () -> {
            crd.set(client.getCluster().putCustomResourceDefinition(builder.withGeneration(1L).endMetadata().build()));
        });
        attachment("CustomResourceDefinition", objectMapper.writeValueAsString(crd.get()));
    });
    step("And I have waited for the Operator to start observing state changes against the related CustomResources", () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get())).getCrdGeneration() == 1L);
        observer.set(entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get())));
    });
    step("When I apply an updated version of my CustomResourceDefinition ", () -> {
        crd.get().getMetadata().setGeneration(2L);
        client.getCluster().putCustomResourceDefinition(crd.get());
    });
    step("Then the Operator has started a new state change observer against the CustomResources", () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get())).getCrdGeneration() == 2L);
        assertThat(entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get()))).isNotSameAs(observer.get());
    });
}
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) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 13 with CustomResourceDefinitionBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder 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");
    });
}
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) 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 14 with CustomResourceDefinitionBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder in project kubernetes-client by fabric8io.

the class CustomResourceDefinitionContextTest method fromCrdV1beta1OldVersionStyle.

@Test
@DisplayName("fromCrd, with v1beta1 CRD with spec.version, should infer correct properties")
void fromCrdV1beta1OldVersionStyle() {
    // Given
    final io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition crd = new io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder().withNewMetadata().withName("foobar.the-foo.com").endMetadata().withNewSpec().withGroup("the-foo.com").withScope("Namespaced").withNewNames().withSingular("foobar").withPlural("foobars").withKind("Foobar").endNames().withVersion("v1").endSpec().build();
    // When
    final CustomResourceDefinitionContext result = CustomResourceDefinitionContext.fromCrd(crd);
    // Then
    assertThat(result).hasFieldOrPropertyWithValue("group", "the-foo.com").hasFieldOrPropertyWithValue("version", "v1").hasFieldOrPropertyWithValue("scope", "Namespaced").hasFieldOrPropertyWithValue("name", "foobar.the-foo.com").hasFieldOrPropertyWithValue("plural", "foobars").hasFieldOrPropertyWithValue("kind", "Foobar");
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 15 with CustomResourceDefinitionBuilder

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder in project kubernetes-client by fabric8io.

the class CustomResourceDefinitionContextTest method fromCrdV1beta1.

@Test
@DisplayName("fromCrd, with v1beta1 CRD with versions, should infer correct properties")
void fromCrdV1beta1() {
    // Given
    final io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition crd = new io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder().withNewMetadata().withName("foobar.the-foo.com").endMetadata().withNewSpec().withGroup("the-foo.com").withScope("Namespaced").withNewNames().withSingular("foobar").withPlural("foobars").withKind("Foobar").endNames().addNewVersion().withName("v1beta1").endVersion().addNewVersion().withName("v1alpha1").endVersion().addNewVersion().withName("v1").endVersion().endSpec().build();
    // When
    final CustomResourceDefinitionContext result = CustomResourceDefinitionContext.fromCrd(crd);
    // Then
    assertThat(result).hasFieldOrPropertyWithValue("group", "the-foo.com").hasFieldOrPropertyWithValue("version", "v1").hasFieldOrPropertyWithValue("scope", "Namespaced").hasFieldOrPropertyWithValue("name", "foobar.the-foo.com").hasFieldOrPropertyWithValue("plural", "foobars").hasFieldOrPropertyWithValue("kind", "Foobar");
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)12 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)11 Test (org.junit.jupiter.api.Test)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)7 Description (io.qameta.allure.Description)7 StartupEvent (io.quarkus.runtime.StartupEvent)7 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder)4 SerializedEntandoResource (org.entando.kubernetes.controller.spi.client.SerializedEntandoResource)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)3 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)3 ProvidedCapability (org.entando.kubernetes.model.capability.ProvidedCapability)3 ProvidedCapabilityBuilder (org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder)3 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)2 CustomResourceDefinitionVersion (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)2 CustomResourceDefinitionVersionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersionBuilder)2 CustomResourceSubresourceStatus (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceSubresourceStatus)2 KafkaBridge (io.strimzi.api.kafka.model.KafkaBridge)2 KafkaConnect (io.strimzi.api.kafka.model.KafkaConnect)2