2023-08-24 10:40:47 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/retry"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// maximumRetryAttempts is the maximum number of attempts to retry a helm install.
|
|
|
|
maximumRetryAttempts = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
type retrieableApplier interface {
|
|
|
|
apply(context.Context) error
|
|
|
|
ReleaseName() string
|
|
|
|
IsAtomic() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryApply retries the given retriable action.
|
2023-09-08 16:54:01 -04:00
|
|
|
func retryApply(ctx context.Context, action retrieableApplier, retryInterval time.Duration, log debugLog) error {
|
2023-08-24 10:40:47 -04:00
|
|
|
var retries int
|
|
|
|
retriable := func(err error) bool {
|
|
|
|
// abort after maximumRetryAttempts tries.
|
|
|
|
if retries >= maximumRetryAttempts {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
retries++
|
|
|
|
// only retry if atomic is set
|
2023-09-08 16:54:01 -04:00
|
|
|
// otherwise helm doesn't uninstall the release on failure
|
|
|
|
return action.IsAtomic()
|
2023-08-24 10:40:47 -04:00
|
|
|
}
|
|
|
|
doer := applyDoer{
|
2023-09-08 16:54:01 -04:00
|
|
|
applier: action,
|
|
|
|
log: log,
|
2023-08-24 10:40:47 -04:00
|
|
|
}
|
2023-09-08 16:54:01 -04:00
|
|
|
retrier := retry.NewIntervalRetrier(doer, retryInterval, retriable)
|
2023-08-24 10:40:47 -04:00
|
|
|
|
|
|
|
retryLoopStartTime := time.Now()
|
|
|
|
if err := retrier.Do(ctx); err != nil {
|
|
|
|
return fmt.Errorf("helm install: %w", err)
|
|
|
|
}
|
|
|
|
retryLoopFinishDuration := time.Since(retryLoopStartTime)
|
|
|
|
log.Debugf("Helm chart %q installation finished after %s", action.ReleaseName(), retryLoopFinishDuration)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyDoer is a helper struct to enable retrying helm actions.
|
|
|
|
type applyDoer struct {
|
2023-09-08 16:54:01 -04:00
|
|
|
applier retrieableApplier
|
2023-08-24 10:40:47 -04:00
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do tries to apply the action.
|
|
|
|
func (i applyDoer) Do(ctx context.Context) error {
|
2023-09-08 16:54:01 -04:00
|
|
|
i.log.Debugf("Trying to apply Helm chart %s", i.applier.ReleaseName())
|
|
|
|
if err := i.applier.apply(ctx); err != nil {
|
|
|
|
i.log.Debugf("Helm chart installation %s failed: %v", i.applier.ReleaseName(), err)
|
2023-08-24 10:40:47 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|