rollout_resume.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // ResumeConfig 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 ResumeConfig struct {
  28. ResumeObject 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. resume_long = dedent.Dedent(`
  38. Resume a paused resource
  39. Paused resources will not be reconciled by a controller. By resuming a
  40. resource, we allow it to be reconciled again.
  41. Currently only deployments support being resumed.`)
  42. resume_example = dedent.Dedent(`
  43. # Resume an already paused deployment
  44. kubectl rollout resume deployment/nginx`)
  45. )
  46. func NewCmdRolloutResume(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  47. opts := &ResumeConfig{}
  48. validArgs := []string{"deployment"}
  49. argAliases := kubectl.ResourceAliases(validArgs)
  50. cmd := &cobra.Command{
  51. Use: "resume RESOURCE",
  52. Short: "Resume a paused resource",
  53. Long: resume_long,
  54. Example: resume_example,
  55. Run: func(cmd *cobra.Command, args []string) {
  56. allErrs := []error{}
  57. err := opts.CompleteResume(f, cmd, out, args)
  58. if err != nil {
  59. allErrs = append(allErrs, err)
  60. }
  61. err = opts.RunResume()
  62. if err != nil {
  63. allErrs = append(allErrs, err)
  64. }
  65. cmdutil.CheckErr(utilerrors.Flatten(utilerrors.NewAggregate(allErrs)))
  66. },
  67. ValidArgs: validArgs,
  68. ArgAliases: argAliases,
  69. }
  70. usage := "Filename, directory, or URL to a file identifying the resource to get from a server."
  71. kubectl.AddJsonFilenameFlag(cmd, &opts.Filenames, usage)
  72. cmdutil.AddRecursiveFlag(cmd, &opts.Recursive)
  73. return cmd
  74. }
  75. func (o *ResumeConfig) CompleteResume(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error {
  76. if len(args) == 0 && len(o.Filenames) == 0 {
  77. return cmdutil.UsageError(cmd, cmd.Use)
  78. }
  79. o.Mapper, o.Typer = f.Object(false)
  80. o.ResumeObject = f.ResumeObject
  81. o.Out = out
  82. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  83. if err != nil {
  84. return err
  85. }
  86. r := resource.NewBuilder(o.Mapper, o.Typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  87. NamespaceParam(cmdNamespace).DefaultNamespace().
  88. FilenameParam(enforceNamespace, o.Recursive, o.Filenames...).
  89. ResourceTypeOrNameArgs(true, args...).
  90. ContinueOnError().
  91. Latest().
  92. Flatten().
  93. Do()
  94. err = r.Err()
  95. if err != nil {
  96. return err
  97. }
  98. err = r.Visit(func(info *resource.Info, err error) error {
  99. if err != nil {
  100. return err
  101. }
  102. o.Infos = append(o.Infos, info)
  103. return nil
  104. })
  105. if err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. func (o ResumeConfig) RunResume() error {
  111. allErrs := []error{}
  112. for _, info := range o.Infos {
  113. isAlreadyResumed, err := o.ResumeObject(info.Object)
  114. if err != nil {
  115. allErrs = append(allErrs, cmdutil.AddSourceToErr("resuming", info.Source, err))
  116. continue
  117. }
  118. if isAlreadyResumed {
  119. cmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, "already resumed")
  120. continue
  121. }
  122. cmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, "resumed")
  123. }
  124. return utilerrors.NewAggregate(allErrs)
  125. }