Skip to content

Commit

Permalink
upgrade in separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Feb 13, 2019
1 parent 58a66b0 commit aa60f0f
Showing 1 changed file with 71 additions and 55 deletions.
126 changes: 71 additions & 55 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,67 +382,18 @@ func (t *Testing) LintChart(chart string, valuesFiles []string) TestResult {
return result
}

// InstallChart installs the specified chart into a namespace, waits for resources to become ready, and eventually
// uninstalls it and deletes the namespace again. InstallChart also tests in-place upgrades of charts from their
// previous revisions.
// InstallChart installs the specified chart into a new namespace, waits for resources to become ready, and eventually
// uninstalls it and deletes the namespace again.
func (t *Testing) InstallChart(chart string, valuesFiles []string) TestResult {
result := TestResult{Chart: chart}
var result TestResult

if t.config.Upgrade {
breakingChangeAllowed, reasons, err := t.checkBreakingChangeAllowed(chart)
if err != nil {
fmt.Printf("Error comparing chart versions for '%s'\n", chart)
result.Error = err
} else if breakingChangeAllowed {
for _, r := range reasons {
fmt.Println(errors.Wrap(r, fmt.Sprintf("Skipping upgrade test of '%s' because", chart)))
}
} else {
fmt.Printf("Testing upgrades of chart '%s' relative to previous revision '%s'...\n", chart, t.mergeBase)
prevChartDir := tempChartPath(chart)
oldValuesFiles := t.FindValuesFilesForCI(prevChartDir)
if len(oldValuesFiles) == 0 {
oldValuesFiles = append(valuesFiles, "")
}
for _, oldValuesFile := range oldValuesFiles {
if oldValuesFile != "" {
fmt.Printf("\nInstalling chart at ref '%s' with values file '%s'...\n\n", t.mergeBase, oldValuesFile)
}

// Use anonymous function. Otherwise deferred calls would pile up
// and be executed in reverse order after the loop.
fun := func() error {
namespace, release, releaseSelector, cleanup := t.generateInstallConfig(chart)
defer cleanup()

// Install previous version of chart. If installation fails, ignore this release.
if err := t.helm.InstallWithValues(prevChartDir, oldValuesFile, namespace, release); err != nil {
fmt.Println("Release ignored")
return nil
}
if err := t.testRelease(release, namespace, releaseSelector); err != nil {
fmt.Println("Release ignored")
return nil
}

if err := t.helm.Upgrade(chart, release); err != nil {
return err
}

return t.testRelease(release, namespace, releaseSelector)
}

if err := fun(); err != nil {
result.Error = err
break
}
}
}

result = t.UpgradeChart(chart)
if result.Error != nil {
// Upgrading chart from previous release failed; return result.
return result
}
} else {
result = TestResult{Chart: chart}
}

fmt.Printf("Installing chart '%s'...\n", chart)
Expand Down Expand Up @@ -478,6 +429,71 @@ func (t *Testing) InstallChart(chart string, valuesFiles []string) TestResult {
return result
}

// UpgradeChart tests in-place upgrades of the specified chart relative to its previous revisions. If the
// initial install or helm test of a previous revision of the chart fails, that release is ignored and no
// error will be returned. If the latest revision of the chart introduces a potentially breaking change
// according to the SemVer specification, upgrade testing will be skipped.
func (t *Testing) UpgradeChart(chart string) TestResult {
result := TestResult{Chart: chart}

breakingChangeAllowed, reasons, err := t.checkBreakingChangeAllowed(chart)
if err != nil {
fmt.Printf("Error comparing chart versions for '%s'\n", chart)
result.Error = err
} else if breakingChangeAllowed {
for _, r := range reasons {
fmt.Println(errors.Wrap(r, fmt.Sprintf("Skipping upgrade test of '%s' because", chart)))
}
} else {
fmt.Printf("Testing upgrades of chart '%s' relative to previous revision '%s'...\n", chart, t.mergeBase)
prevChartDir := tempChartPath(chart)
valuesFiles := t.FindValuesFilesForCI(prevChartDir)
if len(valuesFiles) == 0 {
valuesFiles = append(valuesFiles, "")
}
for _, valuesFile := range valuesFiles {
if valuesFile != "" {
fmt.Printf("\nInstalling chart at ref '%s' with values file '%s'...\n\n", t.mergeBase, valuesFile)
}

// Use anonymous function. Otherwise deferred calls would pile up
// and be executed in reverse order after the loop.
fun := func() error {
namespace, release, releaseSelector, cleanup := t.generateInstallConfig(chart)
defer cleanup()

// Install previous version of chart. If installation fails, ignore this release.
if err := t.helm.InstallWithValues(prevChartDir, valuesFile, namespace, release); err != nil {
fmt.Println(errors.Wrap(err, fmt.Sprintf("Upgrade testing for release '%s' skipped because of previous revision installation error", release)))
return nil
}
if err := t.testRelease(release, namespace, releaseSelector); err != nil {
fmt.Println(errors.Wrap(err, fmt.Sprintf("Upgrade testing for release '%s' skipped because of previous revision testing error", release)))
return nil
}

if err := t.helm.Upgrade(chart, release); err != nil {
return err
}

return t.testRelease(release, namespace, releaseSelector)
}

if err := fun(); err != nil {
result.Error = err
break
}
}
}

if result.Error != nil {
// Upgrading chart from previous release failed; return result.
return result
}

return result
}

func (t *Testing) testRelease(release, namespace, releaseSelector string) error {
if err := t.kubectl.WaitForDeployments(namespace, releaseSelector); err != nil {
return err
Expand Down

0 comments on commit aa60f0f

Please sign in to comment.