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

Use the correct volum handle format for GCE regional PD. #99169

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion staging/src/k8s.io/csi-translation-lib/plugins/gce_pd.go
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a unit test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only test we have that covers PV translation is

func TestTranslationStability(t *testing.T) {
. Since translation from CSI to in-tree doesn't rely on the volume handle, this bug doesn't affect the test. We could add a PD specific test to cover TranslateInTreePVToCSI().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think a unit test to cover this case would be good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack - will add a test case.

} else {
// Unspecified
volID = fmt.Sprintf(volIDZonalFmt, UnspecifiedValue, UnspecifiedValue, pv.Spec.GCEPersistentDisk.PDName)
Expand Down
59 changes: 59 additions & 0 deletions staging/src/k8s.io/csi-translation-lib/plugins/gce_pd_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})
}
}