fix(workloadmonitor): report a bucket's workloads not ready until its claim is provisioned (#5096)

## What this PR does

A Bucket app reports WorkloadsReady=True while its BucketClaim is never
provisioned. On a v1.6.5 cluster a tenant got a BucketClaim for bucket
class `tenant-root`, the root tenant there runs no SeaweedFS, and for
three days the claim had no status while the objectstorage controller
kept emitting `FailedCreateBucket` with
`bucketclasses.objectstorage.k8s.io "tenant-root" not found`. The Bucket
app showed Ready and WorkloadsReady the whole time.

The WorkloadMonitor controller already writes a Workload per BucketClaim
with `Operational` set from `status.bucketReady`, but the monitor itself
counts only pods. The bucket chart renders its monitor with `replicas:
0` and `minReplicas: 0`, so the monitor stays operational whatever
happens to the claim.

The monitor now names every selected BucketClaim that is not provisioned
in `status.message`, sets `status.reason` to the new
`BucketClaimNotReady` and turns `Operational` false, the same way it
already reports a DataVolume that is not ready. A DataVolume verdict
keeps the fields when both apply. BucketClaims are listed on every pass,
so a stored BucketClaim verdict is recomputed rather than kept like an
unread DataVolume verdict, and it clears once the claim is provisioned.
Any monitor whose selector picks a BucketClaim follows its readiness,
which is what the Workload objects already say. The Application then
shows WorkloadsReady=False with that message and reason. Its Ready
condition keeps following the HelmRelease, as
`pkg/registry/apps/application/rest.go` intends.

Three new tests cover a provisioned claim, a claim not provisioned with
and without a class, a claim outside the selector, the verdict clearing
after provisioning with and without a DataVolume reader, and the
DataVolume verdict winning over the BucketClaim one. Against the
controller on main the four not-provisioned and clearing cases fail and
the rest pass. `go test ./internal/controller/...` and `go vet` pass.
`make generate` does not run on my machine, gengo fails to parse the
go1.27.1 standard library, so I ran the controller-gen `object` and
`crd` steps of `hack/update-codegen.sh` for `./api/v1alpha1/...`
directly. The deepcopy file is unchanged and the generated
WorkloadMonitor CRD matches
`packages/system/cozystack-controller/definitions/cozystack.io_workloadmonitors.yaml`.
I have not run the change on a live cluster.

#5083 changes the Workload names of the same BucketClaims and does not
touch the monitor status, so the two do not overlap.

### Screenshots

Not a UI change.

### Downstream repositories

The controller, one exported constant and its tests. No CRD schema,
values key, default, telemetry metric or package change, so no trigger
map entry applies.

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(workloadmonitor): a Bucket app reports WorkloadsReady=False with reason BucketClaimNotReady while its BucketClaim is not provisioned, instead of WorkloadsReady=True.
```

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Workload monitors now report when a selected bucket claim is not
provisioned and mark the monitor as non-operational. Status messages
include the bucket class when available and list multiple unprovisioned
claims in sorted order.
* **Bug Fixes**
* The bucket-claim status clears when the claim is provisioned or no
longer selected. When DataVolume information is available, its status
takes precedence over the bucket-claim status.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Backported-from: 6f8171db89
This commit is contained in:
Myasnikov Daniil committed 2026-11-25 10:15:40 +00:00
commit b8cc7b6f3e
3 files changed
+168 -3

No files matched your search

+4
View File
@@ -39,6 +39,10 @@ type WorkloadMonitorSpec struct {
// that is not operational because a DataVolume it selects is not ready.
const WorkloadMonitorReasonDataVolumeNotReady = "DataVolumeNotReady"
// WorkloadMonitorReasonBucketClaimNotReady is the status Reason of a monitor
// that is not operational because a BucketClaim it selects is not provisioned.
const WorkloadMonitorReasonBucketClaimNotReady = "BucketClaimNotReady"
// WorkloadMonitorStatus defines the observed state of WorkloadMonitor
type WorkloadMonitorStatus struct {
// Operational indicates if the workload meets all operational requirements
@@ -374,6 +374,25 @@ func (r *WorkloadMonitorReconciler) isBucketClaimReady(bc *cosiv1alpha1.BucketCl
return bc.Status.BucketReady
}
// bucketClaimsMessage names every BucketClaim that has not been provisioned,
// with its bucket class, and is empty when there is none.
func (r *WorkloadMonitorReconciler) bucketClaimsMessage(claims []cosiv1alpha1.BucketClaim) string {
var pending []string
for i := range claims {
bc := &claims[i]
if r.isBucketClaimReady(bc) {
continue
}
entry := fmt.Sprintf("BucketClaim %s is not provisioned", bc.Name)
if bc.Spec.BucketClassName != "" {
entry = fmt.Sprintf("BucketClaim %s of bucket class %s is not provisioned", bc.Name, bc.Spec.BucketClassName)
}
pending = append(pending, entry)
}
sort.Strings(pending)
return strings.Join(pending, "; ")
}
// isServiceReady checks if the service has an external IP bound
func (r *WorkloadMonitorReconciler) isServiceReady(svc *corev1.Service) bool {
return len(svc.Status.LoadBalancer.Ingress) > 0
@@ -1023,6 +1042,8 @@ func (r *WorkloadMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
}
bucketsMessage := r.bucketClaimsMessage(bucketClaimList.Items)
// Update WorkloadMonitor status based on observed pods
monitor.Status.ObservedReplicas = observedReplicas
monitor.Status.AvailableReplicas = availableReplicas
@@ -1044,15 +1065,23 @@ func (r *WorkloadMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
if fresh.Spec.MinReplicas != nil && availableReplicas < *fresh.Spec.MinReplicas {
fresh.Status.Operational = pointer.Bool(false)
}
// Only the DataVolume check writes Message and Reason, so the stored ones
// are the last DataVolume verdict: kept while no reader is installed yet, so a
// controller restart does not report a stuck disk as ready.
// A stored DataVolume verdict is the last one: kept while no reader is
// installed yet, so a controller restart does not report a stuck disk as
// ready. BucketClaims are listed on every pass, so their verdict is
// recomputed and only fills fields no DataVolume verdict holds.
if dataVolumesRead {
fresh.Status.Message = dataVolumesMessage
fresh.Status.Reason = ""
if dataVolumesMessage != "" {
fresh.Status.Reason = cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady
}
} else if fresh.Status.Reason == cozyv1alpha1.WorkloadMonitorReasonBucketClaimNotReady {
fresh.Status.Message = ""
fresh.Status.Reason = ""
}
if fresh.Status.Message == "" && bucketsMessage != "" {
fresh.Status.Message = bucketsMessage
fresh.Status.Reason = cozyv1alpha1.WorkloadMonitorReasonBucketClaimNotReady
}
if fresh.Status.Message != "" {
fresh.Status.Operational = pointer.Bool(false)
@@ -2433,3 +2433,135 @@ func TestReconcile_PVCOfANotReadyDataVolumeIsNotOperational(t *testing.T) {
})
}
}
func newBucketClaim(name string, labels map[string]string, class string, ready bool) *cosiv1alpha1.BucketClaim {
return &cosiv1alpha1.BucketClaim{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "default", Labels: labels},
Spec: cosiv1alpha1.BucketClaimSpec{
BucketClassName: class,
Protocols: []cosiv1alpha1.Protocol{cosiv1alpha1.ProtocolS3},
},
Status: cosiv1alpha1.BucketClaimStatus{BucketReady: ready},
}
}
func reconcileBucketMonitor(t *testing.T, c client.Client, s *runtime.Scheme, withReader bool) *cozyv1alpha1.WorkloadMonitor {
t.Helper()
reconciler := &WorkloadMonitorReconciler{Client: c, Scheme: s}
if withReader {
reconciler.DataVolumeReader = c
}
req := reconcile.Request{NamespacedName: types.NamespacedName{Name: "my-bucket", Namespace: "default"}}
if _, err := reconciler.Reconcile(context.TODO(), req); err != nil {
t.Fatalf("Reconcile returned error: %v", err)
}
updated := &cozyv1alpha1.WorkloadMonitor{}
if err := c.Get(context.TODO(), req.NamespacedName, updated); err != nil {
t.Fatalf("Failed to get updated WorkloadMonitor: %v", err)
}
if updated.Status.Operational == nil {
t.Fatal("Expected Operational to be set, got nil")
}
return updated
}
func newBucketMonitorClient(s *runtime.Scheme, objs ...client.Object) client.Client {
monitor := &cozyv1alpha1.WorkloadMonitor{
ObjectMeta: metav1.ObjectMeta{Name: "my-bucket", Namespace: "default"},
Spec: cozyv1alpha1.WorkloadMonitorSpec{
Kind: "bucket",
Type: "s3",
Selector: map[string]string{"app.kubernetes.io/instance": "my-bucket"},
Replicas: ptr.To[int32](0),
MinReplicas: ptr.To[int32](0),
},
}
return fake.NewClientBuilder().
WithScheme(s).
WithObjects(append([]client.Object{monitor}, objs...)...).
WithStatusSubresource(monitor).
Build()
}
func TestReconcile_BucketClaimDecidesOperational(t *testing.T) {
selected := map[string]string{"app.kubernetes.io/instance": "my-bucket"}
cases := []struct {
name string
claim *cosiv1alpha1.BucketClaim
operational bool
message string
}{
{"provisioned", newBucketClaim("my-bucket", selected, "tenant-root", true), true, ""},
{"not provisioned", newBucketClaim("my-bucket", selected, "tenant-root", false), false, "BucketClaim my-bucket of bucket class tenant-root is not provisioned"},
{"not provisioned without a class", newBucketClaim("my-bucket", selected, "", false), false, "BucketClaim my-bucket is not provisioned"},
{"outside the selector", newBucketClaim("other", map[string]string{"app.kubernetes.io/instance": "other"}, "tenant-root", false), true, ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s := newTestScheme()
got := reconcileBucketMonitor(t, newBucketMonitorClient(s, tc.claim), s, true)
if *got.Status.Operational != tc.operational {
t.Errorf("Operational = %v, want %v", *got.Status.Operational, tc.operational)
}
if got.Status.Message != tc.message {
t.Errorf("Message = %q, want %q", got.Status.Message, tc.message)
}
wantReason := ""
if tc.message != "" {
wantReason = cozyv1alpha1.WorkloadMonitorReasonBucketClaimNotReady
}
if got.Status.Reason != wantReason {
t.Errorf("Reason = %q, want %q", got.Status.Reason, wantReason)
}
})
}
}
func TestReconcile_ProvisionedBucketClaimClearsTheVerdict(t *testing.T) {
for _, withReader := range []bool{true, false} {
t.Run(fmt.Sprintf("dataVolumeReader=%v", withReader), func(t *testing.T) {
s := newTestScheme()
claim := newBucketClaim("my-bucket", map[string]string{"app.kubernetes.io/instance": "my-bucket"}, "tenant-root", false)
c := newBucketMonitorClient(s, claim)
got := reconcileBucketMonitor(t, c, s, withReader)
if *got.Status.Operational || got.Status.Reason != cozyv1alpha1.WorkloadMonitorReasonBucketClaimNotReady {
t.Fatalf("before provisioning: Operational = %v, Reason = %q", *got.Status.Operational, got.Status.Reason)
}
stored := &cosiv1alpha1.BucketClaim{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: "my-bucket", Namespace: "default"}, stored); err != nil {
t.Fatalf("Failed to get BucketClaim: %v", err)
}
stored.Status.BucketReady = true
if err := c.Update(context.TODO(), stored); err != nil {
t.Fatalf("Failed to update BucketClaim: %v", err)
}
got = reconcileBucketMonitor(t, c, s, withReader)
if !*got.Status.Operational || got.Status.Message != "" || got.Status.Reason != "" {
t.Errorf("after provisioning: Operational = %v, Message = %q, Reason = %q", *got.Status.Operational, got.Status.Message, got.Status.Reason)
}
})
}
}
func TestReconcile_DataVolumeVerdictOutranksBucketClaim(t *testing.T) {
s := newTestScheme()
selected := map[string]string{"app.kubernetes.io/instance": "my-bucket"}
phase := "ImportInProgress"
c := newBucketMonitorClient(s,
newBucketClaim("my-bucket", selected, "tenant-root", false),
newDataVolume("my-bucket", selected, &phase),
)
got := reconcileBucketMonitor(t, c, s, true)
if *got.Status.Operational {
t.Error("Operational = true, want false")
}
if got.Status.Reason != cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady {
t.Errorf("Reason = %q, want %q", got.Status.Reason, cozyv1alpha1.WorkloadMonitorReasonDataVolumeNotReady)
}
if got.Status.Message != "DataVolume my-bucket is ImportInProgress" {
t.Errorf("Message = %q", got.Status.Message)
}
}