← Back to Pull requests

Add distributed node locking to prevent concurrent maintenance operations #737

Closed
unknown closed this pull request without merging on Jan 30, 2026 07:07 UTC
View on GitHub
unknown unknown commented on Jan 23, 2026 23:32 UTC
Author

Summary

This PR implements Kubernetes Lease-based distributed locking to prevent concurrent maintenance operations from operating on the same node simultaneously. This critical enhancement ensures safe coordination when multiple RebootNode or TerminateNode CRs target the same node.

Changes

New Package: pkg/distributedlock

File: nodelock.go (~200 lines)

  • NodeLock interface with LockNode() and CheckUnlock() methods

  • Lease-based implementation using coordination.k8s.io/v1 Leases

  • Automatic cleanup via Kubernetes garbage collection (OwnerReferences)

  • Comprehensive error handling with metrics emission
  • Controller Integration

    RebootNode Controller (pkg/controller/rebootnode_controller.go):

  • Acquire lock before reconciliation begins

  • Release lock after CompletionTime is set

  • Requeue if lock cannot be acquired
  • TerminateNode Controller (pkg/controller/terminatenode_controller.go):

  • Same locking pattern as RebootNode

  • Ensures node termination operations are serialized
  • Metrics

    Added lock/unlock action types to pkg/metrics/metrics.go:

  • ActionTypeLock - Lock acquisition failures

  • ActionTypeUnlock - Lock release failures
  • RBAC

    Updated both controllers with lease permissions:

    // +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;delete
    

    How It Works

    1. Lock Acquisition: Before reconciling, controller tries to create a Lease with the node name in the nvsentinel-system namespace
    2. Lock Hold: If successful, reconciliation proceeds; the Lease remains until CompletionTime is set
    3. Lock Release: After CompletionTime is set, the next reconcile deletes the Lease
    4. Automatic Cleanup: OwnerReferences ensure Leases are deleted if the maintenance CR is deleted

    Concurrency Handling

  • Same Node, Different CRs: If RebootNode-A holds a lock on node-1, RebootNode-B targeting the same node will requeue until the lock is released

  • Different Nodes: Operations on different nodes proceed in parallel without interference

  • Cross-Controller: RebootNode and TerminateNode share the same lock namespace, preventing concurrent operations across controller types
  • Testing

  • ✅ All 57 tests pass

  • ✅ Controller-specific tests verify locking behavior

  • ✅ Test fixtures properly set GVK for Lease OwnerReferences

  • ✅ Lock acquisition, holding, and release tested

  • ✅ Cleanup on CR deletion verified
  • Performance Impact

  • Minimal: One additional API call per reconcile loop (Lease GET/CREATE/DELETE)

  • No polling or watches on Leases

  • Efficient requeue mechanism when lock unavailable
  • 🤖 Generated with Claude Code

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    New Features
    Per-node distributed locking added to maintenance controllers to serialize operations; lock namespace configurable via POD_NAMESPACE; lock/unlock actions emit metrics.
    Controllers now refresh status after reconciliation and perform unlock checks when work is complete.

    Tests
    Extensive unit and integration tests covering lock acquisition, release, ownership scenarios, error paths, and metrics validation.

    Chores
    * RBAC updated to allow lease management; deployment updated to supply POD_NAMESPACE.

    <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub>
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 440 lines (+330 / -110) across 5 files Reviewer: jtschelling Review duration: 9095m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.45) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 440 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.364
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #737"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.3% (MEDIUM) · Higher = larger blast radius
    45.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.6% · Safe to merge
    19.6%