rollout_status.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "fmt"
  16. "io"
  17. "github.com/renstrom/dedent"
  18. "k8s.io/kubernetes/pkg/kubectl"
  19. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  20. "k8s.io/kubernetes/pkg/kubectl/resource"
  21. "k8s.io/kubernetes/pkg/watch"
  22. "github.com/spf13/cobra"
  23. )
  24. // StatusOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
  25. // referencing the cmd.Flags()
  26. type StatusOptions struct {
  27. Filenames []string
  28. Recursive bool
  29. }
  30. var (
  31. status_long = dedent.Dedent(`
  32. Watch the status of current rollout, until it's done.`)
  33. status_example = dedent.Dedent(`
  34. # Watch the rollout status of a deployment
  35. kubectl rollout status deployment/nginx`)
  36. )
  37. func NewCmdRolloutStatus(f *cmdutil.Factory, out io.Writer) *cobra.Command {
  38. options := &StatusOptions{}
  39. validArgs := []string{"deployment"}
  40. argAliases := kubectl.ResourceAliases(validArgs)
  41. cmd := &cobra.Command{
  42. Use: "status (TYPE NAME | TYPE/NAME) [flags]",
  43. Short: "Watch rollout status until it's done",
  44. Long: status_long,
  45. Example: status_example,
  46. Run: func(cmd *cobra.Command, args []string) {
  47. cmdutil.CheckErr(RunStatus(f, cmd, out, args, options))
  48. },
  49. ValidArgs: validArgs,
  50. ArgAliases: argAliases,
  51. }
  52. usage := "Filename, directory, or URL to a file identifying the resource to get from a server."
  53. kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
  54. cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
  55. return cmd
  56. }
  57. func RunStatus(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *StatusOptions) error {
  58. if len(args) == 0 && len(options.Filenames) == 0 {
  59. return cmdutil.UsageError(cmd, "Required resource not specified.")
  60. }
  61. mapper, typer := f.Object(false)
  62. cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
  63. if err != nil {
  64. return err
  65. }
  66. r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
  67. NamespaceParam(cmdNamespace).DefaultNamespace().
  68. FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
  69. ResourceTypeOrNameArgs(true, args...).
  70. SingleResourceType().
  71. Latest().
  72. Do()
  73. err = r.Err()
  74. if err != nil {
  75. return err
  76. }
  77. infos, err := r.Infos()
  78. if err != nil {
  79. return err
  80. }
  81. if len(infos) != 1 {
  82. return fmt.Errorf("rollout status is only supported on individual resources and resource collections - %d resources were found", len(infos))
  83. }
  84. info := infos[0]
  85. mapping := info.ResourceMapping()
  86. obj, err := r.Object()
  87. if err != nil {
  88. return err
  89. }
  90. rv, err := mapping.MetadataAccessor.ResourceVersion(obj)
  91. if err != nil {
  92. return err
  93. }
  94. statusViewer, err := f.StatusViewer(mapping)
  95. if err != nil {
  96. return err
  97. }
  98. // check if deployment's has finished the rollout
  99. status, done, err := statusViewer.Status(cmdNamespace, info.Name)
  100. if err != nil {
  101. return err
  102. }
  103. fmt.Fprintf(out, "%s", status)
  104. if done {
  105. return nil
  106. }
  107. // watch for changes to the deployment
  108. w, err := r.Watch(rv)
  109. if err != nil {
  110. return err
  111. }
  112. // if the rollout isn't done yet, keep watching deployment status
  113. kubectl.WatchLoop(w, func(e watch.Event) error {
  114. // print deployment's status
  115. status, done, err := statusViewer.Status(cmdNamespace, info.Name)
  116. if err != nil {
  117. return err
  118. }
  119. fmt.Fprintf(out, "%s", status)
  120. // Quit waiting if the rollout is done
  121. if done {
  122. w.Stop()
  123. }
  124. return nil
  125. })
  126. return nil
  127. }