Search in sources :

Example 41 with CloudLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.

the class AwsUpscaleServiceTest method upscaleWithLoadBalancers.

@Test
void upscaleWithLoadBalancers() {
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    AmazonCloudFormationClient amazonCloudFormationClient = mock(AmazonCloudFormationClient.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(new DescribeAutoScalingGroupsResult().withAutoScalingGroups(newAutoScalingGroup("masterASG", List.of("i-master1", "i-master2")), newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3"))));
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationClient);
    when(awsClient.createEc2Client(any(), any())).thenReturn(mock(AmazonEc2Client.class));
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("worker"))).thenReturn("workerASG");
    CloudContext cloudContext = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, new CloudCredential());
    List<CloudResource> allInstances = List.of(newInstanceResource("worker1", "worker", "i-worker1"), newInstanceResource("worker2", "worker", "i-worker2"), newInstanceResource("worker3", "worker", "i-worker3"), newInstanceResource("worker4", "worker", "i-worker4"), newInstanceResource("worker5", "worker", "i-worker5"));
    when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationClient), eq(amazonAutoScalingClient), anyList())).thenReturn(allInstances);
    doNothing().when(cfStackUtil).addLoadBalancerTargets(any(), any(), any());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();
    List<CloudLoadBalancer> loadBalancers = List.of(new CloudLoadBalancer(LoadBalancerType.PRIVATE), new CloudLoadBalancer(LoadBalancerType.PUBLIC));
    Group master = getMasterGroup(instanceAuthentication);
    groups.add(master);
    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);
    Map<String, String> tags = new HashMap<>();
    tags.put("owner", "cbuser");
    tags.put("created", "yesterday");
    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), tags, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null, loadBalancers);
    List<CloudResource> cloudResourceList = Collections.emptyList();
    AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L);
    awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList, adjustmentTypeWithThreshold);
    verify(cfStackUtil, times(2)).addLoadBalancerTargets(any(), any(), any());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.jupiter.api.Test)

Example 42 with CloudLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.

the class AwsLoadBalancerCommonService method getAwsLoadBalancers.

public List<AwsLoadBalancer> getAwsLoadBalancers(List<CloudLoadBalancer> cloudLoadBalancers, Map<String, List<String>> instanceIdsByGroupName, AwsNetworkView awsNetworkView) {
    LOGGER.debug("Converting internal load balancer model to AWS cloud provider model.");
    List<AwsLoadBalancer> awsLoadBalancers = new ArrayList<>();
    for (CloudLoadBalancer cloudLoadBalancer : cloudLoadBalancers) {
        LOGGER.debug("Found load balancer model of type {}", cloudLoadBalancer.getType());
        AwsLoadBalancer loadBalancer = convertLoadBalancer(cloudLoadBalancer, instanceIdsByGroupName, awsNetworkView, awsLoadBalancers);
        if (loadBalancer != null && !awsLoadBalancers.contains(loadBalancer)) {
            awsLoadBalancers.add(loadBalancer);
        }
    }
    Set<String> requestedTypes = cloudLoadBalancers.stream().map(lb -> lb.getType().name()).collect(Collectors.toSet());
    Set<String> awsTypes = awsLoadBalancers.stream().map(lb -> AwsLoadBalancerScheme.INTERNAL.awsScheme().equals(lb.getAwsScheme()) ? "PRIVATE" : "PUBLIC").collect(Collectors.toSet());
    if (!requestedTypes.equals(awsTypes)) {
        throw new CloudConnectorException(String.format("Can not create all requested AWS load balancers. " + "Types requested: [%s]; type to be created: [%s]", requestedTypes, awsTypes));
    }
    return awsLoadBalancers;
}
Also used : Logger(org.slf4j.Logger) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) List(java.util.List) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) Service(org.springframework.stereotype.Service) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ArrayList(java.util.ArrayList) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)

Example 43 with CloudLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.

the class AwsLoadBalancerCommonService method setupLoadBalancer.

private void setupLoadBalancer(CloudLoadBalancer cloudLoadBalancer, Map<String, List<String>> instanceIdsByGroupName, AwsLoadBalancer awsLoadBalancer) {
    LOGGER.debug("Configuring target instances for listeners.");
    for (Map.Entry<TargetGroupPortPair, Set<Group>> entry : cloudLoadBalancer.getPortToTargetGroupMapping().entrySet()) {
        AwsListener listener = awsLoadBalancer.getOrCreateListener(entry.getKey().getTrafficPort(), entry.getKey().getHealthCheckPort());
        Set<String> instanceIds = entry.getValue().stream().flatMap(tg -> instanceIdsByGroupName.get(tg.getName()).stream()).collect(Collectors.toSet());
        LOGGER.debug(String.format("Adding instances %s to listener %s", instanceIds, listener.getName()));
        listener.addInstancesToTargetGroup(instanceIds);
    }
}
Also used : Logger(org.slf4j.Logger) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) List(java.util.List) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) Service(org.springframework.stereotype.Service) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) Set(java.util.Set) HashSet(java.util.HashSet) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) Map(java.util.Map) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair)

Example 44 with CloudLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.

the class AwsLaunchServiceLoadBalancerTest method createCloudLoadBalancer.

private CloudLoadBalancer createCloudLoadBalancer(LoadBalancerType type) {
    Group group = new Group(INSTANCE_NAME, GATEWAY, List.of(), null, null, null, null, null, null, 100, null, createGroupNetwork(), emptyMap());
    CloudLoadBalancer cloudLoadBalancer = new CloudLoadBalancer(type);
    cloudLoadBalancer.addPortToTargetGroupMapping(new TargetGroupPortPair(PORT, PORT), Set.of(group));
    return cloudLoadBalancer;
}
Also used : AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair)

Example 45 with CloudLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.

the class AwsLoadBalancerLaunchService method updateCloudformationWithLoadBalancers.

public List<CloudResourceStatus> updateCloudformationWithLoadBalancers(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, ModelContext modelContext) {
    List<CloudResourceStatus> statuses = new ArrayList<>();
    List<CloudLoadBalancer> cloudLoadBalancers = stack.getLoadBalancers();
    String cFStackName = cfStackUtil.getCfStackName(ac);
    if (!cloudLoadBalancers.isEmpty()) {
        LOGGER.debug("Creating load balancers of types " + cloudLoadBalancers.stream().map(CloudLoadBalancer::getType).map(LoadBalancerType::name).collect(Collectors.joining(",")));
        AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
        String regionName = ac.getCloudContext().getLocation().getRegion().value();
        AmazonCloudFormationClient cfRetryClient = awsClient.createCloudFormationClient(credentialView, regionName);
        AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
        List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfRetryClient, amazonASClient, stack.getGroups());
        Network network = stack.getNetwork();
        AwsNetworkView awsNetworkView = new AwsNetworkView(network);
        if (modelContext == null) {
            LOGGER.debug("Creating default model context");
            modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
        }
        Map<String, List<String>> instanceIdsByGroupName = instances.stream().collect(Collectors.groupingBy(CloudResource::getGroup, mapping(CloudResource::getInstanceId, toList())));
        List<AwsLoadBalancer> awsLoadBalancers = loadBalancerCommonService.getAwsLoadBalancers(cloudLoadBalancers, instanceIdsByGroupName, awsNetworkView);
        modelContext.withLoadBalancers(awsLoadBalancers);
        LOGGER.debug("Starting CloudFormation update to create load balancer and target groups.");
        ListStackResourcesResult result;
        if (checkForLoadBalancerAndTargetGroupResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
            LOGGER.debug("Load balancer and target group resources already exist, skipping creation");
            result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
        } else {
            result = updateCloudFormationStack(ac, stack, modelContext);
        }
        setLoadBalancerMetadata(awsLoadBalancers, result);
        LOGGER.debug("Starting CloudFormation update to create listeners.");
        if (checkForListenerResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
            LOGGER.debug("Listener resources already exist, skipping creation");
            result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
        } else {
            result = updateCloudFormationStack(ac, stack, modelContext);
        }
        ListStackResourcesResult finalResult = result;
        awsLoadBalancers.forEach(lb -> statuses.add(createLoadBalancerStatus(ac, lb, finalResult)));
    } else {
        LOGGER.debug("No load balancers in stack");
    }
    return statuses;
}
Also used : ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ArrayList(java.util.ArrayList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Aggregations

CloudLoadBalancer (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer)47 TargetGroupPortPair (com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair)27 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)21 ArrayList (java.util.ArrayList)21 Test (org.junit.jupiter.api.Test)20 List (java.util.List)15 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)13 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)13 HashMap (java.util.HashMap)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)12 Set (java.util.Set)12 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)10 GroupNetwork (com.sequenceiq.cloudbreak.cloud.model.GroupNetwork)10 Network (com.sequenceiq.cloudbreak.cloud.model.Network)10 Map (java.util.Map)10 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)9