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

add sts regional endpoint logic #313

Merged
merged 4 commits into from
Mar 3, 2022
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
43 changes: 32 additions & 11 deletions pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,8 +1209,18 @@ func init() {
return nil, fmt.Errorf("unable to validate custom endpoint overrides: %v", err)
}

metadata, err := newAWSSDKProvider(nil, cfg).Metadata()
if err != nil {
return nil, fmt.Errorf("error creating AWS metadata client: %q", err)
}

regionName, _, err := getRegionFromMetadata(*cfg, metadata)
if err != nil {
return nil, err
}

sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{},
Config: *aws.NewConfig().WithRegion(regionName).WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint),
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
Expand Down Expand Up @@ -1302,16 +1312,7 @@ func newAWSCloud(cfg CloudConfig, awsServices Services) (*Cloud, error) {
return nil, fmt.Errorf("error creating AWS metadata client: %q", err)
}

err = updateConfigZone(&cfg, metadata)
if err != nil {
return nil, fmt.Errorf("unable to determine AWS zone from cloud provider config or EC2 instance metadata: %v", err)
}

zone := cfg.Global.Zone
if len(zone) <= 1 {
return nil, fmt.Errorf("invalid AWS zone in config file: %s", zone)
}
regionName, err := azToRegion(zone)
regionName, zone, err := getRegionFromMetadata(cfg, metadata)
prasita123 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -5130,3 +5131,23 @@ func (c *Cloud) describeNetworkInterfaces(nodeName string) (*ec2.NetworkInterfac
}
return eni.NetworkInterfaces[0], nil
}

func getRegionFromMetadata(cfg CloudConfig, metadata EC2Metadata) (string, string, error) {
klog.Infof("Get AWS region from metadata client")
err := updateConfigZone(&cfg, metadata)
if err != nil {
return "", "", fmt.Errorf("unable to determine AWS zone from cloud provider config or EC2 instance metadata: %v", err)
}

zone := cfg.Global.Zone
if len(zone) <= 1 {
return "", "", fmt.Errorf("invalid AWS zone in config file: %s", zone)
}

regionName, err := azToRegion(zone)
if err != nil {
return "", "", err
}

return regionName, zone, nil
}