Skip to content

Commit

Permalink
Add Get methods for remediations
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddeco committed Aug 24, 2020
1 parent d60e269 commit 856b54b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
46 changes: 26 additions & 20 deletions api/v2alpha1/helmrelease_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func (in Install) GetTimeout(defaultTimeout metav1.Duration) metav1.Duration {
return *in.Timeout
}

// GetRemediation returns the remediation configuration for the Helm install action.
func (in Install) GetRemediation() InstallRemediation {
if in.Remediation == nil {
return InstallRemediation{}
}
return *in.Remediation
}

// InstallRemediation holds the configuration for Helm install remediation.
type InstallRemediation struct {
// Retries is the number of retries that should be attempted on failures before
Expand Down Expand Up @@ -263,22 +271,18 @@ func (in InstallRemediation) GetRetries() int {

// MustIgnoreTestFailures returns the configured IgnoreTestFailures or the given default.
func (in InstallRemediation) MustIgnoreTestFailures(def bool) bool {
switch in.IgnoreTestFailures {
case nil:
if in.IgnoreTestFailures == nil {
return def
default:
return *in.IgnoreTestFailures
}
return *in.IgnoreTestFailures
}

// MustRemediateLastFailure returns whether to remediate the last failure when no retries remain.
func (in InstallRemediation) MustRemediateLastFailure() bool {
switch in.RemediateLastFailure {
case nil:
if in.RemediateLastFailure == nil {
return false
default:
return *in.RemediateLastFailure
}
return *in.RemediateLastFailure
}

// GetStrategy returns the strategy to use for failure remediation.
Expand Down Expand Up @@ -354,6 +358,14 @@ func (in Upgrade) GetTimeout(defaultTimeout metav1.Duration) metav1.Duration {
return *in.Timeout
}

// GetRemediation returns the remediation configuration for the Helm upgrade action.
func (in Upgrade) GetRemediation() UpgradeRemediation {
if in.Remediation == nil {
return UpgradeRemediation{}
}
return *in.Remediation
}

// UpgradeRemediation holds the configuration for Helm install remediation.
type UpgradeRemediation struct {
// Retries is the number of retries that should be attempted on failures before
Expand Down Expand Up @@ -388,32 +400,26 @@ func (in UpgradeRemediation) GetRetries() int {

// MustIgnoreTestFailures returns the configured IgnoreTestFailures or the given default.
func (in UpgradeRemediation) MustIgnoreTestFailures(def bool) bool {
switch in.IgnoreTestFailures {
case nil:
if in.IgnoreTestFailures == nil {
return def
default:
return *in.IgnoreTestFailures
}
return *in.IgnoreTestFailures
}

// MustRemediateLastFailure returns whether to remediate the last failure when no retries remain.
func (in UpgradeRemediation) MustRemediateLastFailure() bool {
switch in.RemediateLastFailure {
case nil:
if in.RemediateLastFailure == nil {
return in.Retries > 0
default:
return *in.RemediateLastFailure
}
return *in.RemediateLastFailure
}

// GetStrategy returns the strategy to use for failure remediation.
func (in UpgradeRemediation) GetStrategy() RemediationStrategy {
switch in.Strategy {
case nil:
if in.Strategy == nil {
return RollbackRemediationStrategy
default:
return *in.Strategy
}
return *in.Strategy
}

// GetFailureCount gets the failure count.
Expand Down
14 changes: 7 additions & 7 deletions controllers/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,31 +347,31 @@ func (r *HelmReleaseReconciler) release(log logr.Logger, hr v2.HelmRelease, sour
var remediation v2.Remediation
switch {
// Install retries exhausted, retain status from prior attempt.
case hr.Spec.Install.Remediation.RetriesExhausted(hr):
case hr.Spec.GetInstall().GetRemediation().RetriesExhausted(hr):
return hr, fmt.Errorf("install retries exhausted")
// Upgrade retries exhausted, retain status from prior attempt.
case hr.Spec.Upgrade.Remediation.RetriesExhausted(hr):
case hr.Spec.GetUpgrade().GetRemediation().RetriesExhausted(hr):
return hr, fmt.Errorf("upgrade retries exhausted")
// Install the release as there is none.
case rel == nil:
remediation = hr.Spec.Install.Remediation
remediation = hr.Spec.GetInstall().GetRemediation()
rel, err = install(cfg, loadedChart, hr, values)
err = r.handleHelmActionResult(&hr, revision, err, "install", v2.InstalledCondition, v2.InstallSucceededReason, v2.InstallFailedReason)
// Upgrade the release if the state changed or this is a retry.
default:
remediation = hr.Spec.Upgrade.Remediation
remediation = hr.Spec.GetUpgrade().GetRemediation()
if changed || remediation.GetFailureCount(hr) > 0 {
rel, err = upgrade(cfg, loadedChart, hr, values)
err = r.handleHelmActionResult(&hr, revision, err, "upgrade", v2.UpgradedCondition, v2.UpgradeSucceededReason, v2.UpgradeFailedReason)
}
}

// Run tests if enabled and there is a successful new release revision.
if getReleaseRevision(rel) > releaseRevision && err == nil && hr.Spec.Test.Enable {
if getReleaseRevision(rel) > releaseRevision && err == nil && hr.Spec.GetTest().Enable {
_, testErr := test(cfg, hr)
testErr = r.handleHelmActionResult(&hr, revision, testErr, "test", v2.TestedCondition, v2.TestSucceededReason, v2.TestFailedReason)
// Propagate any test error if not marked ignored.
if testErr != nil && !remediation.MustIgnoreTestFailures(hr.Spec.Test.IgnoreFailures) {
if testErr != nil && !remediation.MustIgnoreTestFailures(hr.Spec.GetTest().IgnoreFailures) {
err = testErr
}
}
Expand Down Expand Up @@ -670,7 +670,7 @@ func upgrade(cfg *action.Configuration, chart *chart.Chart, hr v2.HelmRelease, v
func test(cfg *action.Configuration, hr v2.HelmRelease) (*release.Release, error) {
test := action.NewReleaseTesting(cfg)
test.Namespace = hr.GetReleaseNamespace()
test.Timeout = hr.Spec.Test.GetTimeout(hr.GetTimeout()).Duration
test.Timeout = hr.Spec.GetTest().GetTimeout(hr.GetTimeout()).Duration

return test.Run(hr.GetReleaseName())
}
Expand Down

0 comments on commit 856b54b

Please sign in to comment.