-
Notifications
You must be signed in to change notification settings - Fork 914
Implement Asynchronous EC2 Metadata Client #3523
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c81a347
IMDS Ec2 Metada refactoring
L-Applin 2eb31a1
IMDS Ec2 Metada refactoring
L-Applin c4bfaf0
IMDS Ec2 Metada refactoring
L-Applin 6a6bb3b
IMDS Ec2 Metada refactoring
L-Applin ce7e5cf
IMDS Ec2 Metada refactoring
L-Applin 38486f5
IMDS Ec2 Metada refactoring
L-Applin d5a44a7
IMDS Ec2 Metada refactoring
L-Applin 80f6c2f
IMDS Async
L-Applin 7eb6f00
IMDS Async client
L-Applin 1b25ae1
IMDS Async client
L-Applin 3d14c96
IMDS Async client
L-Applin e0c31c4
IMDS Async client
L-Applin cdb801c
IMDS Async client
L-Applin 884d552
IMDS Async
L-Applin a130632
IMDS Async client
L-Applin 30be64c
IMDS Async client
L-Applin 9981b14
IMDS Async client
L-Applin 73eeefa
IMDS Async client
L-Applin c5991e9
IMDS Async client
L-Applin d50c0be
IMDS Async client
L-Applin 58c589a
IMDS Async client
L-Applin d59b4b8
IMDS Async client
L-Applin 0db213f
Fix test classes
L-Applin 2c04edb
JavaDoc
L-Applin 132ce9b
PR comments and added IMDS to test-coverage-reporting
L-Applin 71b0ab3
Improve JavaDoc and rename some variables
L-Applin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "L-Applin", | ||
"description": "EC2Metadata Asynchronous Client" | ||
} |
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
106 changes: 0 additions & 106 deletions
106
core/imds/src/main/java/software/amazon/awssdk/imds/Ec2Metadata.java
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataAsyncClient.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,88 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.imds; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.core.exception.SdkClientException; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
import software.amazon.awssdk.imds.internal.DefaultEc2MetadataAsyncClient; | ||
import software.amazon.awssdk.utils.SdkAutoCloseable; | ||
|
||
/** | ||
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance. | ||
L-Applin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
@SdkPublicApi | ||
public interface Ec2MetadataAsyncClient extends SdkAutoCloseable { | ||
|
||
/** | ||
* Gets the specified instance metadata value by the given path. | ||
* | ||
* @param path Input path | ||
* @return A CompletableFuture that completes when the MetadataResponse is made available. | ||
*/ | ||
CompletableFuture<MetadataResponse> get(String path); | ||
|
||
/** | ||
* Create an {@link Ec2MetadataAsyncClient} instance using the default values. | ||
* | ||
* @return | ||
*/ | ||
static Ec2MetadataAsyncClient create() { | ||
return builder().build(); | ||
} | ||
|
||
static Ec2MetadataAsyncClient.Builder builder() { | ||
return DefaultEc2MetadataAsyncClient.builder(); | ||
} | ||
|
||
/** | ||
* The builder definition for a {@link Ec2MetadataClient}. All parameters are optional and have default values if not | ||
* specified. Therefore, an instance can be simply created with {@code Ec2MetadataAsyncClient.builder().build()} or | ||
* {@code Ec2MetadataAsyncClient.create()}, both having the same result. | ||
*/ | ||
interface Builder extends Ec2MetadataClientBuilder<Ec2MetadataAsyncClient.Builder, Ec2MetadataAsyncClient> { | ||
|
||
/** | ||
* Define the {@link ScheduledExecutorService} used to schedule asynchronous retry attempts. If provided, the | ||
* Ec2MetadataClient will <em>NOT</em> manage the lifetime if the httpClient and must therefore be | ||
* closed explicitly by calling the {@link SdkAsyncHttpClient#close()} method on it. | ||
* <p> | ||
* If not specified, defaults to {@link Executors#newScheduledThreadPool} with a default value of 3 thread in the | ||
* pool. | ||
* </p> | ||
* @param scheduledExecutorService the ScheduledExecutorService to use for retry attempt. | ||
* @return a reference to this builder | ||
*/ | ||
Builder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService); | ||
|
||
/** | ||
* Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will <em>NOT</em> manage the | ||
* lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()} | ||
* method on it. | ||
* <p> | ||
* If not specified, the IMDS client will look for a SdkAsyncHttpClient class included in the classpath of the | ||
* application and creates a new instance of that class, managed by the IMDS Client, that will be closed when the IMDS | ||
* Client is closed. If no such class can be found, will throw a {@link SdkClientException}. | ||
* </p> | ||
* @param httpClient the http client | ||
* @return a reference to this builder | ||
*/ | ||
Builder httpClient(SdkAsyncHttpClient httpClient); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClient.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,73 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.imds; | ||
|
||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.core.exception.SdkClientException; | ||
import software.amazon.awssdk.http.SdkHttpClient; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
import software.amazon.awssdk.imds.internal.DefaultEc2MetadataClient; | ||
import software.amazon.awssdk.utils.SdkAutoCloseable; | ||
|
||
|
||
/** | ||
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance. | ||
*/ | ||
@SdkPublicApi | ||
public interface Ec2MetadataClient extends SdkAutoCloseable { | ||
|
||
/** | ||
* Gets the specified instance metadata value by the given path. | ||
* @param path Input path | ||
* @return Instance metadata value as part of MetadataResponse Object | ||
*/ | ||
MetadataResponse get(String path); | ||
|
||
/** | ||
* Create an {@link Ec2MetadataClient} instance using the default values. | ||
*/ | ||
static Ec2MetadataClient create() { | ||
return builder().build(); | ||
} | ||
|
||
/** | ||
* Creates a default builder for {@link Ec2MetadataClient}. | ||
*/ | ||
static Builder builder() { | ||
return DefaultEc2MetadataClient.builder(); | ||
} | ||
|
||
/** | ||
* The builder definition for a {@link Ec2MetadataClient}. | ||
*/ | ||
interface Builder extends Ec2MetadataClientBuilder<Ec2MetadataClient.Builder, Ec2MetadataClient> { | ||
|
||
/** | ||
* Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will <em>NOT</em> manage the | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()} | ||
* method on it. | ||
* <p> | ||
* If not specified, the IMDS client will look for a SdkHttpClient class included in the classpath of the | ||
* application and creates a new instance of that class, managed by the IMDS Client, that will be closed when the IMDS | ||
* Client is closed. If no such class can be found, will throw a {@link SdkClientException}. | ||
* </p> | ||
* @param httpClient the http client | ||
* @return a reference to this builder | ||
*/ | ||
Builder httpClient(SdkHttpClient httpClient); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClientBuilder.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,86 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.imds; | ||
|
||
import java.net.URI; | ||
import java.time.Duration; | ||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.core.retry.RetryMode; | ||
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy; | ||
import software.amazon.awssdk.imds.internal.Ec2MetadataEndpointProvider; | ||
import software.amazon.awssdk.utils.builder.SdkBuilder; | ||
|
||
/** | ||
* Base shared builder interface for Ec2MetadataClient | ||
* @param <B> the Builder Type | ||
* @param <T> the Ec2MetadataClient Type | ||
*/ | ||
@SdkPublicApi | ||
public interface Ec2MetadataClientBuilder<B, T> extends SdkBuilder<Ec2MetadataClientBuilder<B, T>, T> { | ||
/** | ||
* Define the retry policy which includes the number of retry attempts for any failed request. | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* <p> | ||
* If not specified, defaults to 3 retry attempts and a {@link BackoffStrategy#defaultStrategy()} backoff strategy} that | ||
* uses {@link RetryMode#STANDARD} | ||
* </p> | ||
* @param retryPolicy The retry policy which includes the number of retry attempts for any failed request. | ||
* @return a reference to this builder | ||
*/ | ||
B retryPolicy(Ec2MetadataRetryPolicy retryPolicy); | ||
|
||
Ec2MetadataRetryPolicy getRetryPolicy(); | ||
|
||
/** | ||
* Define the endpoint of IMDS. | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* <p> | ||
* If not specified, the IMDS client will attempt to automatically resolve the endpoint value | ||
* based on the logic of {@link Ec2MetadataEndpointProvider}. | ||
* </p> | ||
* @param endpoint The endpoint of IMDS. | ||
* @return a reference to this builder | ||
*/ | ||
B endpoint(URI endpoint); | ||
|
||
URI getEndpoint(); | ||
|
||
/** | ||
* Define the Time to live (TTL) of the token. The token represents a logical session. The TTL specifies the length of time | ||
* that the token is valid and, therefore, the duration of the session. Defaults to 21,600 seconds (6 hours) if not specified. | ||
* | ||
* @param tokenTtl The Time to live (TTL) of the token. | ||
* @return a reference to this builder | ||
*/ | ||
B tokenTtl(Duration tokenTtl); | ||
|
||
Duration getTokenTtl(); | ||
|
||
/** | ||
* Define the endpoint mode of IMDS. Supported values include IPv4 and IPv6. Used to determine the endpoint of the IMDS | ||
* Client only if {@link Ec2MetadataClientBuilder#endpoint(URI)} is not specified. Only one of both endpoint or endpoint mode | ||
* but not both. If both are specified in the Builder, a {@link IllegalArgumentException} will be thrown. | ||
* <p> | ||
* If not specified, the IMDS client will attempt to automatically resolve the endpoint mode value | ||
* based on the logic of {@link Ec2MetadataEndpointProvider}. | ||
* </p> | ||
* | ||
* @param endpointMode The endpoint mode of IMDS. Supported values include IPv4 and IPv6. | ||
* @return a reference to this builder | ||
*/ | ||
B endpointMode(EndpointMode endpointMode); | ||
|
||
EndpointMode getEndpointMode(); | ||
|
||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.