Skip to content

Conversation

@Trojan295
Copy link
Contributor

Add optional feature to wait for VolumeAttachments to be deleted after draining a node, preventing Multi-Attach errors when CSI drivers need time to clean up volumes before the node is terminated.

Uses controller-runtime's cached client with field indexes for efficient VolumeAttachment and Pod queries without hitting the API server directly. Follows Karpenter's approach of excluding VAs from non-drainable pods (DaemonSets, static pods) to avoid deadlocks.

Key changes:

  • Add Drain config section with WaitForVolumeDetach, VolumeDetachTimeout, and CacheSyncTimeout settings
  • Create controller-runtime cache with field indexes for VolumeAttachment and Pod resources by spec.nodeName
  • Implement getVolumeAttachmentsForNode() using Karpenter-style filtering
  • Graceful degradation: if cache sync fails, feature is disabled but drain operations continue normally

Environment variables:

  • DRAIN_WAIT_FOR_VOLUME_DETACH: Enable feature (default: false)
  • DRAIN_VOLUME_DETACH_TIMEOUT: Max wait time (default: 60s)
  • CACHE_SYNC_TIMEOUT: Cache sync timeout (default: 120s)

Add optional feature to wait for VolumeAttachments to be deleted after
draining a node, preventing Multi-Attach errors when CSI drivers need
time to clean up volumes before the node is terminated.

Uses controller-runtime's cached client with field indexes for efficient
VolumeAttachment and Pod queries without hitting the API server directly.
Follows Karpenter's approach of excluding VAs from non-drainable pods
(DaemonSets, static pods) to avoid deadlocks.

Key changes:
- Add Drain config section with WaitForVolumeDetach, VolumeDetachTimeout,
  and CacheSyncTimeout settings
- Create controller-runtime cache with field indexes for VolumeAttachment
  and Pod resources by spec.nodeName
- Implement getVolumeAttachmentsForNode() using Karpenter-style filtering
- Graceful degradation: if cache sync fails, feature is disabled but
  drain operations continue normally

Environment variables:
- DRAIN_WAIT_FOR_VOLUME_DETACH: Enable feature (default: false)
- DRAIN_VOLUME_DETACH_TIMEOUT: Max wait time (default: 60s)
- CACHE_SYNC_TIMEOUT: Cache sync timeout (default: 120s)
…tach

The spec.nodeName field selector is not supported by the Kubernetes API
server for VolumeAttachment resources. This caused the waitForVolumeDetach
function to fail silently.

Switch to using the controller-runtime cached client which has a custom
field indexer configured for spec.nodeName lookups on VolumeAttachments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants