@@ -17,12 +17,16 @@ limitations under the License.
17
17
package delete
18
18
19
19
import (
20
+ "fmt"
21
+ "strconv"
20
22
"time"
21
23
22
24
"github.com/spf13/cobra"
23
25
26
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
27
"k8s.io/cli-runtime/pkg/genericclioptions"
25
28
"k8s.io/client-go/dynamic"
29
+ "k8s.io/klog/v2"
26
30
)
27
31
28
32
// DeleteFlags composes common printer flag structs
@@ -32,20 +36,20 @@ type DeleteFlags struct {
32
36
LabelSelector * string
33
37
FieldSelector * string
34
38
35
- All * bool
36
- AllNamespaces * bool
37
- Cascade * bool
38
- Force * bool
39
- GracePeriod * int
40
- IgnoreNotFound * bool
41
- Now * bool
42
- Timeout * time.Duration
43
- Wait * bool
44
- Output * string
45
- Raw * string
39
+ All * bool
40
+ AllNamespaces * bool
41
+ CascadingStrategy * string
42
+ Force * bool
43
+ GracePeriod * int
44
+ IgnoreNotFound * bool
45
+ Now * bool
46
+ Timeout * time.Duration
47
+ Wait * bool
48
+ Output * string
49
+ Raw * string
46
50
}
47
51
48
- func (f * DeleteFlags ) ToOptions (dynamicClient dynamic.Interface , streams genericclioptions.IOStreams ) * DeleteOptions {
52
+ func (f * DeleteFlags ) ToOptions (dynamicClient dynamic.Interface , streams genericclioptions.IOStreams ) ( * DeleteOptions , error ) {
49
53
options := & DeleteOptions {
50
54
DynamicClient : dynamicClient ,
51
55
IOStreams : streams ,
@@ -73,8 +77,12 @@ func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams generic
73
77
if f .AllNamespaces != nil {
74
78
options .DeleteAllNamespaces = * f .AllNamespaces
75
79
}
76
- if f .Cascade != nil {
77
- options .Cascade = * f .Cascade
80
+ if f .CascadingStrategy != nil {
81
+ var err error
82
+ options .CascadingStrategy , err = getCascadingStrategy (* f .CascadingStrategy )
83
+ if err != nil {
84
+ return nil , err
85
+ }
78
86
}
79
87
if f .Force != nil {
80
88
options .ForceDeletion = * f .Force
@@ -98,7 +106,7 @@ func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams generic
98
106
options .Raw = * f .Raw
99
107
}
100
108
101
- return options
109
+ return options , nil
102
110
}
103
111
104
112
func (f * DeleteFlags ) AddFlags (cmd * cobra.Command ) {
@@ -118,8 +126,13 @@ func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
118
126
if f .Force != nil {
119
127
cmd .Flags ().BoolVar (f .Force , "force" , * f .Force , "If true, immediately remove resources from API and bypass graceful deletion. Note that immediate deletion of some resources may result in inconsistency or data loss and requires confirmation." )
120
128
}
121
- if f .Cascade != nil {
122
- cmd .Flags ().BoolVar (f .Cascade , "cascade" , * f .Cascade , "If true, run background cascade deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true." )
129
+ if f .CascadingStrategy != nil {
130
+ cmd .Flags ().StringVar (
131
+ f .CascadingStrategy ,
132
+ "cascade" ,
133
+ * f .CascadingStrategy ,
134
+ `Must be "background", "orphan", or "foreground". Selects the deletion cascading strategy for the dependents (e.g. Pods created by a ReplicationController). Defaults to background.` )
135
+ cmd .Flags ().Lookup ("cascade" ).NoOptDefVal = "background"
123
136
}
124
137
if f .Now != nil {
125
138
cmd .Flags ().BoolVar (f .Now , "now" , * f .Now , "If true, resources are signaled for immediate shutdown (same as --grace-period=1)." )
@@ -146,7 +159,7 @@ func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
146
159
147
160
// NewDeleteCommandFlags provides default flags and values for use with the "delete" command
148
161
func NewDeleteCommandFlags (usage string ) * DeleteFlags {
149
- cascade := true
162
+ cascadingStrategy := "background"
150
163
gracePeriod := - 1
151
164
152
165
// setup command defaults
@@ -172,8 +185,8 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags {
172
185
LabelSelector : & labelSelector ,
173
186
FieldSelector : & fieldSelector ,
174
187
175
- Cascade : & cascade ,
176
- GracePeriod : & gracePeriod ,
188
+ CascadingStrategy : & cascadingStrategy ,
189
+ GracePeriod : & gracePeriod ,
177
190
178
191
All : & all ,
179
192
AllNamespaces : & allNamespaces ,
@@ -189,7 +202,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags {
189
202
190
203
// NewDeleteFlags provides default flags and values for use in commands outside of "delete"
191
204
func NewDeleteFlags (usage string ) * DeleteFlags {
192
- cascade := true
205
+ cascadingStrategy := "background"
193
206
gracePeriod := - 1
194
207
195
208
force := false
@@ -203,12 +216,36 @@ func NewDeleteFlags(usage string) *DeleteFlags {
203
216
return & DeleteFlags {
204
217
FileNameFlags : & genericclioptions.FileNameFlags {Usage : usage , Filenames : & filenames , Kustomize : & kustomize , Recursive : & recursive },
205
218
206
- Cascade : & cascade ,
207
- GracePeriod : & gracePeriod ,
219
+ CascadingStrategy : & cascadingStrategy ,
220
+ GracePeriod : & gracePeriod ,
208
221
209
222
// add non-defaults
210
223
Force : & force ,
211
224
Timeout : & timeout ,
212
225
Wait : & wait ,
213
226
}
214
227
}
228
+
229
+ func getCascadingStrategy (cascadingFlag string ) (metav1.DeletionPropagation , error ) {
230
+ b , err := strconv .ParseBool (cascadingFlag )
231
+ // The flag is not a boolean
232
+ if err != nil {
233
+ switch cascadingFlag {
234
+ case "orphan" :
235
+ return metav1 .DeletePropagationOrphan , nil
236
+ case "foreground" :
237
+ return metav1 .DeletePropagationForeground , nil
238
+ case "background" :
239
+ return metav1 .DeletePropagationBackground , nil
240
+ default :
241
+ return metav1 .DeletePropagationBackground , fmt .Errorf (`Invalid cascade value (%v). Must be "background", "foreground", or "orphan".` , cascadingFlag )
242
+ }
243
+ }
244
+ // The flag was a boolean
245
+ if b {
246
+ klog .Warningf (`--cascade=%v is deprecated (boolean value) and can be replaced with --cascade=%s.` , cascadingFlag , "background" )
247
+ return metav1 .DeletePropagationBackground , nil
248
+ }
249
+ klog .Warningf (`--cascade=%v is deprecated (boolean value) and can be replaced with --cascade=%s.` , cascadingFlag , "orphan" )
250
+ return metav1 .DeletePropagationOrphan , nil
251
+ }
0 commit comments