history.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "bytes"
  16. "fmt"
  17. "io"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/api/meta"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/apis/extensions"
  22. clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
  23. deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
  24. "k8s.io/kubernetes/pkg/runtime"
  25. sliceutil "k8s.io/kubernetes/pkg/util/slice"
  26. )
  27. const (
  28. ChangeCauseAnnotation = "kubernetes.io/change-cause"
  29. )
  30. // HistoryViewer provides an interface for resources have historical information.
  31. type HistoryViewer interface {
  32. ViewHistory(namespace, name string, revision int64) (string, error)
  33. }
  34. func HistoryViewerFor(kind unversioned.GroupKind, c clientset.Interface) (HistoryViewer, error) {
  35. switch kind {
  36. case extensions.Kind("Deployment"):
  37. return &DeploymentHistoryViewer{c}, nil
  38. }
  39. return nil, fmt.Errorf("no history viewer has been implemented for %q", kind)
  40. }
  41. type DeploymentHistoryViewer struct {
  42. c clientset.Interface
  43. }
  44. // ViewHistory returns a revision-to-replicaset map as the revision history of a deployment
  45. func (h *DeploymentHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) {
  46. deployment, err := h.c.Extensions().Deployments(namespace).Get(name)
  47. if err != nil {
  48. return "", fmt.Errorf("failed to retrieve deployment %s: %v", name, err)
  49. }
  50. _, allOldRSs, newRS, err := deploymentutil.GetAllReplicaSets(deployment, h.c)
  51. if err != nil {
  52. return "", fmt.Errorf("failed to retrieve replica sets from deployment %s: %v", name, err)
  53. }
  54. allRSs := allOldRSs
  55. if newRS != nil {
  56. allRSs = append(allRSs, newRS)
  57. }
  58. historyInfo := make(map[int64]*api.PodTemplateSpec)
  59. for _, rs := range allRSs {
  60. v, err := deploymentutil.Revision(rs)
  61. if err != nil {
  62. continue
  63. }
  64. historyInfo[v] = &rs.Spec.Template
  65. changeCause := getChangeCause(rs)
  66. if historyInfo[v].Annotations == nil {
  67. historyInfo[v].Annotations = make(map[string]string)
  68. }
  69. if len(changeCause) > 0 {
  70. historyInfo[v].Annotations[ChangeCauseAnnotation] = changeCause
  71. }
  72. }
  73. if len(historyInfo) == 0 {
  74. return "No rollout history found.", nil
  75. }
  76. if revision > 0 {
  77. // Print details of a specific revision
  78. template, ok := historyInfo[revision]
  79. if !ok {
  80. return "", fmt.Errorf("unable to find the specified revision")
  81. }
  82. buf := bytes.NewBuffer([]byte{})
  83. DescribePodTemplate(template, buf)
  84. return buf.String(), nil
  85. }
  86. // Sort the revisionToChangeCause map by revision
  87. revisions := make([]int64, 0, len(historyInfo))
  88. for r := range historyInfo {
  89. revisions = append(revisions, r)
  90. }
  91. sliceutil.SortInts64(revisions)
  92. return tabbedString(func(out io.Writer) error {
  93. fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n")
  94. for _, r := range revisions {
  95. // Find the change-cause of revision r
  96. changeCause := historyInfo[r].Annotations[ChangeCauseAnnotation]
  97. if len(changeCause) == 0 {
  98. changeCause = "<none>"
  99. }
  100. fmt.Fprintf(out, "%d\t%s\n", r, changeCause)
  101. }
  102. return nil
  103. })
  104. }
  105. // getChangeCause returns the change-cause annotation of the input object
  106. func getChangeCause(obj runtime.Object) string {
  107. accessor, err := meta.Accessor(obj)
  108. if err != nil {
  109. return ""
  110. }
  111. return accessor.GetAnnotations()[ChangeCauseAnnotation]
  112. }