Skip to content

Commit 0ece154

Browse files
fix: repair project for underspecified volume key
1 parent 1364a18 commit 0ece154

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

pkg/gce-cloud-provider/compute/fake-gce.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,34 @@ func (cloud *FakeCloudProvider) GetDefaultZone() string {
8080
return cloud.zone
8181
}
8282

83-
func (cloud *FakeCloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (*meta.Key, error) {
83+
func (cloud *FakeCloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (string, *meta.Key, error) {
84+
if project == common.UnspecifiedValue {
85+
project = cloud.project
86+
}
8487
switch volumeKey.Type() {
8588
case meta.Zonal:
8689
if volumeKey.Zone != common.UnspecifiedValue {
87-
return volumeKey, nil
90+
return project, volumeKey, nil
8891
}
8992
for name, d := range cloud.disks {
9093
if name == volumeKey.Name {
9194
volumeKey.Zone = d.GetZone()
92-
return volumeKey, nil
95+
return project, volumeKey, nil
9396
}
9497
}
95-
return nil, notFoundError()
98+
return "", nil, notFoundError()
9699
case meta.Regional:
97100
if volumeKey.Region != common.UnspecifiedValue {
98-
return volumeKey, nil
101+
return project, volumeKey, nil
99102
}
100103
r, err := common.GetRegionFromZones([]string{cloud.zone})
101104
if err != nil {
102-
return nil, fmt.Errorf("failed to get region from zones: %v", err)
105+
return "", nil, fmt.Errorf("failed to get region from zones: %v", err)
103106
}
104107
volumeKey.Region = r
105-
return volumeKey, nil
108+
return project, volumeKey, nil
106109
default:
107-
return nil, fmt.Errorf("Volume key %v not zonal nor regional", volumeKey.Name)
110+
return "", nil, fmt.Errorf("Volume key %v not zonal nor regional", volumeKey.Name)
108111
}
109112
}
110113

pkg/gce-cloud-provider/compute/gce-compute.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type GCECompute interface {
5454
GetDefaultZone() string
5555
// Disk Methods
5656
GetDisk(ctx context.Context, project string, volumeKey *meta.Key, gceAPIVersion GCEAPIVersion) (*CloudDisk, error)
57-
RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (*meta.Key, error)
57+
RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (string, *meta.Key, error)
5858
ValidateExistingDisk(ctx context.Context, disk *CloudDisk, params common.DiskParameters, reqBytes, limBytes int64, multiWriter bool) error
5959
InsertDisk(ctx context.Context, project string, volKey *meta.Key, params common.DiskParameters, capBytes int64, capacityRange *csi.CapacityRange, replicaZones []string, snapshotID string, multiWriter bool) error
6060
DeleteDisk(ctx context.Context, project string, volumeKey *meta.Key) error
@@ -106,11 +106,14 @@ func (cloud *CloudProvider) ListDisks(ctx context.Context, maxEntries int64, pag
106106

107107
// RepairUnderspecifiedVolumeKey will query the cloud provider and check each zone for the disk specified
108108
// by the volume key and return a volume key with a correct zone
109-
func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (*meta.Key, error) {
109+
func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, project string, volumeKey *meta.Key) (string, *meta.Key, error) {
110110
klog.V(5).Infof("Repairing potentially underspecified volume key %v", volumeKey)
111+
if project == common.UnspecifiedValue {
112+
project = cloud.project
113+
}
111114
region, err := common.GetRegionFromZones([]string{cloud.zone})
112115
if err != nil {
113-
return nil, fmt.Errorf("failed to get region from zones: %v", err)
116+
return "", nil, fmt.Errorf("failed to get region from zones: %v", err)
114117
}
115118
switch volumeKey.Type() {
116119
case meta.Zonal:
@@ -119,7 +122,7 @@ func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, p
119122
// list all zones, try to get disk in each zone
120123
zones, err := cloud.ListZones(ctx, region)
121124
if err != nil {
122-
return nil, err
125+
return "", nil, err
123126
}
124127
for _, zone := range zones {
125128
_, err := cloud.getZonalDiskOrError(ctx, project, zone, volumeKey.Name)
@@ -131,28 +134,28 @@ func (cloud *CloudProvider) RepairUnderspecifiedVolumeKey(ctx context.Context, p
131134
}
132135
// There is some miscellaneous error getting disk from zone
133136
// so we return error immediately
134-
return nil, err
137+
return "", nil, err
135138
}
136139
if len(foundZone) > 0 {
137-
return nil, fmt.Errorf("found disk %s in more than one zone: %s and %s", volumeKey.Name, foundZone, zone)
140+
return "", nil, fmt.Errorf("found disk %s in more than one zone: %s and %s", volumeKey.Name, foundZone, zone)
138141
}
139142
foundZone = zone
140143
}
141144

142145
if len(foundZone) == 0 {
143-
return nil, notFoundError()
146+
return "", nil, notFoundError()
144147
}
145148
volumeKey.Zone = foundZone
146-
return volumeKey, nil
149+
return project, volumeKey, nil
147150
}
148-
return volumeKey, nil
151+
return project, volumeKey, nil
149152
case meta.Regional:
150153
if volumeKey.Region == common.UnspecifiedValue {
151154
volumeKey.Region = region
152155
}
153-
return volumeKey, nil
156+
return project, volumeKey, nil
154157
default:
155-
return nil, fmt.Errorf("key was neither zonal nor regional, got: %v", volumeKey.String())
158+
return "", nil, fmt.Errorf("key was neither zonal nor regional, got: %v", volumeKey.String())
156159
}
157160
}
158161

pkg/gce-pd-csi-driver/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
253253
return &csi.DeleteVolumeResponse{}, nil
254254
}
255255

256-
volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
256+
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
257257
if err != nil {
258258
if gce.IsGCENotFoundError(err) {
259259
klog.Warningf("DeleteVolume treating volume as deleted because cannot find volume %v: %v", volumeID, err)
@@ -297,7 +297,7 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
297297
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerPublishVolume volume ID is invalid: %v", err))
298298
}
299299

300-
volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
300+
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
301301
if err != nil {
302302
if gce.IsGCENotFoundError(err) {
303303
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err)

0 commit comments

Comments
 (0)