Skip to content

Commit f39b557

Browse files
committed
fix(operator): carry the readiness-wait setting to existing releases
A HelmRelease created before its ApplicationDefinition carried release.cozystack.io/helm-install-disable-wait keeps waiting on readiness, because cozystack-api only reads that annotation while building a release and an existing release is not rebuilt until its Application is written through the apps API again. Where the wait is what broke, that is every existing instance of the kind at once, each looping upgrade and timeout, and rewriting them by hand is the operator's only way out. The definition already drives chartRef, valuesFrom and labels on releases that exist, so drive the wait from there too, in both directions: the API rebuilds the whole spec on every write and emits DisableWait=false once the annotation is gone, so a release left disabled would drift back anyway. An absent install or upgrade block behaves as DisableWait=false already and is only materialised when the definition asks to disable the wait, which keeps a release nothing asks to change from being rewritten. Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent e28e67d commit f39b557

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎internal/controller/applicationdefinition_helmreconciler.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
8+
"github.com/cozystack/cozystack/pkg/config"
89
helmv2 "github.com/fluxcd/helm-controller/api/v2"
910

1011
"k8s.io/apimachinery/pkg/runtime"
@@ -163,6 +164,44 @@ func (r *ApplicationDefinitionHelmReconciler) updateHelmReleaseChart(ctx context
163164
updated = true
164165
}
165166

167+
// Check and update the readiness wait. cozystack-api reads
168+
// release.cozystack.io/helm-install-disable-wait off the definition when it
169+
// builds a release, and only there, so a release created before the
170+
// annotation appeared keeps waiting on readiness until its Application is
171+
// written through the apps API again. Where the wait is what broke, that is
172+
// every existing instance of the kind at once, each looping upgrade and
173+
// timeout until an operator rewrites it by hand. The definition is the
174+
// source of truth in both directions, since the API rebuilds the whole spec
175+
// on every write and emits DisableWait=false once the annotation is gone.
176+
disableWait, err := config.ParseHelmInstallDisableWaitAnnotation(
177+
appDef.Annotations[config.HelmInstallDisableWaitAnnotation],
178+
)
179+
if err != nil {
180+
// cozystack-api refuses to start on this same value, so the cluster
181+
// already has a loud signal; leave the field as it is rather than guess.
182+
logger.Error(err, "Skipping HelmRelease wait update: invalid annotation",
183+
"appDef", appDef.Name, "annotation", config.HelmInstallDisableWaitAnnotation)
184+
} else {
185+
// An absent install/upgrade block already behaves as DisableWait=false,
186+
// so it is only materialised when the definition asks to disable the wait.
187+
if disableWait && hrCopy.Spec.Install == nil {
188+
hrCopy.Spec.Install = &helmv2.Install{}
189+
}
190+
if hrCopy.Spec.Install != nil && hrCopy.Spec.Install.DisableWait != disableWait {
191+
logger.V(4).Info("Updating HelmRelease install wait", "name", hr.Name, "namespace", hr.Namespace, "disableWait", disableWait)
192+
hrCopy.Spec.Install.DisableWait = disableWait
193+
updated = true
194+
}
195+
if disableWait && hrCopy.Spec.Upgrade == nil {
196+
hrCopy.Spec.Upgrade = &helmv2.Upgrade{}
197+
}
198+
if hrCopy.Spec.Upgrade != nil && hrCopy.Spec.Upgrade.DisableWait != disableWait {
199+
logger.V(4).Info("Updating HelmRelease upgrade wait", "name", hr.Name, "namespace", hr.Namespace, "disableWait", disableWait)
200+
hrCopy.Spec.Upgrade.DisableWait = disableWait
201+
updated = true
202+
}
203+
}
204+
166205
// Check and update labels from ApplicationDefinition
167206
if len(appDef.Spec.Release.Labels) > 0 {
168207
if hrCopy.Labels == nil {

‎internal/controller/applicationdefinition_helmreconciler_test.go‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
8+
"github.com/cozystack/cozystack/pkg/config"
89
helmv2 "github.com/fluxcd/helm-controller/api/v2"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/runtime"
@@ -352,3 +353,172 @@ func TestValuesFromEqual(t *testing.T) {
352353
})
353354
}
354355
}
356+
357+
// vmInstanceRelease builds a VMInstance HelmRelease carrying the application
358+
// labels this reconciler selects on, with the given wait configuration.
359+
func vmInstanceRelease(install, upgrade *bool) *helmv2.HelmRelease {
360+
hr := &helmv2.HelmRelease{
361+
ObjectMeta: metav1.ObjectMeta{
362+
Name: "vm-instance-web",
363+
Namespace: "tenant-foo",
364+
Labels: map[string]string{
365+
"apps.cozystack.io/application.kind": "VMInstance",
366+
"apps.cozystack.io/application.group": "apps.cozystack.io",
367+
},
368+
},
369+
Spec: helmv2.HelmReleaseSpec{
370+
ChartRef: &helmv2.CrossNamespaceSourceReference{
371+
Kind: "ExternalArtifact",
372+
Name: "vm-instance-app",
373+
Namespace: "cozy-system",
374+
},
375+
},
376+
}
377+
if install != nil {
378+
hr.Spec.Install = &helmv2.Install{DisableWait: *install}
379+
}
380+
if upgrade != nil {
381+
hr.Spec.Upgrade = &helmv2.Upgrade{DisableWait: *upgrade}
382+
}
383+
return hr
384+
}
385+
386+
// vmInstanceAppDef builds the VMInstance ApplicationDefinition, optionally
387+
// carrying the disable-wait annotation.
388+
func vmInstanceAppDef(annotations map[string]string) *cozyv1alpha1.ApplicationDefinition {
389+
return &cozyv1alpha1.ApplicationDefinition{
390+
ObjectMeta: metav1.ObjectMeta{Name: "vm-instance", Annotations: annotations},
391+
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
392+
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "VMInstance"},
393+
Release: cozyv1alpha1.ApplicationDefinitionRelease{
394+
ChartRef: &helmv2.CrossNamespaceSourceReference{
395+
Kind: "ExternalArtifact",
396+
Name: "vm-instance-app",
397+
Namespace: "cozy-system",
398+
},
399+
},
400+
},
401+
}
402+
}
403+
404+
func reconcileVMInstance(t *testing.T, appDef *cozyv1alpha1.ApplicationDefinition, hr *helmv2.HelmRelease) *helmv2.HelmRelease {
405+
t.Helper()
406+
407+
scheme := newAppDefHelmScheme(t)
408+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build()
409+
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
410+
411+
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
412+
NamespacedName: types.NamespacedName{Name: appDef.Name},
413+
}); err != nil {
414+
t.Fatalf("unexpected error: %v", err)
415+
}
416+
417+
got := &helmv2.HelmRelease{}
418+
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, got); err != nil {
419+
t.Fatalf("get HR: %v", err)
420+
}
421+
return got
422+
}
423+
424+
// TestAppDefHelm_DisableWaitApplied pins the drift this reconciler exists to
425+
// close, for the one field a running release cannot pick up on its own: a
426+
// HelmRelease created before the definition carried
427+
// release.cozystack.io/helm-install-disable-wait keeps waiting on readiness
428+
// until its Application is written through the apps API again. On a cluster
429+
// where the wait is what broke (a KubeVirt VirtualMachine the kstatus poller
430+
// reads as permanently InProgress), that is every existing VM, each one
431+
// looping upgrade and timeout, and re-writing them by hand is the operator's
432+
// only way out. Bring the field to what the definition asks for instead.
433+
func TestAppDefHelm_DisableWaitApplied(t *testing.T) {
434+
no := false
435+
got := reconcileVMInstance(t,
436+
vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}),
437+
vmInstanceRelease(&no, &no))
438+
439+
if got.Spec.Install == nil || !got.Spec.Install.DisableWait {
440+
t.Fatalf("expected Install.DisableWait=true, got %+v", got.Spec.Install)
441+
}
442+
if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait {
443+
t.Fatalf("expected Upgrade.DisableWait=true, got %+v", got.Spec.Upgrade)
444+
}
445+
}
446+
447+
// TestAppDefHelm_DisableWaitAppliedToAbsentBlocks pins the same on a release
448+
// that carries no install/upgrade block at all: the field still has to land.
449+
func TestAppDefHelm_DisableWaitAppliedToAbsentBlocks(t *testing.T) {
450+
got := reconcileVMInstance(t,
451+
vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}),
452+
vmInstanceRelease(nil, nil))
453+
454+
if got.Spec.Install == nil || !got.Spec.Install.DisableWait {
455+
t.Fatalf("expected Install.DisableWait=true, got %+v", got.Spec.Install)
456+
}
457+
if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait {
458+
t.Fatalf("expected Upgrade.DisableWait=true, got %+v", got.Spec.Upgrade)
459+
}
460+
}
461+
462+
// TestAppDefHelm_DisableWaitRemoved pins the other direction, because the
463+
// definition is the source of truth in both: cozystack-api rebuilds the whole
464+
// spec on every write and emits DisableWait=false once the annotation is gone,
465+
// so a release left at true would drift back the moment anyone touched it.
466+
func TestAppDefHelm_DisableWaitRemoved(t *testing.T) {
467+
yes := true
468+
got := reconcileVMInstance(t, vmInstanceAppDef(nil), vmInstanceRelease(&yes, &yes))
469+
470+
if got.Spec.Install == nil || got.Spec.Install.DisableWait {
471+
t.Fatalf("expected Install.DisableWait=false, got %+v", got.Spec.Install)
472+
}
473+
if got.Spec.Upgrade == nil || got.Spec.Upgrade.DisableWait {
474+
t.Fatalf("expected Upgrade.DisableWait=false, got %+v", got.Spec.Upgrade)
475+
}
476+
}
477+
478+
// TestAppDefHelm_DisableWaitLeavesAbsentBlocksAlone pins that the false case
479+
// does not materialise empty install/upgrade blocks on a release that has
480+
// none: writing them would change nothing about the release's behaviour and
481+
// would make every such reconcile an update.
482+
func TestAppDefHelm_DisableWaitLeavesAbsentBlocksAlone(t *testing.T) {
483+
got := reconcileVMInstance(t, vmInstanceAppDef(nil), vmInstanceRelease(nil, nil))
484+
485+
if got.Spec.Install != nil {
486+
t.Fatalf("expected Install left absent, got %+v", got.Spec.Install)
487+
}
488+
if got.Spec.Upgrade != nil {
489+
t.Fatalf("expected Upgrade left absent, got %+v", got.Spec.Upgrade)
490+
}
491+
}
492+
493+
// TestAppDefHelm_DisableWaitInSyncSkipsUpdate pins idempotence: a release that
494+
// already matches must not be written, or every reconcile of every definition
495+
// bumps the resourceVersion of every release it owns and wakes helm-controller.
496+
func TestAppDefHelm_DisableWaitInSyncSkipsUpdate(t *testing.T) {
497+
yes := true
498+
hr := vmInstanceRelease(&yes, &yes)
499+
hr.Spec.ValuesFrom = expectedValuesFrom()
500+
501+
scheme := newAppDefHelmScheme(t)
502+
appDef := vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"})
503+
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build()
504+
505+
before := &helmv2.HelmRelease{}
506+
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, before); err != nil {
507+
t.Fatalf("get HR: %v", err)
508+
}
509+
510+
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
511+
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
512+
NamespacedName: types.NamespacedName{Name: appDef.Name},
513+
}); err != nil {
514+
t.Fatalf("unexpected error: %v", err)
515+
}
516+
517+
after := &helmv2.HelmRelease{}
518+
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, after); err != nil {
519+
t.Fatalf("get HR after reconcile: %v", err)
520+
}
521+
if after.ResourceVersion != before.ResourceVersion {
522+
t.Fatalf("release rewritten with nothing to change: %q -> %q", before.ResourceVersion, after.ResourceVersion)
523+
}
524+
}

0 commit comments

Comments
 (0)