This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: k8s-gateway-api-v1-2-cross-namespace-referencegrant-invariant

Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant

Enforces strict bi-directional boundary verification for Kubernetes Gateway API v1.2 deployments across namespace perimeters. Eliminates cross-tenant traffic blackholing and route hijacking by requiring platform Gateways to scope listener attachments via namespace selectors and application namespaces to explicitly permit routing via ReferenceGrant resources.

Prohibited Anti-Pattern: Cross-Namespace Gateway Binding via Permissive Wildcards or Missing ReferenceGrant Handshakes

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant - Enforces strict bi-directional boundary verification for Kubernetes Gateway API v1.2 deployments across namespace perimeters. Eliminates cross-tenant traffic blackholing and route hijacking by requiring platform Gateways to scope listener attachments via namespace selectors and application namespaces to explicitly permit routing via ReferenceGrant resources.
globs: **/*.yaml, **/*.yml, k8s/**/*.yaml, helm/**/*.yaml, deployments/**/*.yaml, infra/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-gateway-api-v1-2-cross-namespace-referencegrant-invariant"
---

# Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Cross-Namespace Gateway Binding via Permissive Wildcards or Missing ReferenceGrant Handshakes

## System Constraints
You are a Principal Software Architect and DevOps Rules Author enforcing the Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant. You must reject any configuration that binds routes across namespaces without an explicit ReferenceGrant or that uses unbounded wildcard namespace selectors on Gateway listeners. Enforce least-privilege tenancy, mutual attachment contracts, and valid object references.

## Strict Guidelines
- Engineers MUST ALWAYS configure `listeners[].allowedRoutes.namespaces.from: Selector` on shared Gateways rather than `from: All` to enforce explicit namespace labeling.
- Engineers MUST ALWAYS deploy a `gateway.networking.k8s.io/v1beta1` `ReferenceGrant` in the target namespace whenever an HTTPRoute references a Gateway or backend Service outside its own namespace.
- HTTPRoutes MUST explicitly declare `parentRefs[].namespace` and `parentRefs[].sectionName` when attaching to a platform Gateway across namespace boundaries.
- Platform Gateways MUST NOT bind to untrusted application namespaces without cryptographic or label-verified isolation boundaries.
- Cross-namespace backend references inside an `HTTPRoute` rule (e.g., routing to a Service in a different namespace) MUST be backed by a corresponding `ReferenceGrant` in the destination Service's namespace.
- Continuous integration pipelines MUST fail linting if an HTTPRoute references a cross-namespace resource lacking a co-located or reconciling ReferenceGrant.

## Bad vs Good Implementations
### Cross-Namespace Gateway Route Attachment and Service Forwarding

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The Gateway exposes an unrestricted `from: All` listener that allows any tenant namespace to hijack hostnames. Furthermore, `tenant-checkout` routes traffic directly to `tenant-payments` backend without an authoritative ReferenceGrant in `tenant-payments`, which causes traffic drops or violates multi-tenant cryptographic isolation boundaries.
```yaml
# INSECURE: Wildcard listener allows arbitrary tenant attachment, missing mutual ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: platform-ingress
  namespace: ingress-infra
spec:
  gatewayClassName: istio-gateway
  listeners:
    - name: https-wildcard
      protocol: HTTPS
      port: 443
      allowedRoutes:
        namespaces:
          from: All # CRITICAL: Allows any namespace to attach and hijack ingress hostnames
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: checkout-route
  namespace: tenant-checkout
spec:
  parentRefs:
    - name: platform-ingress
      namespace: ingress-infra
  hostnames:
    - checkout.example.com
  rules:
    - backendRefs:
      - name: payment-svc
        namespace: tenant-payments # CRITICAL: Cross-namespace backend forward without ReferenceGrant
        port: 8080
```

#### Production Standard (Correct)
> **Standard**: The platform Gateway limits listener attachments to namespaces verified by an explicit label selector. The HTTPRoute accurately designates `sectionName` and `namespace`. The destination namespace `tenant-payments` explicitly authorizes cross-namespace backend forwarding using an authoritative ReferenceGrant resource.
```yaml
# SECURE: Mutual opt-in via Label Selector and explicit ReferenceGrant
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: platform-ingress
  namespace: ingress-infra
spec:
  gatewayClassName: cilium-gateway
  listeners:
    - name: tenant-https
      protocol: HTTPS
      port: 443
      allowedRoutes:
        namespaces:
          from: Selector
          selector:
            matchLabels:
              ingress.tenant.io/attachment: allowed
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: checkout-route
  namespace: tenant-checkout
spec:
  parentRefs:
    - name: platform-ingress
      namespace: ingress-infra
      sectionName: tenant-https
  hostnames:
    - checkout.example.com
  rules:
    - backendRefs:
      - name: payment-svc
        namespace: tenant-payments
        port: 8080
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
  name: allow-checkout-to-payment
  namespace: tenant-payments
spec:
  from:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      namespace: tenant-checkout
  to:
    - group: ""
      kind: Service
      name: payment-svc
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant

Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant defines an industrial-grade behavioral standard for devops engineering. Enforces strict bi-directional boundary verification for Kubernetes Gateway API v1.2 deployments across namespace perimeters. Eliminates cross-tenant traffic blackholing and route hijacking by requiring platform Gateways to scope listener attachments via namespace selectors and application namespaces to explicitly permit routing via ReferenceGrant resources. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Cross-Namespace Gateway Binding via Permissive Wildcards or Missing ReferenceGrant Handshakes Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftCross-Namespace Gateway Binding via Permissive Wildcards or Missing ReferenceGrant HandshakesCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS configure `listeners[].allowedRoutes.namespaces.from

Engineers MUST ALWAYS configure `listeners[].allowedRoutes.namespaces.from: Selector` on shared Gateways rather than `from: All` to enforce explicit namespace labeling.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST ALWAYS deploy a `gateway.networking.k8s.io/v1beta1` `ReferenceGrant` in the target namespace whenever an HTTPRoute references a Gateway or backend Service outside its own namespace.

Engineers MUST ALWAYS deploy a `gateway.networking.k8s.io/v1beta1` `ReferenceGrant` in the target namespace whenever an HTTPRoute references a Gateway or backend Service outside its own namespace.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

HTTPRoutes MUST explicitly declare `parentRefs[].namespace` and `parentRefs[].sectionName` when attaching to a platform Gateway across namespace boundaries.

HTTPRoutes MUST explicitly declare `parentRefs[].namespace` and `parentRefs[].sectionName` when attaching to a platform Gateway across namespace boundaries.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Platform Gateways MUST NOT bind to untrusted application namespaces without cryptographic or label-verified isolation boundaries.

Platform Gateways MUST NOT bind to untrusted application namespaces without cryptographic or label-verified isolation boundaries.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Cross-namespace backend references inside an `HTTPRoute` rule (e.g., routing to a Service in a different namespace) MUST be backed by a corresponding `ReferenceGrant` in the destination Service's namespace.

Cross-namespace backend references inside an `HTTPRoute` rule (e.g., routing to a Service in a different namespace) MUST be backed by a corresponding `ReferenceGrant` in the destination Service's namespace.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Continuous integration pipelines MUST fail linting if an HTTPRoute references a cross-namespace resource lacking a co-located or reconciling ReferenceGrant.

Continuous integration pipelines MUST fail linting if an HTTPRoute references a cross-namespace resource lacking a co-located or reconciling ReferenceGrant.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Kubernetes Gateway API v1.2 Cross-Namespace Route Attachment Invariant critical for AI coding assistants?

Enforces strict bi-directional boundary verification for Kubernetes Gateway API v1.2 deployments across namespace perimeters. Eliminates cross-tenant traffic blackholing and route hijacking by requiring platform Gateways to scope listener attachments via namespace selectors and application namespaces to explicitly permit routing via ReferenceGrant resources. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Cross-Namespace Gateway Binding via Permissive Wildcards or Missing ReferenceGrant Handshakes) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Kubernetes