From eecdced3559f9999c3fe251a7835ad82520dbbc7 Mon Sep 17 00:00:00 2001 From: "andrei.khoriakov" Date: Thu, 17 Aug 2023 15:34:52 +0200 Subject: [PATCH] Add SanitizeTags to stable CreateFromSource --- pkg/kube/snapshot/snapshot_stable.go | 3 +- pkg/kube/snapshot/snapshot_test.go | 48 +++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/pkg/kube/snapshot/snapshot_stable.go b/pkg/kube/snapshot/snapshot_stable.go index 4d410b98d68..41fe47d52e0 100644 --- a/pkg/kube/snapshot/snapshot_stable.go +++ b/pkg/kube/snapshot/snapshot_stable.go @@ -17,6 +17,7 @@ package snapshot import ( "context" + "github.com/kanisterio/kanister/pkg/blockstorage" v1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -126,7 +127,7 @@ func (sna *SnapshotStable) CreateFromSource(ctx context.Context, source *Source, return errors.Wrap(err, "Failed to get DeletionPolicy from VolumeSnapshotClass") } contentName := snapshotName + "-content-" + string(uuid.NewUUID()) - snap := UnstructuredVolumeSnapshot(VolSnapGVR, snapshotName, namespace, "", contentName, source.VolumeSnapshotClassName, labels) + snap := UnstructuredVolumeSnapshot(VolSnapGVR, snapshotName, namespace, "", contentName, source.VolumeSnapshotClassName, blockstorage.SanitizeTags(labels)) if err := sna.CreateContentFromSource(ctx, source, contentName, snapshotName, namespace, deletionPolicy); err != nil { return err diff --git a/pkg/kube/snapshot/snapshot_test.go b/pkg/kube/snapshot/snapshot_test.go index 72f647f37ff..c895393f7cf 100644 --- a/pkg/kube/snapshot/snapshot_test.go +++ b/pkg/kube/snapshot/snapshot_test.go @@ -998,13 +998,59 @@ func (s *SnapshotTestSuite) TestCreateFromSourceBeta(c *C) { c.Assert(err, NotNil) } +func (s *SnapshotTestSuite) TestCreateFromSource(c *C) { + ctx := context.Background() + namespace := "namespace" + existingSnapshotName := "existingSnapname" + snapshotName := "snapname" + snapshotClass := "volSnapClass" + driver := "driver" + labels := map[string]string{"Label": "1/"} + source := &snapshot.Source{ + Handle: namespace, + Driver: driver, + VolumeSnapshotClassName: snapshotClass, + } + scheme := runtime.NewScheme() + + volSnap := snapshot.UnstructuredVolumeSnapshot(v1alpha1.VolSnapGVR, existingSnapshotName, namespace, "pvcName", "content", snapshotClass, nil) + volSnapClass := snapshot.UnstructuredVolumeSnapshotClass(v1alpha1.VolSnapClassGVR, snapshotClass, "driver", "DELETE", nil) + dynCli := dynfake.NewSimpleDynamicClient(scheme, volSnap, volSnapClass) + kubeCli := fake.NewSimpleClientset() + snapshoterAlpha := snapshot.NewSnapshotAlpha(kubeCli, dynCli) + + volSnap = snapshot.UnstructuredVolumeSnapshot(v1beta1.VolSnapGVR, existingSnapshotName, namespace, "pvcName", "content", snapshotClass, nil) + volSnapClass = snapshot.UnstructuredVolumeSnapshotClass(v1beta1.VolSnapClassGVR, snapshotClass, "driver", "DELETE", nil) + dynCli = dynfake.NewSimpleDynamicClient(scheme, volSnap, volSnapClass) + kubeCli = fake.NewSimpleClientset() + snapshoterBeta := snapshot.NewSnapshotBeta(kubeCli, dynCli) + + volSnap = snapshot.UnstructuredVolumeSnapshot(snapshot.VolSnapGVR, existingSnapshotName, namespace, "pvcName", "content", snapshotClass, nil) + volSnapClass = snapshot.UnstructuredVolumeSnapshotClass(snapshot.VolSnapClassGVR, snapshotClass, "driver", "DELETE", nil) + dynCli = dynfake.NewSimpleDynamicClient(scheme, volSnap, volSnapClass) + kubeCli = fake.NewSimpleClientset() + snapshoterStable := snapshot.NewSnapshotStable(kubeCli, dynCli) + + for _, snapshoter := range []snapshot.Snapshotter{snapshoterAlpha, snapshoterBeta, snapshoterStable} { + err := snapshoter.CreateFromSource(ctx, source, snapshotName, namespace, false, labels) + foundSns, err := snapshoter.List(ctx, namespace, labels) + c.Assert(err, IsNil) + c.Assert(foundSns.Items, HasLen, 1) + c.Assert(foundSns.Items[0].Name, Equals, snapshotName) + } +} + func (s *SnapshotTestSuite) TestCreateFromSourceStable(c *C) { ctx := context.Background() namespace := "namespace" snapshotName := "snapname" snapshotClass := "volSnapClass" - volSnap := snapshot.UnstructuredVolumeSnapshot(snapshot.VolSnapGVR, snapshotName, namespace, "pvcName", "content", snapshotClass, nil) + createLabels := map[string]string{ + "Label": "1/", + } + + volSnap := snapshot.UnstructuredVolumeSnapshot(snapshot.VolSnapGVR, snapshotName, namespace, "pvcName", "content", snapshotClass, createLabels) volSnap.Object["status"] = map[string]interface{}{ "readyToUse": false, }