Merge pull request #1072 from justinsb/nat-gateway-detection

Fixes to existing NAT gateway detection
This commit is contained in:
Chris Love 2016-12-06 20:05:38 -07:00 committed by GitHub
commit be5fd71852
2 changed files with 24 additions and 11 deletions

View File

@ -42,6 +42,12 @@ type ElasticIP struct {
Subnet *Subnet
}
var _ fi.CompareWithID = &ElasticIP{}
func (e *ElasticIP) CompareWithID() *string {
return e.ID
}
var _ fi.HasAddress = &ElasticIP{}
func (e *ElasticIP) FindAddress(context *fi.Context) (*string, error) {

View File

@ -21,9 +21,7 @@ import (
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
//"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"fmt"
"github.com/aws/aws-sdk-go/aws"
)
@ -43,12 +41,11 @@ func (e *NatGateway) CompareWithID() *string {
func (e *NatGateway) Find(c *fi.Context) (*NatGateway, error) {
cloud := c.Cloud.(awsup.AWSCloud)
ID := e.ID
ElasticIp := e.ElasticIp
Subnet := e.Subnet
id := e.ID
// Find via tag on foreign resource
if ID == nil && ElasticIp == nil && Subnet != nil {
if id == nil && e.Subnet != nil {
var filters []*ec2.Filter
filters = append(filters, awsup.NewEC2Filter("key", "AssociatedNatgateway"))
filters = append(filters, awsup.NewEC2Filter("resource-id", *e.Subnet.ID))
@ -70,13 +67,14 @@ func (e *NatGateway) Find(c *fi.Context) (*NatGateway, error) {
return nil, fmt.Errorf("found multiple tags for: %v", e)
}
t := response.Tags[0]
ID = t.Value
glog.V(2).Infof("Found nat gateway via tag: %v", *ID)
id = t.Value
glog.V(2).Infof("Found nat gateway via tag: %v", *id)
}
if ID != nil {
if id != nil {
request := &ec2.DescribeNatGatewaysInput{}
request.NatGatewayIds = []*string{ID}
request.NatGatewayIds = []*string{id}
response, err := cloud.EC2().DescribeNatGateways(request)
if err != nil {
return nil, fmt.Errorf("error listing NAT Gateways: %v", err)
@ -94,7 +92,15 @@ func (e *NatGateway) Find(c *fi.Context) (*NatGateway, error) {
ID: a.NatGatewayId,
}
actual.Subnet = e.Subnet
actual.ElasticIp = e.ElasticIp
if len(a.NatGatewayAddresses) == 0 {
// Not sure if this ever happens
actual.ElasticIp = nil
} else if len(a.NatGatewayAddresses) == 1 {
actual.ElasticIp = &ElasticIP{ID: a.NatGatewayAddresses[0].AllocationId}
} else {
return nil, fmt.Errorf("found multiple elastic IPs attached to NatGateway %q", aws.StringValue(a.NatGatewayId))
}
e.ID = actual.ID
return actual, nil
}
@ -181,6 +187,7 @@ func (_ *NatGateway) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *NatGateway)
} else if e.Subnet.ID == nil {
return fmt.Errorf("Subnet ID not set")
}
tags := make(map[string]string)
tags["AssociatedNatgateway"] = *id
err := t.AddAWSTags(*e.Subnet.ID, tags)