stop.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "github.com/renstrom/dedent"
  18. "github.com/spf13/cobra"
  19. "k8s.io/kubernetes/pkg/kubectl"
  20. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. "k8s.io/kubernetes/pkg/kubectl/resource"
  22. )
  23. // StopOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  24. // referencing the cmd.Flags()
  25. type StopOptions struct {
  26. Filenames []string
  27. Recursive bool
  28. }
  29. var (
  30. stop_long = dedent.Dedent(`
  31. Deprecated: Gracefully shut down a resource by name or filename.
  32. The stop command is deprecated, all its functionalities are covered by delete command.
  33. See 'kubectl delete --help' for more details.
  34. Attempts to shut down and delete a resource that supports graceful termination.
  35. If the resource is scalable it will be scaled to 0 before deletion.`)
  36. stop_example = dedent.Dedent(`
  37. # Shut down foo.
  38. kubectl stop replicationcontroller foo
  39. # Stop pods and services with label name=myLabel.
  40. kubectl stop pods,services -l name=myLabel
  41. # Shut down the service defined in service.json
  42. kubectl stop -f service.json
  43. # Shut down all resources in the path/to/resources directory
  44. kubectl stop -f path/to/resources`)
  45. )
  46. func NewCmdStop(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  47. options := &StopOptions{}
  48. cmd := &cobra.Command{
  49. Use: "stop (-f FILENAME | TYPE (NAME | -l label | --all))",
  50. Short: "Deprecated: Gracefully shut down a resource by name or filename",
  51. Long: stop_long,
  52. Example: stop_example,
  53. Deprecated: fmt.Sprintf("use %q instead.", "delete"),
  54. Run: func(cmd *cobra.Command, args []string) {
  55. cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
  56. cmdutil.CheckErr(RunStop(f, cmd, args, out, options))
  57. },
  58. }
  59. usage := "Filename, directory, or URL to file of resource(s) to be stopped."
  60. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  61. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  62. cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on.")
  63. cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
  64. cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful stop.")
  65. cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
  66. 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")
  67. cmdutil.AddOutputFlagsForMutation(cmd)
  68. cmdutil.AddInclude3rdPartyFlags(cmd)
  69. return cmd
  70. }
  71. func RunStop(f *cmdutil.Factory, cmd *cobra.Command, args []string, out io.Writer, options *StopOptions) error {
  72. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  73. if err != nil {
  74. return err
  75. }
  76. mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd))
  77. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  78. ContinueOnError().
  79. NamespaceParam(cmdNamespace).DefaultNamespace().
  80. ResourceTypeOrNameArgs(false, args...).
  81. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  82. SelectorParam(cmdutil.GetFlagString(cmd, "selector")).
  83. SelectAllParam(cmdutil.GetFlagBool(cmd, "all")).
  84. Flatten().
  85. Do()
  86. if r.Err() != nil {
  87. return r.Err()
  88. }
  89. shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
  90. return ReapResult(r, f, out, false, cmdutil.GetFlagBool(cmd, "ignore-not-found"), cmdutil.GetFlagDuration(cmd, "timeout"), cmdutil.GetFlagInt(cmd, "grace-period"), shortOutput, mapper, false)
  91. }