Merge pull request #8715 from Lyndon-Li/issue-fix-8706
Some checks failed
Run the E2E test on kind / build (push) Failing after 6m45s
Run the E2E test on kind / setup-test-matrix (push) Successful in 3s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 36s
Close stale issues and PRs / stale (push) Successful in 9s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 55s
Trivy Nightly Scan / Trivy nightly scan (velero-restore-helper, main) (push) Failing after 44s

Issue 8706: for immediate volumes, get node from volumeattachment
This commit is contained in:
Wenkai Yin(尹文开)
2025-02-25 14:25:45 +08:00
committed by GitHub
3 changed files with 126 additions and 6 deletions

View File

@@ -0,0 +1 @@
Fix issue #8706, for immediate volumes, there is no selected-node annotation on PVC, so deduce the attached node from VolumeAttachment CRs

View File

@@ -441,10 +441,34 @@ func GetPVCAttachingNodeOS(pvc *corev1api.PersistentVolumeClaim, nodeClient core
return NodeOSLinux, nil
}
if pvc.Spec.VolumeName == "" {
log.Warnf("PVC %s/%s is not bound to a PV", pvc.Namespace, pvc.Name)
}
if pvc.Spec.StorageClassName == nil {
log.Warnf("PVC %s/%s is not with storage class", pvc.Namespace, pvc.Name)
}
nodeName := ""
if value := pvc.Annotations[KubeAnnSelectedNode]; value != "" {
os, err := GetNodeOS(context.Background(), value, nodeClient)
nodeName = value
}
if nodeName == "" {
if pvc.Spec.VolumeName != "" {
n, err := GetPVAttachedNode(context.Background(), pvc.Spec.VolumeName, storageClient)
if err != nil {
return "", errors.Wrapf(err, "error to get attached node for PVC %s/%s", pvc.Namespace, pvc.Name)
}
nodeName = n
}
}
if nodeName != "" {
os, err := GetNodeOS(context.Background(), nodeName, nodeClient)
if err != nil {
return "", errors.Wrapf(err, "error to get os from node %s for PVC %s/%s", value, pvc.Namespace, pvc.Name)
return "", errors.Wrapf(err, "error to get os from node %s for PVC %s/%s", nodeName, pvc.Namespace, pvc.Name)
}
nodeOS = os
@@ -474,3 +498,18 @@ func GetPVCAttachingNodeOS(pvc *corev1api.PersistentVolumeClaim, nodeClient core
log.Warnf("Cannot deduce node os for PVC %s/%s, default to linux", pvc.Namespace, pvc.Name)
return NodeOSLinux, nil
}
func GetPVAttachedNode(ctx context.Context, pv string, storageClient storagev1.StorageV1Interface) (string, error) {
vaList, err := storageClient.VolumeAttachments().List(ctx, metav1.ListOptions{})
if err != nil {
return "", errors.Wrapf(err, "error listing volumeattachment")
}
for _, va := range vaList.Items {
if va.Spec.Source.PersistentVolumeName != nil && *va.Spec.Source.PersistentVolumeName == pv {
return va.Spec.NodeName, nil
}
}
return "", nil
}

View File

@@ -1594,6 +1594,16 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
},
}
pvcObjWithVolume := &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-namespace",
Name: "fake-pvc",
},
Spec: corev1api.PersistentVolumeClaimSpec{
VolumeName: "fake-volume-name",
},
}
pvcObjWithStorageClass := &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-namespace",
@@ -1604,13 +1614,26 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
},
}
pvcObjWithBoth := &corev1api.PersistentVolumeClaim{
pvName := "fake-volume-name"
pvcObjWithAll := &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-namespace",
Name: "fake-pvc",
Annotations: map[string]string{KubeAnnSelectedNode: "fake-node"},
},
Spec: corev1api.PersistentVolumeClaimSpec{
VolumeName: pvName,
StorageClassName: &storageClass,
},
}
pvcObjWithVolumeSC := &corev1api.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-namespace",
Name: "fake-pvc",
},
Spec: corev1api.PersistentVolumeClaimSpec{
VolumeName: pvName,
StorageClassName: &storageClass,
},
}
@@ -1628,6 +1651,35 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
Parameters: map[string]string{"csi.storage.k8s.io/fstype": "ntfs"},
}
volAttachEmpty := &storagev1api.VolumeAttachment{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-volume-attach-1",
},
}
volAttachWithVolume := &storagev1api.VolumeAttachment{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-volume-attach-2",
},
Spec: storagev1api.VolumeAttachmentSpec{
Source: storagev1api.VolumeAttachmentSource{
PersistentVolumeName: &pvName,
},
},
}
otherPVName := "other-volume-name"
volAttachWithOtherVolume := &storagev1api.VolumeAttachment{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-volume-attach-3",
},
Spec: storagev1api.VolumeAttachmentSpec{
Source: storagev1api.VolumeAttachmentSource{
PersistentVolumeName: &otherPVName,
},
},
}
tests := []struct {
name string
pvc *corev1api.PersistentVolumeClaim
@@ -1636,7 +1688,7 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
err string
}{
{
name: "no selected node and storage class",
name: "no selected node, volume name and storage class",
pvc: pvcObj,
expectedNodeOS: NodeOSLinux,
},
@@ -1653,11 +1705,27 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
},
expectedNodeOS: NodeOSLinux,
},
{
name: "no attach volume",
pvc: pvcObjWithVolume,
expectedNodeOS: NodeOSLinux,
},
{
name: "sc doesn't exist",
pvc: pvcObjWithStorageClass,
err: "error to get storage class fake-storage-class: storageclasses.storage.k8s.io \"fake-storage-class\" not found",
},
{
name: "volume attachment not exist",
pvc: pvcObjWithVolume,
kubeClientObj: []runtime.Object{
nodeWindows,
scObjWithFSType,
volAttachEmpty,
volAttachWithOtherVolume,
},
expectedNodeOS: NodeOSLinux,
},
{
name: "sc without fsType",
pvc: pvcObjWithStorageClass,
@@ -1668,7 +1736,7 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
},
{
name: "deduce from node os",
pvc: pvcObjWithBoth,
pvc: pvcObjWithAll,
kubeClientObj: []runtime.Object{
nodeWindows,
scObjWithFSType,
@@ -1677,13 +1745,25 @@ func TestGetPVCAttachingNodeOS(t *testing.T) {
},
{
name: "deduce from sc",
pvc: pvcObjWithBoth,
pvc: pvcObjWithAll,
kubeClientObj: []runtime.Object{
nodeNoOSLabel,
scObjWithFSType,
},
expectedNodeOS: NodeOSWindows,
},
{
name: "deduce from attached node os",
pvc: pvcObjWithVolumeSC,
kubeClientObj: []runtime.Object{
nodeWindows,
scObjWithFSType,
volAttachEmpty,
volAttachWithOtherVolume,
volAttachWithVolume,
},
expectedNodeOS: NodeOSWindows,
},
{
name: "block access",
pvc: pvcObjBlockMode,