diff --git a/examples/deploy/rbac/cluster-role.yaml b/examples/deploy/rbac/cluster-role.yaml index f29d39e58..3c611b16b 100644 --- a/examples/deploy/rbac/cluster-role.yaml +++ b/examples/deploy/rbac/cluster-role.yaml @@ -47,3 +47,9 @@ rules: - daemonsets verbs: - get + - apiGroups: + - storage.k8s.io + resources: + - volumeattachments + verbs: + - list diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 2ca395ed1..6b807da87 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -290,6 +290,30 @@ func (k *klocksmith) process(ctx context.Context) error { klog.Info("Node drained, rebooting") + for { + attachments, err := k.clientset.StorageV1().VolumeAttachments().List(ctx, metav1.ListOptions{}) + if err != nil { + klog.Errorf("Listing volume attachments: %v", err) + continue + } + + anyVolumeAttached := false + + for _, attachment := range attachments.Items { + if attachment.Status.Attached && attachment.Spec.NodeName == k.nodeName { + anyVolumeAttached = true + klog.Infof("Volume %q is still attached, waiting for detach", attachment.Name) + } + } + + if !anyVolumeAttached { + klog.Info("All volumes are detached from node, rebooting.") + break + } + + time.Sleep(5 * time.Second) + } + // Reboot. k.lc.Reboot(false)