Skip to content

Commit

Permalink
Merge pull request #872 from UST-MICO/fix/mico#871-status-endpoint
Browse files Browse the repository at this point in the history
Fix/mico#871 status endpoint
  • Loading branch information
davidkopp authored Oct 2, 2019
2 parents 8e985fc + efea809 commit 637af25
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ getServiceInstanceStatus
.. java:method:: public MicoServiceStatusResponseDTO getServiceInstanceStatus(MicoServiceDeploymentInfo serviceDeploymentInfo)
:outertype: MicoStatusService

Get status information for a single \ :java:ref:`MicoServiceDeploymentInfo`\ : # available replicas, # requested replicas, pod metrics (CPU load, memory usage).
Get status information for a single \ :java:ref:`MicoServiceDeploymentInfo`\ if it is deployed: # available replicas, # requested replicas, pod metrics (CPU load, memory usage).

:param serviceDeploymentInfo: the \ :java:ref:`MicoServiceDeploymentInfo`\ .
:return: the \ :java:ref:`MicoServiceStatusResponseDTO`\ which contains status information for a specific instance of a \ :java:ref:`MicoService`\ .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public class MicoServiceStatusResponseDTO {
@ApiModelProperty(extensions = {@Extension(
name = CustomOpenApiExtentionsPlugin.X_MICO_CUSTOM_EXTENSION,
properties = {
@ExtensionProperty(name = "title", value = "Other Applications Using This Service Instance"),
@ExtensionProperty(name = "title", value = "Applications Using This Service Instance"),
@ExtensionProperty(name = "x-order", value = "70"),
@ExtensionProperty(name = "description", value = "List of MicoApplicationDTOs " +
"representing all applications that share the MicoService instance.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,23 @@ public List<MicoServiceStatusResponseDTO> getServiceStatus(MicoService micoServi
List<MicoServiceStatusResponseDTO> responseDTOList = new ArrayList<>();
for (MicoServiceDeploymentInfo serviceDeploymentInfo : serviceDeploymentInfos) {
MicoServiceStatusResponseDTO serviceInstanceStatus = getServiceInstanceStatus(serviceDeploymentInfo);
responseDTOList.add(serviceInstanceStatus);
// Add the instance only to the list if it's considered to be relevant for the service status:
// Either it must be requested to be deployed (requested replicas > 0)
// or it must be currently deployed (available replicas > 0).
if (serviceInstanceStatus.getRequestedReplicas() > 0 || serviceInstanceStatus.getAvailableReplicas() > 0) {
responseDTOList.add(serviceInstanceStatus);
} else {
log.debug("MicoService '{}' '{}' with instance ID '{}' is considered to be not relevant for the status response." +
"There are no requested or available replicas.",
micoService.getShortName(), micoService.getVersion(), serviceDeploymentInfo.getInstanceId());
}
}
return responseDTOList;
}

/**
* Get status information for a single {@link MicoServiceDeploymentInfo}: # available replicas, # requested replicas, pod metrics
* (CPU load, memory usage).
* Get status information for a single {@link MicoServiceDeploymentInfo} if it is deployed:
* # available replicas, # requested replicas, pod metrics (CPU load, memory usage).
*
* @param serviceDeploymentInfo the {@link MicoServiceDeploymentInfo}.
* @return the {@link MicoServiceStatusResponseDTO} which contains status information
Expand Down Expand Up @@ -168,10 +177,10 @@ public MicoServiceStatusResponseDTO getServiceInstanceStatus(MicoServiceDeployme
serviceStatus.setAvailableReplicas(0);
}
} else {
message = "No deployment of MicoService '" + micoService.getShortName() + "' '" + micoService.getVersion() +
"' with instance ID '" + instanceId + "' is available.";
log.warn(message);
MicoMessage errorMessage = MicoMessage.error(message);
message = "MicoService '" + micoService.getShortName() + "' '" + micoService.getVersion() +
"' with instance ID '" + instanceId + "' is not deployed.";
log.debug(message);
MicoMessage errorMessage = MicoMessage.info(message);
return serviceStatus.setErrorMessages(CollectionUtils.listOf(new MicoMessageResponseDTO(errorMessage)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.ust.mico.core.model.*;
import io.github.ust.mico.core.model.MicoMessage.Type;
import io.github.ust.mico.core.persistence.MicoApplicationRepository;
import io.github.ust.mico.core.persistence.MicoServiceDeploymentInfoRepository;
import io.github.ust.mico.core.persistence.MicoServiceInterfaceRepository;
import io.github.ust.mico.core.persistence.MicoServiceRepository;
import io.github.ust.mico.core.service.MicoKubernetesClient;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class MicoStatusServiceTest {
private MicoServiceRepository serviceRepository;
@MockBean
private MicoServiceInterfaceRepository serviceInterfaceRepository;
@MockBean
private MicoServiceDeploymentInfoRepository serviceDeploymentInfoRepository;
@Autowired
private MicoStatusService micoStatusService;

Expand Down Expand Up @@ -487,9 +490,9 @@ public void getApplicationStatusWithMissingDeployment() {
.setName(micoApplication.getServices().get(0).getName())
.setInstanceId(micoApplication.getServiceDeploymentInfos().get(0).getInstanceId())
.setErrorMessages(CollectionUtils
.listOf(new MicoMessageResponseDTO().setContent("No deployment of MicoService '" + micoService.getShortName()
.listOf(new MicoMessageResponseDTO().setContent("MicoService '" + micoService.getShortName()
+ "' '" + micoService.getVersion() + "' with instance ID '" + micoServiceDeploymentInfo.getInstanceId()
+ "' is available.").setType(Type.ERROR)))));
+ "' is not deployed.").setType(Type.INFO)))));
given(micoKubernetesClient.getDeploymentOfMicoServiceInstance(any(MicoServiceDeploymentInfo.class))).willReturn(Optional.empty());
given(micoKubernetesClient.getInterfaceByNameOfMicoServiceInstance(any(MicoServiceDeploymentInfo.class), anyString())).willReturn(Optional.ofNullable(kubernetesService));
given(micoKubernetesClient.getPodsCreatedByDeploymentOfMicoServiceInstance(any(MicoServiceDeploymentInfo.class))).willReturn(podListWithOnePod.getItems());
Expand All @@ -513,8 +516,8 @@ private ResponseEntity getPrometheusResponseEntity(int value) {
@Test
@SuppressWarnings({"rawtypes", "unchecked"})
public void getServiceStatus() throws Exception {
MicoServiceStatusResponseDTO micoServiceStatus = new MicoServiceStatusResponseDTO();
micoServiceStatus
List<MicoServiceStatusResponseDTO> micoServiceStatusList = new ArrayList<>();
MicoServiceStatusResponseDTO micoServiceStatusResponseDTO = new MicoServiceStatusResponseDTO()
.setName(micoService.getName())
.setShortName(micoService.getShortName())
.setVersion(micoService.getVersion())
Expand Down Expand Up @@ -578,22 +581,34 @@ public void getServiceStatus() throws Exception {
.setExternalIpIsAvailable(true)
.setExternalIp("192.168.2.112")
.setPort(8080)));
micoServiceStatusList.add(micoServiceStatusResponseDTO);

given(micoKubernetesClient.getPodsCreatedByDeploymentOfMicoServiceInstance(any(MicoServiceDeploymentInfo.class))).willReturn(podList.getItems());
given(serviceDeploymentInfoRepository.findAllByService(micoService.getShortName(), micoService.getVersion()))
.willReturn(CollectionUtils.listOf(micoServiceDeploymentInfo));
given(micoKubernetesClient.getPodsCreatedByDeploymentOfMicoServiceInstance(eq(micoServiceDeploymentInfo)))
.willReturn(podList.getItems());
Deployment deployment = new DeploymentBuilder()
.withNewMetadata().withName("deployment1").endMetadata()
.withNewSpec().withReplicas(podList.getItems().size()).endSpec()
.withNewStatus().withAvailableReplicas(podList.getItems().size()).endStatus()
.build();
given(micoKubernetesClient.getDeploymentOfMicoServiceInstance(any(MicoServiceDeploymentInfo.class))).willReturn(Optional.of(deployment));
given(micoKubernetesClient.getInterfaceByNameOfMicoServiceInstance(any(MicoServiceDeploymentInfo.class), anyString())).willReturn(Optional.ofNullable(kubernetesService));
given(micoKubernetesClient.getDeploymentOfMicoServiceInstance(eq(micoServiceDeploymentInfo)))
.willReturn(Optional.of(deployment));
given(micoKubernetesClient.getInterfaceByNameOfMicoServiceInstance(
eq(micoServiceDeploymentInfo), eq(micoServiceInterface.getServiceInterfaceName())))
.willReturn(Optional.ofNullable(kubernetesService));
given(micoKubernetesClient.isApplicationDeployed(otherMicoApplication)).willReturn(true);
given(micoKubernetesClient.getApplicationDeploymentStatus(otherMicoApplication)).willReturn(new MicoApplicationDeploymentStatus(MicoApplicationDeploymentStatus.Value.DEPLOYED));
given(applicationRepository.findByShortNameAndVersion(SHORT_NAME, VERSION)).willReturn(Optional.of(micoApplication));
given(applicationRepository.findAllByUsedServiceInstance(INSTANCE_ID)).willReturn(CollectionUtils.listOf(otherMicoApplication));
given(micoKubernetesClient.getApplicationDeploymentStatus(otherMicoApplication))
.willReturn(new MicoApplicationDeploymentStatus(MicoApplicationDeploymentStatus.Value.DEPLOYED));
given(applicationRepository.findByShortNameAndVersion(micoApplication.getShortName(), micoApplication.getVersion()))
.willReturn(Optional.of(micoApplication));
given(applicationRepository.findAllByUsedServiceInstance(micoServiceDeploymentInfo.getInstanceId()))
.willReturn(CollectionUtils.listOf(otherMicoApplication));

given(prometheusConfig.getUri()).willReturn("http://localhost:9090/api/v1/query");
given(serviceInterfaceRepository.findByServiceAndName(micoService.getShortName(), micoService.getVersion(), SERVICE_INTERFACE_NAME)).willReturn(Optional.of(micoServiceInterface));
given(serviceInterfaceRepository.findByServiceAndName(
micoService.getShortName(), micoService.getVersion(), micoServiceInterface.getServiceInterfaceName()))
.willReturn(Optional.of(micoServiceInterface));

given(micoKubernetesClient.getPublicIpOfKubernetesService(kubernetesService.getMetadata().getName(),
kubernetesService.getMetadata().getNamespace())).willReturn(Optional.of("192.168.2.112"));
Expand All @@ -617,7 +632,38 @@ public void getServiceStatus() throws Exception {
.willReturn(responseEntityCpuLoadPod3)
.willReturn(responseEntityMemoryUsagePod4)
.willReturn(responseEntityCpuLoadPod4);
assertEquals(micoServiceStatus, micoStatusService.getServiceInstanceStatus(micoServiceDeploymentInfo));

assertEquals(micoServiceStatusList, micoStatusService.getServiceStatus(micoService));
}

@Test
public void getServiceStatusWithInstanceThatIsNotDeployed() throws Exception {
given(micoKubernetesClient.getPodsCreatedByDeploymentOfMicoServiceInstance(eq(micoServiceDeploymentInfo)))
.willReturn(new ArrayList<>());
given(micoKubernetesClient.getDeploymentOfMicoServiceInstance(eq(micoServiceDeploymentInfo)))
.willReturn(Optional.empty());
given(micoKubernetesClient.getInterfaceByNameOfMicoServiceInstance(
eq(micoServiceDeploymentInfo), eq(micoServiceInterface.getServiceInterfaceName())))
.willReturn(Optional.empty());
given(micoKubernetesClient.isApplicationDeployed(otherMicoApplication)).willReturn(false);
given(micoKubernetesClient.getApplicationDeploymentStatus(otherMicoApplication))
.willReturn(new MicoApplicationDeploymentStatus(MicoApplicationDeploymentStatus.Value.UNDEPLOYED));
given(applicationRepository.findByShortNameAndVersion(micoApplication.getShortName(), micoApplication.getVersion()))
.willReturn(Optional.of(micoApplication));
given(applicationRepository.findAllByUsedServiceInstance(micoServiceDeploymentInfo.getInstanceId()))
.willReturn(CollectionUtils.listOf(otherMicoApplication));

given(prometheusConfig.getUri()).willReturn("http://localhost:9090/api/v1/query");
given(serviceInterfaceRepository.findByServiceAndName(
micoService.getShortName(), micoService.getVersion(), micoServiceInterface.getServiceInterfaceName()))
.willReturn(Optional.of(micoServiceInterface));

given(micoKubernetesClient.getPublicIpOfKubernetesService(kubernetesService.getMetadata().getName(),
kubernetesService.getMetadata().getNamespace())).willReturn(Optional.empty());
given(micoKubernetesClient.getPublicPortsOfKubernetesService(kubernetesService.getMetadata().getName(),
kubernetesService.getMetadata().getNamespace())).willReturn(new ArrayList<>());

assertEquals("Expected there is no instance included", 0, micoStatusService.getServiceStatus(micoService).size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public void getStatusListOfService() throws Exception {
MicoServiceDeploymentInfo micoServiceDeploymentInfo2 = new MicoServiceDeploymentInfo()
.setService(micoService)
.setInstanceId(INSTANCE_ID_2);
MicoServiceDeploymentInfo micoServiceDeploymentInfo3 = new MicoServiceDeploymentInfo()
.setService(micoService)
.setInstanceId(INSTANCE_ID_3);

String nodeName = "testNode";
String podPhase = "Running";
Expand Down Expand Up @@ -254,7 +257,7 @@ public void getStatusListOfService() throws Exception {
.setCpuLoad(cpuLoadPod2)
.setMemoryUsage(memoryUsagePod2));

MicoServiceStatusResponseDTO micoServiceStatus1 = new MicoServiceStatusResponseDTO()
MicoServiceStatusResponseDTO micoServiceInstanceStatus1 = new MicoServiceStatusResponseDTO()
.setVersion(micoService.getVersion())
.setName(micoService.getName())
.setShortName(micoService.getShortName())
Expand All @@ -269,7 +272,7 @@ public void getStatusListOfService() throws Exception {
.setInterfacesInformation(CollectionUtils.listOf(new MicoServiceInterfaceStatusResponseDTO().setName(SERVICE_INTERFACE_NAME)))
.setPodsInformation(Collections.singletonList(kubernetesPodInfo1));

MicoServiceStatusResponseDTO micoServiceStatus2 = new MicoServiceStatusResponseDTO()
MicoServiceStatusResponseDTO micoServiceInstanceStatus2 = new MicoServiceStatusResponseDTO()
.setVersion(micoService.getVersion())
.setName(micoService.getName())
.setShortName(micoService.getShortName())
Expand All @@ -284,10 +287,13 @@ public void getStatusListOfService() throws Exception {
.setInterfacesInformation(CollectionUtils.listOf(new MicoServiceInterfaceStatusResponseDTO().setName(SERVICE_INTERFACE_NAME)))
.setPodsInformation(Collections.singletonList(kubernetesPodInfo2));

given(micoStatusService.getServiceStatus(micoService)).willReturn(CollectionUtils.listOf(micoServiceStatus1, micoServiceStatus2));
// MicoService instance 3 ist not deployed, therefore the status should not be part of the list.
given(micoStatusService.getServiceStatus(micoService))
.willReturn(CollectionUtils.listOf(micoServiceInstanceStatus1, micoServiceInstanceStatus2));
given(serviceDeploymentInfoRepository.findAllByService(micoService.getShortName(), micoService.getVersion()))
.willReturn(CollectionUtils.listOf(micoServiceDeploymentInfo1, micoServiceDeploymentInfo2));
given(serviceRepository.findByShortNameAndVersion(micoService.getShortName(), micoService.getVersion())).willReturn(Optional.of(micoService));
.willReturn(CollectionUtils.listOf(micoServiceDeploymentInfo1, micoServiceDeploymentInfo2, micoServiceDeploymentInfo3));
given(serviceRepository.findByShortNameAndVersion(micoService.getShortName(), micoService.getVersion()))
.willReturn(Optional.of(micoService));

mvc.perform(get(BASE_PATH + "/" + SHORT_NAME + "/" + VERSION + "/status"))
.andDo(print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class TestConstants {
static final String INSTANCE_ID = "instance-id";
static final String INSTANCE_ID_1 = "instance-id-1";
static final String INSTANCE_ID_2 = "instance-id-2";
static final String INSTANCE_ID_3 = "instance-id-3";

static final String INPUT_TOPIC = "input-topic";
static final String INPUT_TOPIC_1 = "input-topic-1";
Expand Down

0 comments on commit 637af25

Please sign in to comment.