fix(ecs): add null check for target group cache data
MODIFY
clouddriver-ecs/src/main/java/com/netflix/spinnaker/clouddriver/ecs/provider/agent/TargetHealthCachingAgent.java
clouddriver-ecs/src/main/java/com/netflix/spinnaker/clouddriver/ecs/provider/agent/TargetHealthCachingAgent.java
@@ -122,7 +122,10 @@ public class TargetHealthCachingAgent extends AbstractEcsAwsAwareCachingAgent<Ec
// TODO: refine search key instead of filtering these cache results
List<EcsTargetGroup> tgList =
cacheClient.find(targetGroupKeys).stream()
- .filter(t -> t.getTargetGroupArn().contains(account.getAccountId()))
+ .filter(
+ t ->
+ t.getTargetGroupArn() != null
+ && t.getTargetGroupArn().contains(account.getAccountId()))
.collect(Collectors.toList());
return tgList.stream().map(EcsTargetGroup::getTargetGroupArn).collect(Collectors.toSet());

MODIFY
clouddriver-ecs/src/test/groovy/com/netflix/spinnaker/clouddriver/ecs/provider/agent/TargetHealthCachingAgentSpec.groovy
clouddriver-ecs/src/test/groovy/com/netflix/spinnaker/clouddriver/ecs/provider/agent/TargetHealthCachingAgentSpec.groovy
@@ -197,4 +197,47 @@ class TargetHealthCachingAgentSpec extends Specification {
EcsTargetHealth targetHealthDescription = targetHealthList.get(0)
targetHealthDescription.getTargetGroupArn() == targetGroupArn
}
+
+ def 'should handle null targetGroupArn in cache data'() {
+ given:
+ def targetGroupAttributes = [
+ loadBalancerNames: ['loadBalancerName'],
+ targetGroupArn: null,
+ targetGroupName: null,
+ vpcId: 'vpc-id',
+ ]
+ def targetGroupKey =
+ Keys.getTargetGroupKey('test-tg', CommonCachingAgent.ACCOUNT, CommonCachingAgent.REGION, 'ip', 'vpc-id')
+ def loadbalancerKey =
+ Keys.getLoadBalancerKey('loadBalancerName', CommonCachingAgent.ACCOUNT, CommonCachingAgent.REGION, 'vpc-id', 'ip')
+ def relations = [loadBalancers: [loadbalancerKey]]
+ def targetGroupCacheData =
+ new DefaultCacheData(targetGroupKey, targetGroupAttributes, relations)
+
+
+ def badAwsProviderCache = Mock(ProviderCache)
+ badAwsProviderCache.filterIdentifiers(_, _) >> []
+ badAwsProviderCache.getAll(TARGET_GROUPS.getNs(), _, _) >> [targetGroupCacheData]
+
+ when:
+ agent.setAwsCache(badAwsProviderCache)
+ def targetHealthList = agent.getItems(ecs, Mock(ProviderCache))
+
+ then:
+ targetHealthList.size() == 0
+ }
+
+ def 'should return empty set when no target group cache data available'() {
+ given:
+ def emptyAwsProviderCache = Mock(ProviderCache)
+ emptyAwsProviderCache.filterIdentifiers(_, _) >> []
+ emptyAwsProviderCache.getAll(TARGET_GROUPS.getNs(), _, _) >> []
+
+ when:
+ agent.setAwsCache(emptyAwsProviderCache)
+ def targetHealthList = agent.getItems(ecs, Mock(ProviderCache))
+
+ then:
+ targetHealthList.size() == 0
+ }
}

fix(ecs): add null check for target group cache data (#4665)