From 284872b0ced8e0a9bd63676e9d0a67ca93791a01 Mon Sep 17 00:00:00 2001 From: Joe Skazinski Date: Fri, 3 Sep 2021 18:55:29 -0600 Subject: [PATCH] bug/snapshots - v0.5.3 show snapshots working with all storage api versions, plus snapshot prefix Signed-off-by: Joe Skazinski --- pkg/common/system.go | 37 +++++++++++++++++------------------ pkg/common/system_test.go | 29 ++++++++++++--------------- pkg/controller/provisioner.go | 6 +++--- pkg/controller/snapshotter.go | 13 ++++++++++-- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/pkg/common/system.go b/pkg/common/system.go index b2c61e4a..f9480d6f 100644 --- a/pkg/common/system.go +++ b/pkg/common/system.go @@ -22,15 +22,12 @@ import ( "strings" "unicode" - "github.com/container-storage-interface/spec/lib/go/csi" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" "k8s.io/klog" ) -// ValidateVolumeName verifies that the string only includes spaces and printable UTF-8 characters except: " , < \ -func ValidateVolumeName(s string) bool { - klog.V(2).Infof("ValidateVolumeName %q", s) +// ValidateName verifies that the string only includes spaces and printable UTF-8 characters except: " , < \ +func ValidateName(s string) bool { + klog.V(2).Infof("ValidateName %q", s) for i := 0; i < len(s); i++ { if s[i] == '"' || s[i] == ',' || s[i] == '<' || s[i] == '\\' { return false @@ -41,33 +38,35 @@ func ValidateVolumeName(s string) bool { return true } -// TranslateVolumeName converts the passed in volume name to the translated volume name -func TranslateVolumeName(req *csi.CreateVolumeRequest) (string, error) { +// TranslateName converts the passed in volume name to the translated volume name +func TranslateName(name, prefix string) (string, error) { - if req.GetName() == "" { - return "", status.Error(codes.InvalidArgument, "cannot create volume with an empty name") - } - - originalVolumeID := req.GetName() - volumeID := originalVolumeID - parameters := req.GetParameters() - prefix := parameters[VolumePrefixKey] + volumeID := name if len(prefix) == 0 { // If string is greater than max, truncate it, otherwise return original string if len(volumeID) > VolumeNameMaxLength { // Skip over 'pvc-' - if volumeID[0:4] == "pvc-" { + if len(volumeID) >= 4 && volumeID[0:4] == "pvc-" { volumeID = volumeID[4:] } + // Skip over 'snapshot-' + if len(volumeID) >= 9 && volumeID[0:9] == "snapshot-" { + volumeID = volumeID[9:] + } volumeID = strings.ReplaceAll(volumeID, "-", "") volumeID = volumeID[:VolumeNameMaxLength] } } else { // Skip over 'pvc-' and remove all dashes uuid := volumeID - if volumeID[0:4] == "pvc-" { + if len(volumeID) >= 4 && volumeID[0:4] == "pvc-" { uuid = volumeID[4:] + klog.Infof("TranslateName(pvc): uuid=%q", uuid) + } + if len(volumeID) >= 9 && volumeID[0:9] == "snapshot-" { + uuid = volumeID[9:] + klog.Infof("TranslateName(snapshot): uuid=%q", uuid) } uuid = strings.ReplaceAll(uuid, "-", "") @@ -85,7 +84,7 @@ func TranslateVolumeName(req *csi.CreateVolumeRequest) (string, error) { } } - klog.Infof("TranslateVolumeName %q[%d], prefix %q[%d], result %q[%d]", originalVolumeID, len(originalVolumeID), prefix, len(prefix), volumeID, len(volumeID)) + klog.Infof("TranslateName %q[%d], prefix %q[%d], result %q[%d]", name, len(name), prefix, len(prefix), volumeID, len(volumeID)) return volumeID, nil } diff --git a/pkg/common/system_test.go b/pkg/common/system_test.go index 5e9a4e5b..c49a8e93 100644 --- a/pkg/common/system_test.go +++ b/pkg/common/system_test.go @@ -45,9 +45,7 @@ func createRequestVolume(name string, prefix string) (csi.CreateVolumeRequest, e } func runTest(t *testing.T, idin string, idout string, prefix string) { - req, _ := createRequestVolume(idin, prefix) - id, err := TranslateVolumeName(&req) - + id, err := TranslateName(idin, prefix) g := NewWithT(t) g.Expect(err).To(BeNil()) g.Expect(id).To(Equal(idout)) @@ -56,13 +54,7 @@ func runTest(t *testing.T, idin string, idout string, prefix string) { func TestTranslate(t *testing.T) { // Test empty name - req := csi.CreateVolumeRequest{ - Name: "", - Parameters: map[string]string{"volPrefix": "csi"}, - } - _, err := TranslateVolumeName(&req) - g := NewWithT(t) - g.Expect(err).ToNot(BeNil()) + runTest(t, "", "csi_", "csi") // Test with no prefix runTest(t, "pvc-03c551d9-7e77-43ff-993e-c2308d2f09a1", "03c551d97e7743ff993ec2308d2f09a1", "") @@ -78,17 +70,20 @@ func TestTranslate(t *testing.T) { runTest(t, "51d97e7743ff993ec2308d2f09a1", "csi_51d97e7743ff993ec2308d2f09a1", "csi_123") runTest(t, "pvc-51d9-7e77-43ff-993e-c2308d2f09a1", "cd_51d97e7743ff993ec2308d2f09a1", "cd") runTest(t, "pvc-51d9-7e77-43ff-993e-c2308d2f09a1", "c_51d97e7743ff993ec2308d2f09a1", "c") + + // Test with prefix + runTest(t, "snapshot-03c551d9-7e77-43ff-993e-c2308d2f09a1", "csi_51d97e7743ff993ec2308d2f09a1", "csi") } func TestValidate(t *testing.T) { g := NewWithT(t) - g.Expect(ValidateVolumeName("abcdefghijklmnopqrstuvwxyz")).To(BeTrue()) - g.Expect(ValidateVolumeName("ABCDEFGHIJKLMNOPQRSTUVWXYZ")).To(BeTrue()) - g.Expect(ValidateVolumeName("a b _ . - c")).To(BeTrue()) + g.Expect(ValidateName("abcdefghijklmnopqrstuvwxyz")).To(BeTrue()) + g.Expect(ValidateName("ABCDEFGHIJKLMNOPQRSTUVWXYZ")).To(BeTrue()) + g.Expect(ValidateName("a b _ . - c")).To(BeTrue()) // Test unaccepable characters: " , < \ - g.Expect(ValidateVolumeName("\"abc")).To(BeFalse()) - g.Expect(ValidateVolumeName("abc,")).To(BeFalse()) - g.Expect(ValidateVolumeName("abc