Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubectl rollback: remove duplicated code for printing pod template #69543

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 8 additions & 28 deletions pkg/kubectl/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ import (
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
api "k8s.io/kubernetes/pkg/apis/core"
apiv1 "k8s.io/kubernetes/pkg/apis/core/v1"
kapps "k8s.io/kubernetes/pkg/kubectl/apps"
"k8s.io/kubernetes/pkg/kubectl/scheme"
sliceutil "k8s.io/kubernetes/pkg/kubectl/util/slice"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop empty line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe not ...

// kubectl should not be taking dependencies on logic in the controllers
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
)
Expand Down Expand Up @@ -229,14 +227,7 @@ func simpleDryRun(deployment *appsv1.Deployment, c kubernetes.Interface, toRevis
if !ok {
return "", revisionNotFoundErr(toRevision)
}
buf := bytes.NewBuffer([]byte{})
internalTemplate := &api.PodTemplateSpec{}
if err := apiv1.Convert_v1_PodTemplateSpec_To_core_PodTemplateSpec(template, internalTemplate, nil); err != nil {
return "", fmt.Errorf("failed to convert podtemplate, %v", err)
}
w := printersinternal.NewPrefixWriter(buf)
printersinternal.DescribePodTemplate(internalTemplate, w)
return buf.String(), nil
return printTemplate(template)
}

// Sort the revisionToSpec map by revision
Expand All @@ -247,15 +238,7 @@ func simpleDryRun(deployment *appsv1.Deployment, c kubernetes.Interface, toRevis
sliceutil.SortInts64(revisions)

template, _ := revisionToSpec[revisions[len(revisions)-2]]
buf := bytes.NewBuffer([]byte{})
buf.WriteString("\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has an extra \n. I don't think it matters ...

internalTemplate := &api.PodTemplateSpec{}
if err := apiv1.Convert_v1_PodTemplateSpec_To_core_PodTemplateSpec(template, internalTemplate, nil); err != nil {
return "", fmt.Errorf("failed to convert podtemplate, %v", err)
}
w := printersinternal.NewPrefixWriter(buf)
printersinternal.DescribePodTemplate(internalTemplate, w)
return buf.String(), nil
return printTemplate(template)
}

type DaemonSetRollbacker struct {
Expand Down Expand Up @@ -473,14 +456,11 @@ func findHistory(toRevision int64, allHistory []*appsv1.ControllerRevision) *app

// printPodTemplate converts a given pod template into a human-readable string.
func printPodTemplate(specTemplate *corev1.PodTemplateSpec) (string, error) {
content := bytes.NewBuffer([]byte{})
w := printersinternal.NewPrefixWriter(content)
internalTemplate := &api.PodTemplateSpec{}
if err := apiv1.Convert_v1_PodTemplateSpec_To_core_PodTemplateSpec(specTemplate, internalTemplate, nil); err != nil {
return "", fmt.Errorf("failed to convert podtemplate while printing: %v", err)
}
printersinternal.DescribePodTemplate(internalTemplate, w)
return fmt.Sprintf("will roll back to %s", content.String()), nil
podSpec, err := printTemplate(specTemplate)
if err != nil {
return "", err
}
return fmt.Sprintf("will roll back to %s", podSpec), nil
}

func revisionNotFoundErr(r int64) error {
Expand Down