mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
331 lines
9.0 KiB
331 lines
9.0 KiB
/*
|
|
Copyright The Helm Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package action
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"helm.sh/helm/pkg/chart"
|
|
"helm.sh/helm/pkg/chartutil"
|
|
"helm.sh/helm/pkg/hooks"
|
|
"helm.sh/helm/pkg/kube"
|
|
"helm.sh/helm/pkg/release"
|
|
)
|
|
|
|
// Upgrade is the action for upgrading releases.
|
|
//
|
|
// It provides the implementation of 'helm upgrade'.
|
|
type Upgrade struct {
|
|
cfg *Configuration
|
|
|
|
ChartPathOptions
|
|
ValueOptions
|
|
|
|
Install bool
|
|
Devel bool
|
|
Namespace string
|
|
Timeout time.Duration
|
|
Wait bool
|
|
// Values is a string containing (unparsed) YAML values.
|
|
Values map[string]interface{}
|
|
DisableHooks bool
|
|
DryRun bool
|
|
Force bool
|
|
ResetValues bool
|
|
ReuseValues bool
|
|
// Recreate will (if true) recreate pods after a rollback.
|
|
Recreate bool
|
|
// MaxHistory limits the maximum number of revisions saved per release
|
|
MaxHistory int
|
|
}
|
|
|
|
// NewUpgrade creates a new Upgrade object with the given configuration.
|
|
func NewUpgrade(cfg *Configuration) *Upgrade {
|
|
return &Upgrade{
|
|
cfg: cfg,
|
|
}
|
|
}
|
|
|
|
// Run executes the upgrade on the given release.
|
|
func (u *Upgrade) Run(name string, chart *chart.Chart) (*release.Release, error) {
|
|
if err := chartutil.ProcessDependencies(chart, u.Values); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := validateReleaseName(name); err != nil {
|
|
return nil, errors.Errorf("upgradeRelease: Release name is invalid: %s", name)
|
|
}
|
|
u.cfg.Log("preparing upgrade for %s", name)
|
|
currentRelease, upgradedRelease, err := u.prepareUpgrade(name, chart)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
u.cfg.Releases.MaxHistory = u.MaxHistory
|
|
|
|
if !u.DryRun {
|
|
u.cfg.Log("creating upgraded release for %s", name)
|
|
if err := u.cfg.Releases.Create(upgradedRelease); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
u.cfg.Log("performing update for %s", name)
|
|
res, err := u.performUpgrade(currentRelease, upgradedRelease)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
if !u.DryRun {
|
|
u.cfg.Log("updating status for upgraded release for %s", name)
|
|
if err := u.cfg.Releases.Update(upgradedRelease); err != nil {
|
|
return res, err
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func validateReleaseName(releaseName string) error {
|
|
if releaseName == "" {
|
|
return errMissingRelease
|
|
}
|
|
|
|
if !ValidName.MatchString(releaseName) || (len(releaseName) > releaseNameMaxLen) {
|
|
return errInvalidName
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// prepareUpgrade builds an upgraded release for an upgrade operation.
|
|
func (u *Upgrade) prepareUpgrade(name string, chart *chart.Chart) (*release.Release, *release.Release, error) {
|
|
if chart == nil {
|
|
return nil, nil, errMissingChart
|
|
}
|
|
|
|
// finds the deployed release with the given name
|
|
currentRelease, err := u.cfg.Releases.Deployed(name)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// determine if values will be reused
|
|
if err := u.reuseValues(chart, currentRelease); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// finds the non-deleted release with the given name
|
|
lastRelease, err := u.cfg.Releases.Last(name)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Increment revision count. This is passed to templates, and also stored on
|
|
// the release object.
|
|
revision := lastRelease.Version + 1
|
|
|
|
options := chartutil.ReleaseOptions{
|
|
Name: name,
|
|
IsUpgrade: true,
|
|
}
|
|
|
|
caps, err := u.cfg.getCapabilities()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
valuesToRender, err := chartutil.ToRenderValues(chart, u.Values, options, caps)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
hooks, manifestDoc, notesTxt, err := u.cfg.renderResources(chart, valuesToRender)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Store an upgraded release.
|
|
upgradedRelease := &release.Release{
|
|
Name: name,
|
|
Namespace: currentRelease.Namespace,
|
|
Chart: chart,
|
|
Config: u.Values,
|
|
Info: &release.Info{
|
|
FirstDeployed: currentRelease.Info.FirstDeployed,
|
|
LastDeployed: Timestamper(),
|
|
Status: release.StatusPendingUpgrade,
|
|
Description: "Preparing upgrade", // This should be overwritten later.
|
|
},
|
|
Version: revision,
|
|
Manifest: manifestDoc.String(),
|
|
Hooks: hooks,
|
|
}
|
|
|
|
if len(notesTxt) > 0 {
|
|
upgradedRelease.Info.Notes = notesTxt
|
|
}
|
|
err = validateManifest(u.cfg.KubeClient, manifestDoc.Bytes())
|
|
return currentRelease, upgradedRelease, err
|
|
}
|
|
|
|
func (u *Upgrade) performUpgrade(originalRelease, upgradedRelease *release.Release) (*release.Release, error) {
|
|
if u.DryRun {
|
|
u.cfg.Log("dry run for %s", upgradedRelease.Name)
|
|
upgradedRelease.Info.Description = "Dry run complete"
|
|
return upgradedRelease, nil
|
|
}
|
|
|
|
// pre-upgrade hooks
|
|
if !u.DisableHooks {
|
|
if err := u.execHook(upgradedRelease.Hooks, hooks.PreUpgrade); err != nil {
|
|
return upgradedRelease, err
|
|
}
|
|
} else {
|
|
u.cfg.Log("upgrade hooks disabled for %s", upgradedRelease.Name)
|
|
}
|
|
if err := u.upgradeRelease(originalRelease, upgradedRelease); err != nil {
|
|
msg := fmt.Sprintf("Upgrade %q failed: %s", upgradedRelease.Name, err)
|
|
u.cfg.Log("warning: %s", msg)
|
|
upgradedRelease.Info.Status = release.StatusFailed
|
|
upgradedRelease.Info.Description = msg
|
|
u.cfg.recordRelease(originalRelease)
|
|
u.cfg.recordRelease(upgradedRelease)
|
|
return upgradedRelease, err
|
|
}
|
|
|
|
// post-upgrade hooks
|
|
if !u.DisableHooks {
|
|
if err := u.execHook(upgradedRelease.Hooks, hooks.PostUpgrade); err != nil {
|
|
return upgradedRelease, err
|
|
}
|
|
}
|
|
|
|
originalRelease.Info.Status = release.StatusSuperseded
|
|
u.cfg.recordRelease(originalRelease)
|
|
|
|
upgradedRelease.Info.Status = release.StatusDeployed
|
|
upgradedRelease.Info.Description = "Upgrade complete"
|
|
|
|
return upgradedRelease, nil
|
|
}
|
|
|
|
// upgradeRelease performs an upgrade from current to target release
|
|
func (u *Upgrade) upgradeRelease(current, target *release.Release) error {
|
|
cm := bytes.NewBufferString(current.Manifest)
|
|
tm := bytes.NewBufferString(target.Manifest)
|
|
// TODO add wait
|
|
return u.cfg.KubeClient.Update(cm, tm, u.Force, u.Recreate)
|
|
}
|
|
|
|
// reuseValues copies values from the current release to a new release if the
|
|
// new release does not have any values.
|
|
//
|
|
// If the request already has values, or if there are no values in the current
|
|
// release, this does nothing.
|
|
//
|
|
// This is skipped if the u.ResetValues flag is set, in which case the
|
|
// request values are not altered.
|
|
func (u *Upgrade) reuseValues(chart *chart.Chart, current *release.Release) error {
|
|
if u.ResetValues {
|
|
// If ResetValues is set, we comletely ignore current.Config.
|
|
u.cfg.Log("resetting values to the chart's original version")
|
|
return nil
|
|
}
|
|
|
|
// If the ReuseValues flag is set, we always copy the old values over the new config's values.
|
|
if u.ReuseValues {
|
|
u.cfg.Log("reusing the old release's values")
|
|
|
|
// We have to regenerate the old coalesced values:
|
|
oldVals, err := chartutil.CoalesceValues(current.Chart, current.Config)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to rebuild old values")
|
|
}
|
|
|
|
u.Values = chartutil.CoalesceTables(current.Config, u.Values)
|
|
|
|
chart.Values = oldVals
|
|
|
|
return nil
|
|
}
|
|
|
|
if len(u.Values) == 0 && len(current.Config) > 0 {
|
|
u.cfg.Log("copying values from %s (v%d) to new release.", current.Name, current.Version)
|
|
u.Values = current.Config
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateManifest(c kube.KubernetesClient, manifest []byte) error {
|
|
_, err := c.BuildUnstructured(bytes.NewReader(manifest))
|
|
return err
|
|
}
|
|
|
|
// execHook executes all of the hooks for the given hook event.
|
|
func (u *Upgrade) execHook(hs []*release.Hook, hook string) error {
|
|
timeout := u.Timeout
|
|
executingHooks := []*release.Hook{}
|
|
|
|
for _, h := range hs {
|
|
for _, e := range h.Events {
|
|
if string(e) == hook {
|
|
executingHooks = append(executingHooks, h)
|
|
}
|
|
}
|
|
}
|
|
|
|
sort.Sort(hookByWeight(executingHooks))
|
|
|
|
for _, h := range executingHooks {
|
|
if err := deleteHookByPolicy(u.cfg, h, hooks.BeforeHookCreation); err != nil {
|
|
return err
|
|
}
|
|
|
|
b := bytes.NewBufferString(h.Manifest)
|
|
if err := u.cfg.KubeClient.Create(b); err != nil {
|
|
return errors.Wrapf(err, "warning: Hook %s %s failed", hook, h.Path)
|
|
}
|
|
b.Reset()
|
|
b.WriteString(h.Manifest)
|
|
|
|
if err := u.cfg.KubeClient.WatchUntilReady(b, timeout); err != nil {
|
|
// If a hook is failed, checkout the annotation of the hook to determine whether the hook should be deleted
|
|
// under failed condition. If so, then clear the corresponding resource object in the hook
|
|
if err := deleteHookByPolicy(u.cfg, h, hooks.HookFailed); err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
// If all hooks are succeeded, checkout the annotation of each hook to determine whether the hook should be deleted
|
|
// under succeeded condition. If so, then clear the corresponding resource object in each hook
|
|
for _, h := range executingHooks {
|
|
if err := deleteHookByPolicy(u.cfg, h, hooks.HookSucceeded); err != nil {
|
|
return err
|
|
}
|
|
h.LastRun = time.Now()
|
|
}
|
|
|
|
return nil
|
|
}
|