Skip to content

Commit

Permalink
dns: don't use IMDS region resolver when it previously failed
Browse files Browse the repository at this point in the history
This should allow use to IMDS on EC2, but not when not running on EC2,
for example when running `kops update cluster`.
  • Loading branch information
justinsb authored and rifelpet committed Aug 26, 2024
1 parent 2b6ce45 commit 86fa71a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dnsprovider/pkg/dnsprovider/providers/aws/route53/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func newRoute53() (*Interface, error) {
imdsRegionResp, err := imdsClient.GetRegion(ctx, &imds.GetRegionInput{})
if err != nil {
klog.V(4).Infof("Unable to discover region by IMDS, using SDK defaults: %s", err)
// Don't use imdsClient if it's erroring (we're probably not running on EC2 here, e.g. kops update)
imdsClient = nil
} else {
region = imdsRegionResp.Region
}
Expand All @@ -83,7 +85,7 @@ func newRoute53() (*Interface, error) {
return nil, fmt.Errorf("failed to load default aws config for STS client: %w", err)
}

cfg, err := awsconfig.LoadDefaultConfig(ctx,
awsOptions := []func(*awsconfig.LoadOptions) error{
awsconfig.WithClientLogMode(aws.LogRetries),
awslog.WithAWSLogger(),
awsconfig.WithRetryer(func() aws.Retryer {
Expand All @@ -93,11 +95,15 @@ func newRoute53() (*Interface, error) {
// Ensure the STS client has a region configured, if discovered by IMDS
aro.Client = sts.NewFromConfig(stsCfg)
}),
awsconfig.WithEC2IMDSRegion(func(o *awsconfig.UseEC2IMDSRegion) {
}

if imdsClient != nil {
awsOptions = append(awsOptions, awsconfig.WithEC2IMDSRegion(func(o *awsconfig.UseEC2IMDSRegion) {
o.Client = imdsClient
}),
)
}))
}

cfg, err := awsconfig.LoadDefaultConfig(ctx, awsOptions...)
if err != nil {
return nil, fmt.Errorf("failed to load default aws config: %w", err)
}
Expand Down

0 comments on commit 86fa71a

Please sign in to comment.