From 068bdcfcefe34ebaae4b85dc44a48c397eb14bbc Mon Sep 17 00:00:00 2001 From: "tewei.luo" Date: Wed, 17 Feb 2021 22:22:51 +0000 Subject: [PATCH] Use the correct volum handle format for GCE regional PD. --- .../csi-translation-lib/plugins/gce_pd.go | 2 +- .../plugins/gce_pd_test.go | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd.go b/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd.go index 7ca1f67c58fd..b5a38bd85c87 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd.go @@ -231,7 +231,7 @@ func (g *gcePersistentDiskCSITranslator) TranslateInTreePVToCSI(pv *v1.Persisten if err != nil { return nil, fmt.Errorf("failed to get region from zones: %v", err) } - volID = fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, region, pv.Spec.GCEPersistentDisk.PDName) + volID = fmt.Sprintf(volIDRegionalFmt, UnspecifiedValue, region, pv.Spec.GCEPersistentDisk.PDName) } else { // Unspecified volID = fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, UnspecifiedValue, pv.Spec.GCEPersistentDisk.PDName) diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd_test.go b/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd_test.go index 9347f51c6de3..3dfeb9b93899 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd_test.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/gce_pd_test.go @@ -23,6 +23,7 @@ import ( v1 "k8s.io/api/core/v1" storage "k8s.io/api/storage/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func NewStorageClass(params map[string]string, allowedTopologies []v1.TopologySelectorTerm) *storage.StorageClass { @@ -294,3 +295,61 @@ func TestInlineReadOnly(t *testing.T) { t.Errorf("got am %v, expected access mode of ReadOnlyMany", ams[0]) } } + +func TestTranslateInTreePVToCSIVolIDFmt(t *testing.T) { + g := NewGCEPersistentDiskCSITranslator() + pdName := "pd-name" + tests := []struct { + desc string + topologyLabelKey string + topologyLabelValue string + wantVolId string + }{ + { + desc: "beta topology key zonal", + topologyLabelKey: v1.LabelFailureDomainBetaZone, + topologyLabelValue: "us-east1-a", + wantVolId: "projects/UNSPECIFIED/zones/us-east1-a/disks/pd-name", + }, + { + desc: "v1 topology key zonal", + topologyLabelKey: v1.LabelTopologyZone, + topologyLabelValue: "us-east1-a", + wantVolId: "projects/UNSPECIFIED/zones/us-east1-a/disks/pd-name", + }, + { + desc: "beta topology key regional", + topologyLabelKey: v1.LabelFailureDomainBetaZone, + topologyLabelValue: "us-central1-a__us-central1-c", + wantVolId: "projects/UNSPECIFIED/regions/us-central1/disks/pd-name", + }, + { + desc: "v1 topology key regional", + topologyLabelKey: v1.LabelTopologyZone, + topologyLabelValue: "us-central1-a__us-central1-c", + wantVolId: "projects/UNSPECIFIED/regions/us-central1/disks/pd-name", + }, + } + for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + translatedPV, err := g.TranslateInTreePVToCSI(&v1.PersistentVolume{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{tc.topologyLabelKey: tc.topologyLabelValue}, + }, + Spec: v1.PersistentVolumeSpec{ + PersistentVolumeSource: v1.PersistentVolumeSource{ + GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{ + PDName: pdName, + }, + }, + }, + }) + if err != nil { + t.Errorf("got error translating in-tree PV to CSI: %v", err) + } + if got := translatedPV.Spec.PersistentVolumeSource.CSI.VolumeHandle; got != tc.wantVolId { + t.Errorf("got translated volume handle: %q, want %q", got, tc.wantVolId) + } + }) + } +}