mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-02 21:30:46 +09:00
## What this PR does
A VMInstance is served as a Flux HelmRelease that renders a KubeVirt
VirtualMachine. Since v1.5.0 (helm-controller v1.4.3 to v1.5.0) that
release is health-assessed by the kstatus poller. vm-instance sets no
`healthCheckExprs`, so the VirtualMachine goes through kstatus's own
rules in `fluxcd/cli-utils` (`pkg/kstatus/status`): `checkGeneration`
returns InProgress for any object whose `status.observedGeneration`
differs from `metadata.generation`, and past that check a `Ready=False`
condition is InProgress too, so a halted VM is never Current even with
its generation in sync. KubeVirt keeps a VirtualMachine's
`observedGeneration` behind its `generation` whenever a `spec.template`
change is pending a restart, the VM is stopped, or a live-updatable
change never converges (kubevirt/kubevirt#18700). So a healthy, Running
VM is read as permanently InProgress, the upgrade waits its full
timeout, and fails with `timeout waiting for: [VirtualMachine/...
status: 'InProgress']`. On releases still carrying the pre-1.5.0
`remediation.retries: -1` + rollback spec, the timeout then reverts the
previous manifest, dropping just-applied changes such as opened ports.
This sets `release.cozystack.io/helm-install-disable-wait: "true"` on
the vm-instance ApplicationDefinition, so cozystack-api generates the
HelmRelease with Install/Upgrade DisableWait. The release settles once
the VirtualMachine is applied, and VM health is read off the
VirtualMachine itself (`status.printableStatus` / `Ready`) and off the
WorkloadMonitor the chart already renders, which is what
`WorkloadsReady` on the Application reports. This restores the pre-1.5.0
behaviour, under which the VM was never a helm readiness gate. The same
mechanism is already used by `kubernetes-rd` and `kubernetes-nodes-rd`.
On its own that annotation does not reach a running cluster, so two gaps
in cozystack-controller are closed with it.
The first is the rollout. `applicationdefinition-controller` hashed each
definition's name and spec only, so a change confined to a
`release.cozystack.io/*` annotation left the hash unmoved and
cozystack-api kept serving the configuration it read at its last
start-up, which is the only place those annotations are ever read. The
hash now covers them too. Only that prefix, because kubectl and Helm
rewrite annotations of their own on every apply and hashing those would
roll the api Deployment on every reconcile.
The second is the releases that already exist.
`applicationdefinition-helm-reconciler` brings chartRef, valuesFrom and
labels on existing HelmReleases in line with their definition; it now
does the same for the readiness wait. Without it the annotation reaches
new instances only, which on an affected cluster is the wrong half:
#3734 reports 134 of 136 releases already looping upgrade and timeout,
and re-applying every VMInstance by hand was the only way out. The
setting is driven in both directions, since cozystack-api rebuilds the
whole spec on every write and emits `DisableWait=false` once the
annotation is gone. An absent install or upgrade block already behaves
as `DisableWait=false` and is materialised only when the definition asks
to disable the wait, so a release with nothing to change is not
rewritten.
Two alternatives were considered. A per-kind `healthCheckExprs` keyed on
`status.printableStatus` cannot work: setting it moves the
VirtualMachine from kstatus to the CEL status evaluator
(`fluxcd/pkg/runtime/cel`), which runs the same `observedGeneration`
check ahead of the expressions, and any `healthCheckExprs` added to this
kind later is a no-op while the wait is disabled. The typed
`spec.release.waitStrategy: legacy` is the narrower option, since the
legacy checker never waits on a custom resource, but it still gates the
release on the chart's Service, and with `external: true` that is a
LoadBalancer the legacy checker holds not ready until it has ingress. So
an external VM would still wait on IP allocation, which `WorkloadsReady`
already watches. Disable-wait avoids that and is the mechanism this
repository already uses for the same class of problem.
Tests: a helm-unittest suite (`tests/readiness_gate_test.yaml`) guards
that the annotation stays on the rendered ApplicationDefinition, and Go
unit tests cover both cozystack-controller paths: the hash moves on a
release annotation and stands still on an unrelated one, and the wait
lands on an existing release, is withdrawn when the annotation goes,
does not materialise empty install/upgrade blocks, does not rewrite a
release already in sync, leaves a release alone when the annotation does
not parse, and does not reach a release of another kind.
Note on existing clusters: no manual step. The definition changes on
upgrade, the config-hash rolls cozystack-api, and the helm reconciler
brings existing VMInstance releases in line. Releases that predate
v1.5.0 also carry a `remediation.retries: -1` + rollback spec, which is
left alone: with the wait disabled the upgrade no longer times out, so
that remediation no longer fires, and any write through the apps API
drops it.
Closes #3734.
### Downstream repositories
- [x] No downstream repository is affected by this change
<!-- Walked the trigger map against the diff. The change is a readiness
annotation on an ApplicationDefinition plus two cozystack-controller
paths. It does not touch openAPISchema, kind, plural, release.prefix,
any app package, any values.schema.json or any installer/platform values
key. The website's application-definitions.md describes the config-hash
as a checksum "over the sorted definitions", which stays accurate now
that the hash also covers the release.cozystack.io/* annotations, and no
page documents those annotations today. -->
### Release note
```release-note
fix(vm-instance): VMInstance HelmReleases no longer hang InProgress or roll back healthy VMs; the release no longer waits on VirtualMachine readiness, which helm-controller's kstatus poller cannot assess correctly for KubeVirt VMs. Existing VMInstance releases are brought in line automatically on upgrade.
```
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* VM installations no longer wait indefinitely when Kubernetes reports
the VM’s observed generation behind its current generation.
* Changes to release settings are now detected and applied to generated
Helm releases, including install and upgrade wait behavior.
* **Tests**
* Added coverage for VM readiness and release-setting reconciliation.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Backport adaptation: release-1.6 has no server-side-apply release
annotation, so TestReleaseAnnotationsShareThePrefix lists the three
annotations this line has, main's TestParseHelmServerSideApplyAnnotation
is not brought in, and the comments count three constants and name
CozyServerOptions.Complete, where this line reads the annotations.
(cherry picked from commit 15035a915b)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
584 lines
21 KiB
Go
584 lines
21 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
|
"github.com/cozystack/cozystack/pkg/config"
|
|
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
)
|
|
|
|
func newAppDefHelmScheme(t *testing.T) *runtime.Scheme {
|
|
t.Helper()
|
|
scheme := runtime.NewScheme()
|
|
if err := cozyv1alpha1.AddToScheme(scheme); err != nil {
|
|
t.Fatalf("add cozyv1alpha1: %v", err)
|
|
}
|
|
if err := helmv2.AddToScheme(scheme); err != nil {
|
|
t.Fatalf("add helmv2: %v", err)
|
|
}
|
|
return scheme
|
|
}
|
|
|
|
// TestAppDefHelm_MissingAppDefIgnored pins IgnoreNotFound: when the
|
|
// ApplicationDefinition referenced by the request has been deleted,
|
|
// Reconcile must not return an error.
|
|
func TestAppDefHelm_MissingAppDefIgnored(t *testing.T) {
|
|
scheme := newAppDefHelmScheme(t)
|
|
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
res, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: "missing"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
if res.RequeueAfter != 0 {
|
|
t.Fatalf("expected empty result, got %+v", res)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_EmptyKindIsNoop pins the early-return: when
|
|
// Application.Kind is empty the reconciler must skip without listing
|
|
// HelmReleases or returning an error.
|
|
func TestAppDefHelm_EmptyKindIsNoop(t *testing.T) {
|
|
scheme := newAppDefHelmScheme(t)
|
|
appDef := &cozyv1alpha1.ApplicationDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "empty-kind"},
|
|
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
|
|
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: ""},
|
|
},
|
|
}
|
|
fakeClient := fake.NewClientBuilder().
|
|
WithScheme(scheme).
|
|
WithObjects(appDef).
|
|
Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: "empty-kind"},
|
|
}); err != nil {
|
|
t.Fatalf("expected nil error, got %v", err)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_ChartRefMismatchPatches pins the happy path: an
|
|
// ApplicationDefinition with a valid ChartRef + a HelmRelease carrying
|
|
// matching application labels but a different ChartRef → the HR's
|
|
// ChartRef is updated to match the appDef.
|
|
func TestAppDefHelm_ChartRefMismatchPatches(t *testing.T) {
|
|
scheme := newAppDefHelmScheme(t)
|
|
|
|
appDef := &cozyv1alpha1.ApplicationDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "harbor"},
|
|
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
|
|
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "Harbor"},
|
|
Release: cozyv1alpha1.ApplicationDefinitionRelease{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "OCIRepository",
|
|
Name: "harbor-app",
|
|
Namespace: "cozy-public",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "harbor-release",
|
|
Namespace: "tenant-foo",
|
|
Labels: map[string]string{
|
|
"apps.cozystack.io/application.kind": "Harbor",
|
|
"apps.cozystack.io/application.group": "apps.cozystack.io",
|
|
},
|
|
},
|
|
Spec: helmv2.HelmReleaseSpec{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "OCIRepository",
|
|
Name: "harbor-app-stale",
|
|
Namespace: "cozy-public",
|
|
},
|
|
},
|
|
}
|
|
|
|
fakeClient := fake.NewClientBuilder().
|
|
WithScheme(scheme).
|
|
WithObjects(appDef, hr).
|
|
Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: "harbor"},
|
|
}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
got := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: "harbor-release", Namespace: "tenant-foo"}, got); err != nil {
|
|
t.Fatalf("get HR: %v", err)
|
|
}
|
|
if got.Spec.ChartRef == nil || got.Spec.ChartRef.Name != "harbor-app" {
|
|
t.Fatalf("expected ChartRef.Name=harbor-app, got %+v", got.Spec.ChartRef)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_ValuesFromMismatchPatches pins the valuesFrom drift
|
|
// path: HR has the right ChartRef but missing/wrong valuesFrom →
|
|
// reconciler must rewrite valuesFrom to the canonical [{Secret,
|
|
// cozystack-values}].
|
|
func TestAppDefHelm_ValuesFromMismatchPatches(t *testing.T) {
|
|
scheme := newAppDefHelmScheme(t)
|
|
|
|
appDef := &cozyv1alpha1.ApplicationDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "harbor"},
|
|
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
|
|
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "Harbor"},
|
|
Release: cozyv1alpha1.ApplicationDefinitionRelease{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "OCIRepository",
|
|
Name: "harbor-app",
|
|
Namespace: "cozy-public",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "harbor-release",
|
|
Namespace: "tenant-foo",
|
|
Labels: map[string]string{
|
|
"apps.cozystack.io/application.kind": "Harbor",
|
|
"apps.cozystack.io/application.group": "apps.cozystack.io",
|
|
},
|
|
},
|
|
Spec: helmv2.HelmReleaseSpec{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "OCIRepository",
|
|
Name: "harbor-app",
|
|
Namespace: "cozy-public",
|
|
},
|
|
ValuesFrom: []helmv2.ValuesReference{
|
|
{Kind: "ConfigMap", Name: "wrong"},
|
|
},
|
|
},
|
|
}
|
|
|
|
fakeClient := fake.NewClientBuilder().
|
|
WithScheme(scheme).
|
|
WithObjects(appDef, hr).
|
|
Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: "harbor"},
|
|
}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
got := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: "harbor-release", Namespace: "tenant-foo"}, got); err != nil {
|
|
t.Fatalf("get HR: %v", err)
|
|
}
|
|
if len(got.Spec.ValuesFrom) != 1 {
|
|
t.Fatalf("expected exactly one valuesFrom entry, got %d (%+v)", len(got.Spec.ValuesFrom), got.Spec.ValuesFrom)
|
|
}
|
|
if got.Spec.ValuesFrom[0].Kind != "Secret" || got.Spec.ValuesFrom[0].Name != "cozystack-values" {
|
|
t.Fatalf("expected {Secret,cozystack-values}, got %+v", got.Spec.ValuesFrom[0])
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_LabelsApplied pins the label propagation: when the
|
|
// ApplicationDefinition declares Release.Labels, those are merged into
|
|
// HR labels.
|
|
func TestAppDefHelm_LabelsApplied(t *testing.T) {
|
|
scheme := newAppDefHelmScheme(t)
|
|
|
|
appDef := &cozyv1alpha1.ApplicationDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "harbor"},
|
|
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
|
|
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "Harbor"},
|
|
Release: cozyv1alpha1.ApplicationDefinitionRelease{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "OCIRepository",
|
|
Name: "harbor-app",
|
|
Namespace: "cozy-public",
|
|
},
|
|
Labels: map[string]string{
|
|
"cozystack.io/managed-by": "applicationdefinition",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "harbor-release",
|
|
Namespace: "tenant-foo",
|
|
Labels: map[string]string{
|
|
"apps.cozystack.io/application.kind": "Harbor",
|
|
"apps.cozystack.io/application.group": "apps.cozystack.io",
|
|
},
|
|
},
|
|
Spec: helmv2.HelmReleaseSpec{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "OCIRepository",
|
|
Name: "harbor-app",
|
|
Namespace: "cozy-public",
|
|
},
|
|
ValuesFrom: []helmv2.ValuesReference{
|
|
{Kind: "Secret", Name: "cozystack-values"},
|
|
},
|
|
},
|
|
}
|
|
|
|
fakeClient := fake.NewClientBuilder().
|
|
WithScheme(scheme).
|
|
WithObjects(appDef, hr).
|
|
Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: "harbor"},
|
|
}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
got := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: "harbor-release", Namespace: "tenant-foo"}, got); err != nil {
|
|
t.Fatalf("get HR: %v", err)
|
|
}
|
|
if got.Labels["cozystack.io/managed-by"] != "applicationdefinition" {
|
|
t.Fatalf("expected appDef label propagated, got %q", got.Labels["cozystack.io/managed-by"])
|
|
}
|
|
// Original labels must still be present
|
|
if got.Labels["apps.cozystack.io/application.kind"] != "Harbor" {
|
|
t.Fatalf("original label dropped: %v", got.Labels)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_NilChartRefSkipped pins the validation guard: when
|
|
// the ApplicationDefinition.Spec.Release.ChartRef is nil, the
|
|
// reconciler must log + skip without erroring.
|
|
func TestAppDefHelm_NilChartRefSkipped(t *testing.T) {
|
|
scheme := newAppDefHelmScheme(t)
|
|
|
|
appDef := &cozyv1alpha1.ApplicationDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "broken"},
|
|
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
|
|
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "Broken"},
|
|
Release: cozyv1alpha1.ApplicationDefinitionRelease{ChartRef: nil},
|
|
},
|
|
}
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "broken-release",
|
|
Namespace: "tenant-foo",
|
|
Labels: map[string]string{
|
|
"apps.cozystack.io/application.kind": "Broken",
|
|
"apps.cozystack.io/application.group": "apps.cozystack.io",
|
|
},
|
|
},
|
|
}
|
|
|
|
fakeClient := fake.NewClientBuilder().
|
|
WithScheme(scheme).
|
|
WithObjects(appDef, hr).
|
|
Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: "broken"},
|
|
}); err != nil {
|
|
t.Fatalf("expected nil error on nil ChartRef, got %v", err)
|
|
}
|
|
|
|
// HR must remain untouched
|
|
got := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: "broken-release", Namespace: "tenant-foo"}, got); err != nil {
|
|
t.Fatalf("get HR: %v", err)
|
|
}
|
|
if got.Spec.ChartRef != nil {
|
|
t.Fatalf("HR ChartRef should remain nil when appDef ChartRef is nil, got %+v", got.Spec.ChartRef)
|
|
}
|
|
}
|
|
|
|
// TestValuesFromEqual pins the helper used to detect drift in the
|
|
// valuesFrom slice.
|
|
func TestValuesFromEqual(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
a, b []helmv2.ValuesReference
|
|
want bool
|
|
}{
|
|
{
|
|
name: "both empty",
|
|
a: nil, b: nil,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "different lengths",
|
|
a: []helmv2.ValuesReference{{Kind: "Secret", Name: "x"}},
|
|
b: nil,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "same single entry",
|
|
a: []helmv2.ValuesReference{{Kind: "Secret", Name: "cozystack-values"}},
|
|
b: []helmv2.ValuesReference{{Kind: "Secret", Name: "cozystack-values"}},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "same length, different Kind",
|
|
a: []helmv2.ValuesReference{{Kind: "Secret", Name: "x"}},
|
|
b: []helmv2.ValuesReference{{Kind: "ConfigMap", Name: "x"}},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "same length, different ValuesKey",
|
|
a: []helmv2.ValuesReference{{Kind: "Secret", Name: "x", ValuesKey: "a"}},
|
|
b: []helmv2.ValuesReference{{Kind: "Secret", Name: "x", ValuesKey: "b"}},
|
|
want: false,
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := valuesFromEqual(tc.a, tc.b); got != tc.want {
|
|
t.Errorf("valuesFromEqual(%+v, %+v) = %v, want %v", tc.a, tc.b, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// vmInstanceRelease builds a VMInstance HelmRelease carrying the application
|
|
// labels this reconciler selects on, with the given wait configuration.
|
|
func vmInstanceRelease(install, upgrade *bool) *helmv2.HelmRelease {
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "vm-instance-web",
|
|
Namespace: "tenant-foo",
|
|
Labels: map[string]string{
|
|
"apps.cozystack.io/application.kind": "VMInstance",
|
|
"apps.cozystack.io/application.group": "apps.cozystack.io",
|
|
},
|
|
},
|
|
Spec: helmv2.HelmReleaseSpec{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "ExternalArtifact",
|
|
Name: "vm-instance-app",
|
|
Namespace: "cozy-system",
|
|
},
|
|
},
|
|
}
|
|
if install != nil {
|
|
hr.Spec.Install = &helmv2.Install{DisableWait: *install}
|
|
}
|
|
if upgrade != nil {
|
|
hr.Spec.Upgrade = &helmv2.Upgrade{DisableWait: *upgrade}
|
|
}
|
|
return hr
|
|
}
|
|
|
|
// vmInstanceAppDef builds the VMInstance ApplicationDefinition, optionally
|
|
// carrying the disable-wait annotation.
|
|
func vmInstanceAppDef(annotations map[string]string) *cozyv1alpha1.ApplicationDefinition {
|
|
return &cozyv1alpha1.ApplicationDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{Name: "vm-instance", Annotations: annotations},
|
|
Spec: cozyv1alpha1.ApplicationDefinitionSpec{
|
|
Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "VMInstance"},
|
|
Release: cozyv1alpha1.ApplicationDefinitionRelease{
|
|
ChartRef: &helmv2.CrossNamespaceSourceReference{
|
|
Kind: "ExternalArtifact",
|
|
Name: "vm-instance-app",
|
|
Namespace: "cozy-system",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func reconcileVMInstance(t *testing.T, appDef *cozyv1alpha1.ApplicationDefinition, hr *helmv2.HelmRelease) *helmv2.HelmRelease {
|
|
t.Helper()
|
|
|
|
scheme := newAppDefHelmScheme(t)
|
|
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build()
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: appDef.Name},
|
|
}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
got := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, got); err != nil {
|
|
t.Fatalf("get HR: %v", err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitApplied pins the drift this reconciler exists to
|
|
// close, for the one field a running release cannot pick up on its own: a
|
|
// HelmRelease created before the definition carried
|
|
// release.cozystack.io/helm-install-disable-wait keeps waiting on readiness
|
|
// until its Application is written through the apps API again. On a cluster
|
|
// where the wait is what broke (a KubeVirt VirtualMachine the kstatus poller
|
|
// reads as permanently InProgress), that is every existing VM, each one
|
|
// looping upgrade and timeout, and re-writing them by hand is the operator's
|
|
// only way out. Bring the field to what the definition asks for instead.
|
|
func TestAppDefHelm_DisableWaitApplied(t *testing.T) {
|
|
no := false
|
|
got := reconcileVMInstance(t,
|
|
vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}),
|
|
vmInstanceRelease(&no, &no))
|
|
|
|
if got.Spec.Install == nil || !got.Spec.Install.DisableWait {
|
|
t.Fatalf("expected Install.DisableWait=true, got %+v", got.Spec.Install)
|
|
}
|
|
if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait {
|
|
t.Fatalf("expected Upgrade.DisableWait=true, got %+v", got.Spec.Upgrade)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitAppliedToAbsentBlocks pins the same on a release
|
|
// that carries no install/upgrade block at all: the field still has to land.
|
|
func TestAppDefHelm_DisableWaitAppliedToAbsentBlocks(t *testing.T) {
|
|
got := reconcileVMInstance(t,
|
|
vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}),
|
|
vmInstanceRelease(nil, nil))
|
|
|
|
if got.Spec.Install == nil || !got.Spec.Install.DisableWait {
|
|
t.Fatalf("expected Install.DisableWait=true, got %+v", got.Spec.Install)
|
|
}
|
|
if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait {
|
|
t.Fatalf("expected Upgrade.DisableWait=true, got %+v", got.Spec.Upgrade)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitRemoved pins the other direction, because the
|
|
// definition is the source of truth in both: cozystack-api rebuilds the whole
|
|
// spec on every write and emits DisableWait=false once the annotation is gone,
|
|
// so a release left at true would drift back the moment anyone touched it.
|
|
func TestAppDefHelm_DisableWaitRemoved(t *testing.T) {
|
|
yes := true
|
|
got := reconcileVMInstance(t, vmInstanceAppDef(nil), vmInstanceRelease(&yes, &yes))
|
|
|
|
if got.Spec.Install == nil || got.Spec.Install.DisableWait {
|
|
t.Fatalf("expected Install.DisableWait=false, got %+v", got.Spec.Install)
|
|
}
|
|
if got.Spec.Upgrade == nil || got.Spec.Upgrade.DisableWait {
|
|
t.Fatalf("expected Upgrade.DisableWait=false, got %+v", got.Spec.Upgrade)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitLeavesAbsentBlocksAlone pins that the false case
|
|
// does not materialise empty install/upgrade blocks on a release that has
|
|
// none: writing them would change nothing about the release's behaviour and
|
|
// would make every such reconcile an update.
|
|
func TestAppDefHelm_DisableWaitLeavesAbsentBlocksAlone(t *testing.T) {
|
|
got := reconcileVMInstance(t, vmInstanceAppDef(nil), vmInstanceRelease(nil, nil))
|
|
|
|
if got.Spec.Install != nil {
|
|
t.Fatalf("expected Install left absent, got %+v", got.Spec.Install)
|
|
}
|
|
if got.Spec.Upgrade != nil {
|
|
t.Fatalf("expected Upgrade left absent, got %+v", got.Spec.Upgrade)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitInSyncSkipsUpdate pins idempotence: a release that
|
|
// already matches must not be written, or every reconcile of every definition
|
|
// bumps the resourceVersion of every release it owns and wakes helm-controller.
|
|
func TestAppDefHelm_DisableWaitInSyncSkipsUpdate(t *testing.T) {
|
|
yes := true
|
|
hr := vmInstanceRelease(&yes, &yes)
|
|
hr.Spec.ValuesFrom = expectedValuesFrom()
|
|
|
|
scheme := newAppDefHelmScheme(t)
|
|
appDef := vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"})
|
|
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build()
|
|
|
|
before := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, before); err != nil {
|
|
t.Fatalf("get HR: %v", err)
|
|
}
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: appDef.Name},
|
|
}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
after := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, after); err != nil {
|
|
t.Fatalf("get HR after reconcile: %v", err)
|
|
}
|
|
if after.ResourceVersion != before.ResourceVersion {
|
|
t.Fatalf("release rewritten with nothing to change: %q -> %q", before.ResourceVersion, after.ResourceVersion)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitInvalidAnnotationLeavesReleaseAlone pins the
|
|
// invalid-annotation branch. The parser returns false alongside its error, so
|
|
// applying that value would switch the wait back on for every release of the
|
|
// kind; the release must keep what it has instead.
|
|
func TestAppDefHelm_DisableWaitInvalidAnnotationLeavesReleaseAlone(t *testing.T) {
|
|
yes := true
|
|
got := reconcileVMInstance(t,
|
|
vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "yes"}),
|
|
vmInstanceRelease(&yes, &yes))
|
|
|
|
if got.Spec.Install == nil || !got.Spec.Install.DisableWait {
|
|
t.Fatalf("expected Install.DisableWait=true kept, got %+v", got.Spec.Install)
|
|
}
|
|
if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait {
|
|
t.Fatalf("expected Upgrade.DisableWait=true kept, got %+v", got.Spec.Upgrade)
|
|
}
|
|
}
|
|
|
|
// TestAppDefHelm_DisableWaitScopedToKind pins the kind label in the selector:
|
|
// a release of another kind in the same group must not pick up the wait
|
|
// setting of the VMInstance definition.
|
|
func TestAppDefHelm_DisableWaitScopedToKind(t *testing.T) {
|
|
no := false
|
|
vm := vmInstanceRelease(&no, &no)
|
|
other := vmInstanceRelease(&no, &no)
|
|
other.Name = "harbor-registry"
|
|
other.Labels["apps.cozystack.io/application.kind"] = "Harbor"
|
|
|
|
scheme := newAppDefHelmScheme(t)
|
|
appDef := vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"})
|
|
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, vm, other).Build()
|
|
|
|
r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme}
|
|
if _, err := r.Reconcile(context.TODO(), reconcile.Request{
|
|
NamespacedName: types.NamespacedName{Name: appDef.Name},
|
|
}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
gotVM := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: vm.Name, Namespace: vm.Namespace}, gotVM); err != nil {
|
|
t.Fatalf("get VMInstance HR: %v", err)
|
|
}
|
|
if gotVM.Spec.Install == nil || !gotVM.Spec.Install.DisableWait {
|
|
t.Fatalf("expected VMInstance Install.DisableWait=true, got %+v", gotVM.Spec.Install)
|
|
}
|
|
|
|
gotOther := &helmv2.HelmRelease{}
|
|
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: other.Name, Namespace: other.Namespace}, gotOther); err != nil {
|
|
t.Fatalf("get Harbor HR: %v", err)
|
|
}
|
|
if gotOther.Spec.Install == nil || gotOther.Spec.Install.DisableWait {
|
|
t.Fatalf("expected Harbor Install.DisableWait=false, got %+v", gotOther.Spec.Install)
|
|
}
|
|
if gotOther.Spec.Upgrade == nil || gotOther.Spec.Upgrade.DisableWait {
|
|
t.Fatalf("expected Harbor Upgrade.DisableWait=false, got %+v", gotOther.Spec.Upgrade)
|
|
}
|
|
}
|