-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Ensure volumes are detached before deleting node
- Loading branch information
1 parent
4a72da3
commit 9087fc7
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package volumeattachment | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/samber/lo" | ||
|
||
"sigs.k8s.io/karpenter/pkg/utils/pod" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
storagev1 "k8s.io/api/storage/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
nodeutil "sigs.k8s.io/karpenter/pkg/utils/node" | ||
volumeutil "sigs.k8s.io/karpenter/pkg/utils/volume" | ||
) | ||
|
||
// FilterVolumeAttachments filters out volumeAttachments that should not block the termination of the passed node | ||
func FilterVolumeAttachments(ctx context.Context, kubeClient client.Client, node *v1.Node, volumeAttachments []*storagev1.VolumeAttachment) ([]*storagev1.VolumeAttachment, error) { | ||
var filteredVolumeAttachments []*storagev1.VolumeAttachment | ||
// No need to filter empty volumeAttachments list | ||
if len(volumeAttachments) == 0 { | ||
return volumeAttachments, nil | ||
} | ||
// Filter out non-drain-able pods | ||
pods, err := nodeutil.GetPods(ctx, kubeClient, node) | ||
if err != nil { | ||
return nil, err | ||
} | ||
drainablePods := lo.Reject(pods, func(p *v1.Pod, _ int) bool { | ||
return pod.ToleratesDisruptionNoScheduleTaint(p) | ||
}) | ||
// Filter out Multi-Attach volumes | ||
shouldFilterOutVolume := make(map[string]bool) | ||
for _, p := range drainablePods { | ||
for _, v := range p.Spec.Volumes { | ||
pvc, err := volumeutil.GetPersistentVolumeClaim(ctx, kubeClient, p, v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if pvc != nil { | ||
shouldFilterOutVolume[pvc.Spec.VolumeName] = CannotMultiAttach(*pvc) | ||
} | ||
} | ||
} | ||
for i := range volumeAttachments { | ||
pvName := volumeAttachments[i].Spec.Source.PersistentVolumeName | ||
if pvName != nil && shouldFilterOutVolume[*pvName] { | ||
filteredVolumeAttachments = append(filteredVolumeAttachments, volumeAttachments[i]) | ||
} | ||
} | ||
return filteredVolumeAttachments, nil | ||
} | ||
|
||
// CannotMultiAttach returns true if the persistentVolumeClaim's underlying volume cannot be attached to multiple nodes | ||
// i.e. its access mode is not ReadWriteOnce/ReadWriteOncePod | ||
func CannotMultiAttach(pvc v1.PersistentVolumeClaim) bool { | ||
for _, accessMode := range pvc.Spec.AccessModes { | ||
if accessMode == v1.ReadWriteOnce || accessMode == v1.ReadWriteOncePod { | ||
return true | ||
} | ||
} | ||
return false | ||
} |