-
-
Notifications
You must be signed in to change notification settings - Fork 353
Add support for exposing ECS task metadata as Spring properties #1485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jpalomaki
wants to merge
1
commit into
awspring:main
Choose a base branch
from
pafcloud:ecs-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...e/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
...gure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataPropertySource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<EcsTaskMetadataPropertySource, EcsTaskMetadataResolver> { | ||
|
|
||
| private final String context; | ||
|
|
||
| private final EcsTaskMetadataResolver ecsTaskMetadataResolver; | ||
|
|
||
| private final Map<String, String> 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); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
...oconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/EcsTaskMetadataResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| * <p> | ||
| * See <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html">AWS | ||
| * documentation</a> 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<String> 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<String, String> getEcsTaskMetadata() { | ||
| Map<String, String> properties = new LinkedHashMap<>(); | ||
| tryPopulateTaskMetadata(properties); | ||
| return properties; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private void tryPopulateTaskMetadata(Map<String, String> properties) { | ||
| try { | ||
| LOGGER.debug("Getting ECS task metadata from {}/task", ECS_CONTAINER_METADATA_URI_V4); | ||
| Map<String, Object> 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); | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...oud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/ecs/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
spring-cloud-aws-starters/spring-cloud-aws-starter-ecs/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need for starter considering it does not bring any extra dependencies? |
||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>spring-cloud-aws</artifactId> | ||
| <groupId>io.awspring.cloud</groupId> | ||
| <version>4.0.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <name>Spring Cloud ECS Starter</name> | ||
| <artifactId>spring-cloud-aws-starter-ecs</artifactId> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.awspring.cloud</groupId> | ||
| <artifactId>spring-cloud-aws-starter</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it should be disabled by default? It will add some time to startup so it should be an opt-in feature.