Example usage for com.amazonaws.services.elasticloadbalancingv2.model TargetDescription TargetDescription

List of usage examples for com.amazonaws.services.elasticloadbalancingv2.model TargetDescription TargetDescription

Introduction

In this page you can find the example usage for com.amazonaws.services.elasticloadbalancingv2.model TargetDescription TargetDescription.

Prototype

TargetDescription

Source Link

Usage

From source file:com.netflix.spinnaker.clouddriver.ecs.provider.agent.TaskHealthCachingAgent.java

License:Apache License

@Override
protected List<TaskHealth> getItems(AmazonECS ecs, ProviderCache providerCache) {
    TaskCacheClient taskCacheClient = new TaskCacheClient(providerCache, objectMapper);
    ServiceCacheClient serviceCacheClient = new ServiceCacheClient(providerCache, objectMapper);

    AmazonElasticLoadBalancing amazonloadBalancing = amazonClientProvider
            .getAmazonElasticLoadBalancingV2(accountName, awsCredentialsProvider, region);

    ContainerInstanceCacheClient containerInstanceCacheClient = new ContainerInstanceCacheClient(providerCache);

    List<TaskHealth> taskHealthList = new LinkedList<>();
    taskEvicitions = new LinkedList<>();
    serviceEvicitions = new LinkedList<>();
    taskDefEvicitions = new LinkedList<>();

    Collection<Task> tasks = taskCacheClient.getAll(accountName, region);
    if (tasks != null) {
        for (Task task : tasks) {
            String containerInstanceCacheKey = Keys.getContainerInstanceKey(accountName, region,
                    task.getContainerInstanceArn());
            ContainerInstance containerInstance = containerInstanceCacheClient.get(containerInstanceCacheKey);

            String serviceName = StringUtils.substringAfter(task.getGroup(), "service:");
            String serviceKey = Keys.getServiceKey(accountName, region, serviceName);
            Service service = serviceCacheClient.get(serviceKey);
            if (service == null) {
                String taskEvictionKey = Keys.getTaskKey(accountName, region, task.getTaskId());
                taskEvicitions.add(taskEvictionKey);
                continue;
            }//from w  ww.j a  v a2 s  .c  o  m

            if (task.getContainers().size() == 0 || task.getContainers().get(0).getNetworkBindings() == null
                    || task.getContainers().get(0).getNetworkBindings().size() == 0
                    || task.getContainers().get(0).getNetworkBindings().get(0) == null) {
                continue;
            }

            int port = task.getContainers().get(0).getNetworkBindings().get(0).getHostPort();

            List<LoadBalancer> loadBalancers = service.getLoadBalancers();

            for (LoadBalancer loadBalancer : loadBalancers) {
                if (loadBalancer.getTargetGroupArn() == null || containerInstance.getEc2InstanceId() == null) {
                    continue;
                }

                DescribeTargetHealthResult describeTargetHealthResult;
                describeTargetHealthResult = amazonloadBalancing.describeTargetHealth(
                        new DescribeTargetHealthRequest().withTargetGroupArn(loadBalancer.getTargetGroupArn())
                                .withTargets(new TargetDescription()
                                        .withId(containerInstance.getEc2InstanceId()).withPort(port)));

                if (describeTargetHealthResult.getTargetHealthDescriptions().size() == 0) {
                    String serviceEvictionKey = Keys.getTaskDefinitionKey(accountName, region,
                            service.getServiceName());
                    serviceEvicitions.add(serviceEvictionKey);
                    String taskEvictionKey = Keys.getTaskKey(accountName, region, task.getTaskId());
                    taskEvicitions.add(taskEvictionKey);

                    String taskDefArn = service.getTaskDefinition();
                    String taskDefKey = Keys.getTaskDefinitionKey(accountName, region, taskDefArn);
                    taskDefEvicitions.add(taskDefKey);
                    continue;
                }

                String targetHealth = describeTargetHealthResult.getTargetHealthDescriptions().get(0)
                        .getTargetHealth().getState();
                // TODO - Return better values, and think of a better strategy at defining health
                targetHealth = targetHealth.equals("healthy") ? "Up" : "Unknown";

                TaskHealth taskHealth = new TaskHealth();
                taskHealth.setType("loadBalancer");
                taskHealth.setState(targetHealth);
                taskHealth.setServiceName(serviceName);
                taskHealth.setTaskId(task.getTaskId());
                taskHealth.setTaskArn(task.getTaskArn());
                taskHealth.setInstanceId(task.getTaskArn());

                taskHealthList.add(taskHealth);
            }
        }
    }

    return taskHealthList;
}