Skip to content
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

Add SanitizeTags to stable CreateFromSource #2273

Merged
merged 8 commits into from
Nov 22, 2023
11 changes: 10 additions & 1 deletion pkg/kube/snapshot/snapshot_stable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -126,7 +127,15 @@ 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
Expand Down
59 changes: 59 additions & 0 deletions pkg/kube/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,65 @@ 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)
c.Assert(err, IsNil)
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)
}
viveksinghggits marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *SnapshotTestSuite) TestCreateFromSourceStable(c *C) {
ctx := context.Background()
namespace := "namespace"
Expand Down