Skip to content

Commit

Permalink
feat(ecs): Return application load balancer info from LB provider (#4526
Browse files Browse the repository at this point in the history
)

Co-authored-by: Clare Liguori <liguori@amazon.com>
  • Loading branch information
allisaurus and clareliguori committed Apr 23, 2020
1 parent 8fa6ef2 commit b8cfcab
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@
import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter;
import com.netflix.spinnaker.clouddriver.aws.data.Keys;
import com.netflix.spinnaker.clouddriver.ecs.cache.model.EcsLoadBalancerCache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.netflix.spinnaker.clouddriver.ecs.provider.view.EcsAccountMapper;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

Expand All @@ -39,10 +35,13 @@ public class EcsLoadbalancerCacheClient {

private final Cache cacheView;
private final ObjectMapper objectMapper;
private final EcsAccountMapper ecsAccountMapper;

public EcsLoadbalancerCacheClient(Cache cacheView, ObjectMapper objectMapper) {
public EcsLoadbalancerCacheClient(
Cache cacheView, ObjectMapper objectMapper, EcsAccountMapper ecsAccountMapper) {
this.cacheView = cacheView;
this.objectMapper = objectMapper;
this.ecsAccountMapper = ecsAccountMapper;
}

public List<EcsLoadBalancerCache> find(String account, String region) {
Expand All @@ -56,6 +55,12 @@ public List<EcsLoadBalancerCache> findAll() {

private Set<Map<String, Object>> fetchFromCache(String account, String region) {
String accountFilter = account != null ? account : "*";
if (!"*".equals(accountFilter)) {
String awsAccountName = ecsAccountMapper.fromEcsAccountNameToAwsAccountName(accountFilter);
if (awsAccountName != null) {
accountFilter = awsAccountName;
}
}
String regionFilter = region != null ? region : "*";

String searchKey = Keys.getLoadBalancerKey("*", accountFilter, regionFilter, "*", "*") + "*";
Expand All @@ -66,12 +71,16 @@ private Set<Map<String, Object>> fetchFromCache(String account, String region) {
return fetchLoadBalancerAttributes(loadbalancerKeys);
}

public Set<EcsLoadBalancerCache> findWithTargetGroups(Set<String> targetGroups) {
return findAll().stream()
.filter(
ecsLoadBalancerCache ->
targetGroups.containsAll(ecsLoadBalancerCache.getTargetGroups()))
.collect(Collectors.toSet());
public List<EcsLoadBalancerCache> findWithTargetGroups(Set<String> targetGroupKeys) {
Set<CacheData> targetGroupCacheData =
new HashSet<>(
cacheView.getAll(
TARGET_GROUPS.getNs(),
targetGroupKeys,
RelationshipCacheFilter.include(LOAD_BALANCERS.getNs())));
Set<String> lbKeys = inferAssociatedLoadBalancers(targetGroupCacheData);
Set<Map<String, Object>> loadbalancerAttributes = fetchLoadBalancerAttributes(lbKeys);
return convertToLoadbalancer(loadbalancerAttributes);
}

private EcsLoadBalancerCache convertToLoadBalancer(Map<String, Object> targetGroupAttributes) {
Expand All @@ -89,8 +98,9 @@ private List<EcsLoadBalancerCache> convertToLoadbalancer(
return ecsTargetGroups;
}

private Set<Map<String, Object>> fetchLoadBalancerAttributes(Collection<String> targetGroupKeys) {
Set<CacheData> loadBalancerCache = fetchLoadBalancers(targetGroupKeys);
private Set<Map<String, Object>> fetchLoadBalancerAttributes(
Collection<String> loadBalancerKeys) {
Set<CacheData> loadBalancerCache = fetchLoadBalancers(loadBalancerKeys);

return loadBalancerCache.stream()
.filter(this::hashTargetGroups)
Expand All @@ -108,7 +118,8 @@ private Map<String, Object> convertCacheData(CacheData loadbalancerCache) {
Map<String, String> parts = Keys.parse(loadbalancerCache.getId());

attributes.put("region", parts.get("region"));
attributes.put("account", parts.get("account"));
String ecsAccount = ecsAccountMapper.fromAwsAccountNameToEcsAccountName(parts.get("account"));
attributes.put("account", ecsAccount);
attributes.put("loadBalancerType", parts.get("loadBalancerType"));
attributes.put(
"targetGroups",
Expand All @@ -119,18 +130,12 @@ private Map<String, Object> convertCacheData(CacheData loadbalancerCache) {
return attributes;
}

private Set<Map<String, Object>> retrieveLoadbalancers(
Set<String> loadbalancersAssociatedWithTargetGroups) {
Collection<CacheData> loadbalancers =
cacheView.getAll(LOAD_BALANCERS.getNs(), loadbalancersAssociatedWithTargetGroups);
return loadbalancers.stream().map(CacheData::getAttributes).collect(Collectors.toSet());
}

private Set<String> inferAssociatedLoadBalancers(Set<CacheData> targetGroups) {
Set<String> loadbalancersAssociatedWithTargetGroups = new HashSet<>();

for (CacheData targetGroup : targetGroups) {
Collection<String> relatedLoadbalancer = targetGroup.getRelationships().get("loadbalancer");
Collection<String> relatedLoadbalancer =
targetGroup.getRelationships().get(LOAD_BALANCERS.ns);
if (relatedLoadbalancer != null && relatedLoadbalancer.size() > 0) {
loadbalancersAssociatedWithTargetGroups.addAll(relatedLoadbalancer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public List<EcsTargetGroup> find(Collection<String> targetGroupKeys) {
return targetGroups;
}

public Collection<String> getAllKeys() {
return cacheView.getIdentifiers(TARGET_GROUPS.ns);
}

private EcsTargetGroup convertToTargetGroup(Map<String, Object> targetGroupAttributes) {
EcsTargetGroup ecsTargetGroup =
objectMapper.convertValue(targetGroupAttributes, EcsTargetGroup.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public String getName() {

@Override
public String getType() {
return loadBalancerType;
return cloudProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under
* the License.
*/

package com.netflix.spinnaker.clouddriver.ecs.model.loadbalancer;

import com.amazonaws.services.elasticloadbalancingv2.model.Listener;
import com.netflix.spinnaker.clouddriver.ecs.EcsCloudProvider;
import com.netflix.spinnaker.clouddriver.model.LoadBalancer;
import com.netflix.spinnaker.clouddriver.model.LoadBalancerServerGroup;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.Data;

@Data
public class EcsLoadBalancer implements LoadBalancer {
// TODO: refactor EcsLoadBalancerCache so can be extended here?

private String account;
private String region;
private String loadBalancerArn;
private String loadBalancerType;
private String cloudProvider = EcsCloudProvider.ID;
private List<Listener> listeners;
private List<String> availabilityZones;
private String ipAddressType;
private String loadBalancerName;
private String canonicalHostedZoneId;
private String vpcId;
private String dnsname;
private Long createdTime;
private List<String> subnets;
private List<String> securityGroups;
private List<EcsTargetGroup> targetGroups;
private Set<LoadBalancerServerGroup> serverGroups;
private Map<String, Set<String>> targetGroupServices;

@Override
public String getName() {
return loadBalancerName;
}

@Override
public String getType() {
return cloudProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EcsLoadBalancerDetail implements Details {
String region;
String name;
String vpcId;
String type = "aws";
String type = "ecs";
String loadBalancerType;
List<String> securityGroups = new LinkedList<>();
List<String> targetGroups = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class EcsTargetGroup implements LoadBalancerProvider.Details {

List<String> loadBalancerNames;
List<String> instances;
String targetType;
Integer healthCheckTimeoutSeconds;
String targetGroupArn;
String healthCheckPort;
Expand Down
Loading

0 comments on commit b8cfcab

Please sign in to comment.