fix(workloadmonitor): read the MetalLB pool from the metallb.io annotation

MetalLB writes the pool a Service's address came from to
metallb.io/ip-allocated-from-pool since v0.14.9; the
metallb.universe.tf key is only read on input now. The controller still
looked for the old key, so every Service Workload has recorded its
addresses under the "default" pool since MetalLB was updated to v0.15.2,
and a Service without annotations got an empty pool name.

Read the current key first and fall back to the deprecated one, then to
"default".

Assisted-by: LLM
Signed-off-by: Andrey Kolkov <androndo@gmail.com>
This commit is contained in:
Andrey Kolkov committed 2026-12-02 17:30:58 +07:00
1 parent 236d039ae3
commit 6729dd8cfe
2 files changed
+84 -9

No files matched your search

@@ -920,6 +920,21 @@ func (r *WorkloadMonitorReconciler) reconcileBucketClaimForMonitor(
return nil
}
// allocatedPool names the MetalLB pool a Service's address came from. MetalLB
// writes the metallb.io key since v0.14.9; the metallb.universe.tf key is read
// for Services still annotated by an older MetalLB.
func allocatedPool(annotations map[string]string) string {
for _, key := range []string{
"metallb.io/ip-allocated-from-pool",
"metallb.universe.tf/ip-allocated-from-pool",
} {
if pool := annotations[key]; pool != "" {
return pool
}
}
return "default"
}
// reconcileServiceForMonitor creates or updates a Workload object for the given Service and WorkloadMonitor.
func (r *WorkloadMonitorReconciler) reconcileServiceForMonitor(
ctx context.Context,
@@ -945,15 +960,7 @@ func (r *WorkloadMonitorReconciler) reconcileServiceForMonitor(
}
}
var resourceLabel string
if svc.Annotations != nil {
var ok bool
resourceLabel, ok = svc.Annotations["metallb.universe.tf/ip-allocated-from-pool"]
if !ok {
resourceLabel = "default"
}
}
resourceLabel = fmt.Sprintf("%s.ipaddresspool.metallb.io/requests.ipaddresses", resourceLabel)
resourceLabel := fmt.Sprintf("%s.ipaddresspool.metallb.io/requests.ipaddresses", allocatedPool(svc.Annotations))
resources[resourceLabel] = quantity
monitorLabels := r.getMonitorLabels(monitor)
@@ -1525,6 +1525,74 @@ func TestReconcile_PVCStorageClassResourceKey(t *testing.T) {
}
}
func TestReconcile_ServiceIPPoolResourceKey(t *testing.T) {
const (
current = "metallb.io/ip-allocated-from-pool"
deprecated = "metallb.universe.tf/ip-allocated-from-pool"
)
cases := []struct {
name string
annotations map[string]string
wantKey string
}{
{"current key", map[string]string{current: "public"}, "public.ipaddresspool.metallb.io/requests.ipaddresses"},
{"deprecated key", map[string]string{deprecated: "legacy"}, "legacy.ipaddresspool.metallb.io/requests.ipaddresses"},
{"current key wins", map[string]string{current: "public", deprecated: "legacy"}, "public.ipaddresspool.metallb.io/requests.ipaddresses"},
{"no annotations fall back to default", nil, "default.ipaddresspool.metallb.io/requests.ipaddresses"},
{"empty value falls back to default", map[string]string{current: ""}, "default.ipaddresspool.metallb.io/requests.ipaddresses"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
scheme := newTestScheme()
monitor := &cozyv1alpha1.WorkloadMonitor{
ObjectMeta: metav1.ObjectMeta{Name: "test-monitor", Namespace: "default"},
Spec: cozyv1alpha1.WorkloadMonitorSpec{
Selector: map[string]string{"app": "test"},
Kind: "ingress",
Type: "controller",
},
}
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "lb",
Namespace: "default",
Labels: map[string]string{"app": "test"},
Annotations: tc.annotations,
},
Spec: corev1.ServiceSpec{Type: corev1.ServiceTypeLoadBalancer},
Status: corev1.ServiceStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{{IP: "192.0.2.10"}},
},
},
}
fakeClient := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(monitor, svc).
WithStatusSubresource(monitor).
Build()
reconciler := &WorkloadMonitorReconciler{Client: fakeClient, Scheme: scheme}
req := reconcile.Request{NamespacedName: types.NamespacedName{Name: "test-monitor", Namespace: "default"}}
if _, err := reconciler.Reconcile(context.TODO(), req); err != nil {
t.Fatalf("Reconcile returned error: %v", err)
}
workload := &cozyv1alpha1.Workload{}
if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: "svc-lb", Namespace: "default"}, workload); err != nil {
t.Fatalf("Failed to get Workload: %v", err)
}
got, ok := workload.Status.Resources[tc.wantKey]
if !ok || len(workload.Status.Resources) != 1 {
t.Fatalf("expected only resource key %q, got %v", tc.wantKey, workload.Status.Resources)
}
if got.Cmp(resource.MustParse("1")) != 0 {
t.Errorf("expected one address, got %s", got.String())
}
})
}
}
func newDataVolume(name string, labels map[string]string, phase *string) *unstructured.Unstructured {
dv := &unstructured.Unstructured{}
dv.SetGroupVersionKind(dataVolumeGVK)