Skip to content

[release-1.12] Add error logging when error is encountered fetching a VolumeSnapshotContent source image #1515

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/gce-cloud-provider/compute/fake-gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package gcecloudprovider
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"

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

var (
// Snaphsot and Image Regex must comply with RFC1035
rfc1035Regex = regexp.MustCompile("^[a-z]([-a-z0-9]*[a-z0-9])?$")
)

func isRFC1035(value string) bool {
return rfc1035Regex.MatchString(strings.ToLower(value))
}

type FakeCloudProvider struct {
project string
zone string
Expand Down Expand Up @@ -332,6 +343,9 @@ func (cloud *FakeCloudProvider) GetInstanceOrError(ctx context.Context, instance

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

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

func invalidError() *googleapi.Error {
return &googleapi.Error{
Code: http.StatusBadRequest,
Errors: []googleapi.ErrorItem{
{
Reason: "invalid",
Expand Down
1 change: 1 addition & 0 deletions pkg/gce-pd-csi-driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ func (gceCS *GCEControllerServer) getSnapshotByID(ctx context.Context, snapshotI
// return empty list if no snapshot is found
return &csi.ListSnapshotsResponse{}, nil
}
return nil, common.LoggedError("Failed to get image snapshot: ", err)
}
e, err := generateDiskImageEntry(image)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions pkg/gce-pd-csi-driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,45 @@ func TestListSnapshotsArguments(t *testing.T) {
numImages: 2,
expectedCount: 1,
},
{
name: "valid image",
req: &csi.ListSnapshotsRequest{
SnapshotId: testImageID + "0",
},
numSnapshots: 3,
numImages: 2,
expectedCount: 1,
},
{
name: "invalid id",
req: &csi.ListSnapshotsRequest{
SnapshotId: testSnapshotID + "/foo",
},
expectedCount: 0,
},
{
name: "invalid image id",
req: &csi.ListSnapshotsRequest{
SnapshotId: testImageID + "/foo",
},
expectedCount: 0,
},
{
name: "invalid snapshot name",
req: &csi.ListSnapshotsRequest{
SnapshotId: testSnapshotID + "-invalid-snapshot-",
},
expectedCount: 0,
expErrCode: codes.InvalidArgument,
},
{
name: "invalid image name",
req: &csi.ListSnapshotsRequest{
SnapshotId: testImageID + "-invalid-image-",
},
expectedCount: 0,
expErrCode: codes.InvalidArgument,
},
{
name: "no id",
req: &csi.ListSnapshotsRequest{
Expand Down