Skip to content

Commit 773d8da

Browse files
committed
fix(gateway): withdraw TLSRoute acceptance when the mode drops passthrough
Edge terminates TLS at the class provider, so its Gateway carries plain HTTP listeners only and a route pinned to a passthrough section has nothing left to attach to. The api, vm-exportproxy and cdi-uploadproxy endpoints of publishing.exposedServices are the ones that pin one. Hostname claims are empty for every whole-apex mode, so the status pass ran over an empty ref set and left the Accepted=True written while the tenant ran HTTP-01 standing on a route the switch had just unpublished. Whoever went looking for the dead endpoint read that condition as proof the route was fine. The rule this settles on is that the controller owns a condition only where it judges. Edge judges every attached route by the absence of the listener it needs and writes Accepted=False with Gateway API's NoMatchingParent, the reason defined for a parentRef whose port or sectionName matches no listener. The other whole-apex modes render those listeners and serve them but collect no claims, so they hold no opinion: they drop the entry rather than leave a withdrawal standing on a listener that is serving, naming a class provider that no longer terminates anything. HTTP-01 collects claims and has already judged every route this pass could speak about. A route declaring no hostnames is judged by none of them, because claims are gathered inside a loop over spec.hostnames and TLSRoute v1alpha2 sets no minItems on that field. Withdrawing one would write a condition with no path back, since HTTP-01 renders the listener again and judges only the routes that produced claims, so both directions skip it. The tests read the controller name from the constant the controller stamps rather than restating it, so a helper cannot go looking for a name that is no longer written and report absence it never checked. The switch already deletes the redirect route, the Issuer and the wildcard Certificate it invalidates. The statuses it owns were the one thing left behind. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 5976ddf commit 773d8da

3 files changed

Lines changed: 515 additions & 38 deletions

File tree

‎internal/controller/tenantgateway/conflict.go‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,119 @@ func (r *Reconciler) updateRouteStatuses(
156156
return nil
157157
}
158158

159+
// reconcileTLSRouteWithdrawal keeps this controller's Accepted condition
160+
// on attached TLSRoutes honest in the modes where updateRouteStatuses
161+
// above says nothing. collectHostnameClaims returns no claims for a
162+
// whole-apex mode, so that pass runs over an empty ref set and neither
163+
// writes nor retracts anything.
164+
//
165+
// Edge renders no TLS-passthrough listener, so every attached TLSRoute
166+
// pins a section that is gone and is told so. NoMatchingParent is
167+
// Gateway API's reason for a parentRef whose port or sectionName
168+
// matches no listener on the Gateway, which is what the platform's own
169+
// api, vm-exportproxy and cdi-uploadproxy routes now have: all three
170+
// pin sectionName tls-<svc>.
171+
//
172+
// The other whole-apex modes render those listeners and serve them, but
173+
// this controller collects no claims there and so has no basis to judge
174+
// any TLSRoute. It therefore owns no condition on them, and drops the
175+
// one it wrote while the tenant ran edge. Leaving that behind inverts
176+
// the problem the withdrawal exists for: a served endpoint reported as
177+
// matching no parent, with a message naming a class provider that no
178+
// longer terminates anything.
179+
//
180+
// HTTP-01 is not handled here at all: it collects claims, so
181+
// updateRouteStatuses has already judged every route this pass would
182+
// have anything to say about.
183+
//
184+
// Both directions therefore skip a route that declares no hostnames.
185+
// collectHostnameClaims gathers refs inside a loop over spec.hostnames
186+
// and TLSRoute v1alpha2 sets no minItems on it, so such a route never
187+
// reaches updateRouteStatuses in any mode. Withdrawing it would write a
188+
// condition with no path back: HTTP-01 renders its listener again and
189+
// judges only the routes that produced claims. This controller does not
190+
// judge a hostname-less route, so it holds no opinion on one either.
191+
func (r *Reconciler) reconcileTLSRouteWithdrawal(ctx context.Context, tgw *gatewayv1alpha1.TenantGateway) error {
192+
if !servesWholeApex(tgw) {
193+
return nil
194+
}
195+
withdraw := !rendersTLSPassthrough(tgw)
196+
logger := log.FromContext(ctx)
197+
198+
allowed, err := r.allowedAttachNamespaces(ctx, tgw)
199+
if err != nil {
200+
return err
201+
}
202+
routes := &gatewayv1alpha2.TLSRouteList{}
203+
if err := r.List(ctx, routes); err != nil {
204+
return fmt.Errorf("list TLSRoutes: %w", err)
205+
}
206+
for i := range routes.Items {
207+
route := &routes.Items[i]
208+
if _, ok := allowed[route.Namespace]; !ok {
209+
continue
210+
}
211+
if len(route.Spec.Hostnames) == 0 {
212+
continue
213+
}
214+
for _, parentRef := range allAttachingParentRefs(route.Spec.ParentRefs, route.Namespace, tgw) {
215+
ref := routeRef{
216+
kind: routeKindTLS,
217+
namespace: route.Namespace,
218+
name: route.Name,
219+
parentRef: parentRef,
220+
}
221+
var err error
222+
if withdraw {
223+
err = r.updateRouteParentStatus(ctx, ref, []metav1.Condition{
224+
{
225+
Type: "Accepted",
226+
Status: metav1.ConditionFalse,
227+
Reason: "NoMatchingParent",
228+
Message: fmt.Sprintf("TenantGateway %s/%s terminates TLS at its class provider and renders no TLS-passthrough listener", tgw.Namespace, tgw.Name),
229+
},
230+
})
231+
} else {
232+
err = r.dropTLSRouteParentStatus(ctx, ref)
233+
}
234+
if err != nil {
235+
logger.Error(err, "reconcile TLSRoute status", "route", ref.namespace+"/"+ref.name)
236+
}
237+
}
238+
}
239+
return nil
240+
}
241+
242+
// dropTLSRouteParentStatus removes this controller's RouteParentStatus
243+
// entry for ref from a TLSRoute, leaving every other controller's entry
244+
// in place. Same idempotency contract as updateRouteParentStatus: the
245+
// Status().Update is issued only when the entry was actually there.
246+
func (r *Reconciler) dropTLSRouteParentStatus(ctx context.Context, ref routeRef) error {
247+
route := &gatewayv1alpha2.TLSRoute{}
248+
if err := r.Get(ctx, types.NamespacedName{Namespace: ref.namespace, Name: ref.name}, route); err != nil {
249+
return fmt.Errorf("get TLSRoute: %w", err)
250+
}
251+
before := route.DeepCopy()
252+
removeRouteParentStatus(&route.Status.Parents, ref.parentRef)
253+
if routeParentStatusEqual(before.Status.Parents, route.Status.Parents) {
254+
return nil
255+
}
256+
return r.Status().Update(ctx, route)
257+
}
258+
259+
// removeRouteParentStatus deletes the entry tagged with (ControllerName,
260+
// ref), the exact key mergeRouteParentStatus writes under.
261+
func removeRouteParentStatus(parents *[]gatewayv1.RouteParentStatus, ref gatewayv1.ParentReference) {
262+
kept := (*parents)[:0]
263+
for _, ps := range *parents {
264+
if ps.ControllerName == ControllerName && parentRefEqual(ps.ParentRef, ref) {
265+
continue
266+
}
267+
kept = append(kept, ps)
268+
}
269+
*parents = kept
270+
}
271+
159272
// updateRouteParentStatus locates or creates the RouteParentStatus
160273
// entry for our ControllerName on the given route (HTTPRoute or
161274
// TLSRoute, by ref.kind) and merges Conditions in.

‎internal/controller/tenantgateway/reconciler.go‎

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ func servesWholeApex(tgw *gatewayv1alpha1.TenantGateway) bool {
8484
return false
8585
}
8686

87+
// rendersTLSPassthrough reports whether the mode can carry
88+
// TLS-passthrough listeners at all. Edge ends TLS at the class
89+
// provider, so its Gateway is HTTP-only and neither a passthrough
90+
// listener nor a TLSRoute has anything to bind to on it.
91+
func rendersTLSPassthrough(tgw *gatewayv1alpha1.TenantGateway) bool {
92+
return tgw.Spec.CertMode != gatewayv1alpha1.CertModeEdge
93+
}
94+
8795
// gatewayIssuerName returns the per-tenant ACME Issuer name. The
8896
// Issuer lives in the same namespace as the TenantGateway and is
8997
// referenced by every Certificate this controller renders.
@@ -188,6 +196,9 @@ func (r *Reconciler) runReconcileSteps(ctx context.Context, tgw *gatewayv1alpha1
188196
if err := r.updateRouteStatuses(ctx, tgw, allRefs, losers); err != nil {
189197
return err
190198
}
199+
if err := r.reconcileTLSRouteWithdrawal(ctx, tgw); err != nil {
200+
return err
201+
}
191202
if err := r.reconcileHTTPToHTTPSRedirect(ctx, tgw); err != nil {
192203
return err
193204
}
@@ -294,6 +305,34 @@ func (r *Reconciler) reconcileHTTPToHTTPSRedirect(ctx context.Context, tgw *gate
294305
return nil
295306
}
296307

308+
// allowedAttachNamespaces returns the namespaces whose routes this
309+
// TenantGateway judges: its own, the static admin-configured
310+
// Spec.AttachedNamespaces list of cozy-* system namespaces, and every
311+
// namespace carrying namespace.cozystack.io/gateway pointing at this
312+
// Gateway's namespace. The label is what makes inheritance work — the
313+
// apps/tenant chart writes it on every tenant namespace, inherited or
314+
// self-owning, so child tenants reach the parent Gateway — and it is
315+
// the same label the Gateway's allowedRoutes selector reads, so the
316+
// two paths agree on which namespaces can attach.
317+
func (r *Reconciler) allowedAttachNamespaces(ctx context.Context, tgw *gatewayv1alpha1.TenantGateway) (map[string]struct{}, error) {
318+
allowed := map[string]struct{}{tgw.Namespace: {}}
319+
for _, ns := range tgw.Spec.AttachedNamespaces {
320+
if ns == "" {
321+
continue
322+
}
323+
allowed[ns] = struct{}{}
324+
}
325+
nsList := &corev1.NamespaceList{}
326+
selector := labels.SelectorFromSet(labels.Set{namespaceGatewayLabel: tgw.Namespace})
327+
if err := r.List(ctx, nsList, client.MatchingLabelsSelector{Selector: selector}); err != nil {
328+
return nil, fmt.Errorf("list namespaces by gateway label: %w", err)
329+
}
330+
for i := range nsList.Items {
331+
allowed[nsList.Items[i].Name] = struct{}{}
332+
}
333+
return allowed, nil
334+
}
335+
297336
// collectHostnameClaims lists HTTPRoutes and TLSRoutes cluster-wide
298337
// and returns a map of hostname -> []routeRef of routes claiming
299338
// it via parentRefs targeting this TenantGateway's Gateway. Routes
@@ -304,46 +343,23 @@ func (r *Reconciler) reconcileHTTPToHTTPSRedirect(ctx context.Context, tgw *gate
304343
// limits and leaks the operator's reachable hostname set). Empty
305344
// map in DNS-01 mode (wildcard handles everything).
306345
//
307-
// A namespace is allowed to attach when:
308-
// - It is the TenantGateway's own namespace, OR
309-
// - It is in Spec.AttachedNamespaces (static admin-configured
310-
// attach list of cozy-* system namespaces), OR
311-
// - It carries the label namespace.cozystack.io/gateway pointing
312-
// at this Gateway's namespace (inheritance — apps/tenant chart
313-
// writes the label on every tenant namespace, inherited or
314-
// self-owning, so child tenants reach the parent Gateway).
315-
//
316-
// The third source is what makes HTTP-01 inheritance work end-to-end:
317-
// without it, a child tenant's HTTPRoute would be silently dropped
318-
// here and no per-listener Certificate would be issued — the
319-
// Gateway's label-based allowedRoutes selector would let the route
320-
// through at runtime but no listener would accept it (no matching
321-
// hostname), so Accepted stays False indefinitely.
346+
// The attach set comes from allowedAttachNamespaces, and its label
347+
// source is what makes HTTP-01 inheritance work end-to-end: without
348+
// it, a child tenant's HTTPRoute would be silently dropped here and no
349+
// per-listener Certificate would be issued — the Gateway's label-based
350+
// allowedRoutes selector would let the route through at runtime but no
351+
// listener would accept it (no matching hostname), so Accepted stays
352+
// False indefinitely.
322353
func (r *Reconciler) collectHostnameClaims(ctx context.Context, tgw *gatewayv1alpha1.TenantGateway) (map[string][]routeRef, error) {
323354
// DNS-01, existingSecret and edge all serve every hostname off
324355
// apex-wide listeners, so none needs per-host listeners or claims.
325356
if servesWholeApex(tgw) {
326357
return nil, nil
327358
}
328359

329-
allowed := map[string]struct{}{tgw.Namespace: {}}
330-
for _, ns := range tgw.Spec.AttachedNamespaces {
331-
if ns == "" {
332-
continue
333-
}
334-
allowed[ns] = struct{}{}
335-
}
336-
// Inheritance: every namespace pointing at this Gateway via the
337-
// label is also allowed. The same label drives the Gateway's
338-
// allowedRoutes selector, so the two paths agree on which
339-
// namespaces can attach.
340-
nsList := &corev1.NamespaceList{}
341-
selector := labels.SelectorFromSet(labels.Set{namespaceGatewayLabel: tgw.Namespace})
342-
if err := r.List(ctx, nsList, client.MatchingLabelsSelector{Selector: selector}); err != nil {
343-
return nil, fmt.Errorf("list namespaces by gateway label: %w", err)
344-
}
345-
for i := range nsList.Items {
346-
allowed[nsList.Items[i].Name] = struct{}{}
360+
allowed, err := r.allowedAttachNamespaces(ctx, tgw)
361+
if err != nil {
362+
return nil, err
347363
}
348364

349365
out := map[string][]routeRef{}
@@ -971,8 +987,7 @@ func (r *Reconciler) renderGateway(tgw *gatewayv1alpha1.TenantGateway, dynHostna
971987
// The corresponding TLSRoute templates (cozystack-api, vm-exportproxy,
972988
// cdi-uploadproxy) attach to these listeners by sectionName.
973989
passthroughServices := tgw.Spec.TLSPassthroughServices
974-
if tgw.Spec.CertMode == gatewayv1alpha1.CertModeEdge {
975-
// An HTTP-only edge cannot serve a TLS listener at all.
990+
if !rendersTLSPassthrough(tgw) {
976991
passthroughServices = nil
977992
}
978993
for _, svc := range passthroughServices {

0 commit comments

Comments
 (0)