Skip to content

Commit

Permalink
More work on the network and EIP things
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Childress committed Oct 28, 2016
1 parent 70634d2 commit b1ac2b2
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ routeTableAssociation/{{ $zone.Name }}.{{ ClusterName }}:
# subnet needs a NGW, lets create it
# ---------------------------------------------------------------
elasticIP/{{ $zone.Name }}.{{ ClusterName }}:
name: {{ $zone.Name }}.{{ ClusterName }}
tagOnResource: vpc/{{ ClusterName }}
tagUsingKey: kubernetes.io/master-nat-gateway
associatedSubnetTag: subnet/{{ $zone.Name }}.{{ ClusterName }}

# ---------------------------------------------------------------
# NGW
Expand Down
221 changes: 115 additions & 106 deletions upup/pkg/fi/cloudup/awstasks/elastic_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,37 @@ limitations under the License.
package awstasks

import (
"fmt"

//"fmt"
//
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"fmt"
"os"
)

//go:generate fitask -type=ElasticIP

// Elastic IP
// Representation the EIP AWS task
type ElasticIP struct {
Name *string
Name *string
ID *string
PublicIP *string



ID *string
PublicIP *string
// Allow support for assicated subnets
// If you need another resource you must add it

AssociatedSubnetTag *string // This is the string for the associated subnet
//AssociatedSubnetTagId *string

//AssociatedElbTag *string
//AssociatedElbTagId *string

// Because ElasticIPs don't supporting tagging (sadly), we instead tag on
// a different resource
TagUsingKey *string
TagOnResource fi.Task
}

var _ fi.HasAddress = &ElasticIP{}
Expand All @@ -56,97 +67,99 @@ func (e *ElasticIP) Find(context *fi.Context) (*ElasticIP, error) {
return e.find(context.Cloud.(awsup.AWSCloud))
}

func (e *ElasticIP) findTagOnResourceID(cloud awsup.AWSCloud) (*string, error) {
if e.TagOnResource == nil {
return nil, nil
}

var tagOnResource TaggableResource
var ok bool
if tagOnResource, ok = e.TagOnResource.(TaggableResource); !ok {
return nil, fmt.Errorf("TagOnResource must implement TaggableResource (type is %T)", e.TagOnResource)
}

id, err := tagOnResource.FindResourceID(cloud)
if err != nil {
return nil, fmt.Errorf("error trying to find id of TagOnResource: %v", err)
}
return id, err
}

func (e *ElasticIP) find(cloud awsup.AWSCloud) (*ElasticIP, error) {
publicIP := e.PublicIP
allocationID := e.ID

tagOnResourceID, err := e.findTagOnResourceID(cloud)
if err != nil {
return nil, err
}
// Find via tag on foreign resource
if allocationID == nil && publicIP == nil && e.TagUsingKey != nil && tagOnResourceID != nil {
var filters []*ec2.Filter
filters = append(filters, awsup.NewEC2Filter("key", *e.TagUsingKey))
filters = append(filters, awsup.NewEC2Filter("resource-id", *tagOnResourceID))

request := &ec2.DescribeTagsInput{
Filters: filters,
}

response, err := cloud.EC2().DescribeTags(request)
if err != nil {
return nil, fmt.Errorf("error listing tags: %v", err)
}

if response == nil || len(response.Tags) == 0 {
return nil, nil
}
func (e *ElasticIP) findAssociatedResourceId(cloud awsup.AWSCloud) (*string, error) {

if len(response.Tags) != 1 {
return nil, fmt.Errorf("found multiple tags for: %v", e)
}
t := response.Tags[0]
publicIP = t.Value
glog.V(2).Infof("Found public IP via tag: %v", *publicIP)
}
// Validate Associated Tags
// We can trust that the values should be populated here for Associated*Tag

if publicIP != nil || allocationID != nil {
request := &ec2.DescribeAddressesInput{}
if allocationID != nil {
request.AllocationIds = []*string{allocationID}
} else if publicIP != nil {
request.Filters = []*ec2.Filter{awsup.NewEC2Filter("public-ip", *publicIP)}
}
// Kris left off here..

response, err := cloud.EC2().DescribeAddresses(request)
if err != nil {
return nil, fmt.Errorf("error listing ElasticIPs: %v", err)
}
// We need to actually get the resource ID for the subnet here..
// TODO Kris - lets code in support for other associations after the fact

if response == nil || len(response.Addresses) == 0 {
return nil, nil
}
//

if len(response.Addresses) != 1 {
return nil, fmt.Errorf("found multiple ElasticIPs for: %v", e)
}
a := response.Addresses[0]
actual := &ElasticIP{
ID: a.AllocationId,
PublicIP: a.PublicIp,
}
fmt.Println("KRIS STOPPED WORKING HERE")
os.Exit(-1)

// These two are weird properties; we copy them so they don't come up as changes
actual.TagUsingKey = e.TagUsingKey
actual.TagOnResource = e.TagOnResource

e.ID = actual.ID

return actual, nil
}
}

func (e *ElasticIP) find(cloud awsup.AWSCloud) (*ElasticIP, error) {
//publicIP := e.PublicIP
//allocationID := e.ID
//
//tagOnResourceID, err := e.findTagOnResourceID(cloud)
//if err != nil {
// return nil, err
//}
//// Find via tag on foreign resource
//if allocationID == nil && publicIP == nil && e.TagUsingKey != nil && tagOnResourceID != nil {
// var filters []*ec2.Filter
// filters = append(filters, awsup.NewEC2Filter("key", *e.TagUsingKey))
// filters = append(filters, awsup.NewEC2Filter("resource-id", *tagOnResourceID))
//
// request := &ec2.DescribeTagsInput{
// Filters: filters,
// }
//
// response, err := cloud.EC2().DescribeTags(request)
// if err != nil {
// return nil, fmt.Errorf("error listing tags: %v", err)
// }
//
// if response == nil || len(response.Tags) == 0 {
// return nil, nil
// }
//
// if len(response.Tags) != 1 {
// return nil, fmt.Errorf("found multiple tags for: %v", e)
// }
// t := response.Tags[0]
// publicIP = t.Value
// glog.V(2).Infof("Found public IP via tag: %v", *publicIP)
//}
//
//if publicIP != nil || allocationID != nil {
// request := &ec2.DescribeAddressesInput{}
// if allocationID != nil {
// request.AllocationIds = []*string{allocationID}
// } else if publicIP != nil {
// request.Filters = []*ec2.Filter{awsup.NewEC2Filter("public-ip", *publicIP)}
// }
//
// response, err := cloud.EC2().DescribeAddresses(request)
// if err != nil {
// return nil, fmt.Errorf("error listing ElasticIPs: %v", err)
// }
//
// if response == nil || len(response.Addresses) == 0 {
// return nil, nil
// }
//
// if len(response.Addresses) != 1 {
// return nil, fmt.Errorf("found multiple ElasticIPs for: %v", e)
// }
// a := response.Addresses[0]
// actual := &ElasticIP{
// ID: a.AllocationId,
// PublicIP: a.PublicIp,
// }
//
// // These two are weird properties; we copy them so they don't come up as changes
// actual.TagUsingKey = e.TagUsingKey
// actual.TagOnResource = e.TagOnResource
//
// e.ID = actual.ID
//
// return actual, nil
//}
return nil, nil
}

// The Run() function is called to execute this task.
// This is the main entry point of the task, and will actually
// connect our internal resource representation to an actual
// resource in AWS
func (e *ElasticIP) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
Expand All @@ -156,17 +169,15 @@ func (s *ElasticIP) CheckChanges(a, e, changes *ElasticIP) error {
}

func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) error {
var publicIP *string

tagOnResourceID, err := e.findTagOnResourceID(t.Cloud)
if err != nil {
return err
}


// If this is a new ElasticIP
if a == nil {
if tagOnResourceID == nil || e.TagUsingKey == nil {
return fmt.Errorf("cannot create ElasticIP without TagOnResource being set (would leak)")
}
glog.V(2).Infof("Creating ElasticIP for VPC")

request := &ec2.AllocateAddressInput{}
Expand All @@ -179,19 +190,17 @@ func (_ *ElasticIP) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ElasticIP) e

e.ID = response.AllocationId
e.PublicIP = response.PublicIp
publicIP = response.PublicIp
} else {
publicIP = a.PublicIP
}
//
//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)
// }
//}

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
}

0 comments on commit b1ac2b2

Please sign in to comment.