delete_flags.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package delete
  14. import (
  15. "time"
  16. "github.com/spf13/cobra"
  17. "k8s.io/cli-runtime/pkg/genericclioptions"
  18. "k8s.io/client-go/dynamic"
  19. )
  20. // DeleteFlags composes common printer flag structs
  21. // used for commands requiring deletion logic.
  22. type DeleteFlags struct {
  23. FileNameFlags *genericclioptions.FileNameFlags
  24. LabelSelector *string
  25. FieldSelector *string
  26. All *bool
  27. AllNamespaces *bool
  28. Cascade *bool
  29. Force *bool
  30. GracePeriod *int
  31. IgnoreNotFound *bool
  32. Now *bool
  33. Timeout *time.Duration
  34. Wait *bool
  35. Output *string
  36. }
  37. func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams genericclioptions.IOStreams) *DeleteOptions {
  38. options := &DeleteOptions{
  39. DynamicClient: dynamicClient,
  40. IOStreams: streams,
  41. }
  42. // add filename options
  43. if f.FileNameFlags != nil {
  44. options.FilenameOptions = f.FileNameFlags.ToOptions()
  45. }
  46. if f.LabelSelector != nil {
  47. options.LabelSelector = *f.LabelSelector
  48. }
  49. if f.FieldSelector != nil {
  50. options.FieldSelector = *f.FieldSelector
  51. }
  52. // add output format
  53. if f.Output != nil {
  54. options.Output = *f.Output
  55. }
  56. if f.All != nil {
  57. options.DeleteAll = *f.All
  58. }
  59. if f.AllNamespaces != nil {
  60. options.DeleteAllNamespaces = *f.AllNamespaces
  61. }
  62. if f.Cascade != nil {
  63. options.Cascade = *f.Cascade
  64. }
  65. if f.Force != nil {
  66. options.ForceDeletion = *f.Force
  67. }
  68. if f.GracePeriod != nil {
  69. options.GracePeriod = *f.GracePeriod
  70. }
  71. if f.IgnoreNotFound != nil {
  72. options.IgnoreNotFound = *f.IgnoreNotFound
  73. }
  74. if f.Now != nil {
  75. options.DeleteNow = *f.Now
  76. }
  77. if f.Timeout != nil {
  78. options.Timeout = *f.Timeout
  79. }
  80. if f.Wait != nil {
  81. options.WaitForDeletion = *f.Wait
  82. }
  83. return options
  84. }
  85. func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
  86. f.FileNameFlags.AddFlags(cmd.Flags())
  87. if f.LabelSelector != nil {
  88. cmd.Flags().StringVarP(f.LabelSelector, "selector", "l", *f.LabelSelector, "Selector (label query) to filter on, not including uninitialized ones.")
  89. }
  90. if f.FieldSelector != nil {
  91. cmd.Flags().StringVarP(f.FieldSelector, "field-selector", "", *f.FieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
  92. }
  93. if f.All != nil {
  94. cmd.Flags().BoolVar(f.All, "all", *f.All, "Delete all resources, including uninitialized ones, in the namespace of the specified resource types.")
  95. }
  96. if f.AllNamespaces != nil {
  97. cmd.Flags().BoolVarP(f.AllNamespaces, "all-namespaces", "A", *f.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
  98. }
  99. if f.Force != nil {
  100. cmd.Flags().BoolVar(f.Force, "force", *f.Force, "Only used when grace-period=0. 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.")
  101. }
  102. if f.Cascade != nil {
  103. cmd.Flags().BoolVar(f.Cascade, "cascade", *f.Cascade, "If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.")
  104. }
  105. if f.Now != nil {
  106. cmd.Flags().BoolVar(f.Now, "now", *f.Now, "If true, resources are signaled for immediate shutdown (same as --grace-period=1).")
  107. }
  108. if f.GracePeriod != nil {
  109. cmd.Flags().IntVar(f.GracePeriod, "grace-period", *f.GracePeriod, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. Set to 1 for immediate shutdown. Can only be set to 0 when --force is true (force deletion).")
  110. }
  111. if f.Timeout != nil {
  112. cmd.Flags().DurationVar(f.Timeout, "timeout", *f.Timeout, "The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object")
  113. }
  114. if f.IgnoreNotFound != nil {
  115. cmd.Flags().BoolVar(f.IgnoreNotFound, "ignore-not-found", *f.IgnoreNotFound, "Treat \"resource not found\" as a successful delete. Defaults to \"true\" when --all is specified.")
  116. }
  117. if f.Wait != nil {
  118. cmd.Flags().BoolVar(f.Wait, "wait", *f.Wait, "If true, wait for resources to be gone before returning. This waits for finalizers.")
  119. }
  120. if f.Output != nil {
  121. cmd.Flags().StringVarP(f.Output, "output", "o", *f.Output, "Output mode. Use \"-o name\" for shorter output (resource/name).")
  122. }
  123. }
  124. // NewDeleteCommandFlags provides default flags and values for use with the "delete" command
  125. func NewDeleteCommandFlags(usage string) *DeleteFlags {
  126. cascade := true
  127. gracePeriod := -1
  128. // setup command defaults
  129. all := false
  130. allNamespaces := false
  131. force := false
  132. ignoreNotFound := false
  133. now := false
  134. output := ""
  135. labelSelector := ""
  136. fieldSelector := ""
  137. timeout := time.Duration(0)
  138. wait := true
  139. filenames := []string{}
  140. recursive := false
  141. kustomize := ""
  142. return &DeleteFlags{
  143. // Not using helpers.go since it provides function to add '-k' for FileNameOptions, but not FileNameFlags
  144. FileNameFlags: &genericclioptions.FileNameFlags{Usage: usage, Filenames: &filenames, Kustomize: &kustomize, Recursive: &recursive},
  145. LabelSelector: &labelSelector,
  146. FieldSelector: &fieldSelector,
  147. Cascade: &cascade,
  148. GracePeriod: &gracePeriod,
  149. All: &all,
  150. AllNamespaces: &allNamespaces,
  151. Force: &force,
  152. IgnoreNotFound: &ignoreNotFound,
  153. Now: &now,
  154. Timeout: &timeout,
  155. Wait: &wait,
  156. Output: &output,
  157. }
  158. }
  159. // NewDeleteFlags provides default flags and values for use in commands outside of "delete"
  160. func NewDeleteFlags(usage string) *DeleteFlags {
  161. cascade := true
  162. gracePeriod := -1
  163. force := false
  164. timeout := time.Duration(0)
  165. wait := false
  166. filenames := []string{}
  167. kustomize := ""
  168. recursive := false
  169. return &DeleteFlags{
  170. FileNameFlags: &genericclioptions.FileNameFlags{Usage: usage, Filenames: &filenames, Kustomize: &kustomize, Recursive: &recursive},
  171. Cascade: &cascade,
  172. GracePeriod: &gracePeriod,
  173. // add non-defaults
  174. Force: &force,
  175. Timeout: &timeout,
  176. Wait: &wait,
  177. }
  178. }