-
Notifications
You must be signed in to change notification settings - Fork 22
/
delete.go
136 lines (102 loc) · 4.52 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package tccp
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
infrastructurev1alpha3 "github.com/giantswarm/apiextensions/v6/pkg/apis/infrastructure/v1alpha3"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/finalizerskeptcontext"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/aws-operator/v14/pkg/label"
"github.com/giantswarm/aws-operator/v14/service/controller/controllercontext"
"github.com/giantswarm/aws-operator/v14/service/controller/key"
)
func (r *Resource) EnsureDeleted(ctx context.Context, obj interface{}) error {
cr, err := key.ToCluster(ctx, obj)
if err != nil {
return microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
mdList := &infrastructurev1alpha3.AWSMachineDeploymentList{}
{
r.logger.Debugf(ctx, "finding machine deployments for the tenant cluster")
l := metav1.AddLabelToSelector(
&metav1.LabelSelector{},
label.Cluster,
key.ClusterID(&cr),
)
o := metav1.ListOptions{
LabelSelector: labels.Set(l.MatchLabels).String(),
}
err = r.ctrlClient.List(ctx, mdList, &client.ListOptions{Raw: &o})
if err != nil {
return microerror.Mask(err)
}
if len(mdList.Items) != 0 {
r.logger.Debugf(ctx, "found %d machine deployments for the tenant cluster", len(mdList.Items))
r.logger.Debugf(ctx, "not deleting the tenant cluster's control plane cloud formation stack")
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
return nil
}
r.logger.Debugf(ctx, "did not find machine deployments for the tenant cluster")
}
{
r.logger.Debugf(ctx, "disabling the termination protection of the tenant cluster's control plane cloud formation stack")
i := &cloudformation.UpdateTerminationProtectionInput{
EnableTerminationProtection: aws.Bool(false),
StackName: aws.String(key.StackNameTCCP(&cr)),
}
_, err = cc.Client.TenantCluster.AWS.CloudFormation.UpdateTerminationProtection(i)
if IsDeleteInProgress(err) {
r.logger.Debugf(ctx, "the tenant cluster's control plane cloud formation stack is being deleted")
r.event.Emit(ctx, &cr, "CFDelete", fmt.Sprintf("the tenant cluster's control plane cloud formation stack has stack status %#q", cloudformation.StackStatusDeleteInProgress))
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if IsDeleteFailed(err) {
r.logger.Debugf(ctx, "the tenant cluster's control plane cloud formation stack failed to delete")
r.event.Emit(ctx, &cr, "CFDeleteFailed", fmt.Sprintf("the tenant cluster's control plane cloud formation stack has stack status %#q", cloudformation.StackStatusDeleteFailed))
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if IsNotExists(err) {
r.logger.Debugf(ctx, "the tenant cluster's control plane cloud formation stack does not exist")
r.event.Emit(ctx, &cr, "CFDeleted", fmt.Sprintf("the tenant cluster's control plane cloud formation stack has stack status %#q", cloudformation.StackStatusDeleteComplete))
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "disabled the termination protection of the tenant cluster's control plane cloud formation stack")
}
{
r.logger.Debugf(ctx, "requesting the deletion of the tenant cluster's control plane cloud formation stack")
i := &cloudformation.DeleteStackInput{
StackName: aws.String(key.StackNameTCCP(&cr)),
}
_, err = cc.Client.TenantCluster.AWS.CloudFormation.DeleteStack(i)
if IsUpdateInProgress(err) {
r.logger.Debugf(ctx, "the tenant cluster's control plane cloud formation stack is being updated")
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "requested the deletion of the tenant cluster's control plane cloud formation stack")
r.event.Emit(ctx, &cr, "CFDeleteRequested", "requested the deletion of the tenant cluster's control plane cloud formation stack")
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
}
return nil
}