Working ElasticIP associations on subnet. Delete and Create!

This commit is contained in:
Kris Childress 2016-10-28 14:37:55 -06:00
parent c1e8dbe9d6
commit a3dd1257ce
3 changed files with 103 additions and 72 deletions

View File

@ -125,7 +125,7 @@ routeTableAssociation/{{ $zone.Name }}.{{ ClusterName }}:
# subnet needs a NGW, lets create it # subnet needs a NGW, lets create it
# --------------------------------------------------------------- # ---------------------------------------------------------------
elasticIP/{{ $zone.Name }}.{{ ClusterName }}: elasticIP/{{ $zone.Name }}.{{ ClusterName }}:
associatedSubnetTag: subnet/{{ $zone.Name }}.{{ ClusterName }} associatedSubnet: subnet/{{ $zone.Name }}.{{ ClusterName }}
# --------------------------------------------------------------- # ---------------------------------------------------------------
# NGW # NGW

View File

@ -25,7 +25,6 @@ import (
"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"
"fmt" "fmt"
"os"
) )
//go:generate fitask -type=ElasticIP //go:generate fitask -type=ElasticIP
@ -42,7 +41,8 @@ type ElasticIP struct {
// Allow support for assicated subnets // Allow support for assicated subnets
// If you need another resource you must add it // If you need another resource you must add it
AssociatedSubnetTag *string // This is the string for the associated subnet AssociatedSubnet *Subnet
//AssociatedSubnetTagId *string //AssociatedSubnetTagId *string
//AssociatedElbTag *string //AssociatedElbTag *string
@ -67,23 +67,6 @@ func (e *ElasticIP) Find(context *fi.Context) (*ElasticIP, error) {
return e.find(context.Cloud.(awsup.AWSCloud)) return e.find(context.Cloud.(awsup.AWSCloud))
} }
func (e *ElasticIP) findAssociatedResourceId(cloud awsup.AWSCloud) (*string, error) {
// Validate Associated Tags
// We can trust that the values should be populated here for Associated*Tag
// Kris left off here..
// We need to actually get the resource ID for the subnet here..
// TODO Kris - lets code in support for other associations after the fact
//
fmt.Println("KRIS STOPPED WORKING HERE")
os.Exit(-1)
}
func (e *ElasticIP) find(cloud awsup.AWSCloud) (*ElasticIP, error) { func (e *ElasticIP) find(cloud awsup.AWSCloud) (*ElasticIP, error) {
//publicIP := e.PublicIP //publicIP := e.PublicIP
//allocationID := e.ID //allocationID := e.ID
@ -170,11 +153,8 @@ func (s *ElasticIP) CheckChanges(a, e, changes *ElasticIP) error {
func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) error { func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) error {
tagOnResourceID, err := e.findTagOnResourceID(t.Cloud) var publicIp *string
if err != nil { var eipId *string
return err
}
// If this is a new ElasticIP // If this is a new ElasticIP
if a == nil { if a == nil {
@ -190,17 +170,27 @@ func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) e
e.ID = response.AllocationId e.ID = response.AllocationId
e.PublicIP = response.PublicIp e.PublicIP = response.PublicIp
publicIp = e.PublicIP
eipId = response.AllocationId
}else {
publicIp = a.PublicIP
eipId = a.ID
}
// Tag the associated subnet
if e.AssociatedSubnet == nil {
return fmt.Errorf("Subnet not set")
} else if e.AssociatedSubnet.ID == nil {
return fmt.Errorf("Subnet ID not set")
}
tags := make(map[string]string)
tags["AssociatedElasticIp"] = *publicIp
tags["AssociatedElasticIpAllocationId"] = *eipId
err := t.AddAWSTags(*e.AssociatedSubnet.ID, tags)
if err != nil {
return fmt.Errorf("Unable to tag subnet %v", err)
} }
//
//if publicIP != nil && e.TagUsingKey != nil && tagOnResourceID != nil {
// tags := map[string]string{
// *e.TagUsingKey: *publicIP,
// }
// err := t.AddAWSTags(*tagOnResourceID, tags)
// if err != nil {
// return fmt.Errorf("error adding tags to resource for ElasticIP: %v", err)
// }
//}
return nil return nil
} }

View File

@ -792,12 +792,14 @@ func DeleteSubnet(cloud fi.Cloud, tracker *ResourceTracker) error {
} }
func ListSubnets(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) { func ListSubnets(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) {
c := cloud.(awsup.AWSCloud)
subnets, err := DescribeSubnets(cloud) subnets, err := DescribeSubnets(cloud)
if err != nil { if err != nil {
return nil, fmt.Errorf("error listing subnets: %v", err) return nil, fmt.Errorf("error listing subnets: %v", err)
} }
var trackers []*ResourceTracker var trackers []*ResourceTracker
elasticIPs := make(map[string]bool)
for _, subnet := range subnets { for _, subnet := range subnets {
tracker := &ResourceTracker{ tracker := &ResourceTracker{
@ -807,12 +809,51 @@ func ListSubnets(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error)
deleter: DeleteSubnet, deleter: DeleteSubnet,
} }
// Get tags and append with EIPs as needed
for _, tag := range subnet.Tags {
name := aws.StringValue(tag.Key)
ip := ""
if name == "AssociatedElasticIp" {
ip = aws.StringValue(tag.Value)
}
if ip != "" {
elasticIPs[ip] = true
}
}
var blocks []string var blocks []string
blocks = append(blocks, "vpc:" + aws.StringValue(subnet.VpcId)) blocks = append(blocks, "vpc:" + aws.StringValue(subnet.VpcId))
tracker.blocks = blocks tracker.blocks = blocks
trackers = append(trackers, tracker) trackers = append(trackers, tracker)
if len(elasticIPs) != 0 {
glog.V(2).Infof("Querying EC2 Elastic IPs")
request := &ec2.DescribeAddressesInput{}
response, err := c.EC2().DescribeAddresses(request)
if err != nil {
return nil, fmt.Errorf("error describing addresses: %v", err)
}
for _, address := range response.Addresses {
ip := aws.StringValue(address.PublicIp)
if !elasticIPs[ip] {
continue
}
tracker := &ResourceTracker{
Name: ip,
ID: aws.StringValue(address.AllocationId),
Type: "elastic-ip",
deleter: DeleteElasticIP,
}
trackers = append(trackers, tracker)
}
}
} }
return trackers, nil return trackers, nil