Skip to content

Commit 6ca5566

Browse files
fix(operator): handle MaxHistory=0 and cover HelmRelease spec build
Chart template now uses `ne (toString …) ""` for the five helmRelease* knobs, so integer 0 (notably MaxHistory=0 = unlimited history per Helm semantics, documented in values.yaml) is no longer silently dropped. Extract buildHelmReleaseSpec from Reconcile and add unit tests asserting Interval, MaxHistory, Install/Upgrade timeouts, Strategy=RetryOnFailure, RetryInterval, and ChartRef propagate from PackageReconciler config to the generated HelmRelease. Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent e410b02 commit 6ca5566

3 files changed

Lines changed: 122 additions & 34 deletions

File tree

‎internal/operator/package_reconciler.go‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ type PackageReconciler struct {
6767
HelmReleaseMaxHistory int
6868
}
6969

70+
// buildHelmReleaseSpec assembles the Spec applied to every generated
71+
// HelmRelease. RetryInterval drives recovery from failed install/upgrade
72+
// attempts; Interval polls healthy releases.
73+
func (r *PackageReconciler) buildHelmReleaseSpec(componentInstall *cozyv1alpha1.ComponentInstall, artifactName string) helmv2.HelmReleaseSpec {
74+
return helmv2.HelmReleaseSpec{
75+
Interval: metav1.Duration{Duration: r.HelmReleaseInterval},
76+
MaxHistory: &r.HelmReleaseMaxHistory,
77+
ChartRef: &helmv2.CrossNamespaceSourceReference{
78+
Kind: "ExternalArtifact",
79+
Name: artifactName,
80+
Namespace: "cozy-system",
81+
},
82+
Install: &helmv2.Install{
83+
Timeout: &metav1.Duration{Duration: r.HelmReleaseInstallTimeout},
84+
Strategy: &helmv2.InstallStrategy{
85+
Name: string(helmv2.ActionStrategyRetryOnFailure),
86+
RetryInterval: &metav1.Duration{Duration: r.HelmReleaseRetryInterval},
87+
},
88+
},
89+
Upgrade: &helmv2.Upgrade{
90+
Timeout: &metav1.Duration{Duration: r.HelmReleaseUpgradeTimeout},
91+
Strategy: &helmv2.UpgradeStrategy{
92+
Name: string(helmv2.ActionStrategyRetryOnFailure),
93+
RetryInterval: &metav1.Duration{Duration: r.HelmReleaseRetryInterval},
94+
},
95+
CRDs: parseCRDPolicy(componentInstall),
96+
},
97+
}
98+
}
99+
70100
// +kubebuilder:rbac:groups=cozystack.io,resources=packages,verbs=get;list;watch;create;update;patch;delete
71101
// +kubebuilder:rbac:groups=cozystack.io,resources=packages/status,verbs=get;update;patch
72102
// +kubebuilder:rbac:groups=cozystack.io,resources=packagesources,verbs=get;list;watch
@@ -219,35 +249,7 @@ func (r *PackageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
219249
Namespace: namespace,
220250
Labels: labels,
221251
},
222-
Spec: helmv2.HelmReleaseSpec{
223-
Interval: metav1.Duration{Duration: r.HelmReleaseInterval},
224-
MaxHistory: &r.HelmReleaseMaxHistory,
225-
ChartRef: &helmv2.CrossNamespaceSourceReference{
226-
Kind: "ExternalArtifact",
227-
Name: artifactName,
228-
Namespace: "cozy-system",
229-
},
230-
Install: &helmv2.Install{
231-
Timeout: &metav1.Duration{Duration: r.HelmReleaseInstallTimeout},
232-
// Strategy=RetryOnFailure (with RetryInterval) replaces the previous
233-
// Remediation{Retries:-1} setup. Functionally equivalent ("retry forever
234-
// on failure"), but decouples retry timing from spec.Interval so failed
235-
// installs recover at HelmReleaseRetryInterval (default 30s) without
236-
// also polling healthy releases at the same fast cadence.
237-
Strategy: &helmv2.InstallStrategy{
238-
Name: string(helmv2.ActionStrategyRetryOnFailure),
239-
RetryInterval: &metav1.Duration{Duration: r.HelmReleaseRetryInterval},
240-
},
241-
},
242-
Upgrade: &helmv2.Upgrade{
243-
Timeout: &metav1.Duration{Duration: r.HelmReleaseUpgradeTimeout},
244-
Strategy: &helmv2.UpgradeStrategy{
245-
Name: string(helmv2.ActionStrategyRetryOnFailure),
246-
RetryInterval: &metav1.Duration{Duration: r.HelmReleaseRetryInterval},
247-
},
248-
CRDs: parseCRDPolicy(component.Install),
249-
},
250-
},
252+
Spec: r.buildHelmReleaseSpec(component.Install, artifactName),
251253
}
252254

253255
// Add valuesFrom for cozystack-values secret unless disabled by annotation on PackageSource

‎internal/operator/package_reconciler_test.go‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"path/filepath"
2323
"testing"
24+
"time"
2425

2526
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
2627
helmv2 "github.com/fluxcd/helm-controller/api/v2"
@@ -71,6 +72,91 @@ func TestParseCRDPolicy(t *testing.T) {
7172
}
7273
}
7374

75+
func TestBuildHelmReleaseSpec(t *testing.T) {
76+
r := &PackageReconciler{
77+
HelmReleaseInterval: 42 * time.Second,
78+
HelmReleaseRetryInterval: 17 * time.Second,
79+
HelmReleaseInstallTimeout: 11 * time.Minute,
80+
HelmReleaseUpgradeTimeout: 13 * time.Minute,
81+
HelmReleaseMaxHistory: 7,
82+
}
83+
componentInstall := &cozyv1alpha1.ComponentInstall{UpgradeCRDs: "Skip"}
84+
85+
spec := r.buildHelmReleaseSpec(componentInstall, "ps-variant-component")
86+
87+
if spec.Interval.Duration != 42*time.Second {
88+
t.Errorf("Interval = %v, want 42s", spec.Interval.Duration)
89+
}
90+
if spec.MaxHistory == nil {
91+
t.Fatal("MaxHistory is nil, want pointer to 7")
92+
}
93+
if *spec.MaxHistory != 7 {
94+
t.Errorf("MaxHistory = %d, want 7", *spec.MaxHistory)
95+
}
96+
97+
if spec.ChartRef == nil {
98+
t.Fatal("ChartRef is nil")
99+
}
100+
if spec.ChartRef.Kind != "ExternalArtifact" {
101+
t.Errorf("ChartRef.Kind = %q, want ExternalArtifact", spec.ChartRef.Kind)
102+
}
103+
if spec.ChartRef.Name != "ps-variant-component" {
104+
t.Errorf("ChartRef.Name = %q, want ps-variant-component", spec.ChartRef.Name)
105+
}
106+
if spec.ChartRef.Namespace != "cozy-system" {
107+
t.Errorf("ChartRef.Namespace = %q, want cozy-system", spec.ChartRef.Namespace)
108+
}
109+
110+
if spec.Install == nil {
111+
t.Fatal("Install is nil")
112+
}
113+
if spec.Install.Timeout == nil || spec.Install.Timeout.Duration != 11*time.Minute {
114+
t.Errorf("Install.Timeout = %v, want 11m", spec.Install.Timeout)
115+
}
116+
if spec.Install.Strategy == nil {
117+
t.Fatal("Install.Strategy is nil")
118+
}
119+
if spec.Install.Strategy.Name != string(helmv2.ActionStrategyRetryOnFailure) {
120+
t.Errorf("Install.Strategy.Name = %q, want %q", spec.Install.Strategy.Name, helmv2.ActionStrategyRetryOnFailure)
121+
}
122+
if spec.Install.Strategy.RetryInterval == nil || spec.Install.Strategy.RetryInterval.Duration != 17*time.Second {
123+
t.Errorf("Install.Strategy.RetryInterval = %v, want 17s", spec.Install.Strategy.RetryInterval)
124+
}
125+
126+
if spec.Upgrade == nil {
127+
t.Fatal("Upgrade is nil")
128+
}
129+
if spec.Upgrade.Timeout == nil || spec.Upgrade.Timeout.Duration != 13*time.Minute {
130+
t.Errorf("Upgrade.Timeout = %v, want 13m", spec.Upgrade.Timeout)
131+
}
132+
if spec.Upgrade.Strategy == nil {
133+
t.Fatal("Upgrade.Strategy is nil")
134+
}
135+
if spec.Upgrade.Strategy.Name != string(helmv2.ActionStrategyRetryOnFailure) {
136+
t.Errorf("Upgrade.Strategy.Name = %q, want %q", spec.Upgrade.Strategy.Name, helmv2.ActionStrategyRetryOnFailure)
137+
}
138+
if spec.Upgrade.Strategy.RetryInterval == nil || spec.Upgrade.Strategy.RetryInterval.Duration != 17*time.Second {
139+
t.Errorf("Upgrade.Strategy.RetryInterval = %v, want 17s", spec.Upgrade.Strategy.RetryInterval)
140+
}
141+
if spec.Upgrade.CRDs != helmv2.Skip {
142+
t.Errorf("Upgrade.CRDs = %q, want Skip", spec.Upgrade.CRDs)
143+
}
144+
}
145+
146+
// TestBuildHelmReleaseSpecZeroMaxHistory pins that MaxHistory=0 (unlimited
147+
// history per Helm semantics) survives the spec build — i.e. is set as a
148+
// non-nil pointer to 0 rather than dropped or replaced with a default.
149+
func TestBuildHelmReleaseSpecZeroMaxHistory(t *testing.T) {
150+
r := &PackageReconciler{HelmReleaseMaxHistory: 0}
151+
spec := r.buildHelmReleaseSpec(nil, "x")
152+
if spec.MaxHistory == nil {
153+
t.Fatal("MaxHistory is nil for HelmReleaseMaxHistory=0; want pointer to 0")
154+
}
155+
if *spec.MaxHistory != 0 {
156+
t.Errorf("MaxHistory = %d, want 0", *spec.MaxHistory)
157+
}
158+
}
159+
74160
// TestPackageSourceCRDHasUpgradeCRDsEnum guards the generated CRD schema: the
75161
// invalid-value case from the spec is enforced at the API server via a
76162
// kubebuilder enum marker, not in the reconciler. If someone drops the marker

‎packages/core/installer/templates/cozystack-operator.yaml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ spec:
6868
{{- if .Values.cozystackOperator.disableTelemetry }}
6969
- --disable-telemetry
7070
{{- end }}
71-
{{- if .Values.cozystackOperator.helmReleaseInterval }}
71+
{{- if ne (toString .Values.cozystackOperator.helmReleaseInterval) "" }}
7272
- --helmrelease-interval={{ .Values.cozystackOperator.helmReleaseInterval }}
7373
{{- end }}
74-
{{- if .Values.cozystackOperator.helmReleaseRetryInterval }}
74+
{{- if ne (toString .Values.cozystackOperator.helmReleaseRetryInterval) "" }}
7575
- --helmrelease-retry-interval={{ .Values.cozystackOperator.helmReleaseRetryInterval }}
7676
{{- end }}
77-
{{- if .Values.cozystackOperator.helmReleaseInstallTimeout }}
77+
{{- if ne (toString .Values.cozystackOperator.helmReleaseInstallTimeout) "" }}
7878
- --helmrelease-install-timeout={{ .Values.cozystackOperator.helmReleaseInstallTimeout }}
7979
{{- end }}
80-
{{- if .Values.cozystackOperator.helmReleaseUpgradeTimeout }}
80+
{{- if ne (toString .Values.cozystackOperator.helmReleaseUpgradeTimeout) "" }}
8181
- --helmrelease-upgrade-timeout={{ .Values.cozystackOperator.helmReleaseUpgradeTimeout }}
8282
{{- end }}
83-
{{- if .Values.cozystackOperator.helmReleaseMaxHistory }}
83+
{{- if ne (toString .Values.cozystackOperator.helmReleaseMaxHistory) "" }}
8484
- --helmrelease-max-history={{ .Values.cozystackOperator.helmReleaseMaxHistory }}
8585
{{- end }}
8686
- --platform-source-name=cozystack-platform

0 commit comments

Comments
 (0)