-
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
Fetch VPC ID from runtime using VPC tags provided via controller flags #3656
Changes from 6 commits
9d19525
69a5d42
8ae3e3c
3a0ebce
e20e081
443d102
22b2e6b
7b3f80d
663315e
8a01478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"os" | ||
|
@@ -10,6 +11,7 @@ import ( | |
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
amerrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
@@ -112,14 +114,11 @@ func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer) (Cloud, | |
|
||
ec2Service := services.NewEC2(sess) | ||
|
||
if len(cfg.VpcID) == 0 { | ||
vpcID, err := inferVPCID(metadata, ec2Service) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to introspect vpcID from EC2Metadata or Node name, specify --aws-vpc-id instead if EC2Metadata is unavailable") | ||
} | ||
cfg.VpcID = vpcID | ||
vpcID, err := getVpcID(cfg, ec2Service, metadata) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get VPC ID") | ||
} | ||
|
||
cfg.VpcID = vpcID | ||
return &defaultCloud{ | ||
cfg: cfg, | ||
ec2: ec2Service, | ||
|
@@ -132,6 +131,21 @@ func NewCloud(cfg CloudConfig, metricsRegisterer prometheus.Registerer) (Cloud, | |
}, nil | ||
} | ||
|
||
func getVpcID(cfg CloudConfig, ec2Service services.EC2, metadata services.EC2Metadata) (string, error) { | ||
|
||
logger := logr.Logger{} | ||
if cfg.VpcID != "" { | ||
logger.V(1).Info("vpcid is specified using flag --aws-vpc-id, controller will use the value %s", cfg.VpcID) | ||
return cfg.VpcID, nil | ||
} | ||
|
||
if cfg.VpcTags != nil { | ||
return inferVPCIDFromTags(ec2Service, cfg.VpcNameTagKey, cfg.VpcTags[cfg.VpcNameTagKey]) | ||
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. @jeswinkoshyninan I don't really get this part: why does this allow to pass a map with an arbitrary number of items for 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. Related to #3889 |
||
} | ||
|
||
return inferVPCID(metadata, ec2Service) | ||
} | ||
|
||
func inferVPCID(metadata services.EC2Metadata, ec2Service services.EC2) (string, error) { | ||
var errList []error | ||
vpcId, err := metadata.VpcID() | ||
|
@@ -168,6 +182,28 @@ func inferVPCID(metadata services.EC2Metadata, ec2Service services.EC2) (string, | |
return "", amerrors.NewAggregate(errList) | ||
} | ||
|
||
func inferVPCIDFromTags(ec2Service services.EC2, VpcNameTagKey string, VpcNameTagValue string) (string, error) { | ||
vpcs, err := ec2Service.DescribeVPCsAsList(context.Background(), &ec2.DescribeVpcsInput{ | ||
Filters: []*ec2.Filter{ | ||
{ | ||
Name: aws.String("tag:" + VpcNameTagKey), | ||
Values: []*string{aws.String(VpcNameTagValue)}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to fetch VPC ID with tag: %w", err) | ||
} | ||
if len(vpcs) == 0 { | ||
return "", fmt.Errorf("no VPC exists with tag: %w", err) | ||
} | ||
if len(vpcs) > 1 { | ||
return "", fmt.Errorf("multiple VPCs exists with tag: %w", err) | ||
} | ||
|
||
return *vpcs[0].VpcId, nil | ||
} | ||
|
||
var _ Cloud = &defaultCloud{} | ||
|
||
type defaultCloud struct { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
nit: reword a bit
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.
refactor the sentence. thanks