delete.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright 2014 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 cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "time"
  18. "github.com/renstrom/dedent"
  19. "github.com/spf13/cobra"
  20. "k8s.io/kubernetes/pkg/api"
  21. "k8s.io/kubernetes/pkg/api/errors"
  22. "k8s.io/kubernetes/pkg/api/meta"
  23. "k8s.io/kubernetes/pkg/kubectl"
  24. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  25. "k8s.io/kubernetes/pkg/kubectl/resource"
  26. )
  27. // DeleteOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  28. // referencing the cmd.Flags()
  29. type DeleteOptions struct {
  30. Filenames []string
  31. Recursive bool
  32. }
  33. var (
  34. delete_long = dedent.Dedent(`
  35. Delete resources by filenames, stdin, resources and names, or by resources and label selector.
  36. JSON and YAML formats are accepted.
  37. Only one type of the arguments may be specified: filenames, resources and names, or resources and label selector
  38. Note that the delete command does NOT do resource version checks, so if someone
  39. submits an update to a resource right when you submit a delete, their update
  40. will be lost along with the rest of the resource.`)
  41. delete_example = dedent.Dedent(`
  42. # Delete a pod using the type and name specified in pod.json.
  43. kubectl delete -f ./pod.json
  44. # Delete a pod based on the type and name in the JSON passed into stdin.
  45. cat pod.json | kubectl delete -f -
  46. # Delete pods and services with same names "baz" and "foo"
  47. kubectl delete pod,service baz foo
  48. # Delete pods and services with label name=myLabel.
  49. kubectl delete pods,services -l name=myLabel
  50. # Delete a pod immediately (no graceful shutdown)
  51. kubectl delete pod foo --now
  52. # Delete a pod with UID 1234-56-7890-234234-456456.
  53. kubectl delete pod 1234-56-7890-234234-456456
  54. # Delete all pods
  55. kubectl delete pods --all`)
  56. )
  57. func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  58. options := &DeleteOptions{}
  59. // retrieve a list of handled resources from printer as valid args
  60. validArgs, argAliases := []string{}, []string{}
  61. p, err := f.Printer(nil, kubectl.PrintOptions{
  62. ColumnLabels: []string{},
  63. })
  64. cmdutil.CheckErr(err)
  65. if p != nil {
  66. validArgs = p.HandledResources()
  67. argAliases = kubectl.ResourceAliases(validArgs)
  68. }
  69. cmd := &cobra.Command{
  70. Use: "delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])",
  71. Short: "Delete resources by filenames, stdin, resources and names, or by resources and label selector",
  72. Long: delete_long,
  73. Example: delete_example,
  74. Run: func(cmd *cobra.Command, args []string) {
  75. cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
  76. err := RunDelete(f, out, cmd, args, options)
  77. cmdutil.CheckErr(err)
  78. },
  79. SuggestFor: []string{"rm"},
  80. ValidArgs: validArgs,
  81. ArgAliases: argAliases,
  82. }
  83. usage := "Filename, directory, or URL to a file containing the resource to delete."
  84. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  85. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  86. cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on.")
  87. cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
  88. cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful delete. Defaults to \"true\" when --all is specified.")
  89. cmd.Flags().Bool("cascade", true, "If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.")
  90. cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
  91. cmd.Flags().Bool("now", false, "If true, resources are force terminated without graceful deletion (same as --grace-period=0).")
  92. cmd.Flags().Duration("timeout", 0, "The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object")
  93. cmdutil.AddOutputFlagsForMutation(cmd)
  94. cmdutil.AddInclude3rdPartyFlags(cmd)
  95. return cmd
  96. }
  97. func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *DeleteOptions) error {
  98. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  99. if err != nil {
  100. return err
  101. }
  102. deleteAll := cmdutil.GetFlagBool(cmd, "all")
  103. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  104. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  105. ContinueOnError().
  106. NamespaceParam(cmdNamespace).DefaultNamespace().
  107. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  108. SelectorParam(cmdutil.GetFlagString(cmd, "selector")).
  109. SelectAllParam(deleteAll).
  110. ResourceTypeOrNameArgs(false, args...).RequireObject(false).
  111. Flatten().
  112. Do()
  113. err = r.Err()
  114. if err != nil {
  115. return err
  116. }
  117. ignoreNotFound := cmdutil.GetFlagBool(cmd, "ignore-not-found")
  118. if deleteAll {
  119. f := cmd.Flags().Lookup("ignore-not-found")
  120. // The flag should never be missing
  121. if f == nil {
  122. return fmt.Errorf("missing --ignore-not-found flag")
  123. }
  124. // If the user didn't explicitly set the option, default to ignoring NotFound errors when used with --all
  125. if !f.Changed {
  126. ignoreNotFound = true
  127. }
  128. }
  129. gracePeriod := cmdutil.GetFlagInt(cmd, "grace-period")
  130. if cmdutil.GetFlagBool(cmd, "now") {
  131. if gracePeriod != -1 {
  132. return fmt.Errorf("--now and --grace-period cannot be specified together")
  133. }
  134. gracePeriod = 0
  135. }
  136. shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
  137. // By default use a reaper to delete all related resources.
  138. if cmdutil.GetFlagBool(cmd, "cascade") {
  139. return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagDuration(cmd, "timeout"), gracePeriod, shortOutput, mapper, false)
  140. }
  141. return DeleteResult(r, out, ignoreNotFound, shortOutput, mapper)
  142. }
  143. func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int, shortOutput bool, mapper meta.RESTMapper, quiet bool) error {
  144. found := 0
  145. if ignoreNotFound {
  146. r = r.IgnoreErrors(errors.IsNotFound)
  147. }
  148. err := r.Visit(func(info *resource.Info, err error) error {
  149. if err != nil {
  150. return err
  151. }
  152. found++
  153. reaper, err := f.Reaper(info.Mapping)
  154. if err != nil {
  155. // If there is no reaper for this resources and the user didn't explicitly ask for stop.
  156. if kubectl.IsNoSuchReaperError(err) && isDefaultDelete {
  157. return deleteResource(info, out, shortOutput, mapper)
  158. }
  159. return cmdutil.AddSourceToErr("reaping", info.Source, err)
  160. }
  161. var options *api.DeleteOptions
  162. if gracePeriod >= 0 {
  163. options = api.NewDeleteOptions(int64(gracePeriod))
  164. }
  165. if err := reaper.Stop(info.Namespace, info.Name, timeout, options); err != nil {
  166. return cmdutil.AddSourceToErr("stopping", info.Source, err)
  167. }
  168. if !quiet {
  169. cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "deleted")
  170. }
  171. return nil
  172. })
  173. if err != nil {
  174. return err
  175. }
  176. if found == 0 {
  177. fmt.Fprintf(out, "No resources found\n")
  178. }
  179. return nil
  180. }
  181. func DeleteResult(r *resource.Result, out io.Writer, ignoreNotFound bool, shortOutput bool, mapper meta.RESTMapper) error {
  182. found := 0
  183. if ignoreNotFound {
  184. r = r.IgnoreErrors(errors.IsNotFound)
  185. }
  186. err := r.Visit(func(info *resource.Info, err error) error {
  187. if err != nil {
  188. return err
  189. }
  190. found++
  191. return deleteResource(info, out, shortOutput, mapper)
  192. })
  193. if err != nil {
  194. return err
  195. }
  196. if found == 0 {
  197. fmt.Fprintf(out, "No resources found\n")
  198. }
  199. return nil
  200. }
  201. func deleteResource(info *resource.Info, out io.Writer, shortOutput bool, mapper meta.RESTMapper) error {
  202. if err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name); err != nil {
  203. return cmdutil.AddSourceToErr("deleting", info.Source, err)
  204. }
  205. cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "deleted")
  206. return nil
  207. }