diff --git a/internal/controller/workloadmonitor_controller.go b/internal/controller/workloadmonitor_controller.go index b77331da4..31f827630 100644 --- a/internal/controller/workloadmonitor_controller.go +++ b/internal/controller/workloadmonitor_controller.go @@ -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) diff --git a/internal/controller/workloadmonitor_controller_test.go b/internal/controller/workloadmonitor_controller_test.go index 656fc3c50..b345d149d 100644 --- a/internal/controller/workloadmonitor_controller_test.go +++ b/internal/controller/workloadmonitor_controller_test.go @@ -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)