Skip to content

Commit

Permalink
Merge pull request helm#1248 from adamreese/feat/rollback-version
Browse files Browse the repository at this point in the history
feat(*): add rollback to a release version
  • Loading branch information
adamreese authored Sep 28, 2016
2 parents 47e425b + 1db7bd6 commit 6860c47
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 73 deletions.
3 changes: 2 additions & 1 deletion _proto/hapi/services/tiller.proto
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ message UpdateReleaseRequest {
hapi.chart.Config values = 3;
// dry_run, if true, will run through the release logic, but neither create
bool dry_run = 4;

// DisableHooks causes the server to skip running any hooks for the upgrade.
bool disable_hooks = 5;
}
Expand All @@ -200,6 +199,8 @@ message RollbackReleaseRequest {
bool dry_run = 2;
// DisableHooks causes the server to skip running any hooks for the rollback
bool disable_hooks = 3;
// Version is the version of the release to deploy.
int32 version = 4;
}

// RollbackReleaseResponse is the response to an update request.
Expand Down
4 changes: 2 additions & 2 deletions cmd/helm/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func newGetCmd(client helm.Interface, out io.Writer) *cobra.Command {
},
}

cmd.PersistentFlags().Int32Var(&get.version, "version", 0, "get the named release with version")
cmd.PersistentFlags().Int32Var(&get.version, "revision", 0, "get the named release with revision")

cmd.AddCommand(newGetValuesCmd(nil, out))
cmd.AddCommand(newGetManifestCmd(nil, out))
cmd.AddCommand(newGetHooksCmd(nil, out))
return cmd
}

var getTemplate = `VERSION: {{.Release.Version}}
var getTemplate = `REVISION: {{.Release.Version}}
RELEASED: {{.ReleaseDate}}
CHART: {{.Release.Chart.Metadata.Name}}-{{.Release.Chart.Metadata.Version}}
USER-SUPPLIED VALUES:
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestGetCmd(t *testing.T) {
name: "get with a release",
resp: releaseMock(&releaseOptions{name: "thomas-guide"}),
args: []string{"thomas-guide"},
expected: "VERSION: 1\nRELEASED: (.*)\nCHART: foo-0.1.0-beta.1\nUSER-SUPPLIED VALUES:\nname: \"value\"\nCOMPUTED VALUES:\nname: value\n\nHOOKS:\n---\n# pre-install-hook\n" + mockHookTemplate + "\nMANIFEST:",
expected: "REVISION: 1\nRELEASED: (.*)\nCHART: foo-0.1.0-beta.1\nUSER-SUPPLIED VALUES:\nname: \"value\"\nCOMPUTED VALUES:\nname: value\n\nHOOKS:\n---\n# pre-install-hook\n" + mockHookTemplate + "\nMANIFEST:",
},
{
name: "get requires release name arg",
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (l *listCmd) statusCodes() []release.Status_Code {
func formatList(rels []*release.Release) string {
table := uitable.New()
table.MaxColWidth = 30
table.AddRow("NAME", "VERSION", "UPDATED", "STATUS", "CHART")
table.AddRow("NAME", "REVISION", "UPDATED", "STATUS", "CHART")
for _, r := range rels {
c := fmt.Sprintf("%s-%s", r.Chart.Metadata.Name, r.Chart.Metadata.Version)
t := timeconv.String(r.Info.LastDeployed)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestListCmd(t *testing.T) {
resp: []*release.Release{
releaseMock(&releaseOptions{name: "atlas"}),
},
expected: "NAME \tVERSION\tUPDATED \tSTATUS \tCHART \natlas\t1 \t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1\n",
expected: "NAME \tREVISION\tUPDATED \tSTATUS \tCHART \natlas\t1 \t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1\n",
},
{
name: "with a release, multiple flags",
Expand Down
13 changes: 10 additions & 3 deletions cmd/helm/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
)

const rollbackDesc = `
This command rolls back a release to the previous version.
This command rolls back a release to the previous revision.
The argument of the rollback command is the name of a release.
`

type rollbackCmd struct {
name string
version int32
dryRun bool
disableHooks bool
out io.Writer
Expand All @@ -46,7 +47,7 @@ func newRollbackCmd(c helm.Interface, out io.Writer) *cobra.Command {

cmd := &cobra.Command{
Use: "rollback [RELEASE]",
Short: "roll back a release to the previous version",
Short: "roll back a release to a previous revision",
Long: rollbackDesc,
PersistentPreRunE: setupConnection,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -60,13 +61,19 @@ func newRollbackCmd(c helm.Interface, out io.Writer) *cobra.Command {
}

f := cmd.Flags()
f.Int32Var(&rollback.version, "revision", 0, "revision to deploy")
f.BoolVar(&rollback.dryRun, "dry-run", false, "simulate a rollback")
f.BoolVar(&rollback.disableHooks, "no-hooks", false, "prevent hooks from running during rollback")
return cmd
}

func (r *rollbackCmd) run() error {
_, err := r.client.RollbackRelease(r.name, helm.RollbackDryRun(r.dryRun), helm.RollbackDisableHooks(r.disableHooks))
_, err := r.client.RollbackRelease(
r.name,
helm.RollbackDryRun(r.dryRun),
helm.RollbackDisableHooks(r.disableHooks),
helm.RollbackVersion(r.version),
)
if err != nil {
return prettyError(err)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/helm/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func TestRollbackCmd(t *testing.T) {
{
name: "rollback a release",
args: []string{"funny-honey"},
resp: nil,
flags: []string{"revision", "1"},
expected: "Rollback was a success! Happy Helming!",
},
{
name: "rollback a release without version",
args: []string{"funny-honey"},
expected: "Rollback was a success! Happy Helming!",
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newStatusCmd(client helm.Interface, out io.Writer) *cobra.Command {
},
}

cmd.PersistentFlags().Int32Var(&status.version, "version", 0, "If set, display the status of the named release with version")
cmd.PersistentFlags().Int32Var(&status.version, "revision", 0, "If set, display the status of the named release with revision")

return cmd
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/tiller/release_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,17 @@ func (s *releaseServer) prepareRollback(req *services.RollbackReleaseRequest) (*
return nil, nil, err
}

previousRelease, err := s.env.Releases.Get(req.Name, currentRelease.Version-1)
v := req.Version
if v == 0 {
v = currentRelease.Version - 1
}
if v < 1 {
return nil, nil, errors.New("cannot rollback to version < 1")
}

log.Printf("rolling back %s to version %d", req.Name, v)

previousRelease, err := s.env.Releases.Get(req.Name, v)
if err != nil {
return nil, nil, err
}
Expand Down
21 changes: 21 additions & 0 deletions cmd/tiller/release_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,27 @@ func TestRollbackReleaseNoHooks(t *testing.T) {
}
}

func TestRollbackWithReleaseVersion(t *testing.T) {
c := helm.NewContext()
rs := rsFixture()
rel := releaseStub()
rs.env.Releases.Create(rel)
upgradedRel := upgradeReleaseVersion(rel)
rs.env.Releases.Update(rel)
rs.env.Releases.Create(upgradedRel)

req := &services.RollbackReleaseRequest{
Name: rel.Name,
DisableHooks: true,
Version: 1,
}

_, err := rs.RollbackRelease(c, req)
if err != nil {
t.Fatalf("Failed rollback: %s", err)
}
}

func TestRollbackRelease(t *testing.T) {
c := helm.NewContext()
rs := rsFixture()
Expand Down
9 changes: 8 additions & 1 deletion pkg/helm/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ func RollbackDryRun(dry bool) RollbackOption {
}
}

// RollbackVersion sets the version of the release to deploy.
func RollbackVersion(ver int32) RollbackOption {
return func(opts *options) {
opts.rollbackReq.Version = ver
}
}

// UpgradeDisableHooks will disable hooks for an upgrade operation.
func UpgradeDisableHooks(disable bool) UpdateOption {
return func(opts *options) {
Expand Down Expand Up @@ -333,7 +340,7 @@ func (o *options) rpcRollbackRelease(rlsName string, rlc rls.ReleaseServiceClien
o.rollbackReq.DryRun = o.dryRun
o.rollbackReq.Name = rlsName

return rlc.RollbackRelease(context.TODO(), &o.rollbackReq)
return rlc.RollbackRelease(NewContext(), &o.rollbackReq)
}

// Executes tiller.GetReleaseStatus RPC.
Expand Down
123 changes: 63 additions & 60 deletions pkg/proto/hapi/services/tiller.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6860c47

Please sign in to comment.