mirror of https://github.com/kubernetes/kops.git
Merge pull request #1947 from kris-nova/cf-delete
Delete CloudFormation stack on kops delete cluster
This commit is contained in:
commit
9e8dd6cfe3
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||||
|
"github.com/aws/aws-sdk-go/service/cloudformation"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
||||||
"github.com/aws/aws-sdk-go/service/elb"
|
"github.com/aws/aws-sdk-go/service/elb"
|
||||||
|
|
@ -67,6 +68,7 @@ type AWSCloud interface {
|
||||||
|
|
||||||
Region() string
|
Region() string
|
||||||
|
|
||||||
|
CloudFormation() *cloudformation.CloudFormation
|
||||||
EC2() ec2iface.EC2API
|
EC2() ec2iface.EC2API
|
||||||
IAM() *iam.IAM
|
IAM() *iam.IAM
|
||||||
ELB() *elb.ELB
|
ELB() *elb.ELB
|
||||||
|
|
@ -111,6 +113,7 @@ type AWSCloud interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type awsCloudImplementation struct {
|
type awsCloudImplementation struct {
|
||||||
|
cf *cloudformation.CloudFormation
|
||||||
ec2 *ec2.EC2
|
ec2 *ec2.EC2
|
||||||
iam *iam.IAM
|
iam *iam.IAM
|
||||||
elb *elb.ELB
|
elb *elb.ELB
|
||||||
|
|
@ -150,6 +153,9 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
|
||||||
|
|
||||||
requestLogger := newRequestLogger(2)
|
requestLogger := newRequestLogger(2)
|
||||||
|
|
||||||
|
c.cf = cloudformation.New(session.New(), config)
|
||||||
|
c.cf.Handlers.Send.PushFront(requestLogger)
|
||||||
|
|
||||||
c.ec2 = ec2.New(session.New(), config)
|
c.ec2 = ec2.New(session.New(), config)
|
||||||
c.ec2.Handlers.Send.PushFront(requestLogger)
|
c.ec2.Handlers.Send.PushFront(requestLogger)
|
||||||
|
|
||||||
|
|
@ -665,6 +671,10 @@ func (c *awsCloudImplementation) DNS() (dnsprovider.Interface, error) {
|
||||||
return provider, nil
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *awsCloudImplementation) CloudFormation() *cloudformation.CloudFormation {
|
||||||
|
return c.cf
|
||||||
|
}
|
||||||
|
|
||||||
func (c *awsCloudImplementation) EC2() ec2iface.EC2API {
|
func (c *awsCloudImplementation) EC2() ec2iface.EC2API {
|
||||||
return c.ec2
|
return c.ec2
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||||
|
"github.com/aws/aws-sdk-go/service/cloudformation"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
||||||
"github.com/aws/aws-sdk-go/service/elb"
|
"github.com/aws/aws-sdk-go/service/elb"
|
||||||
|
|
@ -65,8 +66,9 @@ func BuildMockAWSCloud(region string, zoneLetters string) *MockAWSCloud {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MockCloud struct {
|
type MockCloud struct {
|
||||||
MockEC2 ec2iface.EC2API
|
MockCloudFormation *cloudformation.CloudFormation
|
||||||
MockRoute53 route53iface.Route53API
|
MockEC2 ec2iface.EC2API
|
||||||
|
MockRoute53 route53iface.Route53API
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MockCloud) ProviderID() fi.CloudProviderID {
|
func (c *MockCloud) ProviderID() fi.CloudProviderID {
|
||||||
|
|
@ -149,6 +151,13 @@ func (c *MockAWSCloud) WithTags(tags map[string]string) AWSCloud {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *MockAWSCloud) CloudFormation() *cloudformation.CloudFormation {
|
||||||
|
if c.MockEC2 == nil {
|
||||||
|
glog.Fatalf("MockAWSCloud MockCloudFormation not set")
|
||||||
|
}
|
||||||
|
return c.MockCloudFormation
|
||||||
|
}
|
||||||
|
|
||||||
func (c *MockAWSCloud) EC2() ec2iface.EC2API {
|
func (c *MockAWSCloud) EC2() ec2iface.EC2API {
|
||||||
if c.MockEC2 == nil {
|
if c.MockEC2 == nil {
|
||||||
glog.Fatalf("MockAWSCloud MockEC2 not set")
|
glog.Fatalf("MockAWSCloud MockEC2 not set")
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,10 @@ func (c *AwsCluster) ListResources() (map[string]*ResourceTracker, error) {
|
||||||
// These are the functions that are used for looking up
|
// These are the functions that are used for looking up
|
||||||
// cluster resources by their tags.
|
// cluster resources by their tags.
|
||||||
listFunctions := []listFn{
|
listFunctions := []listFn{
|
||||||
|
|
||||||
|
// CloudFormation
|
||||||
|
ListCloudFormationStacks,
|
||||||
|
|
||||||
// EC2
|
// EC2
|
||||||
ListInstances,
|
ListInstances,
|
||||||
ListKeypairs,
|
ListKeypairs,
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||||
|
"github.com/aws/aws-sdk-go/service/cloudformation"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
"github.com/aws/aws-sdk-go/service/elb"
|
"github.com/aws/aws-sdk-go/service/elb"
|
||||||
"github.com/aws/aws-sdk-go/service/iam"
|
"github.com/aws/aws-sdk-go/service/iam"
|
||||||
|
|
@ -346,6 +347,53 @@ func DeleteInstance(cloud fi.Cloud, t *ResourceTracker) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteCloudFormationStack(cloud fi.Cloud, t *ResourceTracker) error {
|
||||||
|
c := cloud.(awsup.AWSCloud)
|
||||||
|
|
||||||
|
id := t.ID
|
||||||
|
glog.V(2).Infof("deleting CloudFormation stack %q %q", t.Name, id)
|
||||||
|
|
||||||
|
request := &cloudformation.DeleteStackInput{}
|
||||||
|
request.StackName = &t.Name
|
||||||
|
|
||||||
|
_, err := c.CloudFormation().DeleteStack(request)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error deleting CloudFormation stack %q: %v", id, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DumpCloudFormationStack(r *ResourceTracker) (interface{}, error) {
|
||||||
|
data := make(map[string]interface{})
|
||||||
|
data["id"] = r.ID
|
||||||
|
data["type"] = r.Type
|
||||||
|
data["raw"] = r.obj
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListCloudFormationStacks(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) {
|
||||||
|
var trackers []*ResourceTracker
|
||||||
|
request := &cloudformation.ListStacksInput{}
|
||||||
|
c := cloud.(awsup.AWSCloud)
|
||||||
|
response, err := c.CloudFormation().ListStacks(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Unable to list CloudFormation stacks: %v", err)
|
||||||
|
}
|
||||||
|
for _, stack := range response.StackSummaries {
|
||||||
|
tracker := &ResourceTracker{
|
||||||
|
Name: *stack.StackName,
|
||||||
|
ID: *stack.StackId,
|
||||||
|
Type: "cloud-formation",
|
||||||
|
deleter: DeleteCloudFormationStack,
|
||||||
|
Dumper: DumpCloudFormationStack,
|
||||||
|
obj: stack,
|
||||||
|
}
|
||||||
|
trackers = append(trackers, tracker)
|
||||||
|
}
|
||||||
|
|
||||||
|
return trackers, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ListInstances(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) {
|
func ListInstances(cloud fi.Cloud, clusterName string) ([]*ResourceTracker, error) {
|
||||||
c := cloud.(awsup.AWSCloud)
|
c := cloud.(awsup.AWSCloud)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue