github.com/sdbaiguanghe/helm@v2.16.7+incompatible/cmd/helm/rollback.go (about) 1 /* 2 Copyright The Helm Authors. 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'. If you'd like to rollback to the previous release use 35 'helm rollback [RELEASE] 0'. 36 ` 37 38 type rollbackCmd struct { 39 name string 40 revision int32 41 dryRun bool 42 recreate bool 43 force bool 44 disableHooks bool 45 out io.Writer 46 client helm.Interface 47 timeout int64 48 wait bool 49 description string 50 cleanupOnFail bool 51 } 52 53 func newRollbackCmd(c helm.Interface, out io.Writer) *cobra.Command { 54 rollback := &rollbackCmd{ 55 out: out, 56 client: c, 57 } 58 59 cmd := &cobra.Command{ 60 Use: "rollback [flags] [RELEASE] [REVISION]", 61 Short: "Rollback a release to a previous revision", 62 Long: rollbackDesc, 63 PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() }, 64 RunE: func(cmd *cobra.Command, args []string) error { 65 if err := checkArgsLength(len(args), "release name", "revision number"); err != nil { 66 return err 67 } 68 69 rollback.name = args[0] 70 71 v64, err := strconv.ParseInt(args[1], 10, 32) 72 if err != nil { 73 return fmt.Errorf("invalid revision number '%q': %s", args[1], err) 74 } 75 76 rollback.revision = int32(v64) 77 rollback.client = ensureHelmClient(rollback.client) 78 return rollback.run() 79 }, 80 } 81 82 f := cmd.Flags() 83 settings.AddFlagsTLS(f) 84 f.BoolVar(&rollback.dryRun, "dry-run", false, "Simulate a rollback") 85 f.BoolVar(&rollback.recreate, "recreate-pods", false, "Performs pods restart for the resource if applicable") 86 f.BoolVar(&rollback.force, "force", false, "Force resource update through delete/recreate if needed") 87 f.BoolVar(&rollback.disableHooks, "no-hooks", false, "Prevent hooks from running during rollback") 88 f.Int64Var(&rollback.timeout, "timeout", 300, "Time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)") 89 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") 90 f.StringVar(&rollback.description, "description", "", "Specify a description for the release") 91 f.BoolVar(&rollback.cleanupOnFail, "cleanup-on-fail", false, "Allow deletion of new resources created in this rollback when rollback failed") 92 93 // set defaults from environment 94 settings.InitTLS(f) 95 96 return cmd 97 } 98 99 func (r *rollbackCmd) run() error { 100 _, err := r.client.RollbackRelease( 101 r.name, 102 helm.RollbackDryRun(r.dryRun), 103 helm.RollbackRecreate(r.recreate), 104 helm.RollbackForce(r.force), 105 helm.RollbackDisableHooks(r.disableHooks), 106 helm.RollbackVersion(r.revision), 107 helm.RollbackTimeout(r.timeout), 108 helm.RollbackWait(r.wait), 109 helm.RollbackDescription(r.description), 110 helm.RollbackCleanupOnFail(r.cleanupOnFail)) 111 if err != nil { 112 return prettyError(err) 113 } 114 115 fmt.Fprintf(r.out, "Rollback was a success.\n") 116 117 return nil 118 }