Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ignore released volumes when deleting a node #1699

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/controllers/node/termination/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/clock"
Expand Down Expand Up @@ -193,9 +194,18 @@
return len(filteredVolumeAttachments) == 0, nil
}

// volumeIsNotAttachedOrBound returns true if the persistent volume storage is neither Attached nor Bound
func volumeIsNotAttachedOrBound(ctx context.Context, kubeClient client.Client, pvName string) (bool, error) {
pv := &corev1.PersistentVolume{}
if err := kubeClient.Get(ctx, types.NamespacedName{Name: pvName}, pv); err != nil {
return false, fmt.Errorf("getting persistent volume %q, %w", pvName, err)
}
return pv.Status.Phase == corev1.VolumePending || pv.Status.Phase == corev1.VolumeReleased || pv.Status.Phase == corev1.VolumeFailed, nil
}

// filterVolumeAttachments filters out storagev1.VolumeAttachments that should not block the termination
// of the passed corev1.Node
func filterVolumeAttachments(ctx context.Context, kubeClient client.Client, node *corev1.Node, volumeAttachments []*storagev1.VolumeAttachment, clk clock.Clock) ([]*storagev1.VolumeAttachment, error) {

Check failure on line 208 in pkg/controllers/node/termination/controller.go

View workflow job for this annotation

GitHub Actions / presubmit (1.25.x)

cyclomatic complexity 13 of func `filterVolumeAttachments` is high (> 11) (gocyclo)

Check failure on line 208 in pkg/controllers/node/termination/controller.go

View workflow job for this annotation

GitHub Actions / presubmit (1.26.x)

cyclomatic complexity 13 of func `filterVolumeAttachments` is high (> 11) (gocyclo)

Check failure on line 208 in pkg/controllers/node/termination/controller.go

View workflow job for this annotation

GitHub Actions / presubmit (1.27.x)

cyclomatic complexity 13 of func `filterVolumeAttachments` is high (> 11) (gocyclo)

Check failure on line 208 in pkg/controllers/node/termination/controller.go

View workflow job for this annotation

GitHub Actions / presubmit (1.28.x)

cyclomatic complexity 13 of func `filterVolumeAttachments` is high (> 11) (gocyclo)

Check failure on line 208 in pkg/controllers/node/termination/controller.go

View workflow job for this annotation

GitHub Actions / presubmit (1.29.x)

cyclomatic complexity 13 of func `filterVolumeAttachments` is high (> 11) (gocyclo)

Check failure on line 208 in pkg/controllers/node/termination/controller.go

View workflow job for this annotation

GitHub Actions / presubmit (1.30.x)

cyclomatic complexity 13 of func `filterVolumeAttachments` is high (> 11) (gocyclo)
// No need to filter empty VolumeAttachments list
if len(volumeAttachments) == 0 {
return volumeAttachments, nil
Expand Down Expand Up @@ -225,6 +235,21 @@
}
}
}

// ignore volume attachments associated with volumes that are no longer bound
for _, va := range volumeAttachments {
unattached, err := volumeIsNotAttachedOrBound(ctx, kubeClient, *va.Spec.Source.PersistentVolumeName)
if errors.IsNotFound(err) {
continue
}
if err != nil {
return nil, err
}
if unattached {
shouldFilterOutVolume.Insert(*va.Spec.Source.PersistentVolumeName)
}
}

filteredVolumeAttachments := lo.Reject(volumeAttachments, func(v *storagev1.VolumeAttachment, _ int) bool {
pvName := v.Spec.Source.PersistentVolumeName
return pvName == nil || shouldFilterOutVolume.Has(*pvName)
Expand Down
19 changes: 19 additions & 0 deletions pkg/controllers/node/termination/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,25 @@ var _ = Describe("Termination", func() {
ExpectObjectReconciled(ctx, env.Client, terminationController, node)
ExpectNotFound(ctx, env.Client, node)
})
It("should not wait for volume attachments with released persistent volumes", func() {
va := test.VolumeAttachment(test.VolumeAttachmentOptions{
NodeName: node.Name,
VolumeName: "foo",
})
pv := test.PersistentVolume(test.PersistentVolumeOptions{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
})
pv.Status.Phase = corev1.VolumeReleased

ExpectApplied(ctx, env.Client, node, nodeClaim, nodePool, pv, va)
Expect(env.Client.Delete(ctx, node)).To(Succeed())

ExpectObjectReconciled(ctx, env.Client, terminationController, node)
ExpectObjectReconciled(ctx, env.Client, terminationController, node)
ExpectNotFound(ctx, env.Client, node)
})
It("should wait for volume attachments until the nodeclaim's termination grace period expires", func() {
va := test.VolumeAttachment(test.VolumeAttachmentOptions{
NodeName: node.Name,
Expand Down
Loading