forked from KusionStack/kusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.go
412 lines (361 loc) · 10.4 KB
/
destroy.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright 2024 KusionStack Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package destroy
import (
"fmt"
"os"
"strings"
"sync"
"github.com/liu-hm19/pterm"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
"kusionstack.io/kusion/pkg/cmd/meta"
cmdutil "kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/engine/operation"
"kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/release"
"kusionstack.io/kusion/pkg/engine/runtime/terraform"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/util/pretty"
"kusionstack.io/kusion/pkg/util/terminal"
)
var (
destroyLong = i18n.T(`
Destroy resources within the stack.
Please note that the destroy command does NOT perform resource version checks.
Therefore, if someone submits an update to a resource at the same time you execute a destroy command,
their update will be lost along with the rest of the resource.`)
destroyExample = i18n.T(`
# Delete resources of current stack
kusion destroy`)
)
// DeleteFlags directly reflect the information that CLI is gathering via flags. They will be converted to
// DestroyOptions, which reflect the runtime requirements for the command.
//
// This structure reduces the transformation to wiring and makes the logic itself easy to unit test.
type DeleteFlags struct {
MetaFlags *meta.MetaFlags
Operator string
Yes bool
Detail bool
NoStyle bool
UI *terminal.UI
genericiooptions.IOStreams
}
// DestroyOptions defines flags and other configuration parameters for the `delete` command.
type DestroyOptions struct {
*meta.MetaOptions
Yes bool
Detail bool
NoStyle bool
UI *terminal.UI
genericiooptions.IOStreams
}
// NewDeleteFlags returns a default DeleteFlags
func NewDeleteFlags(ui *terminal.UI, streams genericiooptions.IOStreams) *DeleteFlags {
return &DeleteFlags{
MetaFlags: meta.NewMetaFlags(),
UI: ui,
IOStreams: streams,
}
}
// NewCmdDestroy creates the `delete` command.
func NewCmdDestroy(ui *terminal.UI, ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewDeleteFlags(ui, ioStreams)
cmd := &cobra.Command{
Use: "destroy",
Short: "Destroy resources within the stack.",
Long: templates.LongDesc(destroyLong),
Example: templates.Examples(destroyExample),
RunE: func(cmd *cobra.Command, args []string) (err error) {
o, err := flags.ToOptions()
defer cmdutil.RecoverErr(&err)
cmdutil.CheckErr(err)
cmdutil.CheckErr(o.Validate(cmd, args))
cmdutil.CheckErr(o.Run())
return
},
}
flags.AddFlags(cmd)
return cmd
}
// AddFlags registers flags for a cli.
func (flags *DeleteFlags) AddFlags(cmd *cobra.Command) {
// bind flag structs
flags.MetaFlags.AddFlags(cmd)
cmd.Flags().BoolVarP(&flags.Yes, "yes", "y", false, i18n.T("Automatically approve and perform the update after previewing it"))
cmd.Flags().BoolVarP(&flags.Detail, "detail", "d", false, i18n.T("Automatically show preview details after previewing it"))
cmd.Flags().BoolVarP(&flags.NoStyle, "no-style", "", false, i18n.T("no-style sets to RawOutput mode and disables all of styling"))
}
// ToOptions converts from CLI inputs to runtime inputs.
func (flags *DeleteFlags) ToOptions() (*DestroyOptions, error) {
// Convert meta options
metaOptions, err := flags.MetaFlags.ToOptions()
if err != nil {
return nil, err
}
o := &DestroyOptions{
MetaOptions: metaOptions,
Detail: flags.Detail,
Yes: flags.Yes,
NoStyle: flags.NoStyle,
UI: flags.UI,
IOStreams: flags.IOStreams,
}
return o, nil
}
// Validate verifies if DestroyOptions are valid and without conflicts.
func (o *DestroyOptions) Validate(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
}
return nil
}
// Run executes the `delete` command.
func (o *DestroyOptions) Run() (err error) {
// update release to succeeded or failed
var storage release.Storage
var rel *apiv1.Release
releaseCreated := false
defer func() {
if !releaseCreated {
return
}
if err != nil {
rel.Phase = apiv1.ReleasePhaseFailed
_ = release.UpdateDestroyRelease(storage, rel)
} else {
rel.Phase = apiv1.ReleasePhaseSucceeded
err = release.UpdateDestroyRelease(storage, rel)
}
}()
// only destroy resources we managed
storage, err = o.Backend.ReleaseStorage(o.RefProject.Name, o.RefWorkspace.Name)
if err != nil {
return
}
rel, err = release.CreateDestroyRelease(storage, o.RefProject.Name, o.RefStack.Name, o.RefWorkspace.Name)
if err != nil {
return
}
if len(rel.Spec.Resources) == 0 {
pterm.Println(pterm.Green("No managed resources to destroy"))
return
}
releaseCreated = true
// compute changes for preview
changes, err := o.preview(rel.Spec, rel.State, o.RefProject, o.RefStack, storage)
if err != nil {
return
}
// preview
changes.Summary(os.Stdout, o.NoStyle)
// detail detection
if o.Detail {
changes.OutputDiff("all")
return nil
}
// set no style
if o.NoStyle {
pterm.DisableStyling()
}
// prompt
if !o.Yes {
for {
var input string
input, err = prompt(o.UI)
if err != nil {
return
}
if input == "yes" {
break
} else if input == "details" {
var target string
target, err = changes.PromptDetails(o.UI)
if err != nil {
return
}
changes.OutputDiff(target)
} else {
fmt.Println("Operation destroy canceled")
return nil
}
}
}
// update release phase to destroying
rel.Phase = apiv1.ReleasePhaseDestroying
if err = release.UpdateDestroyRelease(storage, rel); err != nil {
return
}
// destroy
fmt.Println("Start destroying resources......")
var updatedRel *apiv1.Release
updatedRel, err = o.destroy(rel, changes, storage)
if err != nil {
return err
}
rel = updatedRel
return nil
}
func (o *DestroyOptions) preview(
planResources *apiv1.Spec,
priorResources *apiv1.State,
proj *apiv1.Project,
stack *apiv1.Stack,
storage release.Storage,
) (*models.Changes, error) {
log.Info("Start compute preview changes ...")
// check and install terraform executable binary for
// resources with the type of Terraform.
tfInstaller := terraform.CLIInstaller{
Intent: planResources,
}
if err := tfInstaller.CheckAndInstall(); err != nil {
return nil, err
}
pc := &operation.PreviewOperation{
Operation: models.Operation{
OperationType: models.DestroyPreview,
Stack: stack,
ReleaseStorage: storage,
ChangeOrder: &models.ChangeOrder{StepKeys: []string{}, ChangeSteps: map[string]*models.ChangeStep{}},
},
}
log.Info("Start call pc.Preview() ...")
rsp, s := pc.Preview(&operation.PreviewRequest{
Request: models.Request{
Project: proj,
Stack: stack,
},
Spec: planResources,
State: priorResources,
})
if v1.IsErr(s) {
return nil, fmt.Errorf("preview failed, status: %v", s)
}
return models.NewChanges(proj, stack, rsp.Order), nil
}
func (o *DestroyOptions) destroy(rel *apiv1.Release, changes *models.Changes, storage release.Storage) (*apiv1.Release, error) {
destroyOpt := &operation.DestroyOperation{
Operation: models.Operation{
Stack: changes.Stack(),
ReleaseStorage: storage,
MsgCh: make(chan models.Message),
},
}
// line summary
var deleted int
// progress bar, print dag walk detail
progressbar, err := o.UI.ProgressbarPrinter.
WithMaxWidth(0).
WithTotal(len(changes.StepKeys)).
WithWriter(o.IOStreams.Out).
WithRemoveWhenDone().
Start()
if err != nil {
return nil, err
}
// wait msgCh close
var wg sync.WaitGroup
// receive msg and print detail
go func() {
defer func() {
if p := recover(); p != nil {
log.Errorf("failed to receive msg and print detail as %v", p)
}
}()
wg.Add(1)
for {
select {
case msg, ok := <-destroyOpt.MsgCh:
if !ok {
wg.Done()
return
}
changeStep := changes.Get(msg.ResourceID)
switch msg.OpResult {
case models.Success, models.Skip:
var title string
if changeStep.Action == models.UnChanged {
title = fmt.Sprintf("%s %s, %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(models.Skip)),
)
} else {
title = fmt.Sprintf("%s %s %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(msg.OpResult)),
)
}
pretty.SuccessT.WithWriter(o.IOStreams.Out).Println(title)
progressbar.UpdateTitle(title)
progressbar.Increment()
deleted++
case models.Failed:
title := fmt.Sprintf("%s %s %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(msg.OpResult)),
)
pretty.ErrorT.WithWriter(o.IOStreams.Out).Printf("%s\n", title)
default:
title := fmt.Sprintf("%s %s %s",
changeStep.Action.Ing(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(msg.OpResult)),
)
progressbar.UpdateTitle(title)
}
}
}
}()
req := &operation.DestroyRequest{
Request: models.Request{
Project: changes.Project(),
Stack: changes.Stack(),
},
Release: rel,
}
rsp, status := destroyOpt.Destroy(req)
if v1.IsErr(status) {
return nil, fmt.Errorf("destroy failed, status: %v", status)
}
updatedRel := rsp.Release
// wait for msgCh closed
wg.Wait()
// print summary
pterm.Println()
pterm.Fprintln(o.IOStreams.Out, fmt.Sprintf("Destroy complete! Resources: %d deleted.", deleted))
return updatedRel, nil
}
func prompt(ui *terminal.UI) (string, error) {
options := []string{"yes", "details", "no"}
input, err := ui.InteractiveSelectPrinter.
WithFilter(false).
WithDefaultText(`Do you want to destroy these diffs?`).
WithOptions(options).
WithDefaultOption("details").
Show()
if err != nil {
fmt.Printf("Prompt failed: %v\n", err)
return "", err
}
return input, nil
}