Skip to content

Commit ffb6564

Browse files
committed
fix(controller): drop stale bucket-class label from BucketClaim labels before applying validated value
maps.Copy(workload.Labels, bc.Labels) can bring in a workloads.cozystack.io/bucket-class label directly set on the BucketClaim, which would survive unmodified when spec.bucketClassName is empty or invalid, leaving the Workload reporting a class that does not match the source BucketClaim. Signed-off-by: harshitrwt <[email protected]>
1 parent f41a500 commit ffb6564

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎internal/controller/workloadmonitor_controller.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ func (r *WorkloadMonitorReconciler) reconcileBucketClaimForMonitor(
326326
maps.Copy(workload.Labels, bc.Labels)
327327
workload.Labels[workloadMonitorLabel] = monitor.Name
328328

329+
delete(workload.Labels, "workloads.cozystack.io/bucket-class")
329330
if bc.Spec.BucketClassName != "" {
330331
if errs := validation.IsValidLabelValue(bc.Spec.BucketClassName); len(errs) == 0 {
331332
workload.Labels["workloads.cozystack.io/bucket-class"] = bc.Spec.BucketClassName

‎internal/controller/workloadmonitor_controller_test.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,72 @@ func TestReconcileBucketClaim_InvalidBucketClassSkipsLabel(t *testing.T) {
596596
}
597597
}
598598

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+
599665
func TestReconcileBucketClaimNotReady(t *testing.T) {
600666
s := newTestScheme()
601667

0 commit comments

Comments
 (0)