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