github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/cmd/helm/upgrade.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"io/ioutil"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"k8s.io/helm/pkg/helm"
    28  )
    29  
    30  const upgradeDesc = `
    31  This command upgrades a release to a new version of a chart.
    32  
    33  The upgrade arguments must be a release and a chart. The chart
    34  argument can be a relative path to a packaged or unpackaged chart.
    35  
    36  To override values in a chart, use either the '--values' flag and pass in a file
    37  or use the '--set' flag and pass configuration from the command line.
    38  `
    39  
    40  type upgradeCmd struct {
    41  	release      string
    42  	chart        string
    43  	out          io.Writer
    44  	client       helm.Interface
    45  	dryRun       bool
    46  	disableHooks bool
    47  	valuesFile   string
    48  	values       *values
    49  	verify       bool
    50  	keyring      string
    51  }
    52  
    53  func newUpgradeCmd(client helm.Interface, out io.Writer) *cobra.Command {
    54  
    55  	upgrade := &upgradeCmd{
    56  		out:    out,
    57  		client: client,
    58  		values: new(values),
    59  	}
    60  
    61  	cmd := &cobra.Command{
    62  		Use:               "upgrade [RELEASE] [CHART]",
    63  		Short:             "upgrade a release",
    64  		Long:              upgradeDesc,
    65  		PersistentPreRunE: setupConnection,
    66  		RunE: func(cmd *cobra.Command, args []string) error {
    67  			if err := checkArgsLength(2, len(args), "release name, chart path"); err != nil {
    68  				return err
    69  			}
    70  
    71  			upgrade.release = args[0]
    72  			upgrade.chart = args[1]
    73  			upgrade.client = ensureHelmClient(upgrade.client)
    74  
    75  			return upgrade.run()
    76  		},
    77  	}
    78  
    79  	f := cmd.Flags()
    80  	f.StringVarP(&upgrade.valuesFile, "values", "f", "", "path to a values YAML file")
    81  	f.BoolVar(&upgrade.dryRun, "dry-run", false, "simulate an upgrade")
    82  	f.Var(upgrade.values, "set", "set values on the command line. Separate values with commas: key1=val1,key2=val2")
    83  	f.BoolVar(&upgrade.disableHooks, "disable-hooks", false, "disable pre/post upgrade hooks")
    84  	f.BoolVar(&upgrade.verify, "verify", false, "verify the provenance of the chart before upgrading")
    85  	f.StringVar(&upgrade.keyring, "keyring", defaultKeyring(), "the path to the keyring that contains public singing keys")
    86  
    87  	return cmd
    88  }
    89  
    90  func (u *upgradeCmd) vals() ([]byte, error) {
    91  	var buffer bytes.Buffer
    92  
    93  	// User specified a values file via -f/--values
    94  	if u.valuesFile != "" {
    95  		bytes, err := ioutil.ReadFile(u.valuesFile)
    96  		if err != nil {
    97  			return []byte{}, err
    98  		}
    99  		buffer.Write(bytes)
   100  	}
   101  
   102  	// User specified value pairs via --set
   103  	// These override any values in the specified file
   104  	if len(u.values.pairs) > 0 {
   105  		bytes, err := u.values.yaml()
   106  		if err != nil {
   107  			return []byte{}, err
   108  		}
   109  		buffer.Write(bytes)
   110  	}
   111  
   112  	return buffer.Bytes(), nil
   113  }
   114  
   115  func (u *upgradeCmd) run() error {
   116  	chartPath, err := locateChartPath(u.chart, u.verify, u.keyring)
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	rawVals, err := u.vals()
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	_, err = u.client.UpdateRelease(u.release, chartPath, helm.UpdateValueOverrides(rawVals), helm.UpgradeDryRun(u.dryRun), helm.UpgradeDisableHooks(u.disableHooks))
   127  	if err != nil {
   128  		return fmt.Errorf("UPGRADE FAILED: %v", prettyError(err))
   129  	}
   130  
   131  	success := u.release + " has been upgraded. Happy Helming!\n"
   132  	fmt.Fprintf(u.out, success)
   133  
   134  	// Print the status like status command does
   135  	status, err := u.client.ReleaseStatus(u.release)
   136  	if err != nil {
   137  		return prettyError(err)
   138  	}
   139  	PrintStatus(u.out, status)
   140  
   141  	return nil
   142  
   143  }