// PrintRolloutHistory prints a formatted table of the input revision history of the deployment func PrintRolloutHistory(historyInfo HistoryInfo, resource, name string) (string, error) { if len(historyInfo.RevisionToTemplate) == 0 { return fmt.Sprintf("No rollout history found in %s %q", resource, name), nil } // Sort the revisionToChangeCause map by revision revisions := make([]int64, 0, len(historyInfo.RevisionToTemplate)) for r := range historyInfo.RevisionToTemplate { revisions = append(revisions, r) } sliceutil.SortInts64(revisions) return tabbedString(func(out io.Writer) error { fmt.Fprintf(out, "%s %q:\n", resource, name) fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n") for _, r := range revisions { // Find the change-cause of revision r changeCause := historyInfo.RevisionToTemplate[r].Annotations[ChangeCauseAnnotation] if len(changeCause) == 0 { changeCause = "<none>" } fmt.Fprintf(out, "%d\t%s\n", r, changeCause) } return nil }) }
func simpleDryRun(deployment *extensions.Deployment, c clientset.Interface, toRevision int64) (string, error) { externalDeployment := &externalextensions.Deployment{} if err := api.Scheme.Convert(deployment, externalDeployment, nil); err != nil { return "", fmt.Errorf("failed to convert deployment, %v", err) } versionedClient := versionedClientsetForDeployment(c) _, allOldRSs, newRS, err := deploymentutil.GetAllReplicaSets(externalDeployment, versionedClient) if err != nil { return "", fmt.Errorf("failed to retrieve replica sets from deployment %s: %v", deployment.Name, err) } allRSs := allOldRSs if newRS != nil { allRSs = append(allRSs, newRS) } revisionToSpec := make(map[int64]*v1.PodTemplateSpec) for _, rs := range allRSs { v, err := deploymentutil.Revision(rs) if err != nil { continue } revisionToSpec[v] = &rs.Spec.Template } if len(revisionToSpec) == 0 { return "No rollout history found.", nil } if toRevision > 0 { template, ok := revisionToSpec[toRevision] if !ok { return "", fmt.Errorf("unable to find specified revision") } buf := bytes.NewBuffer([]byte{}) internalTemplate := &api.PodTemplateSpec{} if err := v1.Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(template, internalTemplate, nil); err != nil { return "", fmt.Errorf("failed to convert podtemplate, %v", err) } DescribePodTemplate(internalTemplate, buf) return buf.String(), nil } // Sort the revisionToSpec map by revision revisions := make([]int64, 0, len(revisionToSpec)) for r := range revisionToSpec { revisions = append(revisions, r) } sliceutil.SortInts64(revisions) template, _ := revisionToSpec[revisions[len(revisions)-1]] buf := bytes.NewBuffer([]byte{}) buf.WriteString("\n") internalTemplate := &api.PodTemplateSpec{} if err := v1.Convert_v1_PodTemplateSpec_To_api_PodTemplateSpec(template, internalTemplate, nil); err != nil { return "", fmt.Errorf("failed to convert podtemplate, %v", err) } DescribePodTemplate(internalTemplate, buf) return buf.String(), nil }
// ViewHistory returns a revision-to-replicaset map as the revision history of a deployment func (h *DeploymentHistoryViewer) ViewHistory(namespace, name string, revision int64) (string, error) { deployment, err := h.c.Extensions().Deployments(namespace).Get(name) if err != nil { return "", fmt.Errorf("failed to retrieve deployment %s: %v", name, err) } _, allOldRSs, newRS, err := deploymentutil.GetAllReplicaSets(deployment, h.c) if err != nil { return "", fmt.Errorf("failed to retrieve replica sets from deployment %s: %v", name, err) } allRSs := allOldRSs if newRS != nil { allRSs = append(allRSs, newRS) } historyInfo := make(map[int64]*api.PodTemplateSpec) for _, rs := range allRSs { v, err := deploymentutil.Revision(rs) if err != nil { continue } historyInfo[v] = &rs.Spec.Template changeCause := getChangeCause(rs) if historyInfo[v].Annotations == nil { historyInfo[v].Annotations = make(map[string]string) } if len(changeCause) > 0 { historyInfo[v].Annotations[ChangeCauseAnnotation] = changeCause } } if len(historyInfo) == 0 { return "No rollout history found.", nil } if revision > 0 { // Print details of a specific revision template, ok := historyInfo[revision] if !ok { return "", fmt.Errorf("unable to find the specified revision") } buf := bytes.NewBuffer([]byte{}) DescribePodTemplate(template, buf) return buf.String(), nil } // Sort the revisionToChangeCause map by revision revisions := make([]int64, 0, len(historyInfo)) for r := range historyInfo { revisions = append(revisions, r) } sliceutil.SortInts64(revisions) return tabbedString(func(out io.Writer) error { fmt.Fprintf(out, "REVISION\tCHANGE-CAUSE\n") for _, r := range revisions { // Find the change-cause of revision r changeCause := historyInfo[r].Annotations[ChangeCauseAnnotation] if len(changeCause) == 0 { changeCause = "<none>" } fmt.Fprintf(out, "%d\t%s\n", r, changeCause) } return nil }) }