Search in sources :

Example 6 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition 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 7 with CustomResourceDefinition

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition 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");
    });
}
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) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) 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 8 with CustomResourceDefinition

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

the class DefaultSimpleKubernetesClientTest method shouldListCustomResourceDefinitionsWithCrdOfInterestLabel.

@Test
@Description("Should list CustomResourceDefinitions with the label 'entando.org/crd-of-interest'")
void shouldListCustomResourceDefinitionsWithCrdOfInterestLabel() {
    step("Given I have removed the CustomResourceDefinition MyCRD", this::deleteMyCrd);
    step("And I have created the CustomResourceDefinition MyCRD with the label 'entando.org/crd-of-interest'", () -> {
        final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class);
        value.getMetadata().setLabels(Map.of(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD"));
        getFabric8Client().apiextensions().v1beta1().customResourceDefinitions().create(value);
    });
    List<CustomResourceDefinition> crds = new ArrayList<>();
    step("When I list the CustomResourceDefinitions of interest", () -> {
        crds.addAll(myClient.loadCustomResourceDefinitionsOfInterest());
    });
    step("Then the CustomResourceDefinition without the entando.org/crd-of-interest label was not inluded in the list", () -> {
        assertThat(crds).anyMatch(crd -> crd.getMetadata().getName().equals("mycrds.test.org"));
    });
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) ArrayList(java.util.ArrayList) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 9 with CustomResourceDefinition

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

the class DefaultSimpleKubernetesClientTest method shouldWatchCustomResourceDefinitionsWithCrdOfInterestLabel.

@Test
@Description("Should watch CustomResourceDefinitions with the label 'entando.org/crd-of-interest'")
void shouldWatchCustomResourceDefinitionsWithCrdOfInterestLabel() {
    step("Given I have removed the CustomResourceDefinition MyCRD", () -> {
        deleteMyCrd();
    });
    Map<String, CustomResourceDefinition> crds = new ConcurrentHashMap<>();
    step("And I have started watching for changes against CustomResourceDefinitions", () -> {
        myClient.watchCustomResourceDefinitions(new Watcher<>() {

            @Override
            public void eventReceived(Action action, CustomResourceDefinition customResourceDefinition) {
                crds.put(customResourceDefinition.getMetadata().getName(), customResourceDefinition);
            }

            @Override
            public void onClose(WatcherException e) {
            }
        });
    });
    step("When I create the CustomResourceDefinition MyCRD without the label 'entando.org/crd-of-interest'", () -> {
        final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class);
        value.getMetadata().setLabels(Map.of(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD"));
        getFabric8Client().apiextensions().v1beta1().customResourceDefinitions().create(value);
    });
    step("Then the CustomResourceDefinition was ignored", () -> {
        await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> crds.containsKey("mycrds.test.org"));
        assertThat(crds).containsKey("mycrds.test.org");
    });
}
Also used : CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) WatcherException(io.fabric8.kubernetes.client.WatcherException) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 10 with CustomResourceDefinition

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

the class ControllerCoordinatorProcessingCriteriaTest method prepareCrds.

@BeforeEach
void prepareCrds() throws IOException {
    System.setProperty(ControllerCoordinatorProperty.ENTANDO_STORE_LOG_ENTRIES.getJvmSystemProperty(), "true");
    System.clearProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty());
    System.clearProperty(ControllerCoordinatorProperty.ENTANDO_K8S_OPERATOR_VERSION.getJvmSystemProperty());
    System.clearProperty(ControllerCoordinatorProperty.ENTANDO_K8S_OPERATOR_VERSION_TO_REPLACE.getJvmSystemProperty());
    final CustomResourceDefinition testResourceDefinition = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("testresources.test.org.crd.yaml"), CustomResourceDefinition.class);
    clientDouble.getCluster().putCustomResourceDefinition(new CustomResourceDefinitionBuilder(testResourceDefinition).editMetadata().addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "TestResource").addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller").addToAnnotations(AnnotationNames.SUPPORTED_CAPABILITIES.getName(), "dbms").endMetadata().build());
    final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("crd/providedcapabilities.entando.org.crd.yaml"), CustomResourceDefinition.class);
    clientDouble.getCluster().putCustomResourceDefinition(new CustomResourceDefinitionBuilder(value).editMetadata().addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "ProvidedCapability").addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-capability-controller").endMetadata().build());
    LogInterceptor.listenToClass(EntandoResourceObserver.class);
    LogInterceptor.listenToClass(EntandoControllerCoordinator.class);
}
Also used : CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)60 Test (org.junit.jupiter.api.Test)49 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)30 Description (io.qameta.allure.Description)12 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)11 IOException (java.io.IOException)11 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)10 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)10 Resource (io.fabric8.kubernetes.client.dsl.Resource)10 CustomResourceDefinitionContext (io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext)10 ArrayList (java.util.ArrayList)10 BeforeEach (org.junit.jupiter.api.BeforeEach)10 CustomResource (io.fabric8.kubernetes.client.CustomResource)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)7 StartupEvent (io.quarkus.runtime.StartupEvent)7 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)6