From 6194fd490c05ba778fd9681250c2ba9be6d680b2 Mon Sep 17 00:00:00 2001 From: jpalomaki Date: Mon, 29 Sep 2025 21:54:07 +0300 Subject: [PATCH] Add support for exposing ECS task metadata --- pom.xml | 1 + .../ecs/EcsTaskMetadataAutoConfiguration.java | 53 +++++++++++++ .../ecs/EcsTaskMetadataPropertySource.java | 64 ++++++++++++++++ .../ecs/EcsTaskMetadataResolver.java | 76 +++++++++++++++++++ .../cloud/autoconfigure/ecs/package-info.java | 22 ++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../spring-cloud-aws-starter-ecs/pom.xml | 23 ++++++ 7 files changed, 240 insertions(+) create mode 100644 spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataAutoConfiguration.java create mode 100644 spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataPropertySource.java create mode 100644 spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java create mode 100644 spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java create mode 100644 spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml diff --git a/pom.xml b/pom.xml index baee1cd9a..e972d16db 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,7 @@ spring-cloud-aws-starters/spring-cloud-aws-starter-integration-sns spring-cloud-aws-starters/spring-cloud-aws-starter-sqs spring-cloud-aws-starters/spring-cloud-aws-starter-integration-sqs + spring-cloud-aws-starters/spring-cloud-aws-starter-ecs spring-cloud-aws-samples spring-cloud-aws-test spring-cloud-aws-modulith diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataAutoConfiguration.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataAutoConfiguration.java new file mode 100644 index 000000000..c453bc91e --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataAutoConfiguration.java @@ -0,0 +1,53 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.awspring.cloud.autoconfigure.ecs; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.web.client.RestClient; + +/** + * Configuration for exposing ECS task metadata as properties in the Spring application context. + * + * @author Jukka Palomäki + * @since 3.5.0 + */ +@AutoConfiguration +@ConditionalOnProperty(name = "spring.cloud.aws.ecs-task-metadata.enabled", havingValue = "true", matchIfMissing = false) +@ConditionalOnExpression("environment['ECS_CONTAINER_METADATA_URI_V4'] != null and environment['ECS_CONTAINER_METADATA_URI_V4'].trim() != ''") +public class EcsTaskMetadataAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public EcsTaskMetadataPropertySource ecsTaskMetadataPropertySource(ConfigurableEnvironment env, + EcsTaskMetadataResolver ecsTaskMetadataResolver) { + EcsTaskMetadataPropertySource propertySource = new EcsTaskMetadataPropertySource("EcsTaskMetadata", + ecsTaskMetadataResolver); + propertySource.init(); + env.getPropertySources().addFirst(propertySource); + return propertySource; + } + + @Bean + @ConditionalOnMissingBean + public EcsTaskMetadataResolver ecsTaskMetadataResolver(RestClient.Builder restClientBuilder) { + return new EcsTaskMetadataResolver(restClientBuilder); + } +} diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataPropertySource.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataPropertySource.java new file mode 100644 index 000000000..8eaab58ef --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataPropertySource.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.awspring.cloud.autoconfigure.ecs; + +import io.awspring.cloud.core.config.AwsPropertySource; +import java.util.LinkedHashMap; +import java.util.Map; +import org.springframework.lang.Nullable; + +/** + * Exposes ECS task metadata as Spring properties. + * + * @author Jukka Palomäki + * @since 3.5.0 + */ +public class EcsTaskMetadataPropertySource + extends AwsPropertySource { + + private final String context; + + private final EcsTaskMetadataResolver ecsTaskMetadataResolver; + + private final Map properties = new LinkedHashMap<>(); + + public EcsTaskMetadataPropertySource(String context, EcsTaskMetadataResolver ecsTaskMetadataResolver) { + super(context, ecsTaskMetadataResolver); + this.context = context; + this.ecsTaskMetadataResolver = ecsTaskMetadataResolver; + } + + @Override + public EcsTaskMetadataPropertySource copy() { + return new EcsTaskMetadataPropertySource(context, source); + } + + @Override + public void init() { + properties.putAll(ecsTaskMetadataResolver.getEcsTaskMetadata()); + } + + @Override + public String[] getPropertyNames() { + return properties.keySet().toArray(String[]::new); + } + + @Override + @Nullable + public Object getProperty(String name) { + return properties.get(name); + } +} diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java new file mode 100644 index 000000000..ae1e74b10 --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java @@ -0,0 +1,76 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.awspring.cloud.autoconfigure.ecs; + +import java.util.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; +import org.springframework.web.client.RestClient; +import org.springframework.web.client.RestClient.Builder; +import org.springframework.web.client.RestClientException; + +/** + * Resolves ECS task metadata. Supports ECS task metadata endpoint version 4. + *

+ * See AWS + * documentation for more details. + * + * @author Jukka Palomäki + * @since 3.5.0 + */ +public class EcsTaskMetadataResolver { + + private static final Logger LOGGER = LoggerFactory.getLogger(EcsTaskMetadataResolver.class); + private static final String ECS_CONTAINER_METADATA_URI_V4 = System.getenv("ECS_CONTAINER_METADATA_URI_V4"); + + /** We expose a subset of available task metadata */ + private static final Set EXPOSED_PROPERTIES = Set.of("Cluster", "ServiceName", "TaskARN", "Family", + "Revision", "AvailabilityZone", "LaunchType"); + + private final RestClient restClient; + + public EcsTaskMetadataResolver(Builder restClientBuilder) { + this.restClient = restClientBuilder.baseUrl(ECS_CONTAINER_METADATA_URI_V4).build(); + } + + /** + * Returns ECS task metadata. If unable to fetch the metadata, an empty Map is returned. + */ + public Map getEcsTaskMetadata() { + Map properties = new LinkedHashMap<>(); + tryPopulateTaskMetadata(properties); + return properties; + } + + @SuppressWarnings("unchecked") + private void tryPopulateTaskMetadata(Map properties) { + try { + LOGGER.debug("Getting ECS task metadata from {}/task", ECS_CONTAINER_METADATA_URI_V4); + Map taskMetadata = restClient.get().uri("/task").accept(MediaType.APPLICATION_JSON) + .retrieve().body(Map.class); + if (taskMetadata != null) { + for (String key : EXPOSED_PROPERTIES) { + if (taskMetadata.containsKey(key)) { + properties.put("ecs-task-metadata." + key, taskMetadata.get(key).toString()); + } + } + } + } catch (IllegalArgumentException | RestClientException e) { + LOGGER.error("Error getting ECS task metadata", e); + } + } +} diff --git a/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java new file mode 100644 index 000000000..68b653c8b --- /dev/null +++ b/spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Auto-configuration for ECS (Elastic Container Service) integration. + */ +@org.springframework.lang.NonNullApi +@org.springframework.lang.NonNullFields +package io.awspring.cloud.autoconfigure.ecs; diff --git a/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 657cbf245..3c1fe936f 100644 --- a/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-aws-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -15,3 +15,4 @@ io.awspring.cloud.autoconfigure.config.secretsmanager.SecretsManagerAutoConfigur io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreReloadAutoConfiguration io.awspring.cloud.autoconfigure.config.parameterstore.ParameterStoreAutoConfiguration io.awspring.cloud.autoconfigure.config.s3.S3ReloadAutoConfiguration +io.awspring.cloud.autoconfigure.ecs.EcsTaskMetadataAutoConfiguration diff --git a/spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml b/spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml new file mode 100644 index 000000000..c73a74e99 --- /dev/null +++ b/spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml @@ -0,0 +1,23 @@ + + + + spring-cloud-aws + io.awspring.cloud + 4.0.0-SNAPSHOT + ../../pom.xml + + 4.0.0 + + Spring Cloud ECS Starter + spring-cloud-aws-starter-ecs + + + + io.awspring.cloud + spring-cloud-aws-starter + + + +