rollback.go 6.4 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 deployment
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/klog"
  20. apps "k8s.io/api/apps/v1"
  21. "k8s.io/api/core/v1"
  22. extensions "k8s.io/api/extensions/v1beta1"
  23. deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
  24. )
  25. // rollback the deployment to the specified revision. In any case cleanup the rollback spec.
  26. func (dc *DeploymentController) rollback(d *apps.Deployment, rsList []*apps.ReplicaSet) error {
  27. newRS, allOldRSs, err := dc.getAllReplicaSetsAndSyncRevision(d, rsList, true)
  28. if err != nil {
  29. return err
  30. }
  31. allRSs := append(allOldRSs, newRS)
  32. rollbackTo := getRollbackTo(d)
  33. // If rollback revision is 0, rollback to the last revision
  34. if rollbackTo.Revision == 0 {
  35. if rollbackTo.Revision = deploymentutil.LastRevision(allRSs); rollbackTo.Revision == 0 {
  36. // If we still can't find the last revision, gives up rollback
  37. dc.emitRollbackWarningEvent(d, deploymentutil.RollbackRevisionNotFound, "Unable to find last revision.")
  38. // Gives up rollback
  39. return dc.updateDeploymentAndClearRollbackTo(d)
  40. }
  41. }
  42. for _, rs := range allRSs {
  43. v, err := deploymentutil.Revision(rs)
  44. if err != nil {
  45. klog.V(4).Infof("Unable to extract revision from deployment's replica set %q: %v", rs.Name, err)
  46. continue
  47. }
  48. if v == rollbackTo.Revision {
  49. klog.V(4).Infof("Found replica set %q with desired revision %d", rs.Name, v)
  50. // rollback by copying podTemplate.Spec from the replica set
  51. // revision number will be incremented during the next getAllReplicaSetsAndSyncRevision call
  52. // no-op if the spec matches current deployment's podTemplate.Spec
  53. performedRollback, err := dc.rollbackToTemplate(d, rs)
  54. if performedRollback && err == nil {
  55. dc.emitRollbackNormalEvent(d, fmt.Sprintf("Rolled back deployment %q to revision %d", d.Name, rollbackTo.Revision))
  56. }
  57. return err
  58. }
  59. }
  60. dc.emitRollbackWarningEvent(d, deploymentutil.RollbackRevisionNotFound, "Unable to find the revision to rollback to.")
  61. // Gives up rollback
  62. return dc.updateDeploymentAndClearRollbackTo(d)
  63. }
  64. // rollbackToTemplate compares the templates of the provided deployment and replica set and
  65. // updates the deployment with the replica set template in case they are different. It also
  66. // cleans up the rollback spec so subsequent requeues of the deployment won't end up in here.
  67. func (dc *DeploymentController) rollbackToTemplate(d *apps.Deployment, rs *apps.ReplicaSet) (bool, error) {
  68. performedRollback := false
  69. if !deploymentutil.EqualIgnoreHash(&d.Spec.Template, &rs.Spec.Template) {
  70. klog.V(4).Infof("Rolling back deployment %q to template spec %+v", d.Name, rs.Spec.Template.Spec)
  71. deploymentutil.SetFromReplicaSetTemplate(d, rs.Spec.Template)
  72. // set RS (the old RS we'll rolling back to) annotations back to the deployment;
  73. // otherwise, the deployment's current annotations (should be the same as current new RS) will be copied to the RS after the rollback.
  74. //
  75. // For example,
  76. // A Deployment has old RS1 with annotation {change-cause:create}, and new RS2 {change-cause:edit}.
  77. // Note that both annotations are copied from Deployment, and the Deployment should be annotated {change-cause:edit} as well.
  78. // Now, rollback Deployment to RS1, we should update Deployment's pod-template and also copy annotation from RS1.
  79. // Deployment is now annotated {change-cause:create}, and we have new RS1 {change-cause:create}, old RS2 {change-cause:edit}.
  80. //
  81. // If we don't copy the annotations back from RS to deployment on rollback, the Deployment will stay as {change-cause:edit},
  82. // and new RS1 becomes {change-cause:edit} (copied from deployment after rollback), old RS2 {change-cause:edit}, which is not correct.
  83. deploymentutil.SetDeploymentAnnotationsTo(d, rs)
  84. performedRollback = true
  85. } else {
  86. klog.V(4).Infof("Rolling back to a revision that contains the same template as current deployment %q, skipping rollback...", d.Name)
  87. eventMsg := fmt.Sprintf("The rollback revision contains the same template as current deployment %q", d.Name)
  88. dc.emitRollbackWarningEvent(d, deploymentutil.RollbackTemplateUnchanged, eventMsg)
  89. }
  90. return performedRollback, dc.updateDeploymentAndClearRollbackTo(d)
  91. }
  92. func (dc *DeploymentController) emitRollbackWarningEvent(d *apps.Deployment, reason, message string) {
  93. dc.eventRecorder.Eventf(d, v1.EventTypeWarning, reason, message)
  94. }
  95. func (dc *DeploymentController) emitRollbackNormalEvent(d *apps.Deployment, message string) {
  96. dc.eventRecorder.Eventf(d, v1.EventTypeNormal, deploymentutil.RollbackDone, message)
  97. }
  98. // updateDeploymentAndClearRollbackTo sets .spec.rollbackTo to nil and update the input deployment
  99. // It is assumed that the caller will have updated the deployment template appropriately (in case
  100. // we want to rollback).
  101. func (dc *DeploymentController) updateDeploymentAndClearRollbackTo(d *apps.Deployment) error {
  102. klog.V(4).Infof("Cleans up rollbackTo of deployment %q", d.Name)
  103. setRollbackTo(d, nil)
  104. _, err := dc.client.AppsV1().Deployments(d.Namespace).Update(context.TODO(), d, metav1.UpdateOptions{})
  105. return err
  106. }
  107. // TODO: Remove this when extensions/v1beta1 and apps/v1beta1 Deployment are dropped.
  108. func getRollbackTo(d *apps.Deployment) *extensions.RollbackConfig {
  109. // Extract the annotation used for round-tripping the deprecated RollbackTo field.
  110. revision := d.Annotations[apps.DeprecatedRollbackTo]
  111. if revision == "" {
  112. return nil
  113. }
  114. revision64, err := strconv.ParseInt(revision, 10, 64)
  115. if err != nil {
  116. // If it's invalid, ignore it.
  117. return nil
  118. }
  119. return &extensions.RollbackConfig{
  120. Revision: revision64,
  121. }
  122. }
  123. // TODO: Remove this when extensions/v1beta1 and apps/v1beta1 Deployment are dropped.
  124. func setRollbackTo(d *apps.Deployment, rollbackTo *extensions.RollbackConfig) {
  125. if rollbackTo == nil {
  126. delete(d.Annotations, apps.DeprecatedRollbackTo)
  127. return
  128. }
  129. if d.Annotations == nil {
  130. d.Annotations = make(map[string]string)
  131. }
  132. d.Annotations[apps.DeprecatedRollbackTo] = strconv.FormatInt(rollbackTo.Revision, 10)
  133. }