rollback.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 kubectl
  14. import (
  15. "fmt"
  16. "os"
  17. "os/signal"
  18. "syscall"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/apis/extensions"
  22. client "k8s.io/kubernetes/pkg/client/unversioned"
  23. deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
  24. "k8s.io/kubernetes/pkg/runtime"
  25. "k8s.io/kubernetes/pkg/watch"
  26. )
  27. // Rollbacker provides an interface for resources that can be rolled back.
  28. type Rollbacker interface {
  29. Rollback(obj runtime.Object, updatedAnnotations map[string]string, toRevision int64) (string, error)
  30. }
  31. func RollbackerFor(kind unversioned.GroupKind, c client.Interface) (Rollbacker, error) {
  32. switch kind {
  33. case extensions.Kind("Deployment"):
  34. return &DeploymentRollbacker{c}, nil
  35. }
  36. return nil, fmt.Errorf("no rollbacker has been implemented for %q", kind)
  37. }
  38. type DeploymentRollbacker struct {
  39. c client.Interface
  40. }
  41. func (r *DeploymentRollbacker) Rollback(obj runtime.Object, updatedAnnotations map[string]string, toRevision int64) (string, error) {
  42. d, ok := obj.(*extensions.Deployment)
  43. if !ok {
  44. return "", fmt.Errorf("passed object is not a Deployment: %#v", obj)
  45. }
  46. if d.Spec.Paused {
  47. return "", fmt.Errorf("you cannot rollback a paused deployment; resume it first with 'kubectl rollout resume deployment/%s' and try again", d.Name)
  48. }
  49. deploymentRollback := &extensions.DeploymentRollback{
  50. Name: d.Name,
  51. UpdatedAnnotations: updatedAnnotations,
  52. RollbackTo: extensions.RollbackConfig{
  53. Revision: toRevision,
  54. },
  55. }
  56. result := ""
  57. // Get current events
  58. events, err := r.c.Events(d.Namespace).List(api.ListOptions{})
  59. if err != nil {
  60. return result, err
  61. }
  62. // Do the rollback
  63. if err := r.c.Extensions().Deployments(d.Namespace).Rollback(deploymentRollback); err != nil {
  64. return result, err
  65. }
  66. // Watch for the changes of events
  67. watch, err := r.c.Events(d.Namespace).Watch(api.ListOptions{Watch: true, ResourceVersion: events.ResourceVersion})
  68. if err != nil {
  69. return result, err
  70. }
  71. result = watchRollbackEvent(watch)
  72. return result, err
  73. }
  74. // watchRollbackEvent watches for rollback events and returns rollback result
  75. func watchRollbackEvent(w watch.Interface) string {
  76. signals := make(chan os.Signal, 1)
  77. signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGTERM)
  78. for {
  79. select {
  80. case event, ok := <-w.ResultChan():
  81. if !ok {
  82. return ""
  83. }
  84. obj, ok := event.Object.(*api.Event)
  85. if !ok {
  86. w.Stop()
  87. return ""
  88. }
  89. isRollback, result := isRollbackEvent(obj)
  90. if isRollback {
  91. w.Stop()
  92. return result
  93. }
  94. case <-signals:
  95. w.Stop()
  96. }
  97. }
  98. }
  99. // isRollbackEvent checks if the input event is about rollback, and returns true and
  100. // related result string back if it is.
  101. func isRollbackEvent(e *api.Event) (bool, string) {
  102. rollbackEventReasons := []string{deploymentutil.RollbackRevisionNotFound, deploymentutil.RollbackTemplateUnchanged, deploymentutil.RollbackDone}
  103. for _, reason := range rollbackEventReasons {
  104. if e.Reason == reason {
  105. if reason == deploymentutil.RollbackDone {
  106. return true, "rolled back"
  107. }
  108. return true, fmt.Sprintf("skipped rollback (%s: %s)", e.Reason, e.Message)
  109. }
  110. }
  111. return false, ""
  112. }