rollout_pause.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright 2016 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 rollout
  14. import (
  15. "io"
  16. "github.com/renstrom/dedent"
  17. "github.com/spf13/cobra"
  18. "k8s.io/kubernetes/pkg/api/meta"
  19. "k8s.io/kubernetes/pkg/kubectl"
  20. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. "k8s.io/kubernetes/pkg/kubectl/resource"
  22. "k8s.io/kubernetes/pkg/runtime"
  23. utilerrors "k8s.io/kubernetes/pkg/util/errors"
  24. )
  25. // PauseConfig is the start of the data required to perform the operation. As new fields are added, add them here instead of
  26. // referencing the cmd.Flags()
  27. type PauseConfig struct {
  28. PauseObject func(object runtime.Object) (bool, error)
  29. Mapper meta.RESTMapper
  30. Typer runtime.ObjectTyper
  31. Infos []*resource.Info
  32. Out io.Writer
  33. Filenames []string
  34. Recursive bool
  35. }
  36. var (
  37. pause_long = dedent.Dedent(`
  38. Mark the provided resource as paused
  39. Paused resources will not be reconciled by a controller.
  40. Use \"kubectl rollout resume\" to resume a paused resource.
  41. Currently only deployments support being paused.`)
  42. pause_example = dedent.Dedent(`
  43. # Mark the nginx deployment as paused. Any current state of
  44. # the deployment will continue its function, new updates to the deployment will not
  45. # have an effect as long as the deployment is paused.
  46. kubectl rollout pause deployment/nginx`)
  47. )
  48. func NewCmdRolloutPause(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  49. opts := &PauseConfig{}
  50. validArgs := []string{"deployment"}
  51. argAliases := kubectl.ResourceAliases(validArgs)
  52. cmd := &cobra.Command{
  53. Use: "pause RESOURCE",
  54. Short: "Mark the provided resource as paused",
  55. Long: pause_long,
  56. Example: pause_example,
  57. Run: func(cmd *cobra.Command, args []string) {
  58. allErrs := []error{}
  59. err := opts.CompletePause(f, cmd, out, args)
  60. if err != nil {
  61. allErrs = append(allErrs, err)
  62. }
  63. err = opts.RunPause()
  64. if err != nil {
  65. allErrs = append(allErrs, err)
  66. }
  67. cmdutil.CheckErr(utilerrors.Flatten(utilerrors.NewAggregate(allErrs)))
  68. },
  69. ValidArgs: validArgs,
  70. ArgAliases: argAliases,
  71. }
  72. usage := "Filename, directory, or URL to a file identifying the resource to get from a server."
  73. kubectl.AddJsonFilenameFlag(cmd, &opts.Filenames, usage)
  74. cmdutil.AddRecursiveFlag(cmd, &opts.Recursive)
  75. return cmd
  76. }
  77. func (o *PauseConfig) CompletePause(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error {
  78. if len(args) == 0 && len(o.Filenames) == 0 {
  79. return cmdutil.UsageError(cmd, cmd.Use)
  80. }
  81. o.Mapper, o.Typer = f.Object(false)
  82. o.PauseObject = f.PauseObject
  83. o.Out = out
  84. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  85. if err != nil {
  86. return err
  87. }
  88. r := resource.NewBuilder(o.Mapper, o.Typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  89. NamespaceParam(cmdNamespace).DefaultNamespace().
  90. FilenameParam(enforceNamespace, o.Recursive, o.Filenames...).
  91. ResourceTypeOrNameArgs(true, args...).
  92. ContinueOnError().
  93. Latest().
  94. Flatten().
  95. Do()
  96. err = r.Err()
  97. if err != nil {
  98. return err
  99. }
  100. o.Infos, err = r.Infos()
  101. if err != nil {
  102. return err
  103. }
  104. return nil
  105. }
  106. func (o PauseConfig) RunPause() error {
  107. allErrs := []error{}
  108. for _, info := range o.Infos {
  109. isAlreadyPaused, err := o.PauseObject(info.Object)
  110. if err != nil {
  111. allErrs = append(allErrs, cmdutil.AddSourceToErr("pausing", info.Source, err))
  112. continue
  113. }
  114. if isAlreadyPaused {
  115. cmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, "already paused")
  116. continue
  117. }
  118. cmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, "paused")
  119. }
  120. return utilerrors.NewAggregate(allErrs)
  121. }