/
delete.go
101 lines (73 loc) · 3.36 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
package tcnp
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/finalizerskeptcontext"
"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.ToMachineDeployment(obj)
if err != nil {
return microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
{
r.logger.Debugf(ctx, "disabling the termination protection of the tenant cluster's node pool cloud formation stack")
i := &cloudformation.UpdateTerminationProtectionInput{
EnableTerminationProtection: aws.Bool(false),
StackName: aws.String(key.StackNameTCNP(&cr)),
}
_, err = cc.Client.TenantCluster.AWS.CloudFormation.UpdateTerminationProtection(i)
if IsDeleteInProgress(err) {
r.logger.Debugf(ctx, "the tenant cluster's node pool cloud formation stack is being deleted")
r.event.Emit(ctx, &cr, "CFDelete", fmt.Sprintf("the tenant cluster's node pool 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 node pool cloud formation stack failed to delete")
r.event.Emit(ctx, &cr, "CFDeleteFailed", fmt.Sprintf("the tenant cluster's node pool 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 node pool cloud formation stack does not exist")
r.event.Emit(ctx, &cr, "CFDeleted", fmt.Sprintf("the tenant cluster's node pool 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 node pool cloud formation stack")
}
{
r.logger.Debugf(ctx, "requesting the deletion of the tenant cluster's node pool cloud formation stack")
i := &cloudformation.DeleteStackInput{
StackName: aws.String(key.StackNameTCNP(&cr)),
}
_, err = cc.Client.TenantCluster.AWS.CloudFormation.DeleteStack(i)
if IsUpdateInProgress(err) {
r.logger.Debugf(ctx, "the tenant cluster's node pool 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 node pool cloud formation stack")
r.logger.LogCtx(ctx, cr, "CFDeleteRequested", "Requested the deletion of the tenant cluster's node pool cloud formation stack")
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
}
return nil
}