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

Query IMDS over IPv6 if no IPv4 interface address #2453

Merged
merged 1 commit into from
Oct 1, 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
33 changes: 30 additions & 3 deletions pkg/aws/cloud.go
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"
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

hasIPv4 = false
for _, addr := range addrs {
str := addr.String()
if !strings.HasPrefix(str, "127.") && !strings.Contains(str, ":") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this check is a bit hacky.
maybe just upgrade the sdk and let users specify AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE env variable via helm chart?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
Expand All @@ -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 {
Expand Down