github.com/kvendingoldo/helm@v3.0.0-beta.3+incompatible/cmd/helm/upgrade.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  	"time"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/spf13/cobra"
    26  
    27  	"helm.sh/helm/cmd/helm/require"
    28  	"helm.sh/helm/pkg/action"
    29  	"helm.sh/helm/pkg/chart/loader"
    30  	"helm.sh/helm/pkg/cli/values"
    31  	"helm.sh/helm/pkg/getter"
    32  	"helm.sh/helm/pkg/storage/driver"
    33  )
    34  
    35  const upgradeDesc = `
    36  This command upgrades a release to a new version of a chart.
    37  
    38  The upgrade arguments must be a release and chart. The chart
    39  argument can be either: a chart reference('example/mariadb'), a path to a chart directory,
    40  a packaged chart, or a fully qualified URL. For chart references, the latest
    41  version will be specified unless the '--version' flag is set.
    42  
    43  To override values in a chart, use either the '--values' flag and pass in a file
    44  or use the '--set' flag and pass configuration from the command line, to force string
    45  values, use '--set-string'.
    46  
    47  You can specify the '--values'/'-f' flag multiple times. The priority will be given to the
    48  last (right-most) file specified. For example, if both myvalues.yaml and override.yaml
    49  contained a key called 'Test', the value set in override.yaml would take precedence:
    50  
    51  	$ helm upgrade -f myvalues.yaml -f override.yaml redis ./redis
    52  
    53  You can specify the '--set' flag multiple times. The priority will be given to the
    54  last (right-most) set specified. For example, if both 'bar' and 'newbar' values are
    55  set for a key called 'foo', the 'newbar' value would take precedence:
    56  
    57  	$ helm upgrade --set foo=bar --set foo=newbar redis ./redis
    58  `
    59  
    60  func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
    61  	client := action.NewUpgrade(cfg)
    62  	valueOpts := &values.Options{}
    63  
    64  	cmd := &cobra.Command{
    65  		Use:   "upgrade [RELEASE] [CHART]",
    66  		Short: "upgrade a release",
    67  		Long:  upgradeDesc,
    68  		Args:  require.ExactArgs(2),
    69  		RunE: func(cmd *cobra.Command, args []string) error {
    70  			client.Namespace = getNamespace()
    71  
    72  			if client.Version == "" && client.Devel {
    73  				debug("setting version to >0.0.0-0")
    74  				client.Version = ">0.0.0-0"
    75  			}
    76  
    77  			vals, err := valueOpts.MergeValues(getter.All(settings))
    78  			if err != nil {
    79  				return err
    80  			}
    81  
    82  			chartPath, err := client.ChartPathOptions.LocateChart(args[1], settings)
    83  			if err != nil {
    84  				return err
    85  			}
    86  
    87  			if client.Install {
    88  				// If a release does not exist, install it. If another error occurs during
    89  				// the check, ignore the error and continue with the upgrade.
    90  				histClient := action.NewHistory(cfg)
    91  				histClient.Max = 1
    92  				if _, err := histClient.Run(args[0]); err == driver.ErrReleaseNotFound {
    93  					fmt.Fprintf(out, "Release %q does not exist. Installing it now.\n", args[0])
    94  					instClient := action.NewInstall(cfg)
    95  					instClient.ChartPathOptions = client.ChartPathOptions
    96  					instClient.DryRun = client.DryRun
    97  					instClient.DisableHooks = client.DisableHooks
    98  					instClient.Timeout = client.Timeout
    99  					instClient.Wait = client.Wait
   100  					instClient.Devel = client.Devel
   101  					instClient.Namespace = client.Namespace
   102  					instClient.Atomic = client.Atomic
   103  
   104  					rel, err := runInstall(args, instClient, valueOpts, out)
   105  					action.PrintRelease(out, rel)
   106  					return err
   107  				}
   108  			}
   109  
   110  			// Check chart dependencies to make sure all are present in /charts
   111  			ch, err := loader.Load(chartPath)
   112  			if err != nil {
   113  				return err
   114  			}
   115  			if req := ch.Metadata.Dependencies; req != nil {
   116  				if err := action.CheckDependencies(ch, req); err != nil {
   117  					return err
   118  				}
   119  			}
   120  
   121  			resp, err := client.Run(args[0], ch, vals)
   122  			if err != nil {
   123  				return errors.Wrap(err, "UPGRADE FAILED")
   124  			}
   125  
   126  			if settings.Debug {
   127  				action.PrintRelease(out, resp)
   128  			}
   129  
   130  			fmt.Fprintf(out, "Release %q has been upgraded. Happy Helming!\n", args[0])
   131  
   132  			// Print the status like status command does
   133  			statusClient := action.NewStatus(cfg)
   134  			rel, err := statusClient.Run(args[0])
   135  			if err != nil {
   136  				return err
   137  			}
   138  			action.PrintRelease(out, rel)
   139  
   140  			return nil
   141  		},
   142  	}
   143  
   144  	f := cmd.Flags()
   145  	f.BoolVarP(&client.Install, "install", "i", false, "if a release by this name doesn't already exist, run an install")
   146  	f.BoolVar(&client.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")
   147  	f.BoolVar(&client.DryRun, "dry-run", false, "simulate an upgrade")
   148  	f.BoolVar(&client.Recreate, "recreate-pods", false, "performs pods restart for the resource if applicable")
   149  	f.BoolVar(&client.Force, "force", false, "force resource update through delete/recreate if needed")
   150  	f.BoolVar(&client.DisableHooks, "no-hooks", false, "disable pre/post upgrade hooks")
   151  	f.DurationVar(&client.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
   152  	f.BoolVar(&client.ResetValues, "reset-values", false, "when upgrading, reset the values to the ones built into the chart")
   153  	f.BoolVar(&client.ReuseValues, "reuse-values", false, "when upgrading, reuse the last release's values and merge in any overrides from the command line via --set and -f. If '--reset-values' is specified, this is ignored.")
   154  	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")
   155  	f.BoolVar(&client.Atomic, "atomic", false, "if set, upgrade process rolls back changes made in case of failed upgrade. The --wait flag will be set automatically if --atomic is used")
   156  	f.IntVar(&client.MaxHistory, "history-max", 0, "limit the maximum number of revisions saved per release. Use 0 for no limit.")
   157  	addChartPathOptionsFlags(f, &client.ChartPathOptions)
   158  	addValueOptionsFlags(f, valueOpts)
   159  
   160  	return cmd
   161  }