Skip to content

Commit

Permalink
Only process target groups in same account/region
Browse files Browse the repository at this point in the history
  • Loading branch information
jradhakrishnan committed Aug 30, 2023
1 parent 33d005d commit 7a8a567
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
76 changes: 39 additions & 37 deletions src/main/java/ai/asserts/aws/exporter/LBToLambdaRoutingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/
package ai.asserts.aws.exporter;

import ai.asserts.aws.AWSClientProvider;
import ai.asserts.aws.AWSApiCallRateLimiter;
import ai.asserts.aws.AWSClientProvider;
import ai.asserts.aws.SimpleTenantTask;
import ai.asserts.aws.TaskExecutorUtil;
import ai.asserts.aws.account.AWSAccount;
Expand Down Expand Up @@ -80,42 +80,44 @@ private Pair<Set<ResourceRelation>, Set<Resource>> buildRelations(String region,
try {
ElasticLoadBalancingV2Client elbV2Client = awsClientProvider.getELBV2Client(region, accountRegion);
Map<Resource, Resource> tgToLB = targetGroupLBMapProvider.getTgToLB();
tgToLB.keySet().forEach(tg -> {
try {
String api = "ElasticLoadBalancingV2Client/describeTargetHealth";
DescribeTargetHealthResponse response = rateLimiter.doWithRateLimit(
api,
ImmutableSortedMap.of(
SCRAPE_REGION_LABEL, region,
SCRAPE_ACCOUNT_ID_LABEL, accountRegion.getAccountId(),
SCRAPE_OPERATION_LABEL, api
)
, () -> elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder()
.targetGroupArn(tg.getArn())
.build()));
if (!isEmpty(response.targetHealthDescriptions())) {
response.targetHealthDescriptions().stream()
.map(tH -> resourceMapper.map(tH.target().id()))
.filter(opt -> opt.isPresent() && opt.get().getType().equals(LambdaFunction))
.map(Optional::get)
.forEach(lambda -> routing.add(ResourceRelation.builder()
.from(tgToLB.get(tg))
.to(lambda)
.name("ROUTES_TO")
.build()));
}
} catch (TargetGroupNotFoundException e) {
log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg);
missingTgs.add(tg);
} catch (Exception e) {
if (e.getCause() instanceof TargetGroupNotFoundException) {
log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg);
missingTgs.add(tg);
} else {
log.error("Failed to build resource relations", e);
}
}
});
tgToLB.keySet().stream()
.filter(tg -> tg.getAccount().equals(accountRegion.getAccountId()) && region.equals(tg.getRegion()))
.forEach(tg -> {
try {
String api = "ElasticLoadBalancingV2Client/describeTargetHealth";
DescribeTargetHealthResponse response = rateLimiter.doWithRateLimit(
api,
ImmutableSortedMap.of(
SCRAPE_REGION_LABEL, region,
SCRAPE_ACCOUNT_ID_LABEL, accountRegion.getAccountId(),
SCRAPE_OPERATION_LABEL, api
)
, () -> elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder()
.targetGroupArn(tg.getArn())
.build()));
if (!isEmpty(response.targetHealthDescriptions())) {
response.targetHealthDescriptions().stream()
.map(tH -> resourceMapper.map(tH.target().id()))
.filter(opt -> opt.isPresent() && opt.get().getType().equals(LambdaFunction))
.map(Optional::get)
.forEach(lambda -> routing.add(ResourceRelation.builder()
.from(tgToLB.get(tg))
.to(lambda)
.name("ROUTES_TO")
.build()));
}
} catch (TargetGroupNotFoundException e) {
log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg);
missingTgs.add(tg);
} catch (Exception e) {
if (e.getCause() instanceof TargetGroupNotFoundException) {
log.warn("LoadBalancer-2-TargetGroup Cache refers to non-existent TargetGroup {}", tg);
missingTgs.add(tg);
} else {
log.error("Failed to build resource relations", e);
}
}
});
} catch (Exception e) {
log.error("Error " + accountRegion, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void setup() {
new TaskExecutorUtil(new TestTaskThreadPool(),
new AWSApiCallRateLimiter(metricCollector, (account) -> "acme")));

AWSAccount awsAccount = new AWSAccount("tenant", "account", "accessId", "secretKey", "role",
AWSAccount awsAccount = new AWSAccount("acme", "account", "accessId", "secretKey", "role",
ImmutableSet.of("region"));
expect(accountProvider.getAccounts()).andReturn(ImmutableSet.of(awsAccount)).anyTimes();
expect(awsClientProvider.getELBV2Client("region", awsAccount)).andReturn(elbV2Client).anyTimes();
Expand All @@ -80,7 +80,11 @@ public void setup() {

@Test
void getRoutings() {
expect(targetGroupResource.getAccount()).andReturn("account");
expect(targetGroupResource.getRegion()).andReturn("region");
expect(targetGroupResource.getArn()).andReturn("tg-arn");
expect(targetGroupResource2.getAccount()).andReturn("account");
expect(targetGroupResource2.getRegion()).andReturn("region");
expect(targetGroupResource2.getArn()).andReturn("tg-arn2");
expect(elbV2Client.describeTargetHealth(DescribeTargetHealthRequest.builder()
.targetGroupArn("tg-arn")
Expand Down

0 comments on commit 7a8a567

Please sign in to comment.