generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 249
Add Azure IMDS Support via Multi-Cloud Metadata Provider #1991
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5500dd0
Implmenting Azure IMDS changes on the agent
Paramadon 2ccbe06
Adding minor comments
Paramadon 744d6ca
Fixing build
Paramadon 8c0eb7a
Fix unused-parameter lint error in provider_test.go
Paramadon 7f3b046
Fix data race: read globalErr under mutex lock
Paramadon c3996fb
Replace sync.Once with atomic flag for race-safe test reset
Paramadon b09deb3
Fix tests to use mock provider for consistent behavior on Azure CI
Paramadon f5e4356
Fix TestTranslator to set mock provider before GetMetadataInfo call
Paramadon 1ed7a3d
minor updates and refactoring
movence e7f71a7
replace ec2util with metadata provider
movence 456ed46
fmt
movence f970232
fixes after rebase
movence 9505717
lint
movence c5c7e8d
fix test
movence 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 |
|---|---|---|
|
|
@@ -10,3 +10,5 @@ CWAGENT_VERSION | |
| terraform.* | ||
| **/.terraform/* | ||
| coverage.txt | ||
|
|
||
| .kiro/ | ||
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,224 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package aws | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/aws/amazon-cloudwatch-agent/internal/ec2metadataprovider" | ||
| "github.com/aws/amazon-cloudwatch-agent/internal/retryer" | ||
| ) | ||
|
|
||
| // Provider implements the metadata provider interface for AWS. | ||
| // Directly uses ec2metadataprovider for IMDS access with retry and fallback support. | ||
| type Provider struct { | ||
| logger *zap.Logger | ||
| metadata ec2metadataprovider.MetadataProvider | ||
|
|
||
| // Cached metadata (fetched once at initialization) | ||
| mu sync.RWMutex | ||
| instanceID string | ||
| instanceType string | ||
| imageID string | ||
| region string | ||
| availabilityZone string | ||
| accountID string | ||
| hostname string | ||
| privateIP string | ||
| available bool | ||
| } | ||
|
|
||
| // NewProvider creates a new AWS metadata provider | ||
| func NewProvider(ctx context.Context, logger *zap.Logger) (*Provider, error) { | ||
| if logger == nil { | ||
| logger = zap.NewNop() | ||
| } | ||
|
|
||
| // Create AWS config | ||
| cfg, err := config.LoadDefaultConfig(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load AWS config: %w", err) | ||
| } | ||
|
|
||
| // Create metadata provider with retry support | ||
| metadataProvider := ec2metadataprovider.NewMetadataProvider(cfg, retryer.GetDefaultRetryNumber()) | ||
|
|
||
| p := &Provider{ | ||
| logger: logger, | ||
| metadata: metadataProvider, | ||
| } | ||
|
|
||
| // Fetch initial metadata | ||
| if err := p.fetchMetadata(ctx); err != nil { | ||
| logger.Warn("Failed to fetch initial AWS metadata", zap.Error(err)) | ||
| // Don't return error - allow agent to start even if metadata unavailable | ||
| } | ||
|
|
||
| return p, nil | ||
| } | ||
|
|
||
| // fetchMetadata retrieves metadata from IMDS and caches it | ||
| func (p *Provider) fetchMetadata(ctx context.Context) error { | ||
| // Fetch instance identity document (critical - must succeed) | ||
| doc, err := p.metadata.Get(ctx) | ||
| if err != nil { | ||
| p.mu.Lock() | ||
| p.available = false | ||
| p.mu.Unlock() | ||
| return fmt.Errorf("failed to get instance identity document: %w", err) | ||
| } | ||
|
|
||
| // Fetch hostname separately (optional - failure is acceptable) | ||
| // Hostname is not critical for CloudWatch functionality | ||
| hostname, err := p.metadata.Hostname(ctx) | ||
| if err != nil { | ||
| p.logger.Debug("Failed to fetch hostname", zap.Error(err)) | ||
| hostname = "" | ||
| } | ||
|
|
||
| // Cache all metadata | ||
| p.mu.Lock() | ||
| p.instanceID = doc.InstanceID | ||
| p.instanceType = doc.InstanceType | ||
| p.imageID = doc.ImageID | ||
| p.region = doc.Region | ||
| p.availabilityZone = doc.AvailabilityZone | ||
| p.accountID = doc.AccountID | ||
| p.privateIP = doc.PrivateIP | ||
| p.hostname = hostname | ||
| p.available = true // Available even if hostname is empty | ||
movence marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| p.mu.Unlock() | ||
|
|
||
| p.logger.Debug("[cloudmetadata/aws] Metadata fetched successfully", | ||
| zap.String("region", doc.Region), | ||
| zap.String("availabilityZone", doc.AvailabilityZone)) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // IsAWS detects if running on AWS by attempting to fetch metadata. | ||
| // This is used during cloud detection. | ||
| func IsAWS(ctx context.Context) bool { | ||
| cfg, err := config.LoadDefaultConfig(ctx) | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| metadataProvider := ec2metadataprovider.NewMetadataProvider(cfg, retryer.GetDefaultRetryNumber()) | ||
| _, err = metadataProvider.Get(ctx) | ||
| return err == nil | ||
| } | ||
|
|
||
| // GetInstanceID returns the EC2 instance ID. | ||
| func (p *Provider) GetInstanceID() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.instanceID | ||
| } | ||
|
|
||
| // GetInstanceType returns the EC2 instance type | ||
| func (p *Provider) GetInstanceType() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.instanceType | ||
| } | ||
|
|
||
| // GetImageID returns the AMI ID | ||
| func (p *Provider) GetImageID() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.imageID | ||
| } | ||
|
|
||
| // GetRegion returns the AWS region | ||
| func (p *Provider) GetRegion() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.region | ||
| } | ||
|
|
||
| // GetAvailabilityZone returns the availability zone | ||
| func (p *Provider) GetAvailabilityZone() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.availabilityZone | ||
| } | ||
|
|
||
| // GetAccountID returns the AWS account ID | ||
| func (p *Provider) GetAccountID() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.accountID | ||
| } | ||
|
|
||
| // GetTags returns all EC2 tags. | ||
| // TODO: Implement using ec2metadataprovider.InstanceTags() (available since IMDSv2) | ||
| // For now, use tagutil package for tag operations. | ||
| func (p *Provider) GetTags() map[string]string { | ||
| return make(map[string]string) | ||
| } | ||
|
|
||
| // GetTag returns a specific EC2 tag value. | ||
| // TODO: Implement using ec2metadataprovider.InstanceTagValue() (available since IMDSv2) | ||
| // For now, use tagutil package for tag operations. | ||
| func (p *Provider) GetTag(_ string) (string, error) { | ||
| return "", fmt.Errorf("EC2 tags not implemented yet - use tagutil package") | ||
| } | ||
|
|
||
| // GetVolumeID returns the EBS volume ID for a given device name. | ||
| // Note: Volume mapping is handled by disktagger processor. | ||
| func (p *Provider) GetVolumeID(_ string) string { | ||
| return "" | ||
| } | ||
|
|
||
| // GetScalingGroupName returns the Auto Scaling Group name. | ||
| // Note: ASG name requires DescribeTags API call, not IMDS. | ||
| // Use tagutil.GetAutoScalingGroupName() for ASG lookup. | ||
| func (p *Provider) GetScalingGroupName() string { | ||
| return "" | ||
| } | ||
|
|
||
| // GetResourceGroupName returns empty string for AWS (Azure-specific concept) | ||
| func (p *Provider) GetResourceGroupName() string { | ||
| return "" | ||
| } | ||
|
|
||
| // Refresh refreshes the metadata from IMDS | ||
| func (p *Provider) Refresh(ctx context.Context) error { | ||
| return p.fetchMetadata(ctx) | ||
| } | ||
|
|
||
| // IsAvailable returns true if EC2 metadata is available. | ||
| // This checks if the provider has successfully fetched instance metadata. | ||
| func (p *Provider) IsAvailable() bool { | ||
|
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. What is the difference between |
||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.available | ||
| } | ||
|
|
||
| // GetHostname returns the EC2 instance hostname | ||
| func (p *Provider) GetHostname() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.hostname | ||
| } | ||
|
|
||
| // GetPrivateIP returns the EC2 instance private IP address | ||
| func (p *Provider) GetPrivateIP() string { | ||
| p.mu.RLock() | ||
| defer p.mu.RUnlock() | ||
| return p.privateIP | ||
| } | ||
|
|
||
| // GetCloudProvider returns the cloud provider type. | ||
| // Returns 1 (CloudProviderAWS from internal/cloudmetadata/constants.go). | ||
| // NOTE: Cannot import cloudmetadata package here due to import cycle. | ||
| func (p *Provider) GetCloudProvider() int { | ||
| return 1 // Must match cloudmetadata.CloudProviderAWS | ||
| } | ||
Oops, something went wrong.
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.
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.
If it's only fetched once, consider using
sync.Once, so we don't have to lock and unlock on eachGet*call.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.
mutex is needed because
Refresh()can update the cached values. While we fetch once at initialization, the interface supports refreshing (used by ec2tagger for periodic updates). The lock protects against concurrent reads during refresh.