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

Kubernetes 1.32 DRA Structured CDI Claim Lifecycle & Deterministic Unprepare Guardrail

Enforces strict conformance to Kubernetes 1.32 Dynamic Resource Allocation (DRA) structured parameters and Container Device Interface (CDI) device claim lifecycles. Mandates idempotent NodeUnprepareResource execution, transactional finalizer synchronization, and atomic cleanup of dynamic /var/run/cdi specifications. Prevents device leakage and node-level orphaned hardware allocations during Pod eviction, ungraceful termination, or kubelet restarts.

Prohibited Anti-Pattern: Leaking physical GPU/accelerator allocations via non-idempotent DRA NodeUnprepare execution and static CDI spec abandonment

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Kubernetes 1.32 DRA Structured CDI Claim Lifecycle & Deterministic Unprepare Guardrail - Enforces strict conformance to Kubernetes 1.32 Dynamic Resource Allocation (DRA) structured parameters and Container Device Interface (CDI) device claim lifecycles. Mandates idempotent NodeUnprepareResource execution, transactional finalizer synchronization, and atomic cleanup of dynamic /var/run/cdi specifications. Prevents device leakage and node-level orphaned hardware allocations during Pod eviction, ungraceful termination, or kubelet restarts.
globs: pkg/dra/**/*.go, controllers/**/*.go, cmd/dra-driver/**/*.go, pkg/cdi/**/*.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/k8s-dra-cdi-claim-lifecycle-standard"
---

# Kubernetes 1.32 DRA Structured CDI Claim Lifecycle & Deterministic Unprepare Guardrail

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Leaking physical GPU/accelerator allocations via non-idempotent DRA NodeUnprepare execution and static CDI spec abandonment

## System Constraints
You are a Principal Cloud-Native Systems Architect enforcing Kubernetes 1.32 Dynamic Resource Allocation (DRA) and Container Device Interface (CDI) driver invariants. You must prevent orphaned hardware allocations by mandating deterministic, idempotent NodeUnprepareResource gRPC handlers, safe cleanup of dynamic CDI JSON specifications under /var/run/cdi, and strictly aligned ResourceClaim finalizer lifecycles.

## Strict Guidelines
- Engineers MUST ALWAYS implement NodeUnprepareResource as a strictly idempotent gRPC handler, returning status Code OK (nil error) if the target claim or CDI device has already been purged.
- Engineers MUST atomically unlink dynamic CDI JSON specification files from /var/run/cdi (or vendor-configured directory) before releasing internal device hardware contexts or lock states.
- Engineers MUST NOT rely on in-memory device state or transient process maps; device state recovery MUST be reconstructed deterministically from checkpoints or filesystem-level CDI registries upon driver startup.
- Driver implementations MUST return non-transient gRPC codes (e.g., codes.Internal) ONLY when low-level hardware or state operations fail irreversibly, allowing the kubelet DRA manager to safely retry unprepare lifecycles without thrashing.
- Services MUST ensure that CDI device names generated within Pod annotations adhere strictly to the CDI Fully Qualified Device Name specification (e.g., 'vendor.com/device=id') matching dynamic CDI manifests.
- ResourceClaim status updates MUST NEVER clear driver-owned finalizers until the node driver has successfully confirmed total physical teardown and unmount of the hardware claim.

## Bad vs Good Implementations
### Deterministic NodeUnprepareResource gRPC Handler with CDI State Teardown

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The handler returns a gRPC NotFound error if the CDI spec file was already unlinked during a previous failed or timed-out RPC attempt. This causes kubelet reconciliation loops to fail continuously or misinterpret driver health, leaving underlying physical accelerator allocations leaked or inaccessible for subsequent Pod ResourceClaims.
```go
package driver

import (
	"context"
	"os"
	"path/filepath"
	drapbv1 "k8s.io/kubelet/pkg/apis/dra/v1alpha4"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// BAD: Fails idempotency; errors out if file does not exist, uses static in-memory maps,
// leaves physical hardware lock uncleared if unprepare receives a partially deleted claim.
type BadDriver struct {
	allocatedDevices map[string]string
}

func (d *BadDriver) NodeUnprepareResource(ctx context.Context, req *drapbv1.NodeUnprepareResourceRequest) (*drapbv1.NodeUnprepareResourceResponse, error) {
	claimUID := req.ClaimUid
	cdiFile := filepath.Join("/var/run/cdi", "vendor-" + claimUID + ".json")

	// Non-idempotent: If kubelet retries after a prior successful file deletion, this returns an error
	if err := os.Remove(cdiFile); err != nil {
		return nil, status.Errorf(codes.NotFound, "failed to remove cdi spec: %v", err)
	}

	// In-memory mutation prone to loss upon driver restart, causing permanent hardware state leakage
	delete(d.allocatedDevices, claimUID)
	return &drapbv1.NodeUnprepareResourceResponse{}, nil
}
```

#### Production Standard (Correct)
> **Standard**: The refactored implementation handles os.ErrNotExist gracefully, ensuring strictly idempotent execution as required by Kubernetes DRA node driver specifications. Physical resource states and dynamic CDI manifests are decoupled and released sequentially under protective synchronization, allowing the kubelet to retry unprepare actions safely without leaking GPU slices or block devices.
```go
package driver

import (
	"context"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"sync"
	drapbv1 "k8s.io/kubelet/pkg/apis/dra/v1alpha4"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type DeviceManager interface {
	ReleaseDevice(ctx context.Context, claimUID string) error
}

type GoodDriver struct {
	drapbv1.UnimplementedNodeServer
	mu      sync.Mutex
	cdiDir  string
	devMgr  DeviceManager
}

func (d *GoodDriver) NodeUnprepareResource(ctx context.Context, req *drapbv1.NodeUnprepareResourceRequest) (*drapbv1.NodeUnprepareResourceResponse, error) {
	if req == nil || req.ClaimUid == "" {
		return nil, status.Error(codes.InvalidArgument, "claim UID must be provided")
	}

	d.mu.Lock()
	defer d.mu.Unlock()

	claimUID := req.ClaimUid
	cdiFileName := fmt.Sprintf("vendor-dra-%s.json", claimUID)
	cdiFilePath := filepath.Join(d.cdiDir, cdiFileName)

	// 1. Idempotently remove dynamic CDI registry spec
	if err := os.Remove(cdiFilePath); err != nil && !errors.Is(err, os.ErrNotExist) {
		return nil, status.Errorf(codes.Internal, "failed to purge CDI manifest %s: %v", cdiFilePath, err)
	}

	// 2. Teardown underlying hardware context idempotently via persistent state tracker
	if err := d.devMgr.ReleaseDevice(ctx, claimUID); err != nil {
		return nil, status.Errorf(codes.Internal, "failed to release hardware allocation for claim %s: %v", claimUID, err)
	}

	return &drapbv1.NodeUnprepareResourceResponse{}, nil
}
```

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: Kubernetes 1.32 DRA Structured CDI Claim Lifecycle & Deterministic Unprepare Guardrail

Kubernetes 1.32 DRA Structured CDI Claim Lifecycle & Deterministic Unprepare Guardrail defines an industrial-grade behavioral standard for devops engineering. Enforces strict conformance to Kubernetes 1.32 Dynamic Resource Allocation (DRA) structured parameters and Container Device Interface (CDI) device claim lifecycles. Mandates idempotent NodeUnprepareResource execution, transactional finalizer synchronization, and atomic cleanup of dynamic /var/run/cdi specifications. Prevents device leakage and node-level orphaned hardware allocations during Pod eviction, ungraceful termination, or kubelet restarts. 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: Leaking physical GPU/accelerator allocations via non-idempotent DRA NodeUnprepare execution and static CDI spec abandonment 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 DriftLeaking physical GPU/accelerator allocations via non-idempotent DRA NodeUnprepare execution and static CDI spec abandonmentCritical
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 implement NodeUnprepareResource as a strictly idempotent gRPC handler, returning status Code OK (nil error) if the target claim or CDI device has already been purged.

Engineers MUST ALWAYS implement NodeUnprepareResource as a strictly idempotent gRPC handler, returning status Code OK (nil error) if the target claim or CDI device has already been purged.

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 atomically unlink dynamic CDI JSON specification files from /var/run/cdi (or vendor-configured directory) before releasing internal device hardware contexts or lock states.

Engineers MUST atomically unlink dynamic CDI JSON specification files from /var/run/cdi (or vendor-configured directory) before releasing internal device hardware contexts or lock states.

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

Engineers MUST NOT rely on in-memory device state or transient process maps

Engineers MUST NOT rely on in-memory device state or transient process maps; device state recovery MUST be reconstructed deterministically from checkpoints or filesystem-level CDI registries upon driver startup.

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

Driver implementations MUST return non-transient gRPC codes (e.g., codes.Internal) ONLY when low-level hardware or state operations fail irreversibly, allowing the kubelet DRA manager to safely retry unprepare lifecycles without thrashing.

Driver implementations MUST return non-transient gRPC codes (e.g., codes.Internal) ONLY when low-level hardware or state operations fail irreversibly, allowing the kubelet DRA manager to safely retry unprepare lifecycles without thrashing.

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

Services MUST ensure that CDI device names generated within Pod annotations adhere strictly to the CDI Fully Qualified Device Name specification (e.g., 'vendor.com/device=id') matching dynamic CDI manifests.

Services MUST ensure that CDI device names generated within Pod annotations adhere strictly to the CDI Fully Qualified Device Name specification (e.g., 'vendor.com/device=id') matching dynamic CDI manifests.

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

ResourceClaim status updates MUST NEVER clear driver-owned finalizers until the node driver has successfully confirmed total physical teardown and unmount of the hardware claim.

ResourceClaim status updates MUST NEVER clear driver-owned finalizers until the node driver has successfully confirmed total physical teardown and unmount of the hardware claim.

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 1.32 DRA Structured CDI Claim Lifecycle & Deterministic Unprepare Guardrail critical for AI coding assistants?

Enforces strict conformance to Kubernetes 1.32 Dynamic Resource Allocation (DRA) structured parameters and Container Device Interface (CDI) device claim lifecycles. Mandates idempotent NodeUnprepareResource execution, transactional finalizer synchronization, and atomic cleanup of dynamic /var/run/cdi specifications. Prevents device leakage and node-level orphaned hardware allocations during Pod eviction, ungraceful termination, or kubelet restarts. 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 (Leaking physical GPU/accelerator allocations via non-idempotent DRA NodeUnprepare execution and static CDI spec abandonment) 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