Skip to content

Commit 6194fd4

Browse files
committed
Add support for exposing ECS task metadata
1 parent 4c2da2e commit 6194fd4

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 exposing ECS task metadata as properties in the Spring application context.
28+
*
29+
* @author Jukka Palomäki
30+
* @since 3.5.0
31+
*/
32+
@AutoConfiguration
33+
@ConditionalOnProperty(name = "spring.cloud.aws.ecs-task-metadata.enabled", havingValue = "true", matchIfMissing = false)
34+
@ConditionalOnExpression("environment['ECS_CONTAINER_METADATA_URI_V4'] != null and environment['ECS_CONTAINER_METADATA_URI_V4'].trim() != ''")
35+
public class EcsTaskMetadataAutoConfiguration {
36+
37+
@Bean
38+
@ConditionalOnMissingBean
39+
public EcsTaskMetadataPropertySource ecsTaskMetadataPropertySource(ConfigurableEnvironment env,
40+
EcsTaskMetadataResolver ecsTaskMetadataResolver) {
41+
EcsTaskMetadataPropertySource propertySource = new EcsTaskMetadataPropertySource("EcsTaskMetadata",
42+
ecsTaskMetadataResolver);
43+
propertySource.init();
44+
env.getPropertySources().addFirst(propertySource);
45+
return propertySource;
46+
}
47+
48+
@Bean
49+
@ConditionalOnMissingBean
50+
public EcsTaskMetadataResolver ecsTaskMetadataResolver(RestClient.Builder restClientBuilder) {
51+
return new EcsTaskMetadataResolver(restClientBuilder);
52+
}
53+
}
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+
* Exposes ECS task metadata as Spring properties.
25+
*
26+
* @author Jukka Palomäki
27+
* @since 3.5.0
28+
*/
29+
public class EcsTaskMetadataPropertySource
30+
extends AwsPropertySource<EcsTaskMetadataPropertySource, EcsTaskMetadataResolver> {
31+
32+
private final String context;
33+
34+
private final EcsTaskMetadataResolver ecsTaskMetadataResolver;
35+
36+
private final Map<String, String> properties = new LinkedHashMap<>();
37+
38+
public EcsTaskMetadataPropertySource(String context, EcsTaskMetadataResolver ecsTaskMetadataResolver) {
39+
super(context, ecsTaskMetadataResolver);
40+
this.context = context;
41+
this.ecsTaskMetadataResolver = ecsTaskMetadataResolver;
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(ecsTaskMetadataResolver.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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.springframework.http.MediaType;
22+
import org.springframework.web.client.RestClient;
23+
import org.springframework.web.client.RestClient.Builder;
24+
import org.springframework.web.client.RestClientException;
25+
26+
/**
27+
* Resolves ECS task metadata. Supports ECS task metadata endpoint version 4.
28+
* <p>
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+
public class EcsTaskMetadataResolver {
36+
37+
private static final Logger LOGGER = LoggerFactory.getLogger(EcsTaskMetadataResolver.class);
38+
private static final String ECS_CONTAINER_METADATA_URI_V4 = System.getenv("ECS_CONTAINER_METADATA_URI_V4");
39+
40+
/** We expose a subset of available task metadata */
41+
private static final Set<String> EXPOSED_PROPERTIES = Set.of("Cluster", "ServiceName", "TaskARN", "Family",
42+
"Revision", "AvailabilityZone", "LaunchType");
43+
44+
private final RestClient restClient;
45+
46+
public EcsTaskMetadataResolver(Builder restClientBuilder) {
47+
this.restClient = restClientBuilder.baseUrl(ECS_CONTAINER_METADATA_URI_V4).build();
48+
}
49+
50+
/**
51+
* Returns ECS task metadata. If unable to fetch the metadata, an empty Map is returned.
52+
*/
53+
public Map<String, String> getEcsTaskMetadata() {
54+
Map<String, String> properties = new LinkedHashMap<>();
55+
tryPopulateTaskMetadata(properties);
56+
return properties;
57+
}
58+
59+
@SuppressWarnings("unchecked")
60+
private void tryPopulateTaskMetadata(Map<String, String> properties) {
61+
try {
62+
LOGGER.debug("Getting ECS task metadata from {}/task", ECS_CONTAINER_METADATA_URI_V4);
63+
Map<String, Object> taskMetadata = restClient.get().uri("/task").accept(MediaType.APPLICATION_JSON)
64+
.retrieve().body(Map.class);
65+
if (taskMetadata != null) {
66+
for (String key : EXPOSED_PROPERTIES) {
67+
if (taskMetadata.containsKey(key)) {
68+
properties.put("ecs-task-metadata." + key, taskMetadata.get(key).toString());
69+
}
70+
}
71+
}
72+
} catch (IllegalArgumentException | RestClientException e) {
73+
LOGGER.error("Error getting ECS task metadata", e);
74+
}
75+
}
76+
}
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)