Skip to content

Commit

Permalink
Auto pick release name from chart
Browse files Browse the repository at this point in the history
Picks the name from the chart based of Annotations then
falls back to the default name in the chart.yaml
This allows us to only pass the chart path to upgrade.
If this is called on a new chart install also works as
expected by setting the chartname before calling install

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Mar 5, 2021
1 parent 0b19606 commit b72f54f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
66 changes: 34 additions & 32 deletions cmd/hypper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/Masterminds/log-go"
logio "github.com/Masterminds/log-go/io"
"github.com/rancher-sandbox/hypper/pkg/eyecandy"

"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -69,22 +68,50 @@ func newUpgradeCmd(cfg *action.Configuration, logger log.Logger) *cobra.Command
var createNamespace bool

cmd := &cobra.Command{
Use: "upgrade [RELEASE] [CHART]",
Use: "upgrade [CHART]",
Short: "upgrade a release",
Long: upgradeDesc,
Args: require.ExactArgs(2),
Args: require.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client.Namespace = settings.Namespace()

wInfo := logio.NewWriter(logger, log.InfoLevel)

chartPath, err := client.ChartPathOptions.LocateChart(args[0], settings.EnvSettings)
if err != nil {
return err
}

vals, err := valueOpts.MergeValues(getter.All(settings.EnvSettings))
if err != nil {
return err
}

// Check chart dependencies to make sure all are present in /charts
ch, err := loader.Load(chartPath)
if err != nil {
return err
}
if req := ch.Metadata.Dependencies; req != nil {
if err := action.CheckDependencies(ch, req); err != nil {
return err
}
}

if ch.Metadata.Deprecated {
logger.Warn(eyecandy.ESPrint(settings.NoEmojis, "This chart has been deprecated :exclamation:"))

}

chartName, _ := client.Name(ch, args)

// Fixes #7002 - Support reading values from STDIN for `upgrade` command
// Must load values AFTER determining if we have to call install so that values loaded from stdin are are not read twice
if client.Install {
// If a release does not exist, install it.
histClient := action.NewHistory(cfg)
histClient.Max = 1
if _, err := histClient.Run(args[0]); err == driver.ErrReleaseNotFound {
if _, err := histClient.Run(chartName); err == driver.ErrReleaseNotFound {
// Only print this to stdout for table output
if outfmt == output.Table {
logger.Info(eyecandy.ESPrintf(settings.NoEmojis, "Release %q does not exist. Installing it now. :toolbox:\n", args[0]))
Expand All @@ -105,6 +132,7 @@ func newUpgradeCmd(cfg *action.Configuration, logger log.Logger) *cobra.Command
instClient.DisableOpenAPIValidation = client.DisableOpenAPIValidation
instClient.SubNotes = client.SubNotes
instClient.Description = client.Description
instClient.ReleaseName = chartName

rel, err := runInstall(args, instClient, valueOpts, logger)
if err != nil {
Expand All @@ -121,39 +149,13 @@ func newUpgradeCmd(cfg *action.Configuration, logger log.Logger) *cobra.Command
client.Version = ">0.0.0-0"
}

chartPath, err := client.ChartPathOptions.LocateChart(args[1], settings.EnvSettings)
if err != nil {
return err
}

vals, err := valueOpts.MergeValues(getter.All(settings.EnvSettings))
if err != nil {
return err
}

// Check chart dependencies to make sure all are present in /charts
ch, err := loader.Load(chartPath)
if err != nil {
return err
}
if req := ch.Metadata.Dependencies; req != nil {
if err := action.CheckDependencies(ch, req); err != nil {
return err
}
}

if ch.Metadata.Deprecated {
logger.Warn(eyecandy.ESPrint(settings.NoEmojis, "This chart has been deprecated :exclamation:"))

}

rel, err := client.Run(args[0], ch, vals)
rel, err := client.Run(chartName, ch, vals)
if err != nil {
return errors.Wrap(err, "UPGRADE FAILED")
}

if outfmt == output.Table {
logger.Info(eyecandy.ESPrintf(settings.NoEmojis, "Release %q has been upgraded :partying_face:", args[0]))
logger.Info(eyecandy.ESPrintf(settings.NoEmojis, "Release %q has been upgraded :partying_face:", chartName))
}

return outfmt.Write(wInfo, &statusPrinter{rel, settings.Debug, false})
Expand Down
24 changes: 24 additions & 0 deletions pkg/action/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package action

import (
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"strings"
)

type Upgrade struct {
Expand All @@ -16,3 +19,24 @@ func NewUpgrade(cfg *Configuration) *Upgrade {
cfg: cfg,
}
}

func (i *Upgrade) Name(chart *chart.Chart, args []string) (string, error) {
// args here will only be: [CHART]
// cobra flags have been already stripped

if len(args) > 2 {
return args[0], errors.Errorf("expected at most two arguments, unexpected arguments: %v", strings.Join(args[2:], ", "))
}

if chart.Metadata.Annotations != nil {
if val, ok := chart.Metadata.Annotations["hypper.cattle.io/release-name"]; ok {
return val, nil
}
if val, ok := chart.Metadata.Annotations["catalog.cattle.io/release-name"]; ok {
return val, nil
}
}

// If we dont have our annotations then return the base name
return chart.Metadata.Name, nil
}

0 comments on commit b72f54f

Please sign in to comment.