mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 23:23:23 +09:00
Some charts run work that matters to the workload in a Job outside the Helm release, such as an index repair after an image upgrade. Once the release has applied, Ready stays True whatever that Job does, so a failed run was visible only to someone who went looking for the Job. A Job that carries a monitor's selector labels and workloads.cozystack.io/report-failure set to "true", and has Failed=True, now makes the monitor not operational with reason JobFailed and a message naming the Job and its failure, which the Application surfaces as WorkloadsReady=False. The opt-in is explicit because the selector labels alone are carried by Jobs that say nothing about the workload's health: Jobs an operator labels by inheritance from the workload, such as CloudNativePG's initdb and join Jobs under the postgres chart, and backup runs. Of the Jobs one controller owns only the newest counts, because a CronJob keeps failed runs in its history after a later run succeeds; a Job that is deleted or replaced by a successful run clears the verdict. A DataVolume verdict still comes first, since its stored value is kept across restarts until the DataVolumes can be read again. A failed Job outranks an unprovisioned BucketClaim: it stays failed until something replaces it, while a claim is usually still being provisioned. The manager caches only Jobs that carry the opt-in label, and of them only their metadata and conditions. The controller's ClusterRole already grants read on every resource. Refs: #5459 Assisted-By: LLM Signed-off-by: Andrey Kolkov <androndo@gmail.com>
432 lines
13 KiB
Go
432 lines
13 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
|
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
|
|
"github.com/cozystack/cozystack/pkg/config"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
|
|
helmv2 "github.com/fluxcd/helm-controller/api/v2"
|
|
)
|
|
|
|
func newTestRESTWithSchemes(objs ...runtime.Object) *REST {
|
|
scheme := runtime.NewScheme()
|
|
_ = cozyv1alpha1.AddToScheme(scheme)
|
|
_ = helmv2.AddToScheme(scheme)
|
|
|
|
builder := fake.NewClientBuilder().WithScheme(scheme)
|
|
for _, obj := range objs {
|
|
builder = builder.WithRuntimeObjects(obj)
|
|
}
|
|
c := builder.Build()
|
|
|
|
return NewREST(c, nil, nil, &config.Resource{
|
|
Application: config.ApplicationConfig{
|
|
Kind: "PostgreSQL",
|
|
Plural: "postgresqls",
|
|
Singular: "postgresql",
|
|
},
|
|
Release: config.ReleaseConfig{
|
|
Prefix: "postgresql-",
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_NoMonitors(t *testing.T) {
|
|
r := newTestRESTWithSchemes()
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if ws.found {
|
|
t.Error("expected found=false when no monitors exist")
|
|
}
|
|
if !ws.operational {
|
|
t.Error("expected operational=true when no monitors exist")
|
|
}
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_AllOperational(t *testing.T) {
|
|
m1 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(true),
|
|
},
|
|
}
|
|
m2 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-2",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(true),
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(m1, m2)
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !ws.found {
|
|
t.Error("expected found=true")
|
|
}
|
|
if !ws.operational {
|
|
t.Error("expected operational=true when all monitors are operational")
|
|
}
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_SomeNotOperational(t *testing.T) {
|
|
m1 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(true),
|
|
},
|
|
}
|
|
m2 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-2",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(false),
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(m1, m2)
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !ws.found {
|
|
t.Error("expected found=true")
|
|
}
|
|
if ws.operational {
|
|
t.Error("expected operational=false when at least one monitor is not operational")
|
|
}
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_OperationalNil(t *testing.T) {
|
|
m := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: nil, // Not yet reconciled
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(m)
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !ws.found {
|
|
t.Error("expected found=true")
|
|
}
|
|
if !ws.unknown {
|
|
t.Error("expected unknown=true when Operational is nil")
|
|
}
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_MixedNilAndOperational(t *testing.T) {
|
|
m1 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(true),
|
|
},
|
|
}
|
|
m2 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-2",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: nil,
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(m1, m2)
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !ws.unknown {
|
|
t.Error("expected unknown=true when at least one monitor has nil Operational")
|
|
}
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_MixedFailedAndPending(t *testing.T) {
|
|
m1 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(false), // Confirmed failure
|
|
},
|
|
}
|
|
m2 := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-2",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: nil, // Not yet reconciled
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(m1, m2)
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if ws.operational {
|
|
t.Error("expected operational=false when at least one monitor is explicitly failed")
|
|
}
|
|
if !ws.unknown {
|
|
t.Error("expected unknown=true when at least one monitor has nil Operational")
|
|
}
|
|
}
|
|
|
|
func TestConvertConditions_MixedFailedAndPendingShowsFalse(t *testing.T) {
|
|
mFailed := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-failed",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(false),
|
|
},
|
|
}
|
|
mPending := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-pending",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: nil,
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(mFailed, mPending)
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "postgresql-mydb",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
ApplicationKindLabel: "PostgreSQL",
|
|
ApplicationGroupLabel: "apps.cozystack.io",
|
|
ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
}
|
|
hr.Status.Conditions = []metav1.Condition{
|
|
{Type: "Ready", Status: metav1.ConditionTrue, Reason: "Succeeded", Message: "ok"},
|
|
}
|
|
|
|
app, err := r.convertHelmReleaseToApplication(context.TODO(), hr, nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
// Concrete failure should take priority over unknown
|
|
wc := findCondition(app.GetConditions(), "WorkloadsReady")
|
|
if wc == nil {
|
|
t.Fatal("expected WorkloadsReady condition")
|
|
}
|
|
if wc.Status != metav1.ConditionFalse {
|
|
t.Errorf("expected WorkloadsReady=False (failure takes priority over unknown), got %s", wc.Status)
|
|
}
|
|
}
|
|
|
|
func TestGetWorkloadsOperational_DifferentApp_NotFound(t *testing.T) {
|
|
m := &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "mon-1",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "other-db",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{
|
|
Operational: new(false),
|
|
},
|
|
}
|
|
|
|
r := newTestRESTWithSchemes(m)
|
|
ws, err := r.getWorkloadsOperational(context.TODO(), "default", "mydb", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if ws.found {
|
|
t.Error("expected found=false for different app name")
|
|
}
|
|
if !ws.operational {
|
|
t.Error("expected operational=true when no matching monitors found")
|
|
}
|
|
}
|
|
|
|
func TestConvertConditions_WorkloadsReadyCarriesMonitorMessages(t *testing.T) {
|
|
monitor := func(name string, operational bool, message, reason string) *cozyv1alpha1.WorkloadMonitor {
|
|
return &cozyv1alpha1.WorkloadMonitor{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: name,
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
appsv1alpha1.ApplicationKindLabel: "PostgreSQL",
|
|
appsv1alpha1.ApplicationGroupLabel: "apps.cozystack.io",
|
|
appsv1alpha1.ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
Status: cozyv1alpha1.WorkloadMonitorStatus{Operational: new(operational), Message: message, Reason: reason},
|
|
}
|
|
}
|
|
hr := &helmv2.HelmRelease{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "postgresql-mydb",
|
|
Namespace: "default",
|
|
Labels: map[string]string{
|
|
ApplicationKindLabel: "PostgreSQL",
|
|
ApplicationGroupLabel: "apps.cozystack.io",
|
|
ApplicationNameLabel: "mydb",
|
|
},
|
|
},
|
|
}
|
|
hr.Status.Conditions = []metav1.Condition{
|
|
{Type: "Ready", Status: metav1.ConditionTrue, Reason: "Succeeded", Message: "ok"},
|
|
}
|
|
|
|
cases := []struct {
|
|
name string
|
|
monitors []runtime.Object
|
|
want string
|
|
reason string
|
|
}{
|
|
{
|
|
"messages of the monitors that are not operational, sorted",
|
|
[]runtime.Object{
|
|
monitor("mon-b", false, "DataVolume b is ImportInProgress", cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady),
|
|
monitor("mon-a", false, "DataVolume a is Failed", cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady),
|
|
monitor("mon-c", true, "", ""),
|
|
},
|
|
"DataVolume a is Failed; DataVolume b is ImportInProgress",
|
|
cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady,
|
|
},
|
|
{
|
|
"a named cause beside a monitor that names none",
|
|
[]runtime.Object{
|
|
monitor("mon-a", false, "", ""),
|
|
monitor("mon-b", false, "DataVolume b is Failed", cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady),
|
|
},
|
|
"DataVolume b is Failed",
|
|
cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady,
|
|
},
|
|
{
|
|
"a failed Job",
|
|
[]runtime.Object{
|
|
monitor("mon-a", false, "Job mydb-repair failed: BackoffLimitExceeded: Job has reached the specified backoff limit", cozyv1alpha1.WorkloadMonitorReasonJobFailed),
|
|
},
|
|
"Job mydb-repair failed: BackoffLimitExceeded: Job has reached the specified backoff limit",
|
|
cozyv1alpha1.WorkloadMonitorReasonJobFailed,
|
|
},
|
|
{
|
|
"generic message when no monitor names a cause",
|
|
[]runtime.Object{monitor("mon-a", false, "", "")},
|
|
"One or more workloads are not operational",
|
|
"WorkloadMonitorCheck",
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := newTestRESTWithSchemes(tc.monitors...)
|
|
app, err := r.convertHelmReleaseToApplication(context.TODO(), hr, nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
wc := findCondition(app.GetConditions(), "WorkloadsReady")
|
|
if wc == nil || wc.Status != metav1.ConditionFalse {
|
|
t.Fatalf("WorkloadsReady = %+v, want False", wc)
|
|
}
|
|
if wc.Message != tc.want {
|
|
t.Errorf("message = %q, want %q", wc.Message, tc.want)
|
|
}
|
|
if wc.Reason != tc.reason {
|
|
t.Errorf("reason = %q, want %q", wc.Reason, tc.reason)
|
|
}
|
|
})
|
|
}
|
|
}
|