Skip to content

Commit 0325b55

Browse files
committed
Introduce RetryOnFailure lifecycle management strategy
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent c059e51 commit 0325b55

13 files changed

+832
-6
lines changed

api/v2/helmrelease_types.go

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,20 @@ type Remediation interface {
437437
RetriesExhausted(hr *HelmRelease) bool
438438
}
439439

440+
// Strategy defines a consistent interface for InstallStrategy and
441+
// UpgradeStrategy.
442+
// +kubebuilder:object:generate=false
443+
type Strategy interface {
444+
GetRetry() Retry
445+
}
446+
447+
// Retry defines a consistent interface for retry strategies from
448+
// InstallStrategy and UpgradeStrategy.
449+
// +kubebuilder:object:generate=false
450+
type Retry interface {
451+
GetRetryInterval() time.Duration
452+
}
453+
440454
// Install holds the configuration for Helm install actions performed for this
441455
// HelmRelease.
442456
type Install struct {
@@ -448,6 +462,11 @@ type Install struct {
448462
// +optional
449463
Timeout *metav1.Duration `json:"timeout,omitempty"`
450464

465+
// Strategy defines the install strategy to use for this HelmRelease.
466+
// Defaults to 'RemediateOnFailure'.
467+
// +optional
468+
Strategy *InstallStrategy `json:"strategy,omitempty"`
469+
451470
// Remediation holds the remediation configuration for when the Helm install
452471
// action for the HelmRelease fails. The default is to not perform any action.
453472
// +optional
@@ -541,6 +560,41 @@ func (in Install) GetRemediation() Remediation {
541560
return *in.Remediation
542561
}
543562

563+
// GetRetry returns the configured retry strategy for the Helm install
564+
// action.
565+
func (in Install) GetRetry() Retry {
566+
if in.Strategy == nil || in.Strategy.Name != string(ActionStrategyRetryOnFailure) {
567+
return nil
568+
}
569+
return in.Strategy
570+
}
571+
572+
// InstallStrategy holds the configuration for Helm install strategy.
573+
// +kubebuilder:validation:XValidation:rule="!has(self.retryInterval) || self.name != 'RemediateOnFailure'", message=".retryInterval cannot be set when .name is 'RemediateOnFailure'"
574+
type InstallStrategy struct {
575+
// Name of the install strategy.
576+
// +kubebuilder:validation:Enum=RemediateOnFailure;RetryOnFailure
577+
// +required
578+
Name string `json:"name"`
579+
580+
// RetryInterval is the interval at which to retry a failed install.
581+
// Can be used only when Name is set to RetryOnFailure.
582+
// Defaults to '5m'.
583+
// +kubebuilder:validation:Type=string
584+
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$"
585+
// +optional
586+
RetryInterval *metav1.Duration `json:"retryInterval,omitempty"`
587+
}
588+
589+
// GetRetryInterval returns the configured retry interval for the Helm install
590+
// action, or the default.
591+
func (in InstallStrategy) GetRetryInterval() time.Duration {
592+
if in.RetryInterval == nil {
593+
return 5 * time.Minute
594+
}
595+
return in.RetryInterval.Duration
596+
}
597+
544598
// InstallRemediation holds the configuration for Helm install remediation.
545599
type InstallRemediation struct {
546600
// Retries is the number of retries that should be attempted on failures before
@@ -631,6 +685,11 @@ type Upgrade struct {
631685
// +optional
632686
Timeout *metav1.Duration `json:"timeout,omitempty"`
633687

688+
// Strategy defines the upgrade strategy to use for this HelmRelease.
689+
// Defaults to 'RemediateOnFailure'.
690+
// +optional
691+
Strategy *UpgradeStrategy `json:"strategy,omitempty"`
692+
634693
// Remediation holds the remediation configuration for when the Helm upgrade
635694
// action for the HelmRelease fails. The default is to not perform any action.
636695
// +optional
@@ -719,6 +778,41 @@ func (in Upgrade) GetRemediation() Remediation {
719778
return *in.Remediation
720779
}
721780

781+
// GetRetry returns the configured retry strategy for the Helm upgrade
782+
// action.
783+
func (in Upgrade) GetRetry() Retry {
784+
if in.Strategy == nil || in.Strategy.Name != string(ActionStrategyRetryOnFailure) {
785+
return nil
786+
}
787+
return in.Strategy
788+
}
789+
790+
// UpgradeStrategy holds the configuration for Helm upgrade strategy.
791+
// +kubebuilder:validation:XValidation:rule="!has(self.retryInterval) || self.name == 'RetryOnFailure'", message=".retryInterval can only be set when .name is 'RetryOnFailure'"
792+
type UpgradeStrategy struct {
793+
// Name of the upgrade strategy.
794+
// +kubebuilder:validation:Enum=RemediateOnFailure;RetryOnFailure
795+
// +required
796+
Name string `json:"name"`
797+
798+
// RetryInterval is the interval at which to retry a failed upgrade.
799+
// Can be used only when Name is set to RetryOnFailure.
800+
// Defaults to '5m'.
801+
// +kubebuilder:validation:Type=string
802+
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$"
803+
// +optional
804+
RetryInterval *metav1.Duration `json:"retryInterval,omitempty"`
805+
}
806+
807+
// GetRetryInterval returns the configured retry interval for the Helm upgrade
808+
// action, or the default.
809+
func (in UpgradeStrategy) GetRetryInterval() time.Duration {
810+
if in.RetryInterval == nil {
811+
return 5 * time.Minute
812+
}
813+
return in.RetryInterval.Duration
814+
}
815+
722816
// UpgradeRemediation holds the configuration for Helm upgrade remediation.
723817
type UpgradeRemediation struct {
724818
// Retries is the number of retries that should be attempted on failures before
@@ -791,6 +885,19 @@ func (in UpgradeRemediation) RetriesExhausted(hr *HelmRelease) bool {
791885
return in.Retries >= 0 && in.GetFailureCount(hr) > int64(in.Retries)
792886
}
793887

888+
// ActionStrategyName is a valid name for an action strategy.
889+
type ActionStrategyName string
890+
891+
const (
892+
// ActionStrategyRemediateOnFailure is the action strategy name for
893+
// remediate on failure.
894+
ActionStrategyRemediateOnFailure ActionStrategyName = "RemediateOnFailure"
895+
896+
// ActionStrategyRetryOnFailure is the action strategy name for retry on
897+
// failure.
898+
ActionStrategyRetryOnFailure ActionStrategyName = "RetryOnFailure"
899+
)
900+
794901
// RemediationStrategy returns the strategy to use to remediate a failed install
795902
// or upgrade.
796903
type RemediationStrategy string
@@ -1012,7 +1119,8 @@ type HelmReleaseStatus struct {
10121119
History Snapshots `json:"history,omitempty"`
10131120

10141121
// LastAttemptedReleaseAction is the last release action performed for this
1015-
// HelmRelease. It is used to determine the active remediation strategy.
1122+
// HelmRelease. It is used to determine the active retry or remediation
1123+
// strategy.
10161124
// +kubebuilder:validation:Enum=install;upgrade
10171125
// +optional
10181126
LastAttemptedReleaseAction ReleaseAction `json:"lastAttemptedReleaseAction,omitempty"`
@@ -1195,6 +1303,19 @@ func (in HelmRelease) GetActiveRemediation() Remediation {
11951303
}
11961304
}
11971305

1306+
// GetActiveRetry returns the active retry configuration for the
1307+
// HelmRelease.
1308+
func (in HelmRelease) GetActiveRetry() Retry {
1309+
switch in.Status.LastAttemptedReleaseAction {
1310+
case ReleaseActionInstall:
1311+
return in.GetInstall().GetRetry()
1312+
case ReleaseActionUpgrade:
1313+
return in.GetUpgrade().GetRetry()
1314+
default:
1315+
return nil
1316+
}
1317+
}
1318+
11981319
// GetRequeueAfter returns the duration after which the HelmRelease
11991320
// must be reconciled again.
12001321
func (in HelmRelease) GetRequeueAfter() time.Duration {

api/v2/zz_generated.deepcopy.go

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/helm.toolkit.fluxcd.io_helmreleases.yaml

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,30 @@ spec:
445445
446446
Deprecated use CRD policy (`crds`) attribute with value `Skip` instead.
447447
type: boolean
448+
strategy:
449+
description: |-
450+
Strategy defines the install strategy to use for this HelmRelease.
451+
Defaults to 'RemediateOnFailure'.
452+
properties:
453+
name:
454+
description: Name of the install strategy.
455+
enum:
456+
- RemediateOnFailure
457+
- RetryOnFailure
458+
type: string
459+
retryInterval:
460+
description: |-
461+
RetryInterval is the interval at which to retry a failed install.
462+
Can be used only when Name is set to RetryOnFailure.
463+
Defaults to '5m'.
464+
pattern: ^([0-9]+(\.[0-9]+)?(ms|s|m|h))+$
465+
type: string
466+
required:
467+
- name
468+
type: object
469+
x-kubernetes-validations:
470+
- message: .retryInterval cannot be set when .name is 'RemediateOnFailure'
471+
rule: '!has(self.retryInterval) || self.name != ''RemediateOnFailure'''
448472
timeout:
449473
description: |-
450474
Timeout is the time to wait for any individual Kubernetes operation (like
@@ -912,6 +936,30 @@ spec:
912936
- uninstall
913937
type: string
914938
type: object
939+
strategy:
940+
description: |-
941+
Strategy defines the upgrade strategy to use for this HelmRelease.
942+
Defaults to 'RemediateOnFailure'.
943+
properties:
944+
name:
945+
description: Name of the upgrade strategy.
946+
enum:
947+
- RemediateOnFailure
948+
- RetryOnFailure
949+
type: string
950+
retryInterval:
951+
description: |-
952+
RetryInterval is the interval at which to retry a failed upgrade.
953+
Can be used only when Name is set to RetryOnFailure.
954+
Defaults to '5m'.
955+
pattern: ^([0-9]+(\.[0-9]+)?(ms|s|m|h))+$
956+
type: string
957+
required:
958+
- name
959+
type: object
960+
x-kubernetes-validations:
961+
- message: .retryInterval can only be set when .name is 'RetryOnFailure'
962+
rule: '!has(self.retryInterval) || self.name == ''RetryOnFailure'''
915963
timeout:
916964
description: |-
917965
Timeout is the time to wait for any individual Kubernetes operation (like
@@ -1178,7 +1226,8 @@ spec:
11781226
lastAttemptedReleaseAction:
11791227
description: |-
11801228
LastAttemptedReleaseAction is the last release action performed for this
1181-
HelmRelease. It is used to determine the active remediation strategy.
1229+
HelmRelease. It is used to determine the active retry or remediation
1230+
strategy.
11821231
enum:
11831232
- install
11841233
- upgrade

0 commit comments

Comments
 (0)