Skip to content

Commit cd24ec2

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 cdc2a9d commit cd24ec2

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
@@ -332,6 +343,9 @@ func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instance
332343

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

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

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,7 @@ func (gceCS *GCEControllerServer) getSnapshotByID(ctx context.Context, snapshotI
13911391
// return empty list if no snapshot is found
13921392
return &csi.ListSnapshotsResponse{}, nil
13931393
}
1394+
return nil, common.LoggedError("Failed to get image snapshot: ", err)
13941395
}
13951396
e, err := generateDiskImageEntry(image)
13961397
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)