From 08a4e9cd0cc198856158adc168b601e8a8eb3d62 Mon Sep 17 00:00:00 2001 From: Michael Costello Date: Tue, 17 Nov 2026 12:44:44 -0500 Subject: [PATCH] feat: Annotate edge NetworkServices with their NLB The gateway labels its per-listener counters with VIP, port and protocol only, and the edge NetworkService carried nothing naming the load balancer or listener it serves. A metrics pipeline had no way to attribute that traffic to its owner. The cell programming controller now writes the load balancer and listener names from the cell intent as annotations on the service it owns. It updates them on every reconcile, removes the listener annotation when the intent names none, and keeps annotations other writers set. These are annotations, not labels, because a load balancer name can be up to 253 characters and a label value allows 63. A label would make the service write fail and leave the load balancer unprogrammed. Fixes #888 --- .../network-load-balancer-cell-contract.md | 7 +- ...kloadbalancercellprogramming_controller.go | 22 ++++++ ...balancercellprogramming_controller_test.go | 74 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/docs/plans/network-load-balancer-cell-contract.md b/docs/plans/network-load-balancer-cell-contract.md index 030a13d1..9e3616c3 100644 --- a/docs/plans/network-load-balancer-cell-contract.md +++ b/docs/plans/network-load-balancer-cell-contract.md @@ -131,7 +131,12 @@ Galactic `NetworkService` with the intent's name in the intent's namespace, owned by the intent so it is deleted with it. The gateways must watch services in every namespace to serve it. That service selects those labels, uses the attachments' fabric VPC as `vpcRef`, and serves the VIP on the listener port. It -forwards to the backend port. An intent is not served if its backends span +forwards to the backend port. The service is annotated with +`networking.datumapis.com/nlb-name` and `networking.datumapis.com/nlb-listener`, +which name the load balancer and listener it serves. Metrics pipelines read +those annotations to attribute the gateway's per-listener counters. They're +annotations, not labels, because a load balancer name can exceed the 63 +characters a label value allows. An intent is not served if its backends span VPCs or ports, if no backend passes, or if it is withdrawn or deleted. In those cases the service is deleted and the labels are removed. diff --git a/internal/controller/networkloadbalancercellprogramming_controller.go b/internal/controller/networkloadbalancercellprogramming_controller.go index b3a989c8..97db291b 100644 --- a/internal/controller/networkloadbalancercellprogramming_controller.go +++ b/internal/controller/networkloadbalancercellprogramming_controller.go @@ -6,6 +6,7 @@ import ( "context" "errors" "fmt" + "maps" "slices" "strings" "time" @@ -37,6 +38,8 @@ const ( nlbProgrammingBackendLabelPrefix = "nlb.networking.datumapis.com/" nlbProgrammingBackendLabelValue = "backend" nlbProgrammingRetry = 30 * time.Second + nlbServiceLoadBalancerAnnotation = "networking.datumapis.com/nlb-name" + nlbServiceListenerAnnotation = "networking.datumapis.com/nlb-listener" ) var nlbPartialBackendReasons = map[string]bool{"BackendsUnhealthy": true, "BackendsUnresolved": true} @@ -142,6 +145,7 @@ func (r *NetworkLoadBalancerCellProgrammingReconciler) Reconcile(ctx context.Con } } current.Spec = service.Spec + current.Annotations = nlbServiceAnnotations(current.Annotations, &intent) return controllerutil.SetControllerReference(&intent, current, r.Scheme) }); err != nil { return ctrl.Result{}, fmt.Errorf("write gateway service %s: %w", service.Name, err) @@ -395,6 +399,24 @@ func desiredNetworkLoadBalancerService( return service, "" } +func nlbServiceAnnotations(current map[string]string, intent *networkingv1alpha.NetworkLoadBalancerCellIntent) map[string]string { + updated := maps.Clone(current) + if updated == nil { + updated = map[string]string{} + } + for key, value := range map[string]string{ + nlbServiceLoadBalancerAnnotation: intent.Spec.Source.ParentName, + nlbServiceListenerAnnotation: intent.Spec.Source.ListenerName, + } { + if value == "" { + delete(updated, key) + } else { + updated[key] = value + } + } + return updated +} + func networkLoadBalancerBackendLabel(intentName string) string { return nlbProgrammingBackendLabelPrefix + intentName } diff --git a/internal/controller/networkloadbalancercellprogramming_controller_test.go b/internal/controller/networkloadbalancercellprogramming_controller_test.go index 961c7083..bc189388 100644 --- a/internal/controller/networkloadbalancercellprogramming_controller_test.go +++ b/internal/controller/networkloadbalancercellprogramming_controller_test.go @@ -4,6 +4,7 @@ package controller import ( "context" + "strings" "testing" "github.com/stretchr/testify/require" @@ -433,3 +434,76 @@ func TestNLBCellProgrammingServesIPv4AndReportsIPv4Served(t *testing.T) { require.Equal(t, metav1.ConditionFalse, announced.Status) require.Equal(t, "BindingsNotBound", announced.Reason) } + +func TestNLBCellProgrammingAnnotatesGatewayServiceWithItsLoadBalancer(t *testing.T) { + cl, r, intent := nlbProgrammingFixture(t) + intent.Spec.Source = networkingv1alpha.NetworkLoadBalancerCellIntentSource{ParentName: "web", ListenerName: "http"} + require.NoError(t, cl.Update(context.Background(), intent)) + reconcileNLBProgramming(t, r, intent) + + var service networkv1alpha1.NetworkService + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), &service)) + require.Equal(t, "web", service.Annotations[nlbServiceLoadBalancerAnnotation]) + require.Equal(t, "http", service.Annotations[nlbServiceListenerAnnotation]) +} + +func TestNLBCellProgrammingAnnotatesLoadBalancerNamesLongerThanALabelValue(t *testing.T) { + cl, r, intent := nlbProgrammingFixture(t) + long := "web." + strings.Repeat("a", 70) + intent.Spec.Source = networkingv1alpha.NetworkLoadBalancerCellIntentSource{ParentName: long, ListenerName: "http"} + require.NoError(t, cl.Update(context.Background(), intent)) + reconcileNLBProgramming(t, r, intent) + + var service networkv1alpha1.NetworkService + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), &service)) + require.Equal(t, long, service.Annotations[nlbServiceLoadBalancerAnnotation]) +} + +func TestNLBCellProgrammingFollowsListenerRenames(t *testing.T) { + cl, r, intent := nlbProgrammingFixture(t) + intent.Spec.Source = networkingv1alpha.NetworkLoadBalancerCellIntentSource{ParentName: "web", ListenerName: "http"} + require.NoError(t, cl.Update(context.Background(), intent)) + reconcileNLBProgramming(t, r, intent) + + var service networkv1alpha1.NetworkService + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), &service)) + service.Annotations["example.com/kept"] = "yes" + require.NoError(t, cl.Update(context.Background(), &service)) + + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), intent)) + intent.Spec.Source.ListenerName = "web-tcp" + require.NoError(t, cl.Update(context.Background(), intent)) + reconcileNLBProgramming(t, r, intent) + + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), &service)) + require.Equal(t, "web-tcp", service.Annotations[nlbServiceListenerAnnotation]) + require.Equal(t, "yes", service.Annotations["example.com/kept"]) + + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), intent)) + intent.Spec.Source.ListenerName = "" + require.NoError(t, cl.Update(context.Background(), intent)) + reconcileNLBProgramming(t, r, intent) + + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), &service)) + require.NotContains(t, service.Annotations, nlbServiceListenerAnnotation) + require.Equal(t, "web", service.Annotations[nlbServiceLoadBalancerAnnotation]) +} + +func TestNLBCellProgrammingLeavesForeignGatewayServiceUnannotated(t *testing.T) { + cl, r, intent := nlbProgrammingFixture(t) + intent.Spec.Source = networkingv1alpha.NetworkLoadBalancerCellIntentSource{ParentName: "web", ListenerName: "http"} + require.NoError(t, cl.Update(context.Background(), intent)) + foreign := &networkv1alpha1.NetworkService{ + ObjectMeta: metav1.ObjectMeta{Namespace: intent.Namespace, Name: intent.Name, CreationTimestamp: metav1.Now()}, + Spec: networkv1alpha1.NetworkServiceSpec{VPCRef: "other", VIPAddresses: []string{"2001:db8::1"}, Protocol: networkv1alpha1.NetworkServiceProtocolTCP, Port: 80}, + } + require.NoError(t, cl.Create(context.Background(), foreign)) + + _, err := r.Reconcile(context.Background(), ctrl.Request{NamespacedName: client.ObjectKeyFromObject(intent)}) + require.Error(t, err) + + var service networkv1alpha1.NetworkService + require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(intent), &service)) + require.Empty(t, service.Annotations) + require.Equal(t, "other", service.Spec.VPCRef) +}