github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"strconv"
    23  	"time"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"helm.sh/helm/v3/cmd/helm/require"
    28  	"helm.sh/helm/v3/pkg/action"
    29  )
    30  
    31  const rollbackDesc = `
    32  This command rolls back a release to a previous revision.
    33  
    34  The first argument of the rollback command is the name of a release, and the
    35  second is a revision (version) number. If this argument is omitted, it will
    36  roll back to the previous release.
    37  
    38  To see revision numbers, run 'helm history RELEASE'.
    39  `
    40  
    41  func newRollbackCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    42  	client := action.NewRollback(cfg)
    43  
    44  	cmd := &cobra.Command{
    45  		Use:   "rollback <RELEASE> [REVISION]",
    46  		Short: "roll back a release to a previous revision",
    47  		Long:  rollbackDesc,
    48  		Args:  require.MinimumNArgs(1),
    49  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    50  			if len(args) == 0 {
    51  				return compListReleases(toComplete, args, cfg)
    52  			}
    53  
    54  			if len(args) == 1 {
    55  				return compListRevisions(toComplete, cfg, args[0])
    56  			}
    57  
    58  			return nil, cobra.ShellCompDirectiveNoFileComp
    59  		},
    60  		RunE: func(cmd *cobra.Command, args []string) error {
    61  			if len(args) > 1 {
    62  				ver, err := strconv.Atoi(args[1])
    63  				if err != nil {
    64  					return fmt.Errorf("could not convert revision to a number: %v", err)
    65  				}
    66  				client.Version = ver
    67  			}
    68  
    69  			if err := client.Run(args[0]); err != nil {
    70  				return err
    71  			}
    72  
    73  			fmt.Fprintf(out, "Rollback was a success! Happy Helming!\n")
    74  			return nil
    75  		},
    76  	}
    77  
    78  	f := cmd.Flags()
    79  	f.BoolVar(&client.DryRun, "dry-run", false, "simulate a rollback")
    80  	f.BoolVar(&client.Recreate, "recreate-pods", false, "performs pods restart for the resource if applicable")
    81  	f.BoolVar(&client.Force, "force", false, "force resource update through delete/recreate if needed")
    82  	f.BoolVar(&client.DisableHooks, "no-hooks", false, "prevent hooks from running during rollback")
    83  	f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
    84  	f.BoolVar(&client.Wait, "wait", false, "if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment, StatefulSet, or ReplicaSet are in a ready state before marking the release as successful. It will wait for as long as --timeout")
    85  	f.BoolVar(&client.WaitForJobs, "wait-for-jobs", false, "if set and --wait enabled, will wait until all Jobs have been completed before marking the release as successful. It will wait for as long as --timeout")
    86  	f.BoolVar(&client.CleanupOnFail, "cleanup-on-fail", false, "allow deletion of new resources created in this rollback when rollback fails")
    87  	f.IntVar(&client.MaxHistory, "history-max", settings.MaxHistory, "limit the maximum number of revisions saved per release. Use 0 for no limit")
    88  
    89  	return cmd
    90  }