Search in sources :

Example 1 with MetadataNested

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project che-server by eclipse-che.

the class OpenShiftProjectTest method testOpenShiftProjectPreparingWhenProjectDoesNotExistWithCheServerSA.

@Test
public void testOpenShiftProjectPreparingWhenProjectDoesNotExistWithCheServerSA() throws Exception {
    // given
    prepareNamespaceGet(PROJECT_NAME);
    Resource resource = prepareProjectResource(PROJECT_NAME);
    doThrow(new KubernetesClientException("error", 403, null)).when(resource).get();
    final MixedOperation mixedOperation = mock(MixedOperation.class);
    final NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
    doReturn(mixedOperation).when(openShiftCheServerClient).serviceAccounts();
    when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
    when(namespaceOperation.withName(anyString())).thenReturn(serviceAccountResource);
    when(serviceAccountResource.get()).thenReturn(mock(ServiceAccount.class));
    doReturn(projectRequestOperation).when(openShiftCheServerClient).projectrequests();
    // doReturn(metadataNested).when(metadataNested).withName(anyString());
    when(openShiftCheServerClient.roleBindings()).thenReturn(mixedRoleBindingOperation);
    lenient().when(mixedRoleBindingOperation.inNamespace(anyString())).thenReturn(nonNamespaceRoleBindingOperation);
    when(openShiftClient.currentUser()).thenReturn(new UserBuilder().withNewMetadata().withName("user").endMetadata().build());
    // when
    openShiftProject.prepare(true, true, Map.of(), Map.of());
    // then
    ArgumentCaptor<ProjectRequest> captor = ArgumentCaptor.forClass(ProjectRequest.class);
    verify(projectRequestOperation).create(captor.capture());
    Assert.assertEquals(captor.getValue().getMetadata().getName(), PROJECT_NAME);
    verifyNoMoreInteractions(openShiftCheServerClient);
    verifyNoMoreInteractions(kubernetesClient);
    ArgumentCaptor<RoleBinding> roleBindingArgumentCaptor = ArgumentCaptor.forClass(RoleBinding.class);
    verify(nonNamespaceRoleBindingOperation).createOrReplace(roleBindingArgumentCaptor.capture());
    assertNotNull(roleBindingArgumentCaptor.getValue());
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) ProjectRequest(io.fabric8.openshift.api.model.ProjectRequest) Resource(io.fabric8.kubernetes.client.dsl.Resource) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) UserBuilder(io.fabric8.openshift.api.model.UserBuilder) Test(org.testng.annotations.Test)

Example 2 with MetadataNested

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project che-server by eclipse-che.

the class OpenShiftProjectTest method testOpenShiftProjectPreparingRoleBindingWhenProjectDoesNotExistWithCheServerSA.

@Test(dependsOnMethods = "testOpenShiftProjectPreparingWhenProjectDoesNotExistWithCheServerSA")
public void testOpenShiftProjectPreparingRoleBindingWhenProjectDoesNotExistWithCheServerSA() throws Exception {
    // given
    prepareNamespaceGet(PROJECT_NAME);
    Resource resource = prepareProjectResource(PROJECT_NAME);
    doThrow(new KubernetesClientException("error", 403, null)).when(resource).get();
    final MixedOperation mixedOperation = mock(MixedOperation.class);
    final NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
    doReturn(mixedOperation).when(openShiftCheServerClient).serviceAccounts();
    when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
    when(namespaceOperation.withName(anyString())).thenReturn(serviceAccountResource);
    when(serviceAccountResource.get()).thenReturn(mock(ServiceAccount.class));
    doReturn(projectRequestOperation).when(openShiftCheServerClient).projectrequests();
    // doReturn(metadataNested).when(metadataNested).withName(anyString());
    when(openShiftCheServerClient.roleBindings()).thenReturn(mixedRoleBindingOperation);
    lenient().when(mixedRoleBindingOperation.inNamespace(anyString())).thenReturn(nonNamespaceRoleBindingOperation);
    when(openShiftClient.currentUser()).thenReturn(new UserBuilder().withNewMetadata().withName("jdoe").endMetadata().build());
    // when
    openShiftProject.prepare(true, true, Map.of(), Map.of());
    // then
    ArgumentCaptor<RoleBinding> roleBindingArgumentCaptor = ArgumentCaptor.forClass(RoleBinding.class);
    verify(nonNamespaceRoleBindingOperation).createOrReplace(roleBindingArgumentCaptor.capture());
    RoleBinding roleBinding = roleBindingArgumentCaptor.getValue();
    assertNotNull(roleBinding);
    assertEquals(roleBinding.getMetadata().getName(), "admin");
    assertEquals(roleBinding.getRoleRef().getName(), "admin");
    assertEquals(roleBinding.getUserNames(), ImmutableList.of("jdoe"));
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Resource(io.fabric8.kubernetes.client.dsl.Resource) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) UserBuilder(io.fabric8.openshift.api.model.UserBuilder) Test(org.testng.annotations.Test)

Example 3 with MetadataNested

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested 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");
    });
}
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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 4 with MetadataNested

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

the class CrdManagementTest method testCapabilityEvent.

@Test
@Description("Capabilities that my controller image supports should also result in my controller image to be executed against the " + "ProvidedCapability")
void testCapabilityEvent() 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("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 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(providedCapability)) != null);
        assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(providedCapability)).getSpec().getContainers().get(0).getImage()).contains("test/my-controller");
    });
}
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) 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 5 with MetadataNested

use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)7 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)7 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)7 Description (io.qameta.allure.Description)7 StartupEvent (io.quarkus.runtime.StartupEvent)7 Test (org.junit.jupiter.api.Test)7 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)6 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)6 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)6 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 Resource (io.fabric8.kubernetes.client.dsl.Resource)4 RoleBinding (io.fabric8.openshift.api.model.RoleBinding)4 UserBuilder (io.fabric8.openshift.api.model.UserBuilder)4 SerializedEntandoResource (org.entando.kubernetes.controller.spi.client.SerializedEntandoResource)4 Test (org.testng.annotations.Test)4 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)3 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)3 Container (io.fabric8.kubernetes.api.model.Container)2 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)2