-
Notifications
You must be signed in to change notification settings - Fork 286
/
delete.go
289 lines (234 loc) Β· 9.15 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package workflows
import (
"context"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/providers"
"github.com/aws/eks-anywhere/pkg/task"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/workflows/interfaces"
)
type Delete struct {
bootstrapper interfaces.Bootstrapper
provider providers.Provider
clusterManager interfaces.ClusterManager
gitOpsManager interfaces.GitOpsManager
writer filewriter.FileWriter
}
func NewDelete(bootstrapper interfaces.Bootstrapper, provider providers.Provider,
clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager,
writer filewriter.FileWriter,
) *Delete {
return &Delete{
bootstrapper: bootstrapper,
provider: provider,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
writer: writer,
}
}
func (c *Delete) Run(ctx context.Context, workloadCluster *types.Cluster, clusterSpec *cluster.Spec, forceCleanup bool, kubeconfig string) error {
if forceCleanup {
if err := c.bootstrapper.DeleteBootstrapCluster(ctx, &types.Cluster{
Name: workloadCluster.Name,
}, constants.Delete, forceCleanup); err != nil {
return err
}
}
commandContext := &task.CommandContext{
Bootstrapper: c.bootstrapper,
Provider: c.provider,
ClusterManager: c.clusterManager,
GitOpsManager: c.gitOpsManager,
WorkloadCluster: workloadCluster,
ClusterSpec: clusterSpec,
}
if clusterSpec.ManagementCluster != nil {
commandContext.BootstrapCluster = clusterSpec.ManagementCluster
}
return task.NewTaskRunner(&setupAndValidate{}, c.writer).RunTask(ctx, commandContext)
}
type setupAndValidate struct{}
type createManagementCluster struct{}
type installCAPI struct{}
type moveClusterManagement struct{}
type deleteWorkloadCluster struct{}
type cleanupGitRepo struct{}
type deletePackageResources struct{}
type deleteManagementCluster struct{}
func (s *setupAndValidate) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Performing provider setup and validations")
err := commandContext.Provider.SetupAndValidateDeleteCluster(ctx, commandContext.WorkloadCluster, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return nil
}
return &createManagementCluster{}
}
func (s *setupAndValidate) Name() string {
return "setup-and-validate"
}
func (s *setupAndValidate) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *setupAndValidate) Checkpoint() *task.CompletedTask {
return nil
}
func (s *createManagementCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
if commandContext.BootstrapCluster != nil && commandContext.BootstrapCluster.ExistingManagement {
return &deleteWorkloadCluster{}
}
logger.Info("Creating management cluster")
bootstrapOptions, err := commandContext.Provider.BootstrapClusterOpts(commandContext.ClusterSpec)
if err != nil {
logger.Error(err, "Error getting management options from provider")
commandContext.SetError(err)
return nil
}
bootstrapCluster, err := commandContext.Bootstrapper.CreateBootstrapCluster(ctx, commandContext.ClusterSpec, bootstrapOptions...)
if err != nil {
commandContext.SetError(err)
return &deleteManagementCluster{}
}
commandContext.BootstrapCluster = bootstrapCluster
logger.Info("Provider specific pre-capi-install-setup on bootstrap cluster")
if err = commandContext.Provider.PreCAPIInstallOnBootstrap(ctx, bootstrapCluster, commandContext.ClusterSpec); err != nil {
commandContext.SetError(err)
return &CollectMgmtClusterDiagnosticsTask{}
}
return &installCAPI{}
}
func (s *createManagementCluster) Name() string {
return "management-cluster-init"
}
func (s *createManagementCluster) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *createManagementCluster) Checkpoint() *task.CompletedTask {
return nil
}
func (s *installCAPI) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Installing cluster-api providers on management cluster")
err := commandContext.ClusterManager.InstallCAPI(ctx, commandContext.ClusterSpec, commandContext.BootstrapCluster, commandContext.Provider)
if err != nil {
commandContext.SetError(err)
return &deleteManagementCluster{}
}
return &moveClusterManagement{}
}
func (s *installCAPI) Name() string {
return "install-capi"
}
func (s *installCAPI) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *installCAPI) Checkpoint() *task.CompletedTask {
return nil
}
func (s *moveClusterManagement) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Moving cluster management from workload cluster")
err := commandContext.ClusterManager.MoveCAPI(ctx, commandContext.WorkloadCluster, commandContext.BootstrapCluster, commandContext.WorkloadCluster.Name, commandContext.ClusterSpec, types.WithNodeRef())
if err != nil {
commandContext.SetError(err)
return &CollectDiagnosticsTask{}
}
return &deleteWorkloadCluster{}
}
func (s *moveClusterManagement) Name() string {
return "cluster-management-move"
}
func (s *moveClusterManagement) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *moveClusterManagement) Checkpoint() *task.CompletedTask {
return nil
}
func (s *deleteWorkloadCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Deleting workload cluster")
err := commandContext.ClusterManager.DeleteCluster(ctx, commandContext.BootstrapCluster, commandContext.WorkloadCluster, commandContext.Provider, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &CollectDiagnosticsTask{}
}
return &cleanupGitRepo{}
}
func (s *deleteWorkloadCluster) Name() string {
return "delete-workload-cluster"
}
func (s *deleteWorkloadCluster) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *deleteWorkloadCluster) Checkpoint() *task.CompletedTask {
return nil
}
func (s *cleanupGitRepo) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
logger.Info("Clean up Git Repo")
err := commandContext.GitOpsManager.CleanupGitRepo(ctx, commandContext.ClusterSpec)
if err != nil {
commandContext.SetError(err)
return &CollectDiagnosticsTask{}
}
return &deletePackageResources{}
}
func (s *cleanupGitRepo) Name() string {
return "clean-up-git-repo"
}
func (s *cleanupGitRepo) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *cleanupGitRepo) Checkpoint() *task.CompletedTask {
return nil
}
func (s *deletePackageResources) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
if !commandContext.BootstrapCluster.ExistingManagement {
return &deleteManagementCluster{}
}
logger.Info("Delete package resources", "clusterName", commandContext.WorkloadCluster.Name)
cluster := commandContext.ManagementCluster
if cluster == nil {
cluster = commandContext.BootstrapCluster
}
err := commandContext.ClusterManager.DeletePackageResources(ctx, cluster, commandContext.WorkloadCluster.Name)
if err != nil {
logger.Info("Problem delete package resources", "error", err)
}
// A bit odd to traverse to this state here, but it is the terminal state
return &deleteManagementCluster{}
}
func (s *deletePackageResources) Name() string {
return "package-resource-delete"
}
func (s *deletePackageResources) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *deletePackageResources) Checkpoint() *task.CompletedTask {
return nil
}
func (s *deleteManagementCluster) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
if commandContext.OriginalError != nil {
collector := &CollectMgmtClusterDiagnosticsTask{}
collector.Run(ctx, commandContext)
}
if commandContext.BootstrapCluster != nil && !commandContext.BootstrapCluster.ExistingManagement {
if err := commandContext.Bootstrapper.DeleteBootstrapCluster(ctx, commandContext.BootstrapCluster, constants.Delete, false); err != nil {
commandContext.SetError(err)
}
return nil
}
logger.Info("Bootstrap cluster information missing - skipping delete kind cluster")
if commandContext.OriginalError == nil {
logger.MarkSuccess("Cluster deleted!")
}
return nil
}
func (s *deleteManagementCluster) Name() string {
return "kind-cluster-delete"
}
func (s *deleteManagementCluster) Restore(ctx context.Context, commandContext *task.CommandContext, completedTask *task.CompletedTask) (task.Task, error) {
return nil, nil
}
func (s *deleteManagementCluster) Checkpoint() *task.CompletedTask {
return nil
}