ViewHistory returns a revision-to-replicaset map as the revision history of a deployment TODO: this should be a describer
(namespace, name string, revision int64)
| 102 | // ViewHistory returns a revision-to-replicaset map as the revision history of a deployment |
| 103 | // TODO: this should be a describer |
| 104 | func (h *DeploymentHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) { |
| 105 | allRSs, err := getDeploymentReplicaSets(h.c.AppsV1(), namespace, name) |
| 106 | if err != nil { |
| 107 | return "", err |
| 108 | } |
| 109 | |
| 110 | historyInfo := make(map[int64]*corev1.PodTemplateSpec) |
| 111 | for _, rs := range allRSs { |
| 112 | v, err := deploymentutil.Revision(rs) |
| 113 | if err != nil { |
| 114 | klog.Warningf("unable to get revision from replicaset %s for deployment %s in namespace %s: %v", rs.Name, name, namespace, err) |
| 115 | continue |
| 116 | } |
| 117 | historyInfo[v] = &rs.Spec.Template |
| 118 | changeCause := getChangeCause(rs) |
| 119 | if historyInfo[v].Annotations == nil { |
| 120 | historyInfo[v].Annotations = make(map[string]string) |
| 121 | } |
| 122 | if len(changeCause) > 0 { |
| 123 | historyInfo[v].Annotations[ChangeCauseAnnotation] = changeCause |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if len(historyInfo) == 0 { |
| 128 | return "No rollout history found.", nil |
| 129 | } |
| 130 | |
| 131 | if revision > 0 { |
| 132 | // Print details of a specific revision |
| 133 | template, ok := historyInfo[revision] |
| 134 | if !ok { |
| 135 | return "", fmt.Errorf("unable to find the specified revision") |
| 136 | } |
| 137 | return printTemplate(template) |
| 138 | } |
| 139 | |
| 140 | // Sort the revisionToChangeCause map by revision |
| 141 | revisions := make([]int64, 0, len(historyInfo)) |
| 142 | for r := range historyInfo { |
| 143 | revisions = append(revisions, r) |
| 144 | } |
| 145 | slices.Sort(revisions) |
| 146 | |
| 147 | return tabbedString(func(out io.Writer) error { |
| 148 | fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n") |
| 149 | for _, r := range revisions { |
| 150 | // Find the change-cause of revision r |
| 151 | changeCause := historyInfo[r].Annotations[ChangeCauseAnnotation] |
| 152 | if len(changeCause) == 0 { |
| 153 | changeCause = "<none>" |
| 154 | } |
| 155 | fmt.Fprintf(out, "%d\t%s\n", r, changeCause) |
| 156 | } |
| 157 | return nil |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | // GetHistory returns the ReplicaSet revisions associated with a Deployment |