Skip to content

Commit ee56c60

Browse files
committed
First version of ECS task metadata lookup
1 parent 4c2da2e commit ee56c60

File tree

7 files changed

+241
-0
lines changed

7 files changed

+241
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-integration-sns</module>
5959
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-sqs</module>
6060
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-integration-sqs</module>
61+
<module>spring-cloud-aws-starters/spring-cloud-aws-starter-ecs</module>
6162
<module>spring-cloud-aws-samples</module>
6263
<module>spring-cloud-aws-test</module>
6364
<module>spring-cloud-aws-modulith</module>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.awspring.cloud.autoconfigure.ecs;
17+
18+
import org.springframework.boot.autoconfigure.AutoConfiguration;
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.core.env.ConfigurableEnvironment;
24+
import org.springframework.web.client.RestClient;
25+
26+
/**
27+
* Configuration for resolving ECS task metadata. Only supports ECS task metadata endpoint version 4.
28+
*
29+
* See <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html">AWS
30+
* documentation</a> for more details.
31+
*
32+
* @author Jukka Palomäki
33+
* @since 3.5.0
34+
*/
35+
@AutoConfiguration
36+
@ConditionalOnProperty(name = "spring.cloud.aws.ecs-task-metadata.enabled", havingValue = "true", matchIfMissing = false)
37+
@ConditionalOnExpression("environment['ECS_CONTAINER_METADATA_URI_V4'] != null and environment['ECS_CONTAINER_METADATA_URI_V4'].trim() != ''")
38+
public class EcsTaskMetadataAutoConfiguration {
39+
40+
@Bean
41+
@ConditionalOnMissingBean
42+
public EcsTaskMetadataPropertySource ecsTaskMetadataPropertySource(ConfigurableEnvironment env,
43+
EcsTaskMetadataUtils ecsTaskMetadataUtils) {
44+
EcsTaskMetadataPropertySource propertySource = new EcsTaskMetadataPropertySource("EcsTaskMetadata",
45+
ecsTaskMetadataUtils);
46+
propertySource.init();
47+
env.getPropertySources().addFirst(propertySource);
48+
return propertySource;
49+
}
50+
51+
@Bean
52+
@ConditionalOnMissingBean
53+
public EcsTaskMetadataUtils ecsTaskMetadataUtils(RestClient.Builder restClientBuilder) {
54+
return new EcsTaskMetadataUtils(restClientBuilder);
55+
}
56+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.awspring.cloud.autoconfigure.ecs;
17+
18+
import io.awspring.cloud.core.config.AwsPropertySource;
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* Adds properties from ECS task metadata when it is available.
25+
*
26+
* @author Jukka Palomäki
27+
* @since 3.5.0
28+
*/
29+
public class EcsTaskMetadataPropertySource
30+
extends AwsPropertySource<EcsTaskMetadataPropertySource, EcsTaskMetadataUtils> {
31+
32+
private final String context;
33+
34+
private final EcsTaskMetadataUtils ecsTaskMetadataUtils;
35+
36+
private final Map<String, String> properties = new LinkedHashMap<>();
37+
38+
public EcsTaskMetadataPropertySource(String context, EcsTaskMetadataUtils ecsTaskMetadataUtils) {
39+
super(context, ecsTaskMetadataUtils);
40+
this.context = context;
41+
this.ecsTaskMetadataUtils = ecsTaskMetadataUtils;
42+
}
43+
44+
@Override
45+
public EcsTaskMetadataPropertySource copy() {
46+
return new EcsTaskMetadataPropertySource(context, source);
47+
}
48+
49+
@Override
50+
public void init() {
51+
properties.putAll(ecsTaskMetadataUtils.getEcsTaskMetadata());
52+
}
53+
54+
@Override
55+
public String[] getPropertyNames() {
56+
return properties.keySet().toArray(String[]::new);
57+
}
58+
59+
@Override
60+
@Nullable
61+
public Object getProperty(String name) {
62+
return properties.get(name);
63+
}
64+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.awspring.cloud.autoconfigure.ecs;
17+
18+
import java.util.*;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.http.MediaType;
23+
import org.springframework.web.client.RestClient;
24+
import org.springframework.web.client.RestClient.Builder;
25+
import org.springframework.web.client.RestClientException;
26+
27+
/**
28+
* Utility object for working with ECS task metadata.
29+
*
30+
* @author Jukka Palomäki
31+
* @since 3.5.0
32+
*/
33+
public class EcsTaskMetadataUtils {
34+
35+
private static final Logger LOGGER = LoggerFactory.getLogger(EcsTaskMetadataUtils.class);
36+
private static final String ECS_CONTAINER_METADATA_URI_V4 = System.getenv("ECS_CONTAINER_METADATA_URI_V4");
37+
private static final Set<String> EXPOSED_PROPERTIES = Set.of("Cluster", "ServiceName", "VPCID", "TaskARN", "Family",
38+
"Revision", "DesiredStatus", "KnownStatus", "PullStartedAt", "PullStoppedAt", "AvailabilityZone",
39+
"LaunchType");
40+
41+
private final RestClient restClient;
42+
43+
public EcsTaskMetadataUtils(Builder restClientBuilder) {
44+
this.restClient = restClientBuilder.baseUrl(ECS_CONTAINER_METADATA_URI_V4).build();
45+
}
46+
47+
/**
48+
* Returns ECS task metadata. If unable to fetch the metadata, an empty Map is returned.
49+
*/
50+
public Map<String, String> getEcsTaskMetadata() {
51+
Map<String, String> properties = new LinkedHashMap<>();
52+
tryPopulateTaskMetadata(properties);
53+
return properties;
54+
}
55+
56+
@SuppressWarnings("unchecked")
57+
private void tryPopulateTaskMetadata(Map<String, String> properties) {
58+
try {
59+
LOGGER.debug("Getting ECS task metadata from {}/task", ECS_CONTAINER_METADATA_URI_V4);
60+
Map<String, Object> taskMetadata = restClient.get().uri("/task").accept(MediaType.APPLICATION_JSON)
61+
.retrieve().body(Map.class);
62+
if (taskMetadata != null) {
63+
for (String key : EXPOSED_PROPERTIES) {
64+
if (taskMetadata.containsKey(key)) {
65+
properties.put("ecs-task-metadata." + key, taskMetadata.get(key).toString());
66+
}
67+
}
68+
}
69+
}
70+
catch (IllegalArgumentException | RestClientException e) {
71+
LOGGER.error("Error getting ECS task metadata", e);
72+
}
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for ECS (Elastic Container Service) integration.
19+
*/
20+
@org.springframework.lang.NonNullApi
21+
@org.springframework.lang.NonNullFields
22+
package io.awspring.cloud.autoconfigure.ecs;

spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerAutoConfigur
1515
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreReloadAutoConfiguration
1616
io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreAutoConfiguration
1717
io.awspring.cloud.autoconfigure.config.s3.S3ReloadAutoConfiguration
18+
io.awspring.cloud.autoconfigure.ecs.EcsTaskMetadataAutoConfiguration
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-cloud-aws</artifactId>
7+
<groupId>io.awspring.cloud</groupId>
8+
<version>4.0.0-SNAPSHOT</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<name>Spring Cloud ECS Starter</name>
14+
<artifactId>spring-cloud-aws-starter-ecs</artifactId>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.awspring.cloud</groupId>
19+
<artifactId>spring-cloud-aws-starter</artifactId>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>

0 commit comments

Comments
 (0)