mirror of https://github.com/kubernetes/kops.git
Fixes to existing NAT gateway detection
This commit is contained in:
parent
03ad0cf4cd
commit
68250e4b9a
|
|
@ -42,6 +42,12 @@ type ElasticIP struct {
|
||||||
Subnet *Subnet
|
Subnet *Subnet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ fi.CompareWithID = &ElasticIP{}
|
||||||
|
|
||||||
|
func (e *ElasticIP) CompareWithID() *string {
|
||||||
|
return e.ID
|
||||||
|
}
|
||||||
|
|
||||||
var _ fi.HasAddress = &ElasticIP{}
|
var _ fi.HasAddress = &ElasticIP{}
|
||||||
|
|
||||||
func (e *ElasticIP) FindAddress(context *fi.Context) (*string, error) {
|
func (e *ElasticIP) FindAddress(context *fi.Context) (*string, error) {
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,7 @@ import (
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"k8s.io/kops/upup/pkg/fi"
|
"k8s.io/kops/upup/pkg/fi"
|
||||||
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
|
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
|
||||||
//"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"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) {
|
func (e *NatGateway) Find(c *fi.Context) (*NatGateway, error) {
|
||||||
cloud := c.Cloud.(awsup.AWSCloud)
|
cloud := c.Cloud.(awsup.AWSCloud)
|
||||||
ID := e.ID
|
|
||||||
ElasticIp := e.ElasticIp
|
id := e.ID
|
||||||
Subnet := e.Subnet
|
|
||||||
|
|
||||||
// Find via tag on foreign resource
|
// Find via tag on foreign resource
|
||||||
if ID == nil && ElasticIp == nil && Subnet != nil {
|
if id == nil && e.Subnet != nil {
|
||||||
var filters []*ec2.Filter
|
var filters []*ec2.Filter
|
||||||
filters = append(filters, awsup.NewEC2Filter("key", "AssociatedNatgateway"))
|
filters = append(filters, awsup.NewEC2Filter("key", "AssociatedNatgateway"))
|
||||||
filters = append(filters, awsup.NewEC2Filter("resource-id", *e.Subnet.ID))
|
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)
|
return nil, fmt.Errorf("found multiple tags for: %v", e)
|
||||||
}
|
}
|
||||||
t := response.Tags[0]
|
t := response.Tags[0]
|
||||||
ID = t.Value
|
id = t.Value
|
||||||
glog.V(2).Infof("Found nat gateway via tag: %v", *ID)
|
glog.V(2).Infof("Found nat gateway via tag: %v", *id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ID != nil {
|
|
||||||
|
if id != nil {
|
||||||
request := &ec2.DescribeNatGatewaysInput{}
|
request := &ec2.DescribeNatGatewaysInput{}
|
||||||
request.NatGatewayIds = []*string{ID}
|
request.NatGatewayIds = []*string{id}
|
||||||
response, err := cloud.EC2().DescribeNatGateways(request)
|
response, err := cloud.EC2().DescribeNatGateways(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error listing NAT Gateways: %v", err)
|
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,
|
ID: a.NatGatewayId,
|
||||||
}
|
}
|
||||||
actual.Subnet = e.Subnet
|
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
|
e.ID = actual.ID
|
||||||
return actual, nil
|
return actual, nil
|
||||||
}
|
}
|
||||||
|
|
@ -181,6 +187,7 @@ func (_ *NatGateway) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *NatGateway)
|
||||||
} else if e.Subnet.ID == nil {
|
} else if e.Subnet.ID == nil {
|
||||||
return fmt.Errorf("Subnet ID not set")
|
return fmt.Errorf("Subnet ID not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
tags := make(map[string]string)
|
tags := make(map[string]string)
|
||||||
tags["AssociatedNatgateway"] = *id
|
tags["AssociatedNatgateway"] = *id
|
||||||
err := t.AddAWSTags(*e.Subnet.ID, tags)
|
err := t.AddAWSTags(*e.Subnet.ID, tags)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue