Search in sources :

Example 1 with LoadBalancer

use of com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer in project cloudbreak by hortonworks.

the class LoadBalancerRecreatorService method collectLoadBalancerMetadata.

private List<CloudLoadBalancerMetadata> collectLoadBalancerMetadata(AuthenticatedContext authenticatedContext, Long stackId) {
    List<LoadBalancerType> loadBalancerTypes = loadBalancerPersistenceService.findByStackId(stackId).stream().map(LoadBalancer::getType).collect(Collectors.toList());
    List<CloudResource> cloudResources = resourceService.findByStackIdAndType(stackId, ResourceType.ELASTIC_LOAD_BALANCER).stream().map(r -> cloudResourceConverter.convert(r)).collect(Collectors.toList());
    CollectLoadBalancerMetadataRequest request = new CollectLoadBalancerMetadataRequest(authenticatedContext.getCloudContext(), authenticatedContext.getCloudCredential(), loadBalancerTypes, cloudResources);
    eventBus.notify(request.selector(), Event.wrap(request));
    try {
        CollectLoadBalancerMetadataResult res = request.await();
        LOGGER.debug("Collect load balancer metadata result: {}", res);
        if (res.getStatus().equals(EventStatus.FAILED)) {
            String msg = "Failed to collect the load balancer metadata. " + res.getErrorDetails().getMessage();
            LOGGER.debug(msg);
            throw new CloudbreakServiceException(msg, res.getErrorDetails());
        }
        return res.getResults();
    } catch (InterruptedException e) {
        LOGGER.error("Error while collect load balancer metadata", e);
        throw new OperationException(e);
    }
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) CollectLoadBalancerMetadataRequest(com.sequenceiq.cloudbreak.cloud.event.loadbalancer.CollectLoadBalancerMetadataRequest) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) Inject(javax.inject.Inject) ResourceToCloudResourceConverter(com.sequenceiq.cloudbreak.converter.spi.ResourceToCloudResourceConverter) ResourceService(com.sequenceiq.cloudbreak.service.resource.ResourceService) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CreateResourcesRequest(com.sequenceiq.cloudbreak.cloud.event.resource.migration.aws.CreateResourcesRequest) Event(reactor.bus.Event) AwsNativeLoadBalancerLaunchService(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer) AwsContext(com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext) MetadataSetupService(com.sequenceiq.cloudbreak.service.stack.flow.MetadataSetupService) ResourceType(com.sequenceiq.common.api.type.ResourceType) LoadBalancerPersistenceService(com.sequenceiq.cloudbreak.service.stack.LoadBalancerPersistenceService) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) EventBus(reactor.bus.EventBus) EventStatus(com.sequenceiq.cloudbreak.cloud.event.model.EventStatus) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Collectors(java.util.stream.Collectors) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Component(org.springframework.stereotype.Component) OperationException(com.sequenceiq.cloudbreak.service.OperationException) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) CollectLoadBalancerMetadataResult(com.sequenceiq.cloudbreak.cloud.event.loadbalancer.CollectLoadBalancerMetadataResult) StackService(com.sequenceiq.cloudbreak.service.stack.StackService) CollectLoadBalancerMetadataRequest(com.sequenceiq.cloudbreak.cloud.event.loadbalancer.CollectLoadBalancerMetadataRequest) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CollectLoadBalancerMetadataResult(com.sequenceiq.cloudbreak.cloud.event.loadbalancer.CollectLoadBalancerMetadataResult) OperationException(com.sequenceiq.cloudbreak.service.OperationException) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType)

Example 2 with LoadBalancer

use of com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer in project cloudbreak by hortonworks.

the class CreateLoadBalancerEntityHandler method doAccept.

@Override
protected Selectable doAccept(HandlerEvent<CreateLoadBalancerEntityRequest> event) {
    CreateLoadBalancerEntityRequest request = event.getData();
    Stack stack = stackService.getById(request.getResourceId());
    try {
        LOGGER.debug("Creating load balancer entity objects for stack {}", stack.getId());
        DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
        if (environment != null && environment.getNetwork() != null && PublicEndpointAccessGateway.ENABLED.equals(environment.getNetwork().getPublicEndpointAccessGateway())) {
            enableEndpointGateway(stack, environment);
        }
        Set<InstanceGroup> instanceGroups = instanceGroupService.getByStackAndFetchTemplates(stack.getId());
        instanceGroups.forEach(ig -> ig.setTargetGroups(targetGroupPersistenceService.findByInstanceGroupId(ig.getId())));
        Set<LoadBalancer> existingLoadBalancers = loadBalancerPersistenceService.findByStackId(stack.getId());
        stack.setInstanceGroups(instanceGroups);
        Set<LoadBalancer> newLoadBalancers = loadBalancerConfigService.createLoadBalancers(stack, environment, null);
        Stack savedStack;
        if (doLoadBalancersAlreadyExist(existingLoadBalancers, newLoadBalancers)) {
            LOGGER.debug("Load balancer entities already exist. Continuing flow.");
            savedStack = stack;
        } else {
            LOGGER.debug("Persisting stack and load balancer objects to database.");
            stack.setLoadBalancers(newLoadBalancers);
            String stackName = stack.getName();
            savedStack = measure(() -> stackService.save(stack), LOGGER, "Stackrepository save took {} ms for stack {}", stackName);
            measure(() -> loadBalancerPersistenceService.saveAll(newLoadBalancers), LOGGER, "Load balancers saved in {} ms for stack {}", stackName);
            measure(() -> targetGroupPersistenceService.saveAll(newLoadBalancers.stream().flatMap(lb -> lb.getTargetGroupSet().stream()).collect(Collectors.toSet())), LOGGER, "Target groups saved in {} ms for stack {}", stackName);
            measure(() -> instanceGroupService.saveAll(newLoadBalancers.stream().flatMap(lb -> lb.getAllInstanceGroups().stream()).collect(Collectors.toSet())), LOGGER, "Instance groups saved in {} ms for stack {}", stackName);
            if (stack.getNetwork() != null) {
                measure(() -> networkService.pureSave(stack.getNetwork()), LOGGER, "Network saved in {} ms for stack {}", stackName);
            }
        }
        LOGGER.debug("Load balancer entities successfully persisted.");
        savedStack.setInstanceGroups(newLoadBalancers.stream().flatMap(lb -> lb.getAllInstanceGroups().stream()).collect(Collectors.toSet()));
        savedStack.setNetwork(stack.getNetwork());
        return new CreateLoadBalancerEntitySuccess(request.getResourceId(), savedStack);
    } catch (Exception e) {
        LOGGER.warn("Failed create load balancer entities and persist them to the database.", e);
        return new CreateLoadBalancerEntityFailure(request.getResourceId(), e);
    }
}
Also used : Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) CreateLoadBalancerEntityFailure(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntityFailure) CreateLoadBalancerEntitySuccess(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntitySuccess) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) LoadBalancerConfigService(com.sequenceiq.cloudbreak.service.LoadBalancerConfigService) LoggerFactory(org.slf4j.LoggerFactory) EventSelectorUtil(com.sequenceiq.flow.event.EventSelectorUtil) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) ENDPOINT_GATEWAY_SUBNET_ID(com.sequenceiq.cloudbreak.common.network.NetworkConstants.ENDPOINT_GATEWAY_SUBNET_ID) InstanceGroupService(com.sequenceiq.cloudbreak.service.stack.InstanceGroupService) Inject(javax.inject.Inject) SUBNET_ID(com.sequenceiq.cloudbreak.common.network.NetworkConstants.SUBNET_ID) ExceptionCatcherEventHandler(com.sequenceiq.flow.reactor.api.handler.ExceptionCatcherEventHandler) Event(reactor.bus.Event) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer) PublicEndpointAccessGateway(com.sequenceiq.common.api.type.PublicEndpointAccessGateway) Map(java.util.Map) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) NetworkService(com.sequenceiq.cloudbreak.service.network.NetworkService) TargetGroupPersistenceService(com.sequenceiq.cloudbreak.service.stack.TargetGroupPersistenceService) CreateLoadBalancerEntityRequest(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntityRequest) SubnetSelector(com.sequenceiq.cloudbreak.converter.v4.environment.network.SubnetSelector) LoadBalancerPersistenceService(com.sequenceiq.cloudbreak.service.stack.LoadBalancerPersistenceService) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) Logger(org.slf4j.Logger) Benchmark.measure(com.sequenceiq.cloudbreak.util.Benchmark.measure) Set(java.util.Set) HandlerEvent(com.sequenceiq.flow.reactor.api.handler.HandlerEvent) EnvironmentClientService(com.sequenceiq.cloudbreak.service.environment.EnvironmentClientService) Collectors(java.util.stream.Collectors) Json(com.sequenceiq.cloudbreak.common.json.Json) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) Collections(java.util.Collections) StackService(com.sequenceiq.cloudbreak.service.stack.StackService) CreateLoadBalancerEntityFailure(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntityFailure) CreateLoadBalancerEntityRequest(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntityRequest) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) InstanceGroup(com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup) CreateLoadBalancerEntitySuccess(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.CreateLoadBalancerEntitySuccess)

Example 3 with LoadBalancer

use of com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer in project cloudbreak by hortonworks.

the class LoadBalancerSANProvider method getLoadBalancerSAN.

public Optional<String> getLoadBalancerSAN(Stack stack) {
    checkNotNull(stack);
    checkNotNull(stack.getCluster());
    Cluster cluster = stack.getCluster();
    CmTemplateProcessor cmTemplateProcessor = new CmTemplateProcessor(cluster.getBlueprint().getBlueprintText());
    String cdhVersion = cmTemplateProcessor.getStackVersion();
    if (isVersionNewerOrEqualThanLimited(cdhVersion, CLOUDERA_STACK_VERSION_7_2_11)) {
        Set<LoadBalancer> loadBalancers = loadBalancerPersistenceService.findByStackId(stack.getId());
        if (!loadBalancers.isEmpty()) {
            Optional<LoadBalancer> loadBalancer = loadBalancerConfigService.selectLoadBalancerForFrontend(loadBalancers, LoadBalancerType.PUBLIC);
            return loadBalancer.flatMap(this::getBestSANForLB);
        }
    }
    return Optional.empty();
}
Also used : Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer) CmTemplateProcessor(com.sequenceiq.cloudbreak.cmtemplate.CmTemplateProcessor)

Example 4 with LoadBalancer

use of com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer in project cloudbreak by hortonworks.

the class LoadBalancerConfigService method createLoadBalancers.

public Set<LoadBalancer> createLoadBalancers(Stack stack, DetailedEnvironmentResponse environment, StackV4Request source) {
    LoadBalancerSku sku = getLoadBalancerSku(source);
    boolean azureLoadBalancerDisabled = CloudPlatform.AZURE.toString().equalsIgnoreCase(stack.getCloudPlatform()) && LoadBalancerSku.NONE.equals(sku);
    if (azureLoadBalancerDisabled) {
        Optional<TargetGroup> oozieTargetGroup = setupOozieHATargetGroup(stack, true);
        if (oozieTargetGroup.isPresent()) {
            throw new CloudbreakServiceException("Unsupported setup: Load balancers are disabled, but Oozie HA is configured. " + "Either enable Azure load balancers, or use a non-HA Oozie setup.");
        }
        LOGGER.debug("Azure load balancers have been explicitly disabled.");
        return Collections.emptySet();
    }
    boolean loadBalancerFlagEnabled = source != null && source.isEnableLoadBalancer();
    Set<LoadBalancer> loadBalancers = setupLoadBalancers(stack, environment, false, loadBalancerFlagEnabled, sku);
    if (stack.getCloudPlatform().equalsIgnoreCase(CloudPlatform.AZURE.toString())) {
        configureLoadBalancerAvailabilitySets(stack.getName(), loadBalancers);
        configureLoadBalancerSku(source, loadBalancers);
    }
    return loadBalancers;
}
Also used : LoadBalancerSku(com.sequenceiq.common.api.type.LoadBalancerSku) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException) TargetGroup(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroup) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer)

Example 5 with LoadBalancer

use of com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer in project cloudbreak by hortonworks.

the class GatewayPublicEndpointManagementService method deleteLoadBalancerDnsEntry.

public void deleteLoadBalancerDnsEntry(Stack stack, String environmentName) {
    Optional<LoadBalancer> loadBalancerOptional = getLoadBalancerWithEndpoint(stack);
    if (loadBalancerOptional.isEmpty()) {
        LOGGER.warn("Unable to find appropriate load balancer in stack. Load balancer public domain name will not be deleted.");
    } else {
        String accountId = ThreadBasedUserCrnProvider.getAccountId();
        if (StringUtils.isEmpty(environmentName)) {
            DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
            environmentName = environment.getName();
        }
        LoadBalancer loadBalancer = loadBalancerOptional.get();
        String endpoint = loadBalancer.getEndpoint();
        if (loadBalancer.getDns() != null && loadBalancer.getHostedZoneId() != null) {
            LOGGER.info("Deleting load balancer DNS entry with endpoint name: '{}', environment name: '{}' and cloud DNS: '{}'", endpoint, environmentName, loadBalancer.getDns());
            getDnsManagementService().deleteDnsEntryWithCloudDns(accountId, endpoint, environmentName, loadBalancer.getDns(), loadBalancer.getHostedZoneId());
        } else if (loadBalancer.getIp() != null) {
            LOGGER.info("Deleting load balancer DNS entry with endpoint name: '{}', environment name: '{}' and IP: '{}'", endpoint, environmentName, loadBalancer.getIp());
            getDnsManagementService().deleteDnsEntryWithIp(accountId, endpoint, environmentName, false, List.of(loadBalancer.getIp()));
        }
    }
}
Also used : DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer)

Aggregations

LoadBalancer (com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer)74 Test (org.junit.Test)48 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)46 SubnetTest (com.sequenceiq.cloudbreak.core.network.SubnetTest)39 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)37 StackV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request)32 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)26 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)20 InstanceGroup (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceGroup)16 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)12 Test (org.junit.jupiter.api.Test)12 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)9 LoadBalancerPersistenceService (com.sequenceiq.cloudbreak.service.stack.LoadBalancerPersistenceService)9 HashSet (java.util.HashSet)9 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)8 TargetGroup (com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroup)8 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)8 Map (java.util.Map)8 Optional (java.util.Optional)8 Json (com.sequenceiq.cloudbreak.common.json.Json)7