Skip to content

Commit 15f57be

Browse files
authored
feat(controller): expose bucket class on BucketClaim Workloads (cozystack#3501)
## What this PR does `WorkloadMonitorReconciler.reconcileBucketClaimForMonitor` builds a `Workload` for each `BucketClaim` but never read `spec.bucketClassName`, so nothing on the resulting `Workload` told a consumer which bucket class was in use (e.g. an encrypted class vs. a plain one). This PR reads `BucketClaim.spec.bucketClassName` and sets it as a `workloads.cozystack.io/bucket-class` label on the `Workload`, following the same `workloads.cozystack.io/*` labeling convention already used for KubeVirt instance-type/profile metadata elsewhere in this controller. A regression test (`TestReconcileBucketClaim_BucketClassLabelPropagated`) asserts the label is present with the correct value. Closes cozystack#3419 ### Downstream repositories - [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: ### Release note ```release-note feat(controller): expose bucket class on BucketClaim Workloads via the workloads.cozystack.io/bucket-class label ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Workloads created from ready BucketClaims include the associated bucket class label when a valid value is specified. * **Bug Fixes** * Invalid or empty bucket class values are omitted from Workloads. * Stale bucket class labels are removed when the associated claim does not provide a valid value. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 9d36614 + ffb6564 commit 15f57be

2 files changed

Lines changed: 211 additions & 0 deletions

File tree

‎internal/controller/workloadmonitor_controller.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
apierrors "k8s.io/apimachinery/pkg/api/errors"
1717
"k8s.io/apimachinery/pkg/runtime"
1818
"k8s.io/apimachinery/pkg/types"
19+
"k8s.io/apimachinery/pkg/util/validation"
1920
"k8s.io/client-go/tools/record"
2021
"k8s.io/client-go/util/retry"
2122
"k8s.io/utils/pointer"
@@ -325,6 +326,16 @@ func (r *WorkloadMonitorReconciler) reconcileBucketClaimForMonitor(
325326
maps.Copy(workload.Labels, bc.Labels)
326327
workload.Labels[workloadMonitorLabel] = monitor.Name
327328

329+
delete(workload.Labels, "workloads.cozystack.io/bucket-class")
330+
if bc.Spec.BucketClassName != "" {
331+
if errs := validation.IsValidLabelValue(bc.Spec.BucketClassName); len(errs) == 0 {
332+
workload.Labels["workloads.cozystack.io/bucket-class"] = bc.Spec.BucketClassName
333+
} else {
334+
logger.Info("Skipping bucket-class label: not a valid label value",
335+
"bucketClaim", bc.Name, "bucketClassName", bc.Spec.BucketClassName, "errors", errs)
336+
}
337+
}
338+
328339
// Start from the sizes already recorded on the Workload: when the
329340
// metrics endpoint is unreachable or reports nothing for this bucket,
330341
// the last known good values must survive rather than collapse to

‎internal/controller/workloadmonitor_controller_test.go‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,206 @@ func TestReconcileBucketClaimCreatesWorkload(t *testing.T) {
462462
}
463463
}
464464

465+
func TestReconcileBucketClaim_BucketClassLabelPropagated(t *testing.T) {
466+
s := newTestScheme()
467+
468+
monitor := &cozyv1alpha1.WorkloadMonitor{
469+
ObjectMeta: metav1.ObjectMeta{
470+
Name: "my-bucket",
471+
Namespace: "tenant-demo",
472+
},
473+
Spec: cozyv1alpha1.WorkloadMonitorSpec{
474+
Kind: "bucket",
475+
Type: "s3",
476+
Selector: map[string]string{
477+
"app.kubernetes.io/instance": "my-bucket",
478+
},
479+
},
480+
}
481+
482+
bc := &cosiv1alpha1.BucketClaim{
483+
ObjectMeta: metav1.ObjectMeta{
484+
Name: "my-bucket",
485+
Namespace: "tenant-demo",
486+
Labels: map[string]string{
487+
"app.kubernetes.io/instance": "my-bucket",
488+
},
489+
},
490+
Spec: cosiv1alpha1.BucketClaimSpec{
491+
BucketClassName: "seaweedfs-encrypted",
492+
Protocols: []cosiv1alpha1.Protocol{cosiv1alpha1.ProtocolS3},
493+
},
494+
Status: cosiv1alpha1.BucketClaimStatus{
495+
BucketReady: true,
496+
BucketName: "cosi-abc123",
497+
},
498+
}
499+
500+
fakeClient := fake.NewClientBuilder().
501+
WithScheme(s).
502+
WithObjects(monitor, bc).
503+
WithStatusSubresource(monitor).
504+
Build()
505+
506+
reconciler := &WorkloadMonitorReconciler{Client: fakeClient, Scheme: s}
507+
req := reconcile.Request{NamespacedName: types.NamespacedName{
508+
Name: "my-bucket",
509+
Namespace: "tenant-demo",
510+
}}
511+
512+
_, err := reconciler.Reconcile(context.TODO(), req)
513+
if err != nil {
514+
t.Fatalf("Reconcile returned error: %v", err)
515+
}
516+
517+
workload := &cozyv1alpha1.Workload{}
518+
err = fakeClient.Get(context.TODO(), types.NamespacedName{
519+
Name: "bucket-my-bucket",
520+
Namespace: "tenant-demo",
521+
}, workload)
522+
if err != nil {
523+
t.Fatalf("expected Workload to be created, got error: %v", err)
524+
}
525+
526+
got := workload.Labels["workloads.cozystack.io/bucket-class"]
527+
if got != "seaweedfs-encrypted" {
528+
t.Errorf("expected workloads.cozystack.io/bucket-class=%q, got %q", "seaweedfs-encrypted", got)
529+
}
530+
}
531+
532+
func TestReconcileBucketClaim_InvalidBucketClassSkipsLabel(t *testing.T) {
533+
s := newTestScheme()
534+
535+
monitor := &cozyv1alpha1.WorkloadMonitor{
536+
ObjectMeta: metav1.ObjectMeta{
537+
Name: "my-bucket",
538+
Namespace: "tenant-demo",
539+
},
540+
Spec: cozyv1alpha1.WorkloadMonitorSpec{
541+
Kind: "bucket",
542+
Type: "s3",
543+
Selector: map[string]string{
544+
"app.kubernetes.io/instance": "my-bucket",
545+
},
546+
},
547+
}
548+
549+
overlong := strings.Repeat("a", 64)
550+
bc := &cosiv1alpha1.BucketClaim{
551+
ObjectMeta: metav1.ObjectMeta{
552+
Name: "my-bucket",
553+
Namespace: "tenant-demo",
554+
Labels: map[string]string{
555+
"app.kubernetes.io/instance": "my-bucket",
556+
},
557+
},
558+
Spec: cosiv1alpha1.BucketClaimSpec{
559+
BucketClassName: overlong,
560+
Protocols: []cosiv1alpha1.Protocol{cosiv1alpha1.ProtocolS3},
561+
},
562+
Status: cosiv1alpha1.BucketClaimStatus{
563+
BucketReady: true,
564+
BucketName: "cosi-abc123",
565+
},
566+
}
567+
568+
fakeClient := fake.NewClientBuilder().
569+
WithScheme(s).
570+
WithObjects(monitor, bc).
571+
WithStatusSubresource(monitor).
572+
Build()
573+
574+
reconciler := &WorkloadMonitorReconciler{Client: fakeClient, Scheme: s}
575+
req := reconcile.Request{NamespacedName: types.NamespacedName{
576+
Name: "my-bucket",
577+
Namespace: "tenant-demo",
578+
}}
579+
580+
_, err := reconciler.Reconcile(context.TODO(), req)
581+
if err != nil {
582+
t.Fatalf("Reconcile returned error: %v", err)
583+
}
584+
585+
workload := &cozyv1alpha1.Workload{}
586+
err = fakeClient.Get(context.TODO(), types.NamespacedName{
587+
Name: "bucket-my-bucket",
588+
Namespace: "tenant-demo",
589+
}, workload)
590+
if err != nil {
591+
t.Fatalf("expected Workload to be created despite invalid bucket class, got error: %v", err)
592+
}
593+
594+
if v, ok := workload.Labels["workloads.cozystack.io/bucket-class"]; ok {
595+
t.Errorf("expected bucket-class label to be skipped for an invalid label value, got %q", v)
596+
}
597+
}
598+
599+
func TestReconcileBucketClaim_InvalidBucketClassDropsStaleLabel(t *testing.T) {
600+
s := newTestScheme()
601+
602+
monitor := &cozyv1alpha1.WorkloadMonitor{
603+
ObjectMeta: metav1.ObjectMeta{
604+
Name: "my-bucket",
605+
Namespace: "tenant-demo",
606+
},
607+
Spec: cozyv1alpha1.WorkloadMonitorSpec{
608+
Kind: "bucket",
609+
Type: "s3",
610+
Selector: map[string]string{
611+
"app.kubernetes.io/instance": "my-bucket",
612+
},
613+
},
614+
}
615+
616+
overlong := strings.Repeat("a", 64)
617+
bc := &cosiv1alpha1.BucketClaim{
618+
ObjectMeta: metav1.ObjectMeta{
619+
Name: "my-bucket",
620+
Namespace: "tenant-demo",
621+
Labels: map[string]string{
622+
"app.kubernetes.io/instance": "my-bucket",
623+
"workloads.cozystack.io/bucket-class": "seaweedfs-encrypted",
624+
},
625+
},
626+
Spec: cosiv1alpha1.BucketClaimSpec{
627+
BucketClassName: overlong,
628+
Protocols: []cosiv1alpha1.Protocol{cosiv1alpha1.ProtocolS3},
629+
},
630+
Status: cosiv1alpha1.BucketClaimStatus{
631+
BucketReady: true,
632+
BucketName: "cosi-abc123",
633+
},
634+
}
635+
636+
fakeClient := fake.NewClientBuilder().
637+
WithScheme(s).
638+
WithObjects(monitor, bc).
639+
WithStatusSubresource(monitor).
640+
Build()
641+
642+
reconciler := &WorkloadMonitorReconciler{Client: fakeClient, Scheme: s}
643+
req := reconcile.Request{NamespacedName: types.NamespacedName{
644+
Name: "my-bucket",
645+
Namespace: "tenant-demo",
646+
}}
647+
648+
if _, err := reconciler.Reconcile(context.TODO(), req); err != nil {
649+
t.Fatalf("Reconcile returned error: %v", err)
650+
}
651+
652+
workload := &cozyv1alpha1.Workload{}
653+
if err := fakeClient.Get(context.TODO(), types.NamespacedName{
654+
Name: "bucket-my-bucket",
655+
Namespace: "tenant-demo",
656+
}, workload); err != nil {
657+
t.Fatalf("expected Workload to be created, got error: %v", err)
658+
}
659+
660+
if v, ok := workload.Labels["workloads.cozystack.io/bucket-class"]; ok {
661+
t.Errorf("expected stale bucket-class label to be dropped, got %q", v)
662+
}
663+
}
664+
465665
func TestReconcileBucketClaimNotReady(t *testing.T) {
466666
s := newTestScheme()
467667

0 commit comments

Comments
 (0)