use of com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties in project gocd-ecs-elastic-agent by gocd.
the class InstanceSelectionStrategy method instanceForScheduling.
public Optional<ContainerInstance> instanceForScheduling(PluginSettings pluginSettings, ElasticAgentProfileProperties elasticAgentProfileProperties, ContainerDefinition containerDefinition) {
List<ContainerInstance> containerInstanceList = containerInstanceHelper.getContainerInstances(pluginSettings);
final EC2Config ec2Config = new EC2Config.Builder().withProfile(elasticAgentProfileProperties).withSettings(pluginSettings).build();
if (containerInstanceList.isEmpty()) {
return Optional.empty();
}
final List<Instance> ec2Instances = containerInstanceHelper.ec2InstancesFromContainerInstances(pluginSettings, containerInstanceList).stream().filter(instance -> ACCEPTABLE_STATES.contains(instance.getState().getName().toLowerCase())).collect(toList());
final Map<String, ContainerInstance> instanceMap = toMap(containerInstanceList, ContainerInstance::getEc2InstanceId, containerInstance -> containerInstance);
sortInstancesForScheduling(ec2Instances);
for (Instance instance : ec2Instances) {
final ContainerInstance containerInstance = instanceMap.get(instance.getInstanceId());
if (instanceMatcher.matches(ec2Config, instance) && containerInstanceMatcher.matches(containerInstance, containerDefinition)) {
if (isSpotInstance(instance)) {
containerInstanceHelper.removeLastSeenIdleTag(pluginSettings, asList(instance.getInstanceId()));
}
return Optional.of(containerInstance);
} else {
LOG.info(format("Skipped container creation on container instance {0}: required resources are not available.", instance.getInstanceId()));
}
}
return Optional.empty();
}
Aggregations