mirror of https://github.com/kubernetes/kops.git
Working ElasticIP associations on subnet. Delete and Create!
This commit is contained in:
parent
c1e8dbe9d6
commit
a3dd1257ce
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,19 +53,19 @@ type DeleteCluster struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResourceTracker struct {
|
type ResourceTracker struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
ID string
|
ID string
|
||||||
|
|
||||||
blocks []string
|
blocks []string
|
||||||
blocked []string
|
blocked []string
|
||||||
done bool
|
done bool
|
||||||
|
|
||||||
deleter func(cloud fi.Cloud, tracker *ResourceTracker) error
|
deleter func(cloud fi.Cloud, tracker *ResourceTracker) error
|
||||||
groupKey string
|
groupKey string
|
||||||
groupDeleter func(cloud fi.Cloud, trackers []*ResourceTracker) error
|
groupDeleter func(cloud fi.Cloud, trackers []*ResourceTracker) error
|
||||||
|
|
||||||
obj interface{}
|
obj interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type listFn func(fi.Cloud, string) ([]*ResourceTracker, error)
|
type listFn func(fi.Cloud, string) ([]*ResourceTracker, error)
|
||||||
|
|
@ -91,7 +91,7 @@ func buildEC2Filters(cloud fi.Cloud) []*ec2.Filter {
|
||||||
|
|
||||||
var filters []*ec2.Filter
|
var filters []*ec2.Filter
|
||||||
for k, v := range tags {
|
for k, v := range tags {
|
||||||
filter := awsup.NewEC2Filter("tag:"+k, v)
|
filter := awsup.NewEC2Filter("tag:" + k, v)
|
||||||
filters = append(filters, filter)
|
filters = append(filters, filter)
|
||||||
}
|
}
|
||||||
return filters
|
return filters
|
||||||
|
|
@ -131,7 +131,7 @@ func (c *DeleteCluster) ListResources() (map[string]*ResourceTracker, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, t := range trackers {
|
for _, t := range trackers {
|
||||||
resources[t.Type+":"+t.ID] = t
|
resources[t.Type + ":" + t.ID] = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,8 +152,8 @@ func (c *DeleteCluster) ListResources() (map[string]*ResourceTracker, error) {
|
||||||
if vpcID == "" || igwID == "" {
|
if vpcID == "" || igwID == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if resources["vpc:"+vpcID] != nil && resources["internet-gateway:"+igwID] == nil {
|
if resources["vpc:" + vpcID] != nil && resources["internet-gateway:" + igwID] == nil {
|
||||||
resources["internet-gateway:"+igwID] = &ResourceTracker{
|
resources["internet-gateway:" + igwID] = &ResourceTracker{
|
||||||
Name: FindName(igw.Tags),
|
Name: FindName(igw.Tags),
|
||||||
ID: igwID,
|
ID: igwID,
|
||||||
Type: "internet-gateway",
|
Type: "internet-gateway",
|
||||||
|
|
@ -192,7 +192,7 @@ func addUntaggedRouteTables(cloud awsup.AWSCloud, clusterName string, resources
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if resources["vpc:"+vpcID] == nil {
|
if resources["vpc:" + vpcID] == nil {
|
||||||
// Not deleting this VPC; ignore
|
// Not deleting this VPC; ignore
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -215,8 +215,8 @@ func addUntaggedRouteTables(cloud awsup.AWSCloud, clusterName string, resources
|
||||||
}
|
}
|
||||||
|
|
||||||
t := buildTrackerForRouteTable(rt)
|
t := buildTrackerForRouteTable(rt)
|
||||||
if resources[t.Type+":"+t.ID] == nil {
|
if resources[t.Type + ":" + t.ID] == nil {
|
||||||
resources[t.Type+":"+t.ID] = t
|
resources[t.Type + ":" + t.ID] = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -470,19 +470,19 @@ func ListInstances(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, erro
|
||||||
}
|
}
|
||||||
|
|
||||||
var blocks []string
|
var blocks []string
|
||||||
blocks = append(blocks, "vpc:"+aws.StringValue(instance.VpcId))
|
blocks = append(blocks, "vpc:" + aws.StringValue(instance.VpcId))
|
||||||
|
|
||||||
for _, volume := range instance.BlockDeviceMappings {
|
for _, volume := range instance.BlockDeviceMappings {
|
||||||
if volume.Ebs == nil {
|
if volume.Ebs == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
blocks = append(blocks, "volume:"+aws.StringValue(volume.Ebs.VolumeId))
|
blocks = append(blocks, "volume:" + aws.StringValue(volume.Ebs.VolumeId))
|
||||||
}
|
}
|
||||||
for _, sg := range instance.SecurityGroups {
|
for _, sg := range instance.SecurityGroups {
|
||||||
blocks = append(blocks, "security-group:"+aws.StringValue(sg.GroupId))
|
blocks = append(blocks, "security-group:" + aws.StringValue(sg.GroupId))
|
||||||
}
|
}
|
||||||
blocks = append(blocks, "subnet:"+aws.StringValue(instance.SubnetId))
|
blocks = append(blocks, "subnet:" + aws.StringValue(instance.SubnetId))
|
||||||
blocks = append(blocks, "vpc:"+aws.StringValue(instance.VpcId))
|
blocks = append(blocks, "vpc:" + aws.StringValue(instance.VpcId))
|
||||||
|
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
||||||
|
|
@ -567,7 +567,7 @@ func ListSecurityGroups(cloud fi.Cloud, clusterName string) ([]*ResourceTracker,
|
||||||
}
|
}
|
||||||
|
|
||||||
var blocks []string
|
var blocks []string
|
||||||
blocks = append(blocks, "vpc:"+aws.StringValue(sg.VpcId))
|
blocks = append(blocks, "vpc:" + aws.StringValue(sg.VpcId))
|
||||||
|
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
||||||
|
|
@ -731,8 +731,8 @@ func ListKeypairs(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error
|
||||||
|
|
||||||
glog.V(2).Infof("Listing EC2 Keypairs")
|
glog.V(2).Infof("Listing EC2 Keypairs")
|
||||||
request := &ec2.DescribeKeyPairsInput{
|
request := &ec2.DescribeKeyPairsInput{
|
||||||
// We need to match both the name and a prefix
|
// We need to match both the name and a prefix
|
||||||
//Filters: []*ec2.Filter{awsup.NewEC2Filter("key-name", keypairName)},
|
//Filters: []*ec2.Filter{awsup.NewEC2Filter("key-name", keypairName)},
|
||||||
}
|
}
|
||||||
response, err := c.EC2().DescribeKeyPairs(request)
|
response, err := c.EC2().DescribeKeyPairs(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -743,7 +743,7 @@ func ListKeypairs(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error
|
||||||
|
|
||||||
for _, keypair := range response.KeyPairs {
|
for _, keypair := range response.KeyPairs {
|
||||||
name := aws.StringValue(keypair.KeyName)
|
name := aws.StringValue(keypair.KeyName)
|
||||||
if name != keypairName && !strings.HasPrefix(name, keypairName+"-") {
|
if name != keypairName && !strings.HasPrefix(name, keypairName + "-") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tracker := &ResourceTracker{
|
tracker := &ResourceTracker{
|
||||||
|
|
@ -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
|
||||||
|
|
@ -909,10 +950,10 @@ func buildTrackerForRouteTable(rt *ec2.RouteTable) *ResourceTracker {
|
||||||
var blocks []string
|
var blocks []string
|
||||||
var blocked []string
|
var blocked []string
|
||||||
|
|
||||||
blocks = append(blocks, "vpc:"+aws.StringValue(rt.VpcId))
|
blocks = append(blocks, "vpc:" + aws.StringValue(rt.VpcId))
|
||||||
|
|
||||||
for _, a := range rt.Associations {
|
for _, a := range rt.Associations {
|
||||||
blocked = append(blocked, "subnet:"+aws.StringValue(a.SubnetId))
|
blocked = append(blocked, "subnet:" + aws.StringValue(a.SubnetId))
|
||||||
}
|
}
|
||||||
|
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
@ -1064,7 +1105,7 @@ func ListInternetGateways(cloud fi.Cloud, clusterName string) ([]*ResourceTracke
|
||||||
var blocks []string
|
var blocks []string
|
||||||
for _, a := range o.Attachments {
|
for _, a := range o.Attachments {
|
||||||
if aws.StringValue(a.VpcId) != "" {
|
if aws.StringValue(a.VpcId) != "" {
|
||||||
blocks = append(blocks, "vpc:"+aws.StringValue(a.VpcId))
|
blocks = append(blocks, "vpc:" + aws.StringValue(a.VpcId))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
@ -1167,7 +1208,7 @@ func ListVPCs(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var blocks []string
|
var blocks []string
|
||||||
blocks = append(blocks, "dhcp-options:"+aws.StringValue(v.DhcpOptionsId))
|
blocks = append(blocks, "dhcp-options:" + aws.StringValue(v.DhcpOptionsId))
|
||||||
|
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
||||||
|
|
@ -1223,9 +1264,9 @@ func ListAutoScalingGroups(cloud fi.Cloud, clusterName string) ([]*ResourceTrack
|
||||||
if subnet == "" {
|
if subnet == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
blocks = append(blocks, "subnet:"+subnet)
|
blocks = append(blocks, "subnet:" + subnet)
|
||||||
}
|
}
|
||||||
blocks = append(blocks, TypeAutoscalingLaunchConfig+":"+aws.StringValue(asg.LaunchConfigurationName))
|
blocks = append(blocks, TypeAutoscalingLaunchConfig + ":" + aws.StringValue(asg.LaunchConfigurationName))
|
||||||
|
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
||||||
|
|
@ -1387,12 +1428,12 @@ func ListELBs(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) {
|
||||||
|
|
||||||
var blocks []string
|
var blocks []string
|
||||||
for _, sg := range elb.SecurityGroups {
|
for _, sg := range elb.SecurityGroups {
|
||||||
blocks = append(blocks, "security-group:"+aws.StringValue(sg))
|
blocks = append(blocks, "security-group:" + aws.StringValue(sg))
|
||||||
}
|
}
|
||||||
for _, s := range elb.Subnets {
|
for _, s := range elb.Subnets {
|
||||||
blocks = append(blocks, "subnet:"+aws.StringValue(s))
|
blocks = append(blocks, "subnet:" + aws.StringValue(s))
|
||||||
}
|
}
|
||||||
blocks = append(blocks, "vpc:"+aws.StringValue(elb.VPCId))
|
blocks = append(blocks, "vpc:" + aws.StringValue(elb.VPCId))
|
||||||
|
|
||||||
tracker.blocks = blocks
|
tracker.blocks = blocks
|
||||||
|
|
||||||
|
|
@ -1648,8 +1689,8 @@ func ListIAMRoles(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error
|
||||||
c := cloud.(awsup.AWSCloud)
|
c := cloud.(awsup.AWSCloud)
|
||||||
|
|
||||||
remove := make(map[string]bool)
|
remove := make(map[string]bool)
|
||||||
remove["masters."+clusterName] = true
|
remove["masters." + clusterName] = true
|
||||||
remove["nodes."+clusterName] = true
|
remove["nodes." + clusterName] = true
|
||||||
|
|
||||||
var roles []*iam.Role
|
var roles []*iam.Role
|
||||||
// Find roles matching remove map
|
// Find roles matching remove map
|
||||||
|
|
@ -1725,8 +1766,8 @@ func ListIAMInstanceProfiles(cloud fi.Cloud, clusterName string) ([]*ResourceTra
|
||||||
c := cloud.(awsup.AWSCloud)
|
c := cloud.(awsup.AWSCloud)
|
||||||
|
|
||||||
remove := make(map[string]bool)
|
remove := make(map[string]bool)
|
||||||
remove["masters."+clusterName] = true
|
remove["masters." + clusterName] = true
|
||||||
remove["nodes."+clusterName] = true
|
remove["nodes." + clusterName] = true
|
||||||
|
|
||||||
var profiles []*iam.InstanceProfile
|
var profiles []*iam.InstanceProfile
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue