Skip to content
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

Automated cherry pick of #9857: Detect AWS region for S3 inside containers #9858

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions util/pkg/vfs/s3context.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ func (s *S3Context) getDetailsForBucket(bucket string) (*S3BucketDetails, error)
}

awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" && isRunningOnEC2() {
region, err := getRegionFromMetadata()
if err != nil {
klog.V(2).Infof("unable to get region from metadata:%v", err)
} else {
awsRegion = region
klog.V(2).Infof("got region from metadata: %q", awsRegion)
if awsRegion == "" {
isEC2, err := isRunningOnEC2()
if isEC2 || err != nil {
region, err := getRegionFromMetadata()
if err != nil {
klog.V(2).Infof("unable to get region from metadata:%v", err)
} else {
awsRegion = region
klog.V(2).Infof("got region from metadata: %q", awsRegion)
}
}
}

Expand Down Expand Up @@ -317,25 +320,25 @@ func bruteforceBucketLocation(region *string, request *s3.GetBucketLocationInput
// isRunningOnEC2 determines if we could be running on EC2.
// It is used to avoid a call to the metadata service to get the current region,
// because that call is slow if not running on EC2
func isRunningOnEC2() bool {
func isRunningOnEC2() (bool, error) {
if runtime.GOOS == "linux" {
// Approach based on https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
productUUID, err := ioutil.ReadFile("/sys/devices/virtual/dmi/id/product_uuid")
if err != nil {
klog.V(2).Infof("unable to read /sys/devices/virtual/dmi/id/product_uuid, assuming not running on EC2: %v", err)
return false
return false, err
}

s := strings.ToLower(strings.TrimSpace(string(productUUID)))
if strings.HasPrefix(s, "ec2") {
klog.V(2).Infof("product_uuid is %q, assuming running on EC2", s)
return true
return true, nil
}
klog.V(2).Infof("product_uuid is %q, assuming not running on EC2", s)
return false
return false, nil
}
klog.V(2).Infof("GOOS=%q, assuming not running on EC2", runtime.GOOS)
return false
return false, nil
}

// getRegionFromMetadata queries the metadata service for the current region, if running in EC2
Expand Down