Skip to content

Commit 274ddc8

Browse files
authored
Merge pull request #1519 from k8s-infra-cherrypick-robot/cherry-pick-1514-to-release-1.8
[release-1.8] Add error logging when error is encountered fetching a VolumeSnapshotContent source image
2 parents 1b817aa + e31e7f7 commit 274ddc8

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

326337
// Snapshot Methods
327338
func (cloud *FakeCloudProvider) GetSnapshot(ctx context.Context, project, snapshotName string) (*computev1.Snapshot, error) {
339+
if !isRFC1035(snapshotName) {
340+
return nil, fmt.Errorf("invalid snapshot name %v: %w", snapshotName, invalidError())
341+
}
328342
snapshot, ok := cloud.snapshots[snapshotName]
329343
if !ok {
330344
return nil, notFoundError()
@@ -403,6 +417,9 @@ func (cloud *FakeCloudProvider) ListImages(ctx context.Context, filter string) (
403417
}
404418

405419
func (cloud *FakeCloudProvider) GetImage(ctx context.Context, project, imageName string) (*computev1.Image, error) {
420+
if !isRFC1035(imageName) {
421+
return nil, fmt.Errorf("invalid image name %v: %w", imageName, invalidError())
422+
}
406423
image, ok := cloud.images[imageName]
407424
if !ok {
408425
return nil, notFoundError()
@@ -559,6 +576,7 @@ func notFoundError() *googleapi.Error {
559576

560577
func invalidError() *googleapi.Error {
561578
return &googleapi.Error{
579+
Code: http.StatusBadRequest,
562580
Errors: []googleapi.ErrorItem{
563581
{
564582
Reason: "invalid",

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

+1
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ func (gceCS *GCEControllerServer) getSnapshotByID(ctx context.Context, snapshotI
13371337
// return empty list if no snapshot is found
13381338
return &csi.ListSnapshotsResponse{}, nil
13391339
}
1340+
return nil, common.LoggedError("Failed to get image snapshot: ", err)
13401341
}
13411342
e, err := generateImageEntry(image)
13421343
if err != nil {

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

+32
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,45 @@ func TestListSnapshotsArguments(t *testing.T) {
286286
numImages: 2,
287287
expectedCount: 1,
288288
},
289+
{
290+
name: "valid image",
291+
req: &csi.ListSnapshotsRequest{
292+
SnapshotId: testImageID + "0",
293+
},
294+
numSnapshots: 3,
295+
numImages: 2,
296+
expectedCount: 1,
297+
},
289298
{
290299
name: "invalid id",
291300
req: &csi.ListSnapshotsRequest{
292301
SnapshotId: testSnapshotID + "/foo",
293302
},
294303
expectedCount: 0,
295304
},
305+
{
306+
name: "invalid image id",
307+
req: &csi.ListSnapshotsRequest{
308+
SnapshotId: testImageID + "/foo",
309+
},
310+
expectedCount: 0,
311+
},
312+
{
313+
name: "invalid snapshot name",
314+
req: &csi.ListSnapshotsRequest{
315+
SnapshotId: testSnapshotID + "-invalid-snapshot-",
316+
},
317+
expectedCount: 0,
318+
expErrCode: codes.InvalidArgument,
319+
},
320+
{
321+
name: "invalid image name",
322+
req: &csi.ListSnapshotsRequest{
323+
SnapshotId: testImageID + "-invalid-image-",
324+
},
325+
expectedCount: 0,
326+
expErrCode: codes.InvalidArgument,
327+
},
296328
{
297329
name: "no id",
298330
req: &csi.ListSnapshotsRequest{

0 commit comments

Comments
 (0)