-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Query IMDS over IPv6 if no IPv4 interface address #2453
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package aws | ||
|
||
import ( | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"os" | ||
epresolver "sigs.k8s.io/aws-load-balancer-controller/pkg/aws/endpoints" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/metrics" | ||
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services" | ||
|
@@ -44,9 +47,28 @@ type Cloud interface { | |
|
||
// NewCloud constructs new Cloud implementation. | ||
func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer) (Cloud, error) { | ||
hasIPv4 := true | ||
addrs, err := net.InterfaceAddrs() | ||
if err == nil { | ||
hasIPv4 = false | ||
for _, addr := range addrs { | ||
str := addr.String() | ||
if !strings.HasPrefix(str, "127.") && !strings.Contains(str, ":") { | ||
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. i think this check is a bit hacky. 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. Why should users have to specify AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE at all? |
||
hasIPv4 = true | ||
break | ||
} | ||
} | ||
} | ||
|
||
endpointsResolver := epresolver.NewResolver(cfg.AWSEndpoints) | ||
metadataCFG := aws.NewConfig().WithEndpointResolver(endpointsResolver) | ||
metadataSess := session.Must(session.NewSession(metadataCFG)) | ||
opts := session.Options{} | ||
opts.Config.MergeIn(metadataCFG) | ||
if !hasIPv4 { | ||
opts.EC2IMDSEndpointMode = endpoints.EC2IMDSEndpointModeStateIPv6 | ||
} | ||
|
||
metadataSess := session.Must(session.NewSessionWithOptions(opts)) | ||
metadata := services.NewEC2Metadata(metadataSess) | ||
if len(cfg.VpcID) == 0 { | ||
vpcId, err := metadata.VpcID() | ||
|
@@ -72,7 +94,12 @@ func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer) (Cloud, | |
cfg.Region = region | ||
} | ||
awsCFG := aws.NewConfig().WithRegion(cfg.Region).WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint).WithMaxRetries(cfg.MaxRetries).WithEndpointResolver(endpointsResolver) | ||
sess := session.Must(session.NewSession(awsCFG)) | ||
opts = session.Options{} | ||
opts.Config.MergeIn(awsCFG) | ||
if !hasIPv4 { | ||
opts.EC2IMDSEndpointMode = endpoints.EC2IMDSEndpointModeStateIPv6 | ||
} | ||
sess := session.Must(session.NewSessionWithOptions(opts)) | ||
injectUserAgent(&sess.Handlers) | ||
|
||
if cfg.ThrottleConfig != nil { | ||
|
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.
in case of error, why not return right away?
Are there situations where the net.InterfaceAddrs() return error, but we still want to continue processing?
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.
The idea was to avoid any negative effects in existing cases. So if the process doesn't have permission to enumerate the interfaces, it would assume IPv4.