github.com/Beeketing/helm@v2.12.1+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'.
    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  	description  string
    49  }
    50  
    51  func newRollbackCmd(c helm.Interface, out io.Writer) *cobra.Command {
    52  	rollback := &rollbackCmd{
    53  		out:    out,
    54  		client: c,
    55  	}
    56  
    57  	cmd := &cobra.Command{
    58  		Use:     "rollback [flags] [RELEASE] [REVISION]",
    59  		Short:   "roll back a release to a previous revision",
    60  		Long:    rollbackDesc,
    61  		PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() },
    62  		RunE: func(cmd *cobra.Command, args []string) error {
    63  			if err := checkArgsLength(len(args), "release name", "revision number"); err != nil {
    64  				return err
    65  			}
    66  
    67  			rollback.name = args[0]
    68  
    69  			v64, err := strconv.ParseInt(args[1], 10, 32)
    70  			if err != nil {
    71  				return fmt.Errorf("invalid revision number '%q': %s", args[1], err)
    72  			}
    73  
    74  			rollback.revision = int32(v64)
    75  			rollback.client = ensureHelmClient(rollback.client)
    76  			return rollback.run()
    77  		},
    78  	}
    79  
    80  	f := cmd.Flags()
    81  	settings.AddFlagsTLS(f)
    82  	f.BoolVar(&rollback.dryRun, "dry-run", false, "simulate a rollback")
    83  	f.BoolVar(&rollback.recreate, "recreate-pods", false, "performs pods restart for the resource if applicable")
    84  	f.BoolVar(&rollback.force, "force", false, "force resource update through delete/recreate if needed")
    85  	f.BoolVar(&rollback.disableHooks, "no-hooks", false, "prevent hooks from running during rollback")
    86  	f.Int64Var(&rollback.timeout, "timeout", 300, "time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)")
    87  	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")
    88  	f.StringVar(&rollback.description, "description", "", "specify a description for the release")
    89  
    90  	// set defaults from environment
    91  	settings.InitTLS(f)
    92  
    93  	return cmd
    94  }
    95  
    96  func (r *rollbackCmd) run() error {
    97  	_, err := r.client.RollbackRelease(
    98  		r.name,
    99  		helm.RollbackDryRun(r.dryRun),
   100  		helm.RollbackRecreate(r.recreate),
   101  		helm.RollbackForce(r.force),
   102  		helm.RollbackDisableHooks(r.disableHooks),
   103  		helm.RollbackVersion(r.revision),
   104  		helm.RollbackTimeout(r.timeout),
   105  		helm.RollbackWait(r.wait),
   106  		helm.RollbackDescription(r.description))
   107  	if err != nil {
   108  		return prettyError(err)
   109  	}
   110  
   111  	fmt.Fprintf(r.out, "Rollback was a success! Happy Helming!\n")
   112  
   113  	return nil
   114  }