Skip to content

Commit a6f44be

Browse files
pwschuurmank8s-infra-cherrypick-robot
authored and
k8s-infra-cherrypick-robot
committed
Add error logging when error is encountered fetching a VolumeSnapshotContent source image
1 parent 75a2c13 commit a6f44be

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package gcecloudprovider
1717
import (
1818
"context"
1919
"fmt"
20+
"net/http"
21+
"regexp"
2022
"strings"
2123

2224
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
@@ -39,6 +41,15 @@ const (
3941
imageURITemplateGlobal = "projects/%s/global/images/%s" //{gce.projectID}/global/images/{image.Name}"
4042
)
4143

44+
var (
45+
// Snaphsot and Image Regex must comply with RFC1035
46+
rfc1035Regex = regexp.MustCompile("^[a-z]([-a-z0-9]*[a-z0-9])?$")
47+
)
48+
49+
func isRFC1035(value string) bool {
50+
return rfc1035Regex.MatchString(strings.ToLower(value))
51+
}
52+
4253
type FakeCloudProvider struct {
4354
project string
4455
zone string
@@ -331,6 +342,9 @@ func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instance
331342

332343
// Snapshot Methods
333344
func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
345+
if !isRFC1035(snapshotName) {
346+
return nil, fmt.Errorf("invalid snapshot name %v: %w", snapshotName, invalidError())
347+
}
334348
snapshot, ok := cloud.snapshots[snapshotName]
335349
if !ok {
336350
return nil, notFoundError()
@@ -409,6 +423,9 @@ func (cloud *FakeCloudProvider) ListImages(ctx context.Context, filter string) (
409423
}
410424

411425
func (cloud *FakeCloudProvider) GetImage(ctx context.Context, project, imageName string) (*computev1.Image, error) {
426+
if !isRFC1035(imageName) {
427+
return nil, fmt.Errorf("invalid image name %v: %w", imageName, invalidError())
428+
}
412429
image, ok := cloud.images[imageName]
413430
if !ok {
414431
return nil, notFoundError()
@@ -566,6 +583,7 @@ func notFoundError() *googleapi.Error {
566583

567584
func invalidError() *googleapi.Error {
568585
return &googleapi.Error{
586+
Code: http.StatusBadRequest,
569587
Errors: []googleapi.ErrorItem{
570588
{
571589
Reason: "invalid",

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

+1
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ func (gceCS *GCEControllerServer) getSnapshotByID(ctx context.Context, snapshotI
13811381
// return empty list if no snapshot is found
13821382
return &csi.ListSnapshotsResponse{}, nil
13831383
}
1384+
return nil, common.LoggedError("Failed to get image snapshot: ", err)
13841385
}
13851386
e, err := generateDiskImageEntry(image)
13861387
if err != nil {

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

+32
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,45 @@ func TestListSnapshotsArguments(t *testing.T) {
293293
numImages: 2,
294294
expectedCount: 1,
295295
},
296+
{
297+
name: "valid image",
298+
req: &csi.ListSnapshotsRequest{
299+
SnapshotId: testImageID + "0",
300+
},
301+
numSnapshots: 3,
302+
numImages: 2,
303+
expectedCount: 1,
304+
},
296305
{
297306
name: "invalid id",
298307
req: &csi.ListSnapshotsRequest{
299308
SnapshotId: testSnapshotID + "/foo",
300309
},
301310
expectedCount: 0,
302311
},
312+
{
313+
name: "invalid image id",
314+
req: &csi.ListSnapshotsRequest{
315+
SnapshotId: testImageID + "/foo",
316+
},
317+
expectedCount: 0,
318+
},
319+
{
320+
name: "invalid snapshot name",
321+
req: &csi.ListSnapshotsRequest{
322+
SnapshotId: testSnapshotID + "-invalid-snapshot-",
323+
},
324+
expectedCount: 0,
325+
expErrCode: codes.InvalidArgument,
326+
},
327+
{
328+
name: "invalid image name",
329+
req: &csi.ListSnapshotsRequest{
330+
SnapshotId: testImageID + "-invalid-image-",
331+
},
332+
expectedCount: 0,
333+
expErrCode: codes.InvalidArgument,
334+
},
303335
{
304336
name: "no id",
305337
req: &csi.ListSnapshotsRequest{

0 commit comments

Comments
 (0)