mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-05 14:42:05 +09:00
fix(workloadmonitor): read the MetalLB pool from the metallb.io annotation (#5562)
## What this PR does The WorkloadMonitor controller records a LoadBalancer Service's public addresses on its `svc-<name>` Workload under the key `<pool>.ipaddresspool.metallb.io/requests.ipaddresses` (#788, #831). It takes `<pool>` from the Service annotation `metallb.universe.tf/ip-allocated-from-pool`. MetalLB renamed its annotation prefix in v0.14.9: it now writes the pool to `metallb.io/ip-allocated-from-pool` and keeps the `metallb.universe.tf` keys only as deprecated *inputs* (`controller/service.go`, `AnnotationIPAllocateFromPool`). Cozystack has shipped MetalLB v0.15.2 and later (now v0.16.1) since mid-2025, so the old key is never set, and every Service Workload has recorded its addresses under the `default` pool regardless of where they came from. A Service with no annotations at all got an empty pool name (`.ipaddresspool.metallb.io/...`). This reads `metallb.io/ip-allocated-from-pool` first, falls back to the deprecated key for Services annotated by an older MetalLB, and then to `default`. ### Why it matters The pool is the only thing that tells two kinds of public address apart in usage data. The billing UsageReport turns this resource key into an `IPAddressHours` line and puts the pool into its metadata (`ipAddressPool`). With every address reported as `default`: - a provider who prices or accounts addresses by pool (for example a general auto-assign pool versus a pool of addresses customers reserve separately and pay for on their own) cannot tell them apart, and either bills both at one rate or has to drop IP-hours altogether; - an address that is already paid for as a reserved address cannot be excluded from hourly IP billing by its pool, so it is either charged twice or the pool split has to be done outside Cozystack; - usage reports and dashboards attribute all public addresses to a pool that may not exist. The address count was and stays correct (it comes from `status.loadBalancer.ingress`); only the pool label was wrong. Seen on a live cluster with MetalLB v0.16.1: a Service carrying `metallb.io/ip-allocated-from-pool: <pool>` and no `metallb.universe.tf` key. ### Test `TestReconcile_ServiceIPPoolResourceKey`: the current key, the deprecated key, both (current wins), no annotations, an empty value. `go test ./internal/controller/` and `go vet` pass. ### Screenshots N/A — no UI changes. ### Downstream repositories Walked the trigger map against the diff: the change is inside `reconcileServiceForMonitor` and a new helper; no API type, CRD, values, schema, metric name or annotation written by Cozystack changes. - [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): LoadBalancer Service workloads record their public addresses under the MetalLB pool the address came from again. MetalLB has written the pool to `metallb.io/ip-allocated-from-pool` since v0.14.9, and the controller still read `metallb.universe.tf/ip-allocated-from-pool`, so every address was reported under the `default` pool. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * LoadBalancer workloads now report the allocated MetalLB IP pool when it is specified using either the current or legacy annotation. When both are present, the current annotation takes precedence. If neither provides a usable value, the pool is reported as `default`. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
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)
|
||||
|
||||
Reference in new issue
Block a user