Search in sources :

Example 6 with ContainerDefinition

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();
}
Also used : ContainerDefinition(com.amazonaws.services.ecs.model.ContainerDefinition) Test(org.junit.jupiter.api.Test)

Example 7 with ContainerDefinition

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));
}
Also used : KeyValuePair(com.amazonaws.services.ecs.model.KeyValuePair) ContainerDefinition(com.amazonaws.services.ecs.model.ContainerDefinition) Test(org.junit.jupiter.api.Test)

Example 8 with ContainerDefinition

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);
}
Also used : ContainerDefinition(com.amazonaws.services.ecs.model.ContainerDefinition) LogConfiguration(com.amazonaws.services.ecs.model.LogConfiguration) Test(org.junit.jupiter.api.Test)

Example 9 with ContainerDefinition

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();
}
Also used : ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) ContainerInstanceHelper(com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper) PENDING(com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.PENDING) WINDOWS(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.WINDOWS) ElasticAgentProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) MessageFormat.format(java.text.MessageFormat.format) Util(com.thoughtworks.gocd.elasticagent.ecs.utils.Util) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) RUNNING(com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.RUNNING) LOG(com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG) Platform(com.thoughtworks.gocd.elasticagent.ecs.domain.Platform) Instance(com.amazonaws.services.ec2.model.Instance) ContainerInstanceMatcher(com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.ContainerInstanceMatcher) InstanceMatcher(com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher) PluginSettings(com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config) Util.toMap(com.thoughtworks.gocd.elasticagent.ecs.utils.Util.toMap) ContainerDefinition(com.amazonaws.services.ecs.model.ContainerDefinition) Optional(java.util.Optional) ContainerInstance(com.amazonaws.services.ecs.model.ContainerInstance) Instance(com.amazonaws.services.ec2.model.Instance) EC2Config(com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config)

Aggregations

ContainerDefinition (com.amazonaws.services.ecs.model.ContainerDefinition)9 Test (org.junit.jupiter.api.Test)7 ElasticAgentProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ElasticAgentProfileProperties)2 Instance (com.amazonaws.services.ec2.model.Instance)1 ContainerInstance (com.amazonaws.services.ecs.model.ContainerInstance)1 KeyValuePair (com.amazonaws.services.ecs.model.KeyValuePair)1 LogConfiguration (com.amazonaws.services.ecs.model.LogConfiguration)1 LOG (com.thoughtworks.gocd.elasticagent.ecs.ECSElasticPlugin.LOG)1 ContainerInstanceHelper (com.thoughtworks.gocd.elasticagent.ecs.aws.ContainerInstanceHelper)1 EC2Config (com.thoughtworks.gocd.elasticagent.ecs.aws.EC2Config)1 ContainerInstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.ContainerInstanceMatcher)1 InstanceMatcher (com.thoughtworks.gocd.elasticagent.ecs.aws.matcher.InstanceMatcher)1 PENDING (com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.PENDING)1 RUNNING (com.thoughtworks.gocd.elasticagent.ecs.domain.EC2InstanceState.RUNNING)1 Platform (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform)1 WINDOWS (com.thoughtworks.gocd.elasticagent.ecs.domain.Platform.WINDOWS)1 PluginSettings (com.thoughtworks.gocd.elasticagent.ecs.domain.PluginSettings)1 Util (com.thoughtworks.gocd.elasticagent.ecs.utils.Util)1 Util.toMap (com.thoughtworks.gocd.elasticagent.ecs.utils.Util.toMap)1 MessageFormat.format (java.text.MessageFormat.format)1