helm: retry uninstall manually if atomic install failed (#2984)

This commit is contained in:
Markus Rudy 2024-03-14 10:52:11 +01:00 committed by GitHub
parent 1334b84c2e
commit 54af083da3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View File

@ -8,8 +8,10 @@ package helm
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/edgelesssys/constellation/v2/internal/constants"
@ -52,6 +54,12 @@ func newHelmInstallAction(config *action.Configuration, release release, timeout
return action
}
func newHelmUninstallAction(config *action.Configuration, timeout time.Duration) *action.Uninstall {
action := action.NewUninstall(config)
action.Timeout = timeout
return action
}
func setWaitMode(a *action.Install, waitMode WaitMode) {
switch waitMode {
case WaitModeNone:
@ -73,6 +81,7 @@ type installAction struct {
preInstall func(context.Context) error
release release
helmAction *action.Install
uninstallAction *action.Uninstall
postInstall func(context.Context) error
log debugLog
}
@ -103,6 +112,11 @@ func (a *installAction) SaveChart(chartsDir string, fileHandler file.Handler) er
func (a *installAction) apply(ctx context.Context) error {
_, err := a.helmAction.RunWithContext(ctx, a.release.chart, a.release.values)
if isUninstallError(err) && a.uninstallAction != nil {
a.log.Debug("cleaning up manually after failed atomic Helm install", "error", err, "release", a.release.releaseName)
_, uninstallErr := a.uninstallAction.Run(a.release.releaseName)
err = errors.Join(err, uninstallErr)
}
return err
}
@ -228,3 +242,8 @@ func helmLog(log debugLog) action.DebugLog {
log.Debug(fmt.Sprintf(format, v...))
}
}
func isUninstallError(err error) bool {
return err != nil && (strings.Contains(err.Error(), "an error occurred while uninstalling the release") ||
strings.Contains(err.Error(), "cannot re-use a name that is still in use"))
}

View File

@ -139,6 +139,9 @@ func (a actionFactory) appendNewAction(
func (a actionFactory) newInstall(release release, timeout time.Duration) *installAction {
action := &installAction{helmAction: newHelmInstallAction(a.cfg, release, timeout), release: release, log: a.log}
if action.IsAtomic() {
action.uninstallAction = newHelmUninstallAction(a.cfg, timeout)
}
return action
}