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) +}