1
2
3
4 package destroy
5
6 import (
7 "context"
8 "fmt"
9 "strings"
10 "time"
11
12 "github.com/spf13/cobra"
13 "k8s.io/cli-runtime/pkg/genericclioptions"
14 cmdutil "k8s.io/kubectl/pkg/cmd/util"
15 "k8s.io/kubectl/pkg/util/i18n"
16 "sigs.k8s.io/cli-utils/cmd/flagutils"
17 "sigs.k8s.io/cli-utils/pkg/apply"
18 "sigs.k8s.io/cli-utils/pkg/common"
19 "sigs.k8s.io/cli-utils/pkg/inventory"
20 "sigs.k8s.io/cli-utils/pkg/manifestreader"
21 "sigs.k8s.io/cli-utils/pkg/printers"
22 )
23
24
25 func GetRunner(factory cmdutil.Factory, invFactory inventory.ClientFactory,
26 loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *Runner {
27 r := &Runner{
28 ioStreams: ioStreams,
29 factory: factory,
30 invFactory: invFactory,
31 loader: loader,
32 }
33 cmd := &cobra.Command{
34 Use: "destroy (DIRECTORY | STDIN)",
35 DisableFlagsInUseLine: true,
36 Short: i18n.T("Destroy all the resources related to configuration"),
37 RunE: r.RunE,
38 }
39
40 cmd.Flags().StringVar(&r.output, "output", printers.DefaultPrinter(),
41 fmt.Sprintf("Output format, must be one of %s", strings.Join(printers.SupportedPrinters(), ",")))
42 cmd.Flags().StringVar(&r.inventoryPolicy, flagutils.InventoryPolicyFlag, flagutils.InventoryPolicyStrict,
43 "It determines the behavior when the resources don't belong to current inventory. Available options "+
44 fmt.Sprintf("%q, %q and %q.", flagutils.InventoryPolicyStrict, flagutils.InventoryPolicyAdopt, flagutils.InventoryPolicyForceAdopt))
45 cmd.Flags().DurationVar(&r.deleteTimeout, "delete-timeout", time.Duration(0),
46 "Timeout threshold for waiting for all deleted resources to complete deletion")
47 cmd.Flags().StringVar(&r.deletePropagationPolicy, "delete-propagation-policy",
48 "Background", "Propagation policy for deletion")
49 cmd.Flags().DurationVar(&r.timeout, "timeout", 0,
50 "How long to wait before exiting")
51 cmd.Flags().BoolVar(&r.printStatusEvents, "status-events", false,
52 "Print status events (always enabled for table output)")
53
54 r.Command = cmd
55 return r
56 }
57
58
59 func Command(f cmdutil.Factory, invFactory inventory.ClientFactory, loader manifestreader.ManifestLoader,
60 ioStreams genericclioptions.IOStreams) *cobra.Command {
61 return GetRunner(f, invFactory, loader, ioStreams).Command
62 }
63
64
65 type Runner struct {
66 Command *cobra.Command
67 ioStreams genericclioptions.IOStreams
68 factory cmdutil.Factory
69 invFactory inventory.ClientFactory
70 loader manifestreader.ManifestLoader
71
72 output string
73 deleteTimeout time.Duration
74 deletePropagationPolicy string
75 inventoryPolicy string
76 timeout time.Duration
77 printStatusEvents bool
78 }
79
80 func (r *Runner) RunE(cmd *cobra.Command, args []string) error {
81 ctx := cmd.Context()
82
83 if r.timeout != 0 {
84 var cancel context.CancelFunc
85 ctx, cancel = context.WithTimeout(ctx, r.timeout)
86 defer cancel()
87 }
88
89 deletePropPolicy, err := flagutils.ConvertPropagationPolicy(r.deletePropagationPolicy)
90 if err != nil {
91 return err
92 }
93 inventoryPolicy, err := flagutils.ConvertInventoryPolicy(r.inventoryPolicy)
94 if err != nil {
95 return err
96 }
97
98 if found := printers.ValidatePrinterType(r.output); !found {
99 return fmt.Errorf("unknown output type %q", r.output)
100 }
101
102
103 reader, err := r.loader.ManifestReader(cmd.InOrStdin(), flagutils.PathFromArgs(args))
104 if err != nil {
105 return err
106 }
107 objs, err := reader.Read()
108 if err != nil {
109 return err
110 }
111 invObj, _, err := inventory.SplitUnstructureds(objs)
112 if err != nil {
113 return err
114 }
115 inv := inventory.WrapInventoryInfoObj(invObj)
116
117 invClient, err := r.invFactory.NewClient(r.factory)
118 if err != nil {
119 return err
120 }
121 d, err := apply.NewDestroyerBuilder().
122 WithFactory(r.factory).
123 WithInventoryClient(invClient).
124 Build()
125 if err != nil {
126 return err
127 }
128
129
130 if r.output == printers.TablePrinter {
131 r.printStatusEvents = true
132 }
133
134
135
136 ch := d.Run(ctx, inv, apply.DestroyerOptions{
137 DeleteTimeout: r.deleteTimeout,
138 DeletePropagationPolicy: deletePropPolicy,
139 InventoryPolicy: inventoryPolicy,
140 EmitStatusEvents: r.printStatusEvents,
141 })
142
143
144
145 printer := printers.GetPrinter(r.output, r.ioStreams)
146 return printer.Print(ch, common.DryRunNone, r.printStatusEvents)
147 }
148
View as plain text