github.com/valdemarpavesi/helm@v2.9.1+incompatible/cmd/helm/rollback.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "io" 22 "strconv" 23 24 "github.com/spf13/cobra" 25 26 "k8s.io/helm/pkg/helm" 27 ) 28 29 const rollbackDesc = ` 30 This command rolls back a release to a previous revision. 31 32 The first argument of the rollback command is the name of a release, and the 33 second is a revision (version) number. To see revision numbers, run 34 'helm history RELEASE'. 35 ` 36 37 type rollbackCmd struct { 38 name string 39 revision int32 40 dryRun bool 41 recreate bool 42 force bool 43 disableHooks bool 44 out io.Writer 45 client helm.Interface 46 timeout int64 47 wait bool 48 } 49 50 func newRollbackCmd(c helm.Interface, out io.Writer) *cobra.Command { 51 rollback := &rollbackCmd{ 52 out: out, 53 client: c, 54 } 55 56 cmd := &cobra.Command{ 57 Use: "rollback [flags] [RELEASE] [REVISION]", 58 Short: "roll back a release to a previous revision", 59 Long: rollbackDesc, 60 PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() }, 61 RunE: func(cmd *cobra.Command, args []string) error { 62 if err := checkArgsLength(len(args), "release name", "revision number"); err != nil { 63 return err 64 } 65 66 rollback.name = args[0] 67 68 v64, err := strconv.ParseInt(args[1], 10, 32) 69 if err != nil { 70 return fmt.Errorf("invalid revision number '%q': %s", args[1], err) 71 } 72 73 rollback.revision = int32(v64) 74 rollback.client = ensureHelmClient(rollback.client) 75 return rollback.run() 76 }, 77 } 78 79 f := cmd.Flags() 80 f.BoolVar(&rollback.dryRun, "dry-run", false, "simulate a rollback") 81 f.BoolVar(&rollback.recreate, "recreate-pods", false, "performs pods restart for the resource if applicable") 82 f.BoolVar(&rollback.force, "force", false, "force resource update through delete/recreate if needed") 83 f.BoolVar(&rollback.disableHooks, "no-hooks", false, "prevent hooks from running during rollback") 84 f.Int64Var(&rollback.timeout, "timeout", 300, "time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)") 85 f.BoolVar(&rollback.wait, "wait", false, "if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment are in a ready state before marking the release as successful. It will wait for as long as --timeout") 86 87 return cmd 88 } 89 90 func (r *rollbackCmd) run() error { 91 _, err := r.client.RollbackRelease( 92 r.name, 93 helm.RollbackDryRun(r.dryRun), 94 helm.RollbackRecreate(r.recreate), 95 helm.RollbackForce(r.force), 96 helm.RollbackDisableHooks(r.disableHooks), 97 helm.RollbackVersion(r.revision), 98 helm.RollbackTimeout(r.timeout), 99 helm.RollbackWait(r.wait)) 100 if err != nil { 101 return prettyError(err) 102 } 103 104 fmt.Fprintf(r.out, "Rollback was a success! Happy Helming!\n") 105 106 return nil 107 }