From 3f86faf8b8f8eb6a15db2f2b8a7a91ed5abc2eec Mon Sep 17 00:00:00 2001 From: reoring Date: Thu, 19 Nov 2026 21:24:15 +0900 Subject: [PATCH 1/2] gatewayendpoint: keep an authorized hostname across a listener-only receipt change (APT-1361) - A receipt that differs from the current binding only by listener digest no longer withdraws the hostname when the route is still in an allowed namespace (All, or Same/default with the Gateway namespace; Selector fails closed), the listener still permits HTTPRoute, and the established targets are unchanged. - While the route parent is Accepted the receipt is refreshed to the new listener digest; while it is Unknown the old receipt is kept unchanged until admission can refresh it. Accepted=False, cross-namespace routes the new policy rejects, removed listeners, changed targets and route UID/generation mismatches still withdraw. - Fresh minting and Gateway UID replacement are unchanged. On appthrust.ink, closing Project Gateway listeners from All to Same changed every listener digest, removed every published hostname, deleted or emptied the EndpointRecordSets, pruned their RecordSets and deleted the Route 53 answers; recreation took about two minutes. --- docs/design/gateway-api-integration.md | 41 +- .../controllers/gateway/controller.go | 14 +- .../controllers/gateway/httproute.go | 15 + .../controllers/gateway/receipt.go | 10 +- .../gateway/receipt_continuity_test.go | 355 ++++++++++++++++++ 5 files changed, 420 insertions(+), 15 deletions(-) create mode 100644 internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go diff --git a/docs/design/gateway-api-integration.md b/docs/design/gateway-api-integration.md index 5afbccf..0e56722 100644 --- a/docs/design/gateway-api-integration.md +++ b/docs/design/gateway-api-integration.md @@ -187,16 +187,37 @@ from a normal reconcile with complete current acceptance and nonempty current targets. -The receipt also records the specific listener. Hostname or parent removal, -listener removal or ownership change, and route/Gateway deletion remove the -affected hostname immediately. Receipt identity rules remain unchanged: a route -with a replacement UID may inherit and rebind a receipt only when all its current -parent references are `Accepted=True`; a replacement under `False` or `Unknown` -cannot inherit it. Gateway UID and class and the listener digest must still -match for retention. A dynamic `allowedRoutes.namespaces.from: Selector` policy -cannot be re-proved without a namespace read, so neither unknown observation -nor route-level `Accepted=False` is retained under that policy. Independent -sibling listener bindings are evaluated separately. +The receipt also records the specific listener, including a SHA-256 digest of +the complete listener. Hostname or parent removal, listener removal, and +route/Gateway deletion remove the affected hostname immediately. A listener-only +digest change may refresh the receipt in place when the route namespace/name, +UID and generation, Gateway UID/class, parent reference, listener name, and +published hostname still match. Every current route parent must be +`Accepted=True`, the hostname must still intersect the current listener, and +the current listener must allow HTTPRoute and its namespace (`All`, or `Same` +including the default for a same-namespace route). `Selector` cannot re-prove +continuity without a namespace read and therefore cannot refresh a receipt. +Current targets must equal the established targets, or be temporarily absent +while the established nonempty targets are retained. This changes only the +receipt annotation, not the hostname intent or its generated RecordSets. + +While the matching parent is `Unknown` (including absent or pending status), +the same listener-only continuity checks retain the hostname and the original +receipt without updating its digest. This requires the exact receipted route +UID and generation, not a pending newer route generation. Current acceptance of +every parent subsequently refreshes the digest. `Accepted=False` does not +authorize this exception and still withdraws a listener-digest conflict. + +Receipt replay rules otherwise remain unchanged: a route with a replacement UID +may inherit and rebind a receipt only when all its current parent references are +`Accepted=True`; a replacement under `False` or `Unknown` cannot inherit it. +Same-UID pending-generation replay still requires the exact listener digest. +Gateway UID and class must match for retention. A dynamic +`allowedRoutes.namespaces.from: Selector` policy cannot be re-proved without a +namespace read, so neither unknown observation nor route-level `Accepted=False` +is retained under that policy. Independent sibling listener bindings are +evaluated separately. Fresh admission still trusts current route acceptance; +listener-refresh checks do not introduce a second Gateway admission controller. The receipt records route namespace and name only so a parent-reference removal, change, deletion, or final not-found reconciliation can requeue the previously diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go b/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go index 5cdcf5e..015c9c7 100644 --- a/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/controller.go @@ -269,6 +269,10 @@ func (r *Reconciler) endpointRecordSetsForGateway(ctx context.Context, gateway * publishedHostnames[hostname] = struct{}{} } } + // Missing addresses retain the established targets, not a replacement. + // A changed target cannot use listener-only receipt continuity. + unchangedTargets := existing != nil && len(existing.Spec.Targets) != 0 && + (len(targets) == 0 || slices.Equal(targets, existing.Spec.Targets)) hostnames := map[string]struct{}{} retainedHostnames := map[string]struct{}{} receiptBindings := map[string]admissionReceiptBinding{} @@ -284,15 +288,18 @@ func (r *Reconciler) endpointRecordSetsForGateway(ctx context.Context, gateway * continue } acceptance := routeParentAcceptance(route, binding.ref) + allowListenerContinuity := (acceptance == parentAcceptanceUnknown || + fullyAccepted && acceptance == parentAcceptanceAccepted) && + unchangedTargets && listenerAllowsReceiptRefresh(route, gateway, binding.listener) for _, hostname := range effectiveHostnamesForListener(route, binding.listener) { match, hasReceipt := admissionReceiptBinding{}, false conflictingReceipt := false if _, published := publishedHostnames[hostname]; published { - match, hasReceipt = receipt.matchingBinding(route, gateway, binding, hostname) + match, hasReceipt = receipt.matchingBinding(route, gateway, binding, hostname, allowListenerContinuity) conflictingReceipt = receipt.conflictsCurrentBinding(route, gateway, binding, hostname) } if fullyAccepted && acceptance == parentAcceptanceAccepted { - if conflictingReceipt { + if conflictingReceipt && !hasReceipt { continue } hostnames[hostname] = struct{}{} @@ -312,7 +319,8 @@ func (r *Reconciler) endpointRecordSetsForGateway(ctx context.Context, gateway * continue } // Route admission controls serving, not publication intent. - // False and Unknown have the same receipt-retention boundary. + // Unknown may retain the old listener receipt until admission + // can refresh it; False still requires an exact listener digest. if (acceptance != parentAcceptanceAccepted && !listenerAllowsUnknownRetention(route, gateway, binding.listener)) || !hasReceipt { continue diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go b/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go index bd21669..a55cb23 100644 --- a/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go @@ -119,6 +119,21 @@ func listenerAllowsUnknownRetention(route *gatewayv1.HTTPRoute, gateway *gateway } } +func listenerAllowsReceiptRefresh(route *gatewayv1.HTTPRoute, gateway *gatewayv1.Gateway, listener gatewayv1.Listener) bool { + if !listenerAllowsUnknownRetention(route, gateway, listener) { + return false + } + if listener.AllowedRoutes == nil || len(listener.AllowedRoutes.Kinds) == 0 { + return true + } + for _, kind := range listener.AllowedRoutes.Kinds { + if kind.Kind == "HTTPRoute" && (kind.Group == nil || *kind.Group == gatewayv1.GroupName) { + return true + } + } + return false +} + func parentRefKey(routeNamespace string, ref gatewayv1.ParentReference) string { namespace := routeNamespace if ref.Namespace != nil { diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/receipt.go b/internal/go/apps/gatewayendpoint/controllers/gateway/receipt.go index abd64b4..d4ee437 100644 --- a/internal/go/apps/gatewayendpoint/controllers/gateway/receipt.go +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/receipt.go @@ -91,7 +91,7 @@ func newAdmissionReceiptBinding(route *gatewayv1.HTTPRoute, gateway *gatewayv1.G }, true } -func (receipt admissionReceipt) matchingBinding(route *gatewayv1.HTTPRoute, gateway *gatewayv1.Gateway, binding routeGatewayBinding, hostname string) (admissionReceiptBinding, bool) { +func (receipt admissionReceipt) matchingBinding(route *gatewayv1.HTTPRoute, gateway *gatewayv1.Gateway, binding routeGatewayBinding, hostname string, allowListenerContinuity bool) (admissionReceiptBinding, bool) { listenerDigest := gatewayListenerDigest(binding.listener) if route.UID == "" || route.Generation <= 0 || gateway.UID == "" || listenerDigest == "" { return admissionReceiptBinding{}, false @@ -105,10 +105,16 @@ func (receipt admissionReceipt) matchingBinding(route *gatewayv1.HTTPRoute, gate candidate.GatewayClass != string(gateway.Spec.GatewayClassName) || candidate.ParentRef != parentRef || candidate.Listener != string(binding.listener.Name) || - candidate.ListenerDigest != listenerDigest || !receiptHasHostname(candidate, hostname) { continue } + if candidate.ListenerDigest != listenerDigest && + (!allowListenerContinuity || candidate.RouteUID != string(route.UID) || candidate.RouteGeneration != route.Generation) { + // Listener-only continuity cannot cross a route incarnation or + // generation. The caller refreshes only after current admission; + // an Unknown observation keeps this original receipt unchanged. + continue + } if candidate.RouteUID == string(route.UID) { if candidate.RouteGeneration == route.Generation { return candidate, true diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go b/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go new file mode 100644 index 0000000..17b2f0f --- /dev/null +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go @@ -0,0 +1,355 @@ +package gateway + +import ( + "net/http/httptest" + "reflect" + "testing" + + endpointcontroller "github.com/appthrust/dns-api/internal/go/apps/endpoint/controllers/endpointrecordset" + corezoneunit "github.com/appthrust/dns-api/internal/go/core/controllers/zoneunit" + route53 "github.com/appthrust/dns-api/internal/go/providers/route53/controllers/zoneunit" + route53conversion "github.com/appthrust/dns-api/internal/go/providers/route53/conversion" + dnsv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/dns/v1alpha1" + endpointv1alpha1 "github.com/appthrust/dns-api/pkg/go/api/endpoint/v1alpha1" + route53v1alpha1 "github.com/appthrust/dns-api/pkg/go/api/route53/v1alpha1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" +) + +func TestGatewayListenerReceiptContinuity(t *testing.T) { + for _, test := range []struct { + name string + routeNamespace string + retain bool + pendingConversion bool + change func(*gatewayv1.Gateway, *gatewayv1.HTTPRoute) + }{ + {name: "SameNamespaceAccepted", retain: true}, + { + name: "SameNamespaceAcceptedAddressObservationPending", retain: true, + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + gateway.Status.Addresses = nil + }, + }, + {name: "SameNamespaceAcceptedConversionObservationPending", retain: true, pendingConversion: true}, + { + name: "CrossNamespaceRejected", routeNamespace: "app", + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionFalse + }, + }, + {name: "CrossNamespaceStillAcceptedCannotRefresh", routeNamespace: "app"}, + { + name: "ListenerRemoved", + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + gateway.Spec.Listeners = nil + }, + }, + { + name: "GatewayClassChanged", + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + gateway.Spec.GatewayClassName = "another-controller" + }, + }, + { + name: "RouteGenerationNotAccepted", + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Generation++ + }, + }, + { + name: "RouteRejected", + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionFalse + }, + }, + { + name: "SameNamespaceUnknownThenAccepted", retain: true, + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + }, + }, + { + name: "CrossNamespaceUnknown", routeNamespace: "app", + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + }, + }, + { + name: "UnknownWithChangedTarget", + change: func(gateway *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + gateway.Status.Addresses[0].Value = "192.0.2.2" + }, + }, + { + name: "UnknownWithRemovedListener", + change: func(gateway *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + gateway.Spec.Listeners = nil + }, + }, + { + name: "UnknownWithRejectedKind", + change: func(gateway *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + gateway.Spec.Listeners[0].AllowedRoutes.Kinds = []gatewayv1.RouteGroupKind{{Kind: "TCPRoute"}} + }, + }, + { + name: "HostnameNoLongerMatches", + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + hostname := gatewayv1.Hostname("other.project.example") + gateway.Spec.Listeners[0].Hostname = &hostname + }, + }, + { + name: "HTTPRouteKindNoLongerAllowed", + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + gateway.Spec.Listeners[0].AllowedRoutes.Kinds = []gatewayv1.RouteGroupKind{{Kind: "TCPRoute"}} + }, + }, + { + name: "SelectorCannotRefresh", + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + selector := gatewayv1.NamespacesFromSelector + gateway.Spec.Listeners[0].AllowedRoutes.Namespaces = &gatewayv1.RouteNamespaces{ + From: &selector, Selector: &metav1.LabelSelector{}, + } + }, + }, + { + name: "ChangedTargetCannotRefresh", + change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { + gateway.Status.Addresses[0].Value = "192.0.2.2" + }, + }, + } { + t.Run(test.name, func(t *testing.T) { + ctx := t.Context() + scheme := testScheme(t) + for _, add := range []func(*runtime.Scheme) error{corev1.AddToScheme, dnsv1alpha1.AddToScheme, route53v1alpha1.AddToScheme} { + publicationDNSRequire(t, add(scheme)) + } + gateway := receiptSyncGateway("public", "listener-continuity-gateway", "192.0.2.1", "https") + gateway.Spec.Listeners[0].Protocol = gatewayv1.HTTPSProtocolType + gateway.Spec.Listeners[0].Port = 443 + namespace := test.routeNamespace + if namespace == "" { + namespace = gateway.Namespace + } + route := retentionRoute(namespace, "ha", "listener-continuity-route", "ha.project.example", metav1.ConditionTrue) + zone := publicationDNSZone() + objects := append(publicationDNSInfrastructure(zone), gateway, route) + incarnation := 0 + cl := publicationDNSClient(scheme, objects, &incarnation) + routeKey := client.ObjectKeyFromObject(route) + recordKey := client.ObjectKey{Namespace: zone.Namespace, Name: generatedGatewayEndpointRecordSetName(gateway.Namespace, gateway.Name)} + r := &Reconciler{Client: cl, EndpointRecordSetNamespace: recordKey.Namespace} + reconcileRetentionRoute(t, r, routeKey) + markUnprotected(t, r, recordKey) + server := httptest.NewServer(route53conversion.NewHandler()) + t.Cleanup(server.Close) + provider := &publicationDNSProvider{publicationProvider: publicationProvider{records: map[string]route53.RecordSetResource{}}} + publicationDNSConverge(t, cl, provider, server.URL, []client.ObjectKey{routeKey}, recordKey, client.ObjectKeyFromObject(zone)) + + var before endpointv1alpha1.EndpointRecordSet + publicationDNSRequire(t, cl.Get(ctx, recordKey, &before)) + oldReceipt := existingAdmissionReceipt(&before) + if before.UID == "" || len(oldReceipt.Bindings) != 1 || oldReceipt.Bindings[0].ListenerDigest != gatewayListenerDigest(gateway.Spec.Listeners[0]) { + t.Fatalf("initial admission receipt was not minted by the reconciler: %#v", oldReceipt) + } + var children dnsv1alpha1.RecordSetList + publicationDNSRequire(t, cl.List(ctx, &children)) + if len(children.Items) != 1 || children.Items[0].UID == "" || provider.upserts != 1 || provider.deletes != 0 || len(provider.records) != 1 { + t.Fatalf("initial publication did not reach DNS: children=%d upserts=%d deletes=%d records=%d", len(children.Items), provider.upserts, provider.deletes, len(provider.records)) + } + records := make(map[string]route53.RecordSetResource, len(provider.records)) + for name, record := range provider.records { + records[name] = record + } + provider.upserts, provider.deletes = 0, 0 + + publicationDNSRequire(t, cl.Get(ctx, client.ObjectKeyFromObject(gateway), gateway)) + publicationDNSRequire(t, cl.Get(ctx, routeKey, route)) + same := gatewayv1.NamespacesFromSame + gateway.Spec.Listeners[0].AllowedRoutes.Namespaces.From = &same + gateway.Generation++ + if test.change != nil { + test.change(gateway, route) + } + gatewayStatus, routeStatus := gateway.Status, route.Status + publicationDNSRequire(t, cl.Update(ctx, gateway)) + gateway.Status = gatewayStatus + publicationDNSRequire(t, cl.Status().Update(ctx, gateway)) + publicationDNSRequire(t, cl.Update(ctx, route)) + route.Status = routeStatus + publicationDNSRequire(t, cl.Status().Update(ctx, route)) + if test.pendingConversion { + publicationDNSRequire(t, cl.Delete(ctx, &endpointv1alpha1.EndpointProviderCapability{ObjectMeta: metav1.ObjectMeta{Name: "route53"}})) + } + + // Restart the source reconciler: only the persisted receipt may + // authorize continuity across the listener policy change. + r = &Reconciler{Client: cl, EndpointRecordSetNamespace: recordKey.Namespace} + reconcileRetentionRoute(t, r, routeKey) + var after endpointv1alpha1.EndpointRecordSet + publicationDNSRequire(t, cl.Get(ctx, recordKey, &after)) + if test.retain { + if after.UID != before.UID || !after.DeletionTimestamp.IsZero() || !reflect.DeepEqual(after.Spec, before.Spec) { + t.Fatalf("listener-only change disrupted publication: before=%#v after=%#v", before, after) + } + refreshed := existingAdmissionReceipt(&after) + newDigest := gatewayListenerDigest(gateway.Spec.Listeners[0]) + if oldReceipt.Bindings[0].ListenerDigest == newDigest { + t.Fatal("test did not change the listener digest") + } + if routeParentAcceptance(route, route.Spec.ParentRefs[0]) == parentAcceptanceUnknown { + if after.Annotations[admissionReceiptAnnotation] != before.Annotations[admissionReceiptAnnotation] || + after.ResourceVersion != before.ResourceVersion { + t.Fatal("Unknown admission rewrote the established receipt") + } + } else { + oldReceipt.Bindings[0].ListenerDigest = newDigest + } + if !reflect.DeepEqual(refreshed, oldReceipt) { + t.Fatalf("receipt does not match its admission state: got=%#v want=%#v", refreshed, oldReceipt) + } + revision := after.ResourceVersion + reconcileRetentionRoute(t, r, routeKey) + publicationDNSRequire(t, cl.Get(ctx, recordKey, &after)) + if after.ResourceVersion != revision { + t.Fatal("refreshed receipt was rewritten on replay") + } + } else if after.DeletionTimestamp.IsZero() { + t.Fatalf("revoked binding retained the established intent: %#v", after) + } + + // Advance only consumers of this decision. Fresh source admission + // after deletion remains a separate Gateway-controller contract. + endpoint := &endpointcontroller.Reconciler{Client: cl, RecordSetNamespace: recordKey.Namespace, RESTConfig: &rest.Config{Host: server.URL}} + composition := &corezoneunit.ZoneUnitCompositionReconciler{Client: cl} + dns := &route53.ZoneReconciler{Client: cl, Provider: provider} + for range 8 { + for _, step := range []struct { + reconciler reconcile.Reconciler + key client.ObjectKey + }{{endpoint, recordKey}, {composition, client.ObjectKeyFromObject(zone)}, {dns, client.ObjectKeyFromObject(zone)}} { + _, err := step.reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: step.key}) + publicationDNSRequire(t, err) + } + } + fixture := publicationDNSContract{} + fixture.Expect.DNS = "withdraw-all" + fixture.Expect.Hostnames = before.Spec.Hostnames + if test.retain { + fixture.Expect.DNS = "retain" + } + publicationDNSAssertOutcome(t, fixture, cl, provider, &before, children.Items, records, zone.Spec.DomainName) + if test.retain { + var child dnsv1alpha1.RecordSet + publicationDNSRequire(t, cl.Get(ctx, client.ObjectKeyFromObject(&children.Items[0]), &child)) + if child.ResourceVersion != children.Items[0].ResourceVersion { + t.Fatal("listener-only receipt refresh wrote an established RecordSet") + } + if routeParentAcceptance(route, route.Spec.ParentRefs[0]) == parentAcceptanceUnknown { + // Current acceptance, not the pending observation above, + // authorizes replacing the old listener digest. + setRouteAcceptance(t, r, routeKey, metav1.ConditionTrue) + r = &Reconciler{Client: cl, EndpointRecordSetNamespace: recordKey.Namespace} + reconcileRetentionRoute(t, r, routeKey) + publicationDNSRequire(t, cl.Get(ctx, recordKey, &after)) + oldReceipt.Bindings[0].ListenerDigest = gatewayListenerDigest(gateway.Spec.Listeners[0]) + if !reflect.DeepEqual(existingAdmissionReceipt(&after), oldReceipt) { + t.Fatalf("current admission did not refresh the pending receipt: %#v", existingAdmissionReceipt(&after)) + } + publicationDNSConverge(t, cl, provider, server.URL, []client.ObjectKey{routeKey}, recordKey, client.ObjectKeyFromObject(zone)) + publicationDNSAssertOutcome(t, fixture, cl, provider, &before, children.Items, records, zone.Spec.DomainName) + publicationDNSRequire(t, cl.Get(ctx, client.ObjectKeyFromObject(&children.Items[0]), &child)) + if child.ResourceVersion != children.Items[0].ResourceVersion { + t.Fatal("Unknown-to-Accepted transition wrote an established RecordSet") + } + } + } + }) + } +} + +func TestListenerAllowsReceiptRefresh(t *testing.T) { + gateway := &gatewayv1.Gateway{ObjectMeta: metav1.ObjectMeta{Namespace: "platform"}} + group := gatewayv1.Group(gatewayv1.GroupName) + otherGroup := gatewayv1.Group("other.example") + for _, test := range []struct { + name string + namespace string + policy gatewayv1.FromNamespaces + kinds []gatewayv1.RouteGroupKind + want bool + }{ + {name: "DefaultSameNamespace", namespace: "platform", want: true}, + {name: "DefaultCrossNamespace", namespace: "app"}, + {name: "AllCrossNamespace", namespace: "app", policy: gatewayv1.NamespacesFromAll, want: true}, + {name: "SameNamespace", namespace: "platform", policy: gatewayv1.NamespacesFromSame, want: true}, + {name: "SameCrossNamespace", namespace: "app", policy: gatewayv1.NamespacesFromSame}, + {name: "SelectorSameNamespace", namespace: "platform", policy: gatewayv1.NamespacesFromSelector}, + {name: "ExplicitHTTPRoute", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Group: &group, Kind: "HTTPRoute"}}, want: true}, + {name: "DefaultGroupHTTPRoute", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Kind: "HTTPRoute"}}, want: true}, + {name: "WrongKind", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Kind: "TCPRoute"}}}, + {name: "WrongGroup", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Group: &otherGroup, Kind: "HTTPRoute"}}}, + } { + t.Run(test.name, func(t *testing.T) { + route := &gatewayv1.HTTPRoute{ObjectMeta: metav1.ObjectMeta{Namespace: test.namespace}} + listener := gatewayv1.Listener{AllowedRoutes: &gatewayv1.AllowedRoutes{Kinds: test.kinds}} + if test.policy != "" { + listener.AllowedRoutes.Namespaces = &gatewayv1.RouteNamespaces{From: &test.policy} + } + if got := listenerAllowsReceiptRefresh(route, gateway, listener); got != test.want { + t.Fatalf("listenerAllowsReceiptRefresh = %t, want %t", got, test.want) + } + }) + } +} + +func TestAdmissionReceiptListenerRefreshIdentity(t *testing.T) { + for _, test := range []struct { + name string + change func(*admissionReceiptBinding) + }{ + {name: "RouteNamespace", change: func(binding *admissionReceiptBinding) { binding.RouteNamespace = "other" }}, + {name: "RouteName", change: func(binding *admissionReceiptBinding) { binding.RouteName = "other" }}, + {name: "RouteUID", change: func(binding *admissionReceiptBinding) { binding.RouteUID = "other" }}, + {name: "RouteGeneration", change: func(binding *admissionReceiptBinding) { binding.RouteGeneration++ }}, + {name: "GatewayUID", change: func(binding *admissionReceiptBinding) { binding.GatewayUID = "other" }}, + {name: "GatewayClass", change: func(binding *admissionReceiptBinding) { binding.GatewayClass = "other" }}, + {name: "ParentRef", change: func(binding *admissionReceiptBinding) { binding.ParentRef = "other" }}, + {name: "Listener", change: func(binding *admissionReceiptBinding) { binding.Listener = "other" }}, + {name: "Hostname", change: func(binding *admissionReceiptBinding) { binding.Hostnames = []string{"other.project.example"} }}, + } { + t.Run(test.name, func(t *testing.T) { + route := retentionRoute("platform", "ha", "route", "ha.project.example", metav1.ConditionTrue) + gateway := receiptSyncGateway("public", "gateway", "192.0.2.1", "https") + binding := routeGatewayBindings(route, gateway)[0] + previous, ok := newAdmissionReceiptBinding(route, gateway, binding) + if !ok { + t.Fatal("could not construct the previous binding") + } + previous.Hostnames = []string{"ha.project.example"} + same := gatewayv1.NamespacesFromSame + binding.listener = *binding.listener.DeepCopy() + binding.listener.AllowedRoutes.Namespaces.From = &same + receipt := admissionReceipt{Bindings: []admissionReceiptBinding{previous}} + if _, ok := receipt.matchingBinding(route, gateway, binding, "ha.project.example", true); !ok { + t.Fatal("listener-only change did not match") + } + test.change(&receipt.Bindings[0]) + if _, ok := receipt.matchingBinding(route, gateway, binding, "ha.project.example", true); ok { + t.Fatal("listener refresh inherited a different identity") + } + }) + } +} From ab9fc1545a259f5e17d5b90051ce4c6ee2747b32 Mon Sep 17 00:00:00 2001 From: reoring Date: Thu, 19 Nov 2026 21:31:53 +0900 Subject: [PATCH 2/2] gatewayendpoint: continue only HTTP/HTTPS listeners without explicit kinds (APT-1361) - With allowedRoutes.kinds omitted, listener-only receipt continuity now requires an HTTP or HTTPS listener, so a same-name listener changed to TLS/TCP/UDP withdraws the hostname. - Tests: HTTPS->TLS, address loss then a different target, Unknown then Rejected, Unknown then hostname mismatch; each first proves retention, then withdrawal through the provider. - Docs: parent acceptance means the observedGeneration matching the current HTTPRoute generation. Review F1/F2/F5 (probe-opus) on #30. --- docs/design/gateway-api-integration.md | 25 ++++--- .../controllers/gateway/httproute.go | 2 +- .../gateway/receipt_continuity_test.go | 66 ++++++++++++++++++- 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/docs/design/gateway-api-integration.md b/docs/design/gateway-api-integration.md index 0e56722..4b8287e 100644 --- a/docs/design/gateway-api-integration.md +++ b/docs/design/gateway-api-integration.md @@ -192,11 +192,14 @@ the complete listener. Hostname or parent removal, listener removal, and route/Gateway deletion remove the affected hostname immediately. A listener-only digest change may refresh the receipt in place when the route namespace/name, UID and generation, Gateway UID/class, parent reference, listener name, and -published hostname still match. Every current route parent must be -`Accepted=True`, the hostname must still intersect the current listener, and -the current listener must allow HTTPRoute and its namespace (`All`, or `Same` -including the default for a same-namespace route). `Selector` cannot re-prove -continuity without a namespace read and therefore cannot refresh a receipt. +published hostname still match. Every route parent reference must have an +`Accepted=True` condition whose `observedGeneration` equals the current +`HTTPRoute.metadata.generation`. The hostname must still intersect the current +listener, and the listener must allow HTTPRoute and its namespace (`All`, or +`Same` including the default for a same-namespace route). With omitted or empty +`allowedRoutes.kinds`, only HTTP and HTTPS listeners allow HTTPRoute continuity. +`Selector` cannot re-prove continuity without a namespace read and therefore +cannot refresh a receipt. Current targets must equal the established targets, or be temporarily absent while the established nonempty targets are retained. This changes only the receipt annotation, not the hostname intent or its generated RecordSets. @@ -204,13 +207,15 @@ receipt annotation, not the hostname intent or its generated RecordSets. While the matching parent is `Unknown` (including absent or pending status), the same listener-only continuity checks retain the hostname and the original receipt without updating its digest. This requires the exact receipted route -UID and generation, not a pending newer route generation. Current acceptance of -every parent subsequently refreshes the digest. `Accepted=False` does not -authorize this exception and still withdraws a listener-digest conflict. +UID and generation, not a pending newer route generation. Acceptance of every +parent for that same route generation subsequently refreshes the digest. +`Accepted=False` does not authorize this exception and still withdraws a +listener-digest conflict. Receipt replay rules otherwise remain unchanged: a route with a replacement UID -may inherit and rebind a receipt only when all its current parent references are -`Accepted=True`; a replacement under `False` or `Unknown` cannot inherit it. +may inherit and rebind a receipt only when all its parent references are +`Accepted=True` for its current route generation; a replacement under `False` +or `Unknown` cannot inherit it. Same-UID pending-generation replay still requires the exact listener digest. Gateway UID and class must match for retention. A dynamic `allowedRoutes.namespaces.from: Selector` policy cannot be re-proved without a diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go b/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go index a55cb23..4d6153b 100644 --- a/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/httproute.go @@ -124,7 +124,7 @@ func listenerAllowsReceiptRefresh(route *gatewayv1.HTTPRoute, gateway *gatewayv1 return false } if listener.AllowedRoutes == nil || len(listener.AllowedRoutes.Kinds) == 0 { - return true + return listener.Protocol == gatewayv1.HTTPProtocolType || listener.Protocol == gatewayv1.HTTPSProtocolType } for _, kind := range listener.AllowedRoutes.Kinds { if kind.Kind == "HTTPRoute" && (kind.Group == nil || *kind.Group == gatewayv1.GroupName) { diff --git a/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go b/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go index 17b2f0f..58efcfa 100644 --- a/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go +++ b/internal/go/apps/gatewayendpoint/controllers/gateway/receipt_continuity_test.go @@ -28,6 +28,7 @@ func TestGatewayListenerReceiptContinuity(t *testing.T) { retain bool pendingConversion bool change func(*gatewayv1.Gateway, *gatewayv1.HTTPRoute) + afterPending func(*testing.T, *Reconciler, client.ObjectKey, client.ObjectKey) }{ {name: "SameNamespaceAccepted", retain: true}, { @@ -74,6 +75,39 @@ func TestGatewayListenerReceiptContinuity(t *testing.T) { route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown }, }, + { + name: "UnknownAddressLossThenChangedTarget", retain: true, + change: func(gateway *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + gateway.Status.Addresses = nil + }, + afterPending: func(t *testing.T, r *Reconciler, _, gatewayKey client.ObjectKey) { + setGatewayAddresses(t, r, gatewayKey, []gatewayv1.GatewayStatusAddress{{Value: "192.0.2.2"}}) + }, + }, + { + name: "UnknownThenRejected", retain: true, + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + }, + afterPending: func(t *testing.T, r *Reconciler, routeKey, _ client.ObjectKey) { + setRouteAcceptance(t, r, routeKey, metav1.ConditionFalse) + }, + }, + { + name: "UnknownThenHostnameMismatch", retain: true, + change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + }, + afterPending: func(t *testing.T, r *Reconciler, _, gatewayKey client.ObjectKey) { + var gateway gatewayv1.Gateway + publicationDNSRequire(t, r.Get(t.Context(), gatewayKey, &gateway)) + hostname := gatewayv1.Hostname("other.project.example") + gateway.Spec.Listeners[0].Hostname = &hostname + gateway.Generation++ + publicationDNSRequire(t, r.Update(t.Context(), &gateway)) + }, + }, { name: "CrossNamespaceUnknown", routeNamespace: "app", change: func(_ *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { @@ -101,6 +135,13 @@ func TestGatewayListenerReceiptContinuity(t *testing.T) { gateway.Spec.Listeners[0].AllowedRoutes.Kinds = []gatewayv1.RouteGroupKind{{Kind: "TCPRoute"}} }, }, + { + name: "UnknownWithHTTPSChangedToTLS", + change: func(gateway *gatewayv1.Gateway, route *gatewayv1.HTTPRoute) { + route.Status.Parents[0].Conditions[0].Status = metav1.ConditionUnknown + gateway.Spec.Listeners[0].Protocol = gatewayv1.TLSProtocolType + }, + }, { name: "HostnameNoLongerMatches", change: func(gateway *gatewayv1.Gateway, _ *gatewayv1.HTTPRoute) { @@ -257,6 +298,21 @@ func TestGatewayListenerReceiptContinuity(t *testing.T) { if child.ResourceVersion != children.Items[0].ResourceVersion { t.Fatal("listener-only receipt refresh wrote an established RecordSet") } + if test.afterPending != nil { + // First prove the old publication survived the pending + // observation, then observe the revocation independently. + test.afterPending(t, r, routeKey, client.ObjectKeyFromObject(gateway)) + r = &Reconciler{Client: cl, EndpointRecordSetNamespace: recordKey.Namespace} + reconcileRetentionRoute(t, r, routeKey) + publicationDNSRequire(t, cl.Get(ctx, recordKey, &after)) + if after.DeletionTimestamp.IsZero() { + t.Fatalf("revocation after pending continuity retained the intent: %#v", after) + } + fixture.Expect.DNS = "withdraw-all" + publicationDNSConverge(t, cl, provider, server.URL, []client.ObjectKey{routeKey}, recordKey, client.ObjectKeyFromObject(zone)) + publicationDNSAssertOutcome(t, fixture, cl, provider, &before, children.Items, records, zone.Spec.DomainName) + return + } if routeParentAcceptance(route, route.Spec.ParentRefs[0]) == parentAcceptanceUnknown { // Current acceptance, not the pending observation above, // authorizes replacing the old listener digest. @@ -288,6 +344,7 @@ func TestListenerAllowsReceiptRefresh(t *testing.T) { name string namespace string policy gatewayv1.FromNamespaces + protocol gatewayv1.ProtocolType kinds []gatewayv1.RouteGroupKind want bool }{ @@ -301,10 +358,17 @@ func TestListenerAllowsReceiptRefresh(t *testing.T) { {name: "DefaultGroupHTTPRoute", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Kind: "HTTPRoute"}}, want: true}, {name: "WrongKind", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Kind: "TCPRoute"}}}, {name: "WrongGroup", namespace: "platform", kinds: []gatewayv1.RouteGroupKind{{Group: &otherGroup, Kind: "HTTPRoute"}}}, + {name: "EmptyKindsHTTP", namespace: "platform", protocol: gatewayv1.HTTPProtocolType, want: true}, + {name: "EmptyKindsTLS", namespace: "platform", protocol: gatewayv1.TLSProtocolType}, + {name: "EmptyKindsTCP", namespace: "platform", protocol: gatewayv1.TCPProtocolType}, + {name: "EmptyKindsUDP", namespace: "platform", protocol: gatewayv1.UDPProtocolType}, } { t.Run(test.name, func(t *testing.T) { route := &gatewayv1.HTTPRoute{ObjectMeta: metav1.ObjectMeta{Namespace: test.namespace}} - listener := gatewayv1.Listener{AllowedRoutes: &gatewayv1.AllowedRoutes{Kinds: test.kinds}} + listener := gatewayv1.Listener{Protocol: gatewayv1.HTTPSProtocolType, AllowedRoutes: &gatewayv1.AllowedRoutes{Kinds: test.kinds}} + if test.protocol != "" { + listener.Protocol = test.protocol + } if test.policy != "" { listener.AllowedRoutes.Namespaces = &gatewayv1.RouteNamespaces{From: &test.policy} }