use of com.amazonaws.services.ecs.model.ContainerDefinition in project gocd-ecs-elastic-agent by gocd.
the class ContainerDefinitionBuilderTest method shouldNotMakeMemoryReservationForWindows.
@Test
void shouldNotMakeMemoryReservationForWindows() {
when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
when(elasticAgentProfileProperties.getMaxMemory()).thenReturn(2048);
when(elasticAgentProfileProperties.getReservedMemory()).thenReturn(1024);
when(elasticAgentProfileProperties.platform()).thenReturn(Platform.WINDOWS);
ContainerDefinitionBuilder builder = new ContainerDefinitionBuilder().withName("foo").pluginSettings(pluginSettings).createAgentRequest(createAgentRequest);
final ContainerDefinition containerDefinition = builder.build();
assertThat(containerDefinition.getMemory()).isEqualTo(2048);
assertThat(containerDefinition.getMemoryReservation()).isNull();
}
use of com.amazonaws.services.ecs.model.ContainerDefinition in project gocd-ecs-elastic-agent by gocd.
the class ContainerDefinitionBuilderTest method shouldBuildContainerDefinitionWithEnvironments.
@Test
void shouldBuildContainerDefinitionWithEnvironments() {
when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
when(elasticAgentProfileProperties.getEnvironment()).thenReturn(Arrays.asList("TZ=PST"));
when(pluginSettings.getEnvironmentVariables()).thenReturn(Arrays.asList("JAVA_HOME=/var/lib/java"));
when(pluginSettings.getGoServerUrl()).thenReturn("https://foo.server/go");
when(createAgentRequest.autoRegisterKey()).thenReturn("some-auto-register-key");
when(createAgentRequest.environment()).thenReturn("some-environment");
when(createAgentRequest.autoRegisterPropertiesAsEnvironmentVars("foo")).thenCallRealMethod();
ContainerDefinitionBuilder builder = new ContainerDefinitionBuilder().withName("foo").pluginSettings(pluginSettings).createAgentRequest(createAgentRequest);
final ContainerDefinition containerDefinition = builder.build();
assertThat(containerDefinition.getEnvironment()).contains(new KeyValuePair().withName("TZ").withValue("PST"), new KeyValuePair().withName("JAVA_HOME").withValue("/var/lib/java"), new KeyValuePair().withName("GO_EA_MODE").withValue("dev"), new KeyValuePair().withName("GO_EA_SERVER_URL").withValue("https://foo.server/go"), new KeyValuePair().withName("GO_EA_AUTO_REGISTER_KEY").withValue("some-auto-register-key"), new KeyValuePair().withName("GO_EA_AUTO_REGISTER_ENVIRONMENT").withValue("some-environment"), new KeyValuePair().withName("GO_EA_AUTO_REGISTER_ELASTIC_AGENT_ID").withValue("foo"), new KeyValuePair().withName("GO_EA_AUTO_REGISTER_ELASTIC_PLUGIN_ID").withValue(PLUGIN_ID));
}
use of com.amazonaws.services.ecs.model.ContainerDefinition in project gocd-ecs-elastic-agent by gocd.
the class ContainerDefinitionBuilderTest method shouldBuildContainerDefinition.
@Test
void shouldBuildContainerDefinition() {
final LogConfiguration logConfiguration = new LogConfiguration().withLogDriver("awslog").withOptions(Collections.singletonMap("group", "foo"));
when(elasticAgentProfileProperties.getImage()).thenReturn("alpine");
when(elasticAgentProfileProperties.getMaxMemory()).thenReturn(2048);
when(elasticAgentProfileProperties.getReservedMemory()).thenReturn(1024);
when(elasticAgentProfileProperties.getCommand()).thenReturn(Arrays.asList("ping x.x.x.x", "-c", "160"));
when(pluginSettings.logConfiguration()).thenReturn(logConfiguration);
ContainerDefinitionBuilder builder = new ContainerDefinitionBuilder().withName("foo").pluginSettings(pluginSettings).createAgentRequest(createAgentRequest);
final ContainerDefinition containerDefinition = builder.build();
assertThat(containerDefinition.getName()).isEqualTo("foo");
assertThat(containerDefinition.getImage()).isEqualTo("alpine:latest");
assertThat(containerDefinition.getMemory()).isEqualTo(2048);
assertThat(containerDefinition.getMemoryReservation()).isEqualTo(1024);
assertThat(containerDefinition.getCommand()).contains("ping x.x.x.x", "-c", "160");
assertThat(containerDefinition.getLogConfiguration()).isEqualTo(logConfiguration);
}
use of com.amazonaws.services.ecs.model.ContainerDefinition 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