This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: k8s-dra-gpu-lifecycle-sanitization-invariant

Deterministic Teardown and Memory Sanitization Invariant for DRA GPU Claims

Enforces deterministic teardown, compute context purging, and volatile memory sanitization for Dynamic Resource Allocation (DRA) GPU ResourceClaims between ephemeral container lifecycles. Prevents inter-tenant cross-contamination and persistent device allocation leaks in hardware-partitioned accelerators (such as NVIDIA MIG or MPS slices) by strictly sequencing driver deallocation phases with hardware sanitization handshakes.

Prohibited Anti-Pattern: Unsanitized Ephemeral Pod Deallocation Leaving Stale Hardware Contexts in Reusable GPU Partitions

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic Teardown and Memory Sanitization Invariant for DRA GPU Claims - Enforces deterministic teardown, compute context purging, and volatile memory sanitization for Dynamic Resource Allocation (DRA) GPU ResourceClaims between ephemeral container lifecycles. Prevents inter-tenant cross-contamination and persistent device allocation leaks in hardware-partitioned accelerators (such as NVIDIA MIG or MPS slices) by strictly sequencing driver deallocation phases with hardware sanitization handshakes.
globs: deployments/**/*.yaml, k8s/**/*.yaml, src/operators/**/*.ts, src/controllers/**/*.ts, src/schedulers/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-dra-gpu-lifecycle-sanitization-invariant"
---

# Deterministic Teardown and Memory Sanitization Invariant for DRA GPU Claims

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unsanitized Ephemeral Pod Deallocation Leaving Stale Hardware Contexts in Reusable GPU Partitions

## System Constraints
You are a Principal Software Architect enforcing production invariants for Kubernetes Dynamic Resource Allocation (DRA) (resource.k8s.io) and hardware-partitioned accelerator workflows. You must ensure that every GPU ResourceClaimTemplate or programmatic DRA allocation enforces deterministic teardown sequences, state zeroization, and compute context destruction prior to claim deallocation or node driver unprepare events. Reject any architectural pattern that treats GPU slices as unmanaged stateful remnants across pod recreations.

## Strict Guidelines
- Engineers MUST ALWAYS enforce explicit Pod termination grace periods synchronized with DRA driver NodeUnprepareResource lifecycle hooks to allow in-flight memory zeroization.
- Engineers MUST NOT bind hardware-partitioned GPU ResourceClaims with deallocation policies set to 'Retain' for untrusted or multi-tenant ephemeral workloads; claims MUST use 'Delete' or managed teardown operators.
- Controllers MUST verify that GPU compute instances (e.g., MIG CIs/GIs or MPS servers) have reset CUDA contexts and flushed high-bandwidth memory (HBM) prior to clearing finalizers on ResourceClaims.
- Workload specifications MUST define an explicit DRA ResourceClaimTemplate per pod replica rather than sharing mutable claim references across disjoint pod execution lifecycles.
- Driver orchestration logic MUST fail closed: if memory wiping or partition reset fails during teardown, the associated ResourceSlice MUST be marked unhealthy and cordoned from scheduling.

## Bad vs Good Implementations
### Enforcing Sanitized DRA ResourceClaim Lifecycle in TypeScript Kubernetes Operator

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Deleting the DRA ResourceClaim without checking partition readiness, triggering device reset routines, or validating that the driver's NodeUnprepareResource cleared registers allows raw GPU memory residual state to leak into subsequent pod allocations.
```typescript
import * as k8s from '@kubernetes/client-node';

// Flawed: Direct resource unbinding without GPU compute context purge or sanitization verification
export async function deallocateGpuClaim(k8sApi: k8s.CustomObjectsApi, claimName: string, namespace: string): Promise<void> {
  // Immediately deletes claim; leaves GPU HBM dirty and partition intact for next tenant
  await k8sApi.deleteNamespacedCustomObject(
    'resource.k8s.io',
    'v1alpha3',
    namespace,
    'resourceclaims',
    claimName
  );
}
```

#### Production Standard (Correct)
> **Standard**: Ensures active verification of hardware reset via driver hooks before initiating deallocation flags and purging Kubernetes ResourceClaims, guaranteeing complete isolation across ephemeral workloads.
```typescript
import * as k8s from '@kubernetes/client-node';

interface ResourceClaimStatus {
  allocation?: {
    devices?: {
      driver: string;
      pool: string;
      device: string;
    }[];
  };
  deallocationRequested?: boolean;
}

// Resilient: Guarantees deterministic teardown, reset verification, and finalizer-guarded deletion
export async function sanitizeAndDeallocateGpuClaim(
  k8sApi: k8s.CustomObjectsApi,
  claimName: string,
  namespace: string,
  sanitizerClient: { resetDevicePartition: (deviceId: string) => Promise<boolean> }
): Promise<void> {
  const res = await k8sApi.getNamespacedCustomObject(
    'resource.k8s.io',
    'v1alpha3',
    namespace,
    'resourceclaims',
    claimName
  );
  const claim = res.body as { status?: ResourceClaimStatus };
  const devices = claim.status?.allocation?.devices ?? [];

  // Step 1: Explicitly trigger hardware partition purge and context deallocation
  for (const dev of devices) {
    const sanitized = await sanitizerClient.resetDevicePartition(dev.device);
    if (!sanitized) {
      throw new Error(`Device sanitization failed for ${dev.device}. Aborting claim deallocation to prevent context leak.`);
    }
  }

  // Step 2: Signal Kubernetes DRA driver that claim deallocation is ready
  await k8sApi.patchNamespacedCustomObject(
    'resource.k8s.io',
    'v1alpha3',
    namespace,
    'resourceclaims',
    claimName,
    { status: { deallocationRequested: true } },
    undefined,
    undefined,
    undefined,
    { headers: { 'Content-Type': 'application/merge-patch+json' } }
  );

  // Step 3: Remove protection finalizer and safely delete
  await k8sApi.deleteNamespacedCustomObject(
    'resource.k8s.io',
    'v1alpha3',
    namespace,
    'resourceclaims',
    claimName
  );
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Deterministic Teardown and Memory Sanitization Invariant for DRA GPU Claims

Deterministic Teardown and Memory Sanitization Invariant for DRA GPU Claims defines an industrial-grade behavioral standard for devops engineering. Enforces deterministic teardown, compute context purging, and volatile memory sanitization for Dynamic Resource Allocation (DRA) GPU ResourceClaims between ephemeral container lifecycles. Prevents inter-tenant cross-contamination and persistent device allocation leaks in hardware-partitioned accelerators (such as NVIDIA MIG or MPS slices) by strictly sequencing driver deallocation phases with hardware sanitization handshakes. 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: Unsanitized Ephemeral Pod Deallocation Leaving Stale Hardware Contexts in Reusable GPU Partitions 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 DriftUnsanitized Ephemeral Pod Deallocation Leaving Stale Hardware Contexts in Reusable GPU PartitionsCritical
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 enforce explicit Pod termination grace periods synchronized with DRA driver NodeUnprepareResource lifecycle hooks to allow in-flight memory zeroization.

Engineers MUST ALWAYS enforce explicit Pod termination grace periods synchronized with DRA driver NodeUnprepareResource lifecycle hooks to allow in-flight memory zeroization.

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 NOT bind hardware-partitioned GPU ResourceClaims with deallocation policies set to 'Retain' for untrusted or multi-tenant ephemeral workloads

Engineers MUST NOT bind hardware-partitioned GPU ResourceClaims with deallocation policies set to 'Retain' for untrusted or multi-tenant ephemeral workloads; claims MUST use 'Delete' or managed teardown operators.

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

Controllers MUST verify that GPU compute instances (e.g., MIG CIs/GIs or MPS servers) have reset CUDA contexts and flushed high-bandwidth memory (HBM) prior to clearing finalizers on ResourceClaims.

Controllers MUST verify that GPU compute instances (e.g., MIG CIs/GIs or MPS servers) have reset CUDA contexts and flushed high-bandwidth memory (HBM) prior to clearing finalizers on ResourceClaims.

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

Workload specifications MUST define an explicit DRA ResourceClaimTemplate per pod replica rather than sharing mutable claim references across disjoint pod execution lifecycles.

Workload specifications MUST define an explicit DRA ResourceClaimTemplate per pod replica rather than sharing mutable claim references across disjoint pod execution lifecycles.

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

Driver orchestration logic MUST fail closed

Driver orchestration logic MUST fail closed: if memory wiping or partition reset fails during teardown, the associated ResourceSlice MUST be marked unhealthy and cordoned from scheduling.

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 Deterministic Teardown and Memory Sanitization Invariant for DRA GPU Claims critical for AI coding assistants?

Enforces deterministic teardown, compute context purging, and volatile memory sanitization for Dynamic Resource Allocation (DRA) GPU ResourceClaims between ephemeral container lifecycles. Prevents inter-tenant cross-contamination and persistent device allocation leaks in hardware-partitioned accelerators (such as NVIDIA MIG or MPS slices) by strictly sequencing driver deallocation phases with hardware sanitization handshakes. 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 (Unsanitized Ephemeral Pod Deallocation Leaving Stale Hardware Contexts in Reusable GPU Partitions) 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

Deterministic